Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 961
michele Avatar asked Aug 01 '16 07:08

michele


1 Answers

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
}
like image 85
Lars Avatar answered Oct 06 '22 22:10

Lars