[英]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. 首先,我是GIS的新手,所以请原谅任何错误。 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
. 在该示例中,从点
p
到多边形的更近距离为d
。 Note: I don't need the point, just the minimum distance. 注意:我不需要这个点,只需要最小距离即可。
After some reading, I've come up with the following minimum working example using the GeoTools API. 阅读后,我使用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: 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 what you are doing is correct, but it will return distance in geodetic units (degrees of arc) rather than metres. 因此,您所做的是正确的,但是它将返回以大地测量单位(弧度)而不是米为单位的距离。 You have two options:
您有两种选择:
To use the GeodeticCalculator you will need to determine the closest point on the polygon boundary to your point. 要使用GeodeticCalculator,您需要确定多边形边界上最接近您的点的点。 eg
DistanceOp.closestPoints(geo, p)[0]
例如,
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.