Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

    }
}
like image 254
Marcos Roriz Junior Avatar asked Jul 15 '16 19:07

Marcos Roriz Junior


People also ask

How do you find the distance between a point and a polygon?

Because a polygon is an area enclosed by an ordered collection of line segments, calculating the distance from a point to a polygon involves identifying the closest line segment to the point, and then Rule 2 is applied to get the distance.

How do you find the distance between geographic coordinates?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

How do you calculate spatial distance?

Distance is relative to the observer. You probably want the distance as measured by static observers (particularly r=const, θ=π/2, ϕ=0), then it is ∫(1−2M/r)−1/2dr.


1 Answers

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();
like image 62
aengus Avatar answered Sep 29 '22 02:09

aengus