Search code examples
geojsongeotools

Geotools GeometryJSON rounding coordinates when transforming Geometry to GeoJSON


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?


Solution

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