JTS::光线投射算法为多边形内的点检查提供意外结果

问题描述 投票:0回答:1

我正在使用光线投射算法来确定给定点是否在多边形内部。多边形的边按逆时针顺序定义。然而,我始终得到意想不到的结果,其中算法表明该点位于多边形内部,即使该点距离多边形边缘相当远。

这是我用来执行检查的代码:

private boolean isPointInsideGeometry(Point point, Geometry geometry) {
    IndexedPointInAreaLocator indexedPointInAreaLocator = new IndexedPointInAreaLocator(geometry);
    int location = indexedPointInAreaLocator.locate(point.getCoordinate());
    return location == Location.INTERIOR;
}

几何是这样的: WKT 格式:

POLYGON ((122.294312 37.40071, 123.75 39.859155, 122.618408 39.59299, 122.091064 39.266284, 121.68457 39.04052, 121.217651 38.796908, 121.190186 38.908133, 121.723022 39.125799, 121.80542 39.406489, 121.712036 39.487085, 121.569214 39.414977, 121.415405 39.470125, 121.629639 39.601456, 121.514282 39.791655, 121.937256 40.010787, 122.360229 40.530502, 121.948242 40.90521, 121.83288574218749 40.96123389519331, 121.49231 40.930115, 120.986938 40.896906, 120.574951 40.526327, 120.162964 40.18307, 119.553223 39.972911, 119.333496 39.783213, 119.196167 39.474365, 118.866577 39.232253, 118.504028 39.198205, 118.405151 39.117276, 118.223877 39.121537, 118.048096 39.266284, 117.784424 39.206719, 117.526245 38.766933, 117.603149 38.449287, 117.844849 38.242495, 118.097534 38.125915, 118.509521 38.00482, 118.690796 38.074041, 118.839111 38.061067, 118.932495 38.022131, 119.025879 37.88786, 119.091797 37.788081, 119.240112 37.796763, 119.21814 37.696861, 118.97644 37.705553, 118.883057 37.561997, 118.88855 37.378888, 118.905029 37.239075, 119.278564 37.081476, 119.393921 37.028869, 119.690552 37.09024, 119.882812 37.230328, 119.94873 37.343959, 120.465088 37.714245, 120.72876 37.809784, 120.882568 37.779399, 121.063843 37.653383, 121.201172 37.527154, 121.470337 37.448697, 121.679077 37.4138, 121.942749 37.448697, 122.052612 37.45447, 122.294312 37.40071))

Geojson 格式:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": null,
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                122.294312,
                37.40071
              ],
              [
                123.75,
                39.859155
              ],
              [
                122.618408,
                39.59299
              ],
              [
                122.091064,
                39.266284
              ],
              [
                121.68457,
                39.04052
              ],
              [
                121.217651,
                38.796908
              ],
              [
                121.190186,
                38.908133
              ],
              [
                121.723022,
                39.125799
              ],
              [
                121.80542,
                39.406489
              ],
              [
                121.712036,
                39.487085
              ],
              [
                121.569214,
                39.414977
              ],
              [
                121.415405,
                39.470125
              ],
              [
                121.629639,
                39.601456
              ],
              [
                121.514282,
                39.791655
              ],
              [
                121.937256,
                40.010787
              ],
              [
                122.360229,
                40.530502
              ],
              [
                121.948242,
                40.90521
              ],
              [
                121.83288574218749,
                40.96123389519331
              ],
              [
                121.49231,
                40.930115
              ],
              [
                120.986938,
                40.896906
              ],
              [
                120.574951,
                40.526327
              ],
              [
                120.162964,
                40.18307
              ],
              [
                119.553223,
                39.972911
              ],
              [
                119.333496,
                39.783213
              ],
              [
                119.196167,
                39.474365
              ],
              [
                118.866577,
                39.232253
              ],
              [
                118.504028,
                39.198205
              ],
              [
                118.405151,
                39.117276
              ],
              [
                118.223877,
                39.121537
              ],
              [
                118.048096,
                39.266284
              ],
              [
                117.784424,
                39.206719
              ],
              [
                117.526245,
                38.766933
              ],
              [
                117.603149,
                38.449287
              ],
              [
                117.844849,
                38.242495
              ],
              [
                118.097534,
                38.125915
              ],
              [
                118.509521,
                38.00482
              ],
              [
                118.690796,
                38.074041
              ],
              [
                118.839111,
                38.061067
              ],
              [
                118.932495,
                38.022131
              ],
              [
                119.025879,
                37.88786
              ],
              [
                119.091797,
                37.788081
              ],
              [
                119.240112,
                37.796763
              ],
              [
                119.21814,
                37.696861
              ],
              [
                118.97644,
                37.705553
              ],
              [
                118.883057,
                37.561997
              ],
              [
                118.88855,
                37.378888
              ],
              [
                118.905029,
                37.239075
              ],
              [
                119.278564,
                37.081476
              ],
              [
                119.393921,
                37.028869
              ],
              [
                119.690552,
                37.09024
              ],
              [
                119.882812,
                37.230328
              ],
              [
                119.94873,
                37.343959
              ],
              [
                120.465088,
                37.714245
              ],
              [
                120.72876,
                37.809784
              ],
              [
                120.882568,
                37.779399
              ],
              [
                121.063843,
                37.653383
              ],
              [
                121.201172,
                37.527154
              ],
              [
                121.470337,
                37.448697
              ],
              [
                121.679077,
                37.4138
              ],
              [
                121.942749,
                37.448697
              ],
              [
                122.052612,
                37.45447
              ],
              [
                122.294312,
                37.40071
              ]
            ]
          ]
        ]
      },
      "id": 0
    }
  ]
}

我正在测试的一点是:

WKT 格式:

POINT (122.72817993164062 38.141727447509766)

GeoJson 格式:

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "coordinates": [
      122.81726307914414,
      38.285666277485404
    ],
    "type": "Point"
  }
}

整个多边形和点看起来像这样:

Polygon and Point mentioned in the question, one can draw it on geojson.io

尽管做出了所有这些努力,算法始终给出错误的结果。

我的实施过程中可能遗漏了什么吗? JTS 库或光线投射算法是否存在可能导致此行为的已知问题?任何有关故障排除的见解或建议将不胜感激。

geospatial polygon raycasting point-in-polygon jts
1个回答
0
投票

对我来说,看起来你的点在多边形内。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.