java - How to calculate the distance in meters between a geographic point and a given polygon? -
first of all, i'm new gis, pardon mistakes. need discover distance between latitude , longitude point , latitude/longitude polygon (regular or not). precisely, need discover minimal distance given point point in polygon's boundary, illustrated below. in example, closer distance point p
polygon d
. note: don't need point, minimum distance.
after reading, i've come following minimum working example using geotools api. however, think i'm messing in output. can enlight me on how minimum distance between point , 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); } }
so doing correct, return distance in geodetic units (degrees of arc) rather metres. have 2 options:
- transform point , polygon geometries planar reference system http://docs.geotools.org/latest/tutorials/geometry/geometrycrs.html
- use geodeticcalculator http://docs.geotools.org/stable/userguide/library/referencing/calculator.html
to use geodeticcalculator need determine closest point on polygon boundary point. e.g. distanceop.closestpoints(geo, p)[0]
// adapted 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();
Comments
Post a Comment