0

I am using the ray casting algorithm to determine whether a given point is inside a polygon. The polygon's edges are defined in counterclockwise order. However, I am consistently getting unexpected results where the algorithm indicates that the point is inside the polygon, even when the point is a considerable distance away from the polygon's edges.

Here's the code I am using to perform the check:

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

Geometry is this: WKT format:

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 format:

{
  "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
    }
  ]
}

And point that I am testing is this:

WKT format:

POINT (122.72817993164062 38.141727447509766)

GeoJson format:

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

The whole polygon and point looks something like this:

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

Despite all these efforts, the algorithm consistently gives incorrect results.

Is there something I might be missing in my implementation? Are there any known issues with the JTS library or the ray casting algorithm that could cause this behavior? Any insights or suggestions for troubleshooting would be greatly appreciated.

Here is the geojson visualization of the polygon and Point: Point and POlygon on geojson.io

1 Answers1

0

To me it looks like your point is within the polygon.

enter image description here

enter image description here

Ian Turton
  • 10,018
  • 1
  • 28
  • 47