michele
michele

Reputation: 26598

Java geotools check if point is contained in polygon with tolerance

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

Upvotes: 7

Views: 3651

Answers (2)

Lars
Lars

Reputation: 2485

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
}

Upvotes: 2

Ian Turton
Ian Turton

Reputation: 10976

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

Upvotes: 1

Related Questions