在JTS拓扑套件是一个不错的选择,在地理空间范围内正确地计算几何关系。可以使用 Java 库(例如JAK或GIScore)轻松解析 KML ,您可以在其中访问要素的坐标。
这是使用 JTS 测试地理空间点是否在多边形内的 Java 代码片段。
import org.locationtech.jts.geom.*;
// parse KML using JAK or another library
// ...
GeometryFactory gf = new GeometryFactory();
// create polygon
int numPoints = ...
Coordinate[] points = new Coordinate[numPoints];
// set points
points[0] = new Coordinate(...);
//...
LinearRing jtsRing = gf.createLinearRing(points);
Polygon poly = gf.createPolygon(jtsRing, null);
// now create point to test if contained inside polygon
Coordinate coord = new Coordinate(lon, lat);
Point pt = gf.createPoint(coord);
if (poly.contains(pt)) {
// point is contained within bounds of polygon
// do something here
}
Run Code Online (Sandbox Code Playgroud)
contains() 几何谓词在JTS javadoc 中定义。
| 归档时间: |
|
| 查看次数: |
4790 次 |
| 最近记录: |