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?

chris
  • 600
  • 7
  • 21

1 Answers1

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);
Boj
  • 3,923
  • 3
  • 22
  • 39
  • 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. – chris Nov 09 '13 at 19:12
  • 1
    Presumably the default aims at generating smaller GeoJSON. – Boj Nov 10 '13 at 02:55
  • 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). – chris Nov 11 '13 at 10:42
  • 16dp in degrees is subnanometer accuracy which I suspect your data source doesn't really have – Ian Turton Feb 29 '16 at 14:43