如果我必须计算点和多边形之间的最近距离(例如点和湖(naturalearthdata.com((,我可以这样做(从这里获取(:
...
LocationIndexedLine line = new LocationIndexedLine(((MultiPolygon) feature.getDefaultGeometry()).getBoundary());
LinearLocation here = line.project(coordinate);
Coordinate point = line.extractPoint(here);
...
和:
...
NearestPolygon polyFinder = new NearestPolygon(features);
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
Point p = gf.createPoint(new Coordinate(-49.2462798, -16.7723987));
Point pointOnLine = polyFinder.findNearestPolygon(p);
if (!pointOnLine.isEmpty()) {
System.out.println(pointOnLine + " is closest to " + p);
SimpleFeature lastMatched2 = polyFinder.getLastMatched();
String attribute = (String) lastMatched2.getAttribute("name");
if(attribute.isEmpty()) {
attribute = (String) lastMatched2.getAttribute("note");
}
if (((Geometry) (lastMatched2.getDefaultGeometry())).contains(p)) {
System.out.println("is in lake " + attribute);
} else {
System.out.println("nearest lake is " + attribute);
}
...
直到这里好吧。
但是,我怎样才能找到点和海岸线之间的最近距离,该距离是多线串而不是多边形。 或者,如果我有线串?
我应该遵循什么方法?LocationIndexedLine
和LinearLocation
呢?
我认为您可以通过将行更改为:
LocationIndexedLine line = new LocationIndexedLine(((Geometry)
feature.getDefaultGeometry()));
出于某种原因,getDefaultGeometry 返回一个Object
因此您需要将其转换为有用的内容。