jvail/spl.js

Unable to add geometry from geoJson

duvifn opened this issue · 2 comments

Hi,
First thanks a lot for creating this library.

I'm trying to execute this script:

const script =`
BEGIN TRANSACTION;
SELECT InitSpatialMetaData();
CREATE TABLE test (
id TEXT NOT NULL
  PRIMARY KEY);
SELECT AddGeometryColumn('test', 'geom', 4326, 'POLYGON', 'XY');

INSERT OR REPLACE INTO test VALUES ('osm998307425',GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[35.172522345781324,31.807637007387367],[35.1730225777626,31.807379406789376],[35.17296088695526,31.807292779878278],[35.17246065497398,31.807550380717725],[35.172522345781324,31.807637007387367]]]}'));
SELECT CreateSpatialIndex('test', 'geom');
COMMIT;`
await db.read(script);

But getting this error:

test.geom violates Geometry constraint [geom-type or SRID not allowed]

However when executing only the following expression, everything works correctly:

db.exec("select GeomFromGeoJSON('{\"type\":\"Polygon\",\"coordinates\":[[[35.172522345781324,31.807637007387367],[35.1730225777626,31.807379406789376],[35.17296088695526,31.807292779878278],[35.17246065497398,31.807550380717725],[35.172522345781324,31.807637007387367]]]}');" );

How should I do this?

Thanks a lot!

jvail commented

Hello @duvifn,

if you set the SRID it does work: SetSRID(GeomFromGeoJSON('{...}', 4326). It seems SpatiaLite does not automatically assume a GeoJSON's SRID to be 4326 - which would be better.

Setting the crs property (non-standard I believe) seems to work as well:

GeomFromGeoJSON('{"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG:4326"}}, ...');

Jan

Wow. Thank you so much @jvail!