多线串与点之间的距离

distance between multilinestring and point

如果我必须计算点和多边形之间的最近距离(例如点和湖 (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);
        }
...

到此为止。

但是,我怎样才能找到点和海岸线之间的最近距离,这是一个多线串而不是多边形。 或者,如果我有线串?

我应该遵循什么方法?LocationIndexedLineLinearLocation 呢?

我认为您可以通过将行更改为:

来解决此问题
LocationIndexedLine line = new LocationIndexedLine(((Geometry)
     feature.getDefaultGeometry()));

出于某种原因 getDefaultGeometry returns 一个 Object 所以你需要把它转换成有用的东西。