Skip to content

Attempt to add Geometry datatype support for PostgreSQL (PostGis) - #3641

Closed
mibrito wants to merge 6 commits into
sequelize:masterfrom
mibrito:master
Closed

mibrito wants to merge 6 commits into
sequelize:masterfrom
mibrito:master

Conversation

@mibrito

@mibrito mibrito commented May 4, 2015

Copy link
Copy Markdown

As discussed on #2839, it would be nice to have this feature on sequelize. Particulary I'm working in a project using sequelize that needs to be able to insert and retrieve geographic data from PostgreSQL with PostGIS. So I made a fork of the sequelize and tried to insert some initial steps to achive this functionality.

Basicaly was created a new datatype GEOMETRY and some of the functions were adapted to store geometries using GeoJSON objects (ST_GeomFromGeoJSON) and retrieve them as GeoJSON (ST_AsGeoJSON).

The new datatype created is based on Geometry Type Definition syntax added on PostGIS version 2.0.0:

Sequelize.GEOMETRY(geometry_type::asString)
Allows the method .sync() to create a new column of the type Geometry(geometry_type), which by default on PostGIS has srid equals -1.

Sequelize.GEOMETRY(geometry_type::asString, srid::asNumber)
Allows the method .sync() to create a column of type Geometry(geometry_type, srid).

postgres.QueryGenerator.escape()
Was modified to "escape" geometry data using PostGIS function ST_GeomFromGeoJSON.

abstract.QueryGenerator.selectQuery()
Was modified to retrieve data as GeoJSON using PostGIS function ST_AsGeoJSON.

Instance.prototype.set()
Was modified to parse GeoJSON object as JSON.

Model now has a vector called _geometryAttributes, a function called _hasGeometryAttributes and a function called _isGeometryAttribute.

As a first attempt it still needs some tests and adjustments, so i'm sharing it now to have some feedback and ideas on the changes that are needed.

Comment thread lib/dialects/postgres/data-types.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to have any special code in postgres - having all the code in abstract datatypes is fine

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dialect specific datatypes are only used if a data type is only supported by a single dialect, or if a dialect uses a different type (for example postgres blobs = bytea)

The geometry type is supported by mysql and mssql as well

@janmeier

janmeier commented May 4, 2015

Copy link
Copy Markdown
Member

Looking good so far! :)

To be able to test this you need to enable postgis in the travis config. http://docs.travis-ci.com/user/using-postgresql/#Using-PostGIS

It seems like geojson is also supported in mysql 5.7 (http://dev.mysql.com/doc/refman/5.7/en/spatial-geojson-functions.html#function_st-geomfromgeojson) - but its still in development, so...
The ideal solution from my point of view would be to use geojson as the abstraction for all dialects in sequelize. For example, inserting { "type": "Point", "coordinates": [125.6, 10.1] } in sequelize would translate to GeomFromText('POINT(125.6, 10.1)') in mysql https://dev.mysql.com/doc/refman/5.0/en/populating-spatial-columns.html.

When fetching data again, the data is returned in the well-known text format, but it should be as simple as pluging in https://www.npmjs.com/package/wellknown or similar to return geojson

Still needs unit tests for escaping geo types, and integration tests, but you probably already know that :)

It could be super-duper awesome if you can get this to work for mysql and mssql as well using WKT - it shouldn't be a lot of added work.

@janmeier janmeier self-assigned this May 4, 2015
@janmeier

Copy link
Copy Markdown
Member

@mibrito , @mikeringrose, @Ghirro Which of you are still working on this feature, and whats the best way to move forward with it? Are any of you tempted by the posibiltiy of implenting this using wkt in mysql / mssql and geojson in PG. If we can only get the postgres support in for now that'd also be fine, but I'd love to support this cross-dialect

@mikeringrose

Copy link
Copy Markdown

@janmeier since i'll be using it in production shortly. i plan on writing more test and add mysql support this weekend.

@janmeier

Copy link
Copy Markdown
Member

@mikeringrose Looking forward to it :)

@arneke

arneke commented May 24, 2015

Copy link
Copy Markdown

Hi, very cool stuff mibrito, I was about to start hacking my own support into the escape functions before I saw this pull request.

I've found one issue while testing this on legacy tables, if the column name is not equal to the attribute name, i.e.

  geometry: {
    type: Sequelize.GEOMETRY,
    field: 'the_geom'
  }

Then the abstract query generator fails because in "mainModel._isGeometryAttribute(attr)" , the value of attr is actually "the_geom,geometry"

(The pull request appears to be for 2.1 instead of 3.0?)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attr may be composed of both the column name and the alias, comma separated. Then the lookup doesn't work.

@mshahriarinia

Copy link
Copy Markdown

Any updates on this? This seems to be a valid feature that is not there. yet (?)

@mikeringrose mikeringrose mentioned this pull request May 29, 2015
@janmeier

Copy link
Copy Markdown
Member

Closed in favor of #3828

@janmeier janmeier closed this Jun 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants