GeoJSON:检查地理坐标是否在面内

时间:2019-02-09 12:31:56

标签: java geometry geojson geotools point-in-polygon

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

[
    {
        "geometry": {
        "type": "Polygon",
        "coordinates": 
[
        [
          [
            9.137248,
            48.790411
          ],
          [
            9.137248,
            48.790263
          ],
          [
            9.13695,
            48.790263
          ],
          [
            9.137248,
            48.790411
          ]
        ]
      ]
    }
    }
  ]

借助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);

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

结果

Type =多边形

点位于多边形= false

a)您能否看到pointIsInPolygon为假的原因。我想念什么吗?

b)我应在此处给出什么坐标,以使pointIsInPolygon产生true

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

1 个答案:

答案 0 :(得分:1)

您的点在多边形之外,因此结果正确。这里的红点是您的,如果将其更改为gf.createPoint(new Coordinate(9.13716433655009652, 48.79030985534630815));(绿点),它将返回true。

enter image description here

相关问题