Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a polygon point is inside another in leaflet

I'm having two sets of polygon coordinates selected from leaflet geoJSON map. The parent and child coordinates are coordinates are:

var parentCoordinates=[
    [
        32.05898221582174,
        -28.31004731142091
    ],
    [
        32.05898221582174,
        -28.308044824292978
    ],
    [
        32.06134255975485,
        -28.308044824292978
    ],
    [
        32.06134255975485,
        -28.31004731142091
    ],
    [
        32.05898221582174,
        -28.31004731142091
    ]
]
var childCoordinates=[
  [
    32.059904895722866,
    -28.30970726909422
  ],
  [
    32.059904895722866,
    -28.308743809931784
  ],
  [
    32.06089194864035,
    -28.308743809931784
  ],
  [
    32.06089194864035,
    -28.30970726909422
  ],
  [
    32.059904895722866,
    -28.30970726909422
  ]
]

The child is drawn inside the parent area as shown in the picture: enter image description here

Using Ray Casting algorithm to determine if the point lies inside polygon I 'm not able to determine as the result I'm getting is false. Please let me know where I'm doing wrong or any other way to determine the solution.Thanks

like image 435
forgottofly Avatar asked Dec 14 '16 08:12

forgottofly


People also ask

How do you know if a point is inside a polygon leaflet?

Click on the map or the markers and a pop-up will show indicating if the point you clicked on, or the marker, is in the polygon. Leaflet.

Which test is used to check whether a point is inside or outside of the polygon?

One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray, starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an even number of times.

How do you find an exterior polygon?

To determine the status of a point (xp,yp) consider a horizontal ray emanating from (xp,yp) and to the right. If the number of times this ray intersects the line segments making up the polygon is even then the point is outside the polygon.


1 Answers

You can try Leaflet's api for that - contains . You create a parent polygon with LatLngBounds and then child too.

parentPolygon.contains(childPolygon)
like image 135
Alex Parij Avatar answered Sep 18 '22 19:09

Alex Parij