如果必须计算点和多边形之间的最近距离(例如,点和湖(naturalearthdata.com)),则可以这样做(取自here):
...
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
,因此您需要将其强制转换为有用的东西。关于java - 多线串和点之间的距离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45144947/