How to calculate the distance in meters between a geographic point and a given polygon?
Asked Answered
S

1

15

First of all, I'm new to GIS, so pardon any mistakes. I need to discover the distance between a latitude and longitude point and a latitude/longitude polygon (regular or not). Precisely, I need to discover the minimal distance from a given point to a point in the polygon's boundary, as illustrated below. In the example, the closer distance from point p to the polygon is d. Note: I don't need the point, just the minimum distance.

Problem Illustration

After some reading, I've come up with the following minimum working example using the GeoTools API. However, I think I'm messing up in the output. Can anyone enlight me on how to get the minimum distance between a point and polygon in mteres?

MWE.java:

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class MWE {

    public static void main(String[] args) throws Exception {
        GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();

        Coordinate[] c = new Coordinate[5];
        c[0] = new Coordinate(-49.242986, -16.662430);
        c[1] = new Coordinate(-49.241999, -16.664465);
        c[2] = new Coordinate(-49.239146, -16.663828);
        c[3] = new Coordinate(-49.239832, -16.661443);
        c[4] = new Coordinate(-49.242986, -16.662430);

        Geometry geo = gf.createPolygon(c);

        Point p = gf.createPoint(new Coordinate(-49.246870, -16.665493));

        double distance = geo.distance(p);

        System.out.println("Distance: " + distance);

    }
}
Spoof answered 15/7, 2016 at 19:50 Comment(5)
Are you working with a regular polygon ?Pb
You should clarify what you mean by distance between point and polygon. Do you mean from the polygon centroid or from the closest point on the boundary?Ascent
Any polygon. It is to one of the polygons points. I tried to clarify in the question.Spoof
Do you mean that you need to find minimum of the distances between a point and 5 points ? Doesn't that mean that you just need to know how to find distance between two lat/long points ?Pb
@Pb Actually is any point in the polygon boundary. I'll try to add an image that explain this.Spoof
A
17

So what you are doing is correct, but it will return distance in geodetic units (degrees of arc) rather than metres. You have two options:

  1. transform the point and the polygon geometries to a planar reference system http://docs.geotools.org/latest/tutorials/geometry/geometrycrs.html
  2. Use the GeodeticCalculator http://docs.geotools.org/stable/userguide/library/referencing/calculator.html

To use the GeodeticCalculator you will need to determine the closest point on the polygon boundary to your point. e.g. DistanceOp.closestPoints(geo, p)[0]

// adapted from http://docs.geotools.org/stable/userguide/library/referencing/calculator.html
CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
GeodeticCalculator gc = new GeodeticCalculator(crs);
gc.setStartingPosition( JTS.toDirectPosition(  DistanceOp.closestPoints(geo, p)[0], crs ) );
gc.setDestinationPosition( JTS.toDirectPosition( p, crs ) );

double distance = gc.getOrthodromicDistance();
Ascent answered 15/7, 2016 at 21:3 Comment(2)
major key 🔑 💪 still can't understand how this answer only has 9 upvotes with 7k views. Thanks!Requirement
Instead of creating GeodeticCalculator and setting positions, you can use JTS.orthodromicDistance(), it does pretty much the same but you save some spaceAgonic

© 2022 - 2025 — McMap. All rights reserved.