spatial4j
spatial4j copied to clipboard
Z coordinate isn't used in JtsGeoJSONWriter
I've found that JtsGeoJSONWriter cannot parse JtsPoint with 3 coordinates (x,y,z) to GeoJSON string. z coordinate is just dropped during parsing.
An example is below. geoJSONPoint should be the same JSON as backToGeoJSONPoint variable but you can see that Z coordinate isn't returned.
String geoJSONPoint = "{\"type\": \"Point\", \"coordinates\": [-117.26028657062011, 32.87234560059042, 12.1]}";
JtsSpatialContextFactory factory = new JtsSpatialContextFactory();
factory.geo = true;
factory.useJtsPoint = true;
factory.useJtsLineString = true;
factory.datelineRule = DatelineRule.none;
JtsSpatialContext context = new JtsSpatialContext(factory);
GeoJSONReader geojsonReader = new GeoJSONReader(context, factory);
JtsGeoJSONWriter geoJSONWriter = new JtsGeoJSONWriter(context, factory);
Shape shape = geojsonReader.read(geoJSONPoint);
String backToGeoJSONPoint = geoJSONWriter.toString(shape);
I guess there are 2 internal issues:
JtsPointisn't an instance ofJtsGeometry. Thus, it is treated as simple point inJtsGeoJSONWriter.- Even if we add a special case to treat
JtsPointsimilarly toJtsGeometryit still stores only 2 coordinates (x and y) because of the next method:
protected void write(Writer output, NumberFormat nf, Coordinate coord) throws IOException {
output.write(91);
output.write(nf.format(coord.x));
output.write(44);
output.write(nf.format(coord.y));
output.write(93);
}
Z dimension is kind of a gray area; not particularly well supported. Feel free to file a PR to rectify the situation. I plan to do a release next week but I can delay for a PR.
I don't want to block the release because I don't think we will work on fixing Z dimension soon. Later if we decide that Z dimension is needed we could work on the PR.