GeoJSON:检查地理坐标是否在多边形内

Aru*_*mar 0 java geometry point-in-polygon geojson geotools

我有以下 GeoJSON 数据文件,其中包含一个带坐标的多边形。

[
    {
        "geometry": {
        "type": "Polygon",
        "coordinates": 
[
        [
          [
            9.137248,
            48.790411
          ],
          [
            9.137248,
            48.790263
          ],
          [
            9.13695,
            48.790263
          ],
          [
            9.137248,
            48.790411
          ]
        ]
      ]
    }
    }
  ]
Run Code Online (Sandbox Code Playgroud)

在 的帮助下org.geotools.geojson.geom.GeometryJSON,我正在解析com.vividsolutions.jts.geom.Polygon类中的 JSON 坐标,如下所示。并检查是否Coordinate(9.13710, 48.790360)在这个多边形内。

GeometryJSON g = new GeometryJSON();
         com.vividsolutions.jts.geom.Polygon polygon = g.readPolygon(new File(fileLocation));
         System.out.println("Type="+polygon.getGeometryType());

         GeometryFactory gf = new GeometryFactory();
         boolean pointIsInPolygon = polygon.contains(gf.createPoint(new Coordinate(9.13710, 48.790360)));
         System.out.println("Point is in polygon="+pointIsInPolygon);
Run Code Online (Sandbox Code Playgroud)

但是我的程序总是给我下面的结果,即给定的坐标不在多边形中。

结果

类型=多边形

点在多边形中=假

a) 你能看出为什么pointIsInPolygon是假的。我错过了什么吗?

b) 我应该在这里给出什么坐标,所以pointIsInPolygon结果是true

c ) 有没有其他方法可以解析多边形中给定的 JSON 文件并确认坐标是否位于多边形中?

Ian*_*ton 5

你的点在多边形之外,所以结果是正确的。这里红点是你的,如果你把它改成gf.createPoint(new Coordinate(9.13716433655009652, 48.79030985534630815));(绿点),它会返回真。

在此处输入图片说明