George
George

Reputation: 5681

distance between multilinestring and point

If I have to calculate the nearest distance between a point and a polygon (point and lake (naturalearthdata.com) for example), I can do (taken from here):

...
LocationIndexedLine line = new LocationIndexedLine(((MultiPolygon)    feature.getDefaultGeometry()).getBoundary());
LinearLocation here = line.project(coordinate);
Coordinate point = line.extractPoint(here);
...

and :

      ...
      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);
        }
...

Until here ok.

But, how can I find the closest distance between a point and a coastline which is a multilinestring and not a polygon. Or, if I have a linestring?

What approach should I follow?What about LocationIndexedLine and LinearLocation?

Upvotes: 1

Views: 1047

Answers (1)

Ian Turton
Ian Turton

Reputation: 10976

I think you can solve this by changing the line to:

LocationIndexedLine line = new LocationIndexedLine(((Geometry)
     feature.getDefaultGeometry()));

For some reason getDefaultGeometry returns an Object so you need to cast it to something useful.

Upvotes: 1

Related Questions