[gdal-dev] Handling PostGIS's geojson output with a Z dimensions
Rutger
kassies at gmail.com
Tue May 9 01:46:00 PDT 2017
Dear all,
I have a Python script rasterizing polygons which are imported with OGR from
GeoJSON. The GeoJSON is created by PostGIS and send over the network (direct
connection to the db is not possible).
This has worked very well until it started to crash the Python interpreter,
a hard crash with no traceback/error whatsoever. Which made it hard to
debug, but after isolating the specific case i noticed it crashed on an
empty polygon with a Z dimension. Rasterizing an empty polygon with GDAL is
useless, but normally works fine.
If you convert an empty 2D MultiPolgon in PostGIS to GeoJSON it looks like:
'{"type": "MultiPolygon", "coordinates": []}'
The above is handled very well, ogr.CreateGeometryFromJson() converts this
into a proper geom, and its IsEmpty() method returns True.
Now an empty 25D MultiPolygon from PostGIS looks like:
'{"type": "MultiPolygon", "coordinates": [[[]]]}'
Which ogr converts to a geom, but the IsEmpty() method retuns False now,
which is why it slipped through and ended up in my gdal.Rasterize call.
Exporting this geom to WKT shows 'MULTIPOLYGON EMPTY' as you would expect.
Passing this to gdal.Rasterize causes the crash, whereas it properly
rasterizes the 2D empty polygon.
A few resulting questions;
Is the output from PostGIS valid GeoJSON? I briefly skimmed the geojson
spec, but couldnt find something about nested empty arrays. If its valid,
should gdal.Rasterize handle this without crashing? Or if I as an end user
have to handle this, what is the appropriate way? Since IsEmpty() doesnt
catch it, a workaround might be to use an additional Export/Import step
running it through wkt for example:
geom = ogr.CreateGeometryFromJson('{"type": "MultiPolygon", "coordinates":
[[]]}')
geom = ogr.CreateGeometryFromWkt(geom.ExportToWkt())
I don't have full control over the PostGIS queries on the back-end, so even
though ST_FORCE2D() would also work, i would rather catch this on the
front-end as well.
Here is a notebook replicating the case:
http://nbviewer.jupyter.org/gist/RutgerK/47412685882ed3f518c70fd09f57691c
Regards,
Rutger
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Handling-PostGIS-s-geojson-output-with-a-Z-dimensions-tp5319840.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.
More information about the gdal-dev
mailing list