question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PostGIS Geometry column loses its SRID after selecting from a database

See original GitHub issue

Issue 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:

  1. Create a table
CREATE TABLE public.test_geom
(
  id integer NOT NULL DEFAULT nextval('test_geom_id_seq'::regclass),
  geom geometry
)
WITH (
  OIDS=FALSE
);
  1. Insert a test record with SRID:
insert into test_geom
(geom)
values(ST_SETSRID(ST_GeomFromText('POINT(30 60)'), 4326));
  1. Check geometry and SRID in the table:
select st_srid(geom),* from test_geom

Output: изображение

  1. 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?

  1. 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.

  1. 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
  1. 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
t0likcommented, Oct 31, 2019

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).

0reactions
papbcommented, Jan 22, 2020

@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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostGIS tables don't retain SRID - GIS Stack Exchange
SELECT Find_SRID ('main', 'gps_animals_data', 'geom') , and it gives me 0. I've tried updating my geometry column such as: UPDATE main.
Read more >
Chapter 4. Data Management - PostGIS
The SRID is required when creating spatial objects for insertion into the database (it may be defaulted to 0). See ST_SRID and Section...
Read more >
Postgis disconnecting during some queries - Stack Overflow
Your client is probably timing out. As it is, you are doing a cross-join between the two tables, so if a polygon from...
Read more >
3092 (Slow performance of geometry_columns in PostGIS >= 2)
select c.relname, a.attname, t.typname, postgis_typmod_dims(a.atttypmod) dim ... The test results on 30,000 geometry columns database, old compared to new 3 ...
Read more >
Chapter 4. PostGIS Usage - Crunchy Data
The SRID is required when creating spatial objects for insertion into the database. ... Distance calculation using GEOMETRY (13.3 "degrees") SELECT ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found