3

I use GeoTools to read the geometries from a Shapefile as in this example. I have noticed that the coordinates used in the source Shapefile are adjusted to a Java Double type. Consequently the geometries are not exactly the same as in the Shapefile. I would like to increase the precision for these geometries. Any ideas?

For example: I have this geometry from a Shapefile:

POLYGON ((
  4799826.09861662145704 2773995.445373429451138,
  4799743.315226384438574 2773981.06054604658857,
  4799740.81541012506932 2774021.374029533006251,
  4799823.087954664602876 2774033.434061083011329,
  4799826.09861662145704 2773995.445373429451138
))

I have read that geometry of Shapefile using GeoTools, and printed the well known text representation. The output is the following:

POLYGON ((
  4799826.098616621 2773995.4453734295, 
  4799743.315226384 2773981.0605460466, 
  4799740.815410125 2774021.374029533, 
  4799823.087954665 2774033.434061083, 
  4799826.098616621 2773995.4453734295
))
brunorey
  • 2,135
  • 1
  • 18
  • 26
Dimis
  • 41
  • 4
  • can you explain exactly what the difference you are seeing is? More than double precision is rarely need for geographic features unless you are wrangling atoms from orbit. – Ian Turton Oct 06 '15 at 14:11
  • I updated the question with an example @iant. – Dimis Oct 06 '15 at 16:34
  • 1
    Assuming your coordinates are in meters there is no need to worry about any number beyond. 0.001 mm – Ian Turton Oct 06 '15 at 16:36

2 Answers2

3

Best answer

Use org.locationtech.jts.precision.GeometryPrecisionReducer. It has the reduce(Geometry g, PrecisionModel precModel) method.

Bad answer

Using Geotools version 18.0, having loaded your features from the Shapefile, and called getDefaultGeometry() on them.

Call getCoordinates() on your Geometry objects, then loop over those coordinates and calling precisionModel.makePrecise() on each of them. Lastly, construct a new geometry with the fixed precise coordinates.

(I don't have any sample code for your because I'm working in Clojure rather than Java.)

Wrong answer

This is obviously an annoying and roundabout way. Shouldn't there be an easier way? I thought so based on the documentation, but it didn't work.

From PrecisionModel JavaDoc:

JTS input routines automatically round coordinates to the precision model before creating Geometries

To me this implies that I could just reconstruct the geometries using the geometry factory and it would sort it out for me.

Unfortunately, what actually happens is that you get geometries which look like they should intersect, but don't because of tiny floating point errors in their coordinates.

GlennS
  • 5,251
  • 6
  • 28
  • 31
0

I had the same issue. In my case, to get correct data from shapefile by geotools, I needed to set CoordinateReferenceSystem manually. Something like this:

FeatureCollection<SimpleFeatureType, SimpleFeature> inputFeatureCollection = 
    new ReprojectingFeatureCollection(source.getFeatures(), CRS.decode("EPSG:4326"));

http://docs.geotools.org/stable/userguide/library/referencing/crs.html https://gis.stackexchange.com/questions/255930/reproject-features-with-geotools

Guillermo Cacheda
  • 2,162
  • 14
  • 23