如何计算地理点和给定多边形之间的距离(以米为单位)?
How to calculate the distance in meters between a geographic point and a given polygon?
首先,我是GIS新手,如有错误请见谅。我需要发现纬度和经度点与 latitude/longitude 多边形(规则与否)之间的距离。准确地说,我需要发现从给定点到多边形边界中的点的最小距离,如下图所示。在示例中,点 p
到多边形的较近距离是 d
。注意:我不需要点,只需要最小距离。
经过一些阅读,我想出了以下使用 GeoTools API 的最小工作示例。但是,我认为我搞砸了输出。谁能启发我如何获得以米为单位的点和多边形之间的最小距离?
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);
}
}
所以您所做的是正确的,但它将 return 以大地测量单位(弧度)而不是米为单位的距离。您有两个选择:
- 将点和多边形几何图形转换为平面参考系http://docs.geotools.org/latest/tutorials/geometry/geometrycrs.html
- 使用大地测量计算器http://docs.geotools.org/stable/userguide/library/referencing/calculator.html
要使用 GeodeticCalculator,您需要确定多边形边界上离您的点最近的点。例如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();
首先,我是GIS新手,如有错误请见谅。我需要发现纬度和经度点与 latitude/longitude 多边形(规则与否)之间的距离。准确地说,我需要发现从给定点到多边形边界中的点的最小距离,如下图所示。在示例中,点 p
到多边形的较近距离是 d
。注意:我不需要点,只需要最小距离。
经过一些阅读,我想出了以下使用 GeoTools API 的最小工作示例。但是,我认为我搞砸了输出。谁能启发我如何获得以米为单位的点和多边形之间的最小距离?
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);
}
}
所以您所做的是正确的,但它将 return 以大地测量单位(弧度)而不是米为单位的距离。您有两个选择:
- 将点和多边形几何图形转换为平面参考系http://docs.geotools.org/latest/tutorials/geometry/geometrycrs.html
- 使用大地测量计算器http://docs.geotools.org/stable/userguide/library/referencing/calculator.html
要使用 GeodeticCalculator,您需要确定多边形边界上离您的点最近的点。例如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();