Opened 5 years ago

Closed 5 years ago

#3061 closed defect (fixed)

ST_GeomFromGeoJSON strips coordinates

Reported by: slaskis Owned by: pramsey
Priority: medium Milestone: PostGIS 2.1.6
Component: postgis Version: 2.1.x
Keywords: geojson, linestring Cc:

Description

I'm storing some geo paths in PostGIS using GeoJSON and the ST_GeomFromGeoJSON()-function but I just found an issue with it:

SELECT ST_NumPoints(ST_GeomFromGeoJSON('{"type":"LineString","coordinates":[[0,0],[1,1],[1,1],[2,2]]}')) = 4; -- FALSE

SELECT ST_NumPoints(ST_GeomFromText('LINESTRING (0 0,1 1,1 1,2 2)')) = 4; -- TRUE

It seems to strip the two 'middle' coordinates because they are the same. And I can buy that it would do that but it doesn't do it consistently, like when I use ST_GeomFromText() with the same list of coordinates. It also does not strip the coordinates when using "type": "MultiPoints?" instead, but it would be the wrong semantics.

It's a problem for me because I need the points to match a sequence of timestamps as well or data will be lost. And the reason I'm using GeoJSON is that the app server is written in Javascript so it just seems natural.

I'm running Postgres 9.4.1 and SELECT PostGIS_Full_Version() gives me:

POSTGIS="2.1.5 r13152" GEOS="3.4.2-CAPI-1.8.2 r3921" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 1.11.1, released 2014/09/24" LIBXML="2.9.1" LIBJSON="UNKNOWN" RASTER

I currently workaround this issue by changing the type to MultiPoint? and then converting it to LineString? but it seems like a hack to me:

ST_LineFromMultiPoint(ST_GeomFromGeoJSON('{"type":"MultiPoint","coordinates":[[0,0],[1,1],[1,1],[2,2]]}'))

Originally posted to http://gis.stackexchange.com/questions/136629/st-geomfromgeojson-strips-coordinates-is-it-a-bug/136966#136966

Change History (6)

comment:1 Changed 5 years ago by pramsey

Yes, the geojson input code is definitely throwing away duplicate points,

http://trac.osgeo.org/postgis/browser/tags/2.1.5/liblwgeom/lwin_geojson.c#L126

the point append call is asking for duplicate removal. I wonder how this compares to other inputs...

comment:2 Changed 5 years ago by pramsey

Milestone: PostGIS 2.1.6
  • WKT input preserves duplicate points
  • WKB input preserves duplicate points
  • GML input strips duplicate points
  • KML input strips duplicate points
  • GeoJSON input strips duplicate points

So... what to do?

comment:3 Changed 5 years ago by strk

My vote goes for NOT removing duplicates on input. The ST_RemoveRepeatedPoints exists since 2.0.0 for those who want to clean up the data.

comment:4 Changed 5 years ago by robe

same here. If specs don't say what to do, DO NOT remove duplicates on input.

comment:5 Changed 5 years ago by pramsey

OK, since also the specs don't say anything about dupes

  • GML doesn't
  • KML doesn't
  • GeoJSON doesn't

I will patch 2.1 and trunk

comment:6 Changed 5 years ago by pramsey

Resolution: fixed
Status: newclosed

Fixed in 2.1 at r13339, in trunk at r13340

Note: See TracTickets for help on using tickets.