georust / geozero

Zero-Copy reading and writing of geospatial data.
Apache License 2.0
322 stars 30 forks source link

Implement `empty_point` for `GeoJsonWriter` #161

Closed Oreilles closed 10 months ago

Oreilles commented 11 months ago

According to the RFC, empty points may be represented with empty coordinates:

RFC 7946

3.1.  Geometry Object

   o  A GeoJSON Geometry object of any type other than "GeometryCollection" has a member with the name "coordinates". The value of the "coordinates" member is an array.  The structure of the elements in this array is determined by the type of geometry.  GeoJSON processors MAY interpret Geometry objects with empty "coordinates" arrays as null objects.

This PR therefore implements the GeometryProcessor method point_empty for GeojsonWriter by writing a point with empty coordinates. For reference, PostGIS also has this behavior:


SELECT ST_AsGeoJSON('POINT EMPTY'::geometry);
--> {"type":"Point","coordinates":[]}
michaelkirk commented 11 months ago

Could you add a test for this?

Oreilles commented 11 months ago

Wkt -> GeoJSON test added. I wanted to add the reverse, but turns out the geojson create doesn't accept it:

https://github.com/georust/geojson/blob/4cfbf3a951cff12818e30c269606d46fb4c80486/src/util.rs#L211-L215

michaelkirk commented 11 months ago

turns out the geojson create doesn't accept [empty points].

TBH I didn't realize geojson could represent empty points, but you've convinced me that it seems like a bug in the geojson crate. I've just opened https://github.com/georust/geojson/issues/231

Oreilles commented 11 months ago

Ok, so after further inspection it seems like PostGIS is the one deviating from the spec here...

https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.1

A position is an array of numbers.  There MUST be two or more
   elements.
michaelkirk commented 11 months ago

heh i noticed the same thing, but I thought maybe it was just sloppy spec writing, and that the "null geometry" thing seemed to trump the "position" thing.

Probably we should be careful with this one, and get some others to weigh in.