Geotools GeometryJSON rounding coordinates when transforming Geometry to GeoJSON
Asked Answered
M

1

5

Any Geotools developers here? We discovered the following strange behaviour of GeometryJSON:

    Geometry geom = getGeometry();
    System.out.println(geom);

    GeometryJSON g = new GeometryJSON();
    StringWriter sw = new StringWriter();
    try {
        g.write(geom, sw);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(sw.toString());

outputs:

POLYGON((1.1212121214354352354235235423521 2.1212121121,4.454545454545445 3.454544545454454,10.515545445454 20.1545454654664, 1.1212121214354352354235235423521 2.1212121121))

{"type":"Polygon","coordinates":[[[1.1212,2.1212],[4.4545,3.4545],[10.5155,20.1545],[1.1212,2.1212]]]}

The polygon coordinates are rounded. Is this intended?

Macule answered 8/11, 2013 at 12:48 Comment(0)
I
7

GeometryJSON has a constructor to which you can pass the number of decimals.

public GeometryJSON(int decimals)

The constructor you're using instantiates it with 4 decimals. Change your code to:

GeometryJSON g = new GeometryJSON(15);
Invalidate answered 9/11, 2013 at 10:32 Comment(4)
Thx for the answer, but why is it implemented like that? Why doesn´t the write function output the maximum available precision by default? When doing the conversion the other way round, from geoJSON String to Geometry, it behaves conversely (like I expected it to behave), a Geometry geo = g2.read(geoJSONString); outputs a geometry with all decimals included, without the need to specify precision points in constructor.Macule
Presumably the default aims at generating smaller GeoJSON.Invalidate
Maybe, still doesn´t make sense to me, WKTWriter/WKTReader from vividsolutions for example has by default a precision of 16 decimals (full double precision).Macule
16dp in degrees is subnanometer accuracy which I suspect your data source doesn't really haveQuartering

© 2022 - 2025 — McMap. All rights reserved.