So I have a shp-file containing a bunch of polygons. In this case, a polygon is a body of in-land water (like lakes and that kind of stuff).
My system is tracking a moving object, so in order to determine what this object is, I would like to see if this object is in water or on land AND how far it is to NEAREST shore (yes, both if it's in or out of water). I will take a sample point from the object once in a while and test it.
The system is written in Java, and I have imported GeoTools snapshot 17. But if other utils is easier to use, there is no requirements to use this.
To test if the point is IN water (that is, inside a polygon), this methods works:
private void findPolygonsForPoint(Coordinate point) {
Filter filter = null;
SimpleFeatureIterator iterator = null;
try {
filter = CQL.toFilter("CONTAINS(the_geom, POINT(" + point.x + " " + point.y + "))");
SimpleFeatureCollection collection = source.getFeatures(filter);
if(collection.size() < 1) {
System.out.println(coordinate2String(point) + " is NOT in a polygon");
} else {
System.out.println(coordinate2String(point) + " IS in a polygon");
insidePolygon++;
iterator = collection.features();
while(iterator.hasNext()) {
SimpleFeature feature = iterator.next();
//find nearest edge of the polygon
}
}
} catch(CQLException e) {
aLog.error("", e);
} catch(IOException e) {
aLog.error("", e);
} finally {
if(iterator != null) {
iterator.close();
}
}
}
Now the questions:
1) If the point is NOT in a polygon, how do I find the nearest polygon in the source (being a SimpleFeatureSource)?
2) How do I find the distance to the edge of the polygon that is closest?
Any help would be highly appreciated! Especially code examples - I'm kind of rusty on math and geometry.
Thank you.