如果我必须计算点和多边形之间的最近距离(例如点和湖(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
怎么样?
答案 0 :(得分:1)
我认为您可以通过将行更改为:
来解决此问题LocationIndexedLine line = new LocationIndexedLine(((Geometry)
feature.getDefaultGeometry()));
由于某种原因,getDefaultGeometry会返回Object
,因此您需要将其强制转换为有用的东西。