PostGIS Geometry column loses its SRID after selecting from a database
See original GitHub issueIssue Description
If I have a column of the Geometry type (PostGIS) and tries to select its value I get GeoJSON without SRID information
What are you doing?
I select records from a table with geometry column (Postgres DB with PostGIS) and I get them without SRID information, only geometry itself. Here is an example:
- Create a table
CREATE TABLE public.test_geom
(
id integer NOT NULL DEFAULT nextval('test_geom_id_seq'::regclass),
geom geometry
)
WITH (
OIDS=FALSE
);
- Insert a test record with SRID:
insert into test_geom
(geom)
values(ST_SETSRID(ST_GeomFromText('POINT(30 60)'), 4326));
- Check geometry and SRID in the table:
select st_srid(geom),* from test_geom
Output:
- Create a sequelize connection and define the table model:
const Sequelize = require('sequelize')
const sequelize = new Sequelize('test_gis', 'postgres', 'postgres', {
host: 'localhost',
dialect: 'postgres'
})
const TestGeom = sequelize.define('testGeom', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
geom: {
type: Sequelize.GEOMETRY
}
}, {
schema: 'public',
tableName: 'test_geom',
timestamps: false
})
What do you expect to happen?
In the case I have SRID in the geometry column of the DB record I expect to retrieve it with SRID information:
const geomObj = await TestGeom.findOne()
console.log(geomObj.geom)
Output:
Executing (default): SELECT "id", "geom" FROM "public"."test_geom" AS "testGeom" LIMIT 1;
{ type:'Point', crs:{ type: 'name', properties: { name: 'EPSG:4326'} }, coordinates: [30,60] }
and updating the record with this geometry does not lose SRID info:
Output:
What is actually happening?
- Get the record from db via sequelize
const geomObj = await TestGeom.findOne()
console.log(geomObj.geom)
Output:
Executing (default): SELECT "id", "geom" FROM "public"."test_geom" AS "testGeom" LIMIT 1;
{ type: 'Point', coordinates: [ 30, 60 ] }
There is no “crs” property in retrieved GeoJSON object.
- Update retrieved geometry in the table:
await TestGeom.update({
geom: geomObj.geom
}, {
where: {
id: geomObj.id
}
})
Output:
Executing (default): UPDATE "public"."test_geom" SET "geom"=ST_GeomFromGeoJSON('{"type":"Point","coordinates":[30,60]}') WHERE "id" = 3
- Check geometry and SRID in the table once again:
select st_srid(geom),* from test_geom
Output:
There is no “SRID” in retrieved record.
Additional context
Environment
- Sequelize version: sequelize@4.44.3
- Node.js version: v10.16.3
- Operating System: Windows 10 1903
Issue Template Checklist
How does this problem relate to dialects?
- I think this problem happens regardless of the dialect.
- I think this problem happens only for the following dialect(s): postgres
- I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
I’m not sure I can correctly fix this issue by PR. I can show the issue location in the source code and you can decide how to fix it: file: postgres/data-types.js
GEOMETRY.parse = GEOMETRY.prototype.parse = function parse(value) {
const b = new Buffer(value, 'hex');
return wkx.Geometry.parse(b).toGeoJSON();
}
This can be resolved by passing options.shortCrs or options.longCrs to toGeoJSON function call. To indicate what exact form of crs to use short or long you can add option in the column definition of the model, for example:
geom: {
type: Sequelize.GEOMETRY,
crs: 'none' // possible values: none - old behaviour, short - return GeoJSON with the short form of crs (options.shortCrs), long - return GeoJSON with the long form of crs (options.longCrs)
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
In #11616 there is no SRID after saving, which is not my case, but can be also related to wkx library. In my case I get geometry with no SRID but if I manually add “crs” prop to geometry then it is saved in the db correctly (i.e. with SRID).
@t0lik Only v6, since this PR was merged to master. If you would like to see this in v5, you are welcome to open a new PR targeting it (just cherry-pick this commit on top of v5 and fix whatever conflicts may arise)