7

I am using Java Geotools library for check if a POINT(...) is contained in a POLYGON(...).

I have done it with:

Geometry sPG = reader.read(wktStartPoint); //startpointgeometry
Geometry sEG = reader.read(wktEndPoint);
if(wktLayerGeo.contains(sPG) || wktLayerGeo.contains(sEG)){
 // do something
}

But now I have to set a tolerance: I would check if a point is contained in the polygon with a tolerance distance of 50 km for example.

Can I do it with GeoTools?

Thank you

michele
  • 26,348
  • 30
  • 111
  • 168
  • Michele. 2 years had it been. I think your question will help me ! I have a list of polygon coordinates `-119.00072399999999 35.36158, -118.99903 35.361576, -118.999026 35.362579, -118.999023 35.363482, -118.999019 35.36432, -118.999408 35.364847999999995` Can you share on how you have coded to check if a point (x,y) is inside the polygon coordinates? – Mike Jan 17 '19 at 07:20

2 Answers2

2

You can use the JTS buffer method on your Polygon geometry (API):

double yourToleranceDistance = 2;
int numberOfSegmentsPerQuadrant = 2;
// get the geometry with your tolerance
Polygon wktLayerGeoWithTolerance = (Polygon) wktLayerGeo.buffer(yourToleranceDistance, numberOfSegmentsPerQuadrant, BufferParameters.CAP_SQUARE);
// continue with your code...
if(wktLayerGeoWithTolerance.contains(sPG) || wktLayerGeoWithTolerance.contains(sEG)){
     // do something
}
Lars
  • 2,315
  • 2
  • 24
  • 29
1

You can use the DWithin operator which will check if a point (or other geometry) is within a provided distance of a geometry. Note the distance is always in the units of your data's projection regardless of the units string.

double distance = 10.0d;

FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
Filter filter = ff.dwithin(ff.property("POLYGON"), ff.literal(point), distance, uom.toString());

return featureSource.getFeatures(filter);
Ian Turton
  • 10,018
  • 1
  • 28
  • 47