1

I'm inserting Open Street Map data into a PostGIS database using the GeometryBuilder of osm4j. I then fetch the data and display it on a Leaflet map using ST_AsGeoJSON.

This usually works flawlessly, but every now and then PostGIS throws an exception at me. For example, OSM relation 13169 does not work:

select ST_GeometryType(geom) from featuretable where id=13169;
    st_geometrytype    
-----------------------
 ST_GeometryCollection
(1 row)

select ST_AsGeoJSON(geom) from featuretable where id=13169;
ERROR:  GeoJson: geometry not supported.

Doing select ST_AsBinary(geom) from featuretable where id=13169; gives this output.

At first I thought that features of type ST_GeometryCollection could not be handled by ST_AsGeoJSON, but for instance OSM relation 3244259 works fine and is also a ST_GeometryCollection.

select ST_GeometryType(geom) from featuretable where id=3244259;
    st_geometrytype    
-----------------------
 ST_GeometryCollection

What is it that makes ST_AsGeoJSON crash on certain features, and how can I prevent it? All I really want is a GeoJSON of the feature; perhaps it is possible to convert/transform the feature somehow to achieve this?

  • Possible 'Winding' issue (Polygon direction of arrays) see macwright.com/2015/03/23/… for the right-hand rule. – Mapperz Oct 7 at 1:09
  • fyi In the past have had issues with compound curves (arcs) from autocad sourced data to geojson. Your error states "geometry not supported" so it will need to be fixed (or omitted) – Mapperz Oct 7 at 1:12
  • 1
    Please ST_AsBinary us that failing geometry to test. – geozelot Oct 7 at 6:16
  • 1
    @geozelot: I have uploaded it here (and updated the question) – sjoblomj Oct 7 at 8:12
  • Could you edit the title of the question? PostGIS does not crash, it just throws an error. That seems to be intentional with As_GeoJSON and the geometry that it gets. – user30184 Oct 8 at 7:26
1

The GeoJSON specification warns about nested GeometryCollections in https://tools.ietf.org/html/rfc7946#page-9

To maximize interoperability, implementations SHOULD avoid nested GeometryCollections.

The failing GeometryCollection begins like this when queried with ST_AsText

GEOMETRYCOLLECTION(GEOMETRYCOLLECTION EMPTY,MULTILINESTRING((1....

Thus it is a nested GeometryCollection and PostGIS does not convert that into GeoJSON. Test with a simple nested GeometryCollection yields the same error.

SELECT ST_AsGeoJSON(
ST_GeomFromText('GEOMETRYCOLLECTION ( GEOMETRYCOLLECTION ( POINT ( 505 396 )), GEOMETRYCOLLECTION ( POINT ( 566 406 )))'));

ERROR:  GeoJson: geometry not supported.
SQL state: XX000

I do not know if nested GeometryCollections are OK for OSM. Anyway, removing the empty GeometryCollection from the OSM relation should be fine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.