spatial4j icon indicating copy to clipboard operation
spatial4j copied to clipboard

Z coordinate isn't used in JtsGeoJSONWriter

Open porunov opened this issue 5 years ago • 3 comments
trafficstars

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);

porunov avatar Aug 12 '20 15:08 porunov

I guess there are 2 internal issues:

  1. JtsPoint isn't an instance of JtsGeometry. Thus, it is treated as simple point in JtsGeoJSONWriter.
  2. Even if we add a special case to treat JtsPoint similarly to JtsGeometry it 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);
}

porunov avatar Aug 12 '20 15:08 porunov

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.

dsmiley avatar Aug 25 '20 19:08 dsmiley

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.

porunov avatar Aug 25 '20 20:08 porunov