bjornharrtell / jts2geojson

JTS from/to GeoJSON converter for Java
MIT License
141 stars 57 forks source link

Allow empty geometry to be parsed by jts2geojson #49

Open rtroilo opened 2 years ago

rtroilo commented 2 years ago

Many thanks for this great library @bjornharrtell !

Currently jts2geojson is not be able to parse empty geometries in either direction. This is a correct behavior as GeoJSON processors are not required to parse empty geometries but may do so.

Would you are interested and accept a PR which brings empty geometry parsing into jts2geojson?

JTS already implements this with the additional dependency org.locationtech.jts.io:jts-io-common and the corresponding org.locationtech.jts.io.geojson.GeoJsonWriter.

var geomFactory = new GeometryFactory();
var geoJsonWriter = new org.locationtech.jts.io.geojson.GeoJsonWriter();
geoJsonWriter.setEncodeCRS(false);

var emptyPoint = geomFactory.createPoint();

var geoJson = geoJsonWriter.write(emptyPoint);
// {"type":"Point","coordinates":[]}

RFT 7946 Geometry Object

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.

bjornharrtell commented 2 years ago

Sure, sounds like a sensible thing.