Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if point is within a polygon

Tags:

gps

geojson

Given a GEO-JSON polygon, such as the below:

[
    [15.520376, 38.231155],
    [15.160243, 37.444046],
    [15.309898, 37.134219],
    [15.099988, 36.619987],
    [14.335229, 36.996631],
    [13.826733, 37.104531],
    [12.431004, 37.61295],
    [12.570944, 38.126381],
    [13.741156, 38.034966],
    [14.761249, 38.143874],
    [15.520376, 38.231155]
]

How can I check if a GPS location is within the polygon region?

For example, if the user is at Lat 37.387617, Long 14.458008, how would I go about searching the array?

I don't need someone to necessarily write the code for me, I just don't understand the logic of how I can check. If you have any example (any language) please point me.

like image 623
Drahcir Avatar asked Mar 25 '13 15:03

Drahcir


1 Answers

This task is called point in polygon test.

Gerve has explained the algorithm that is widley used for this task. But this will not help you in implementing it. There are foot traps, like parallel lines.

One of that algorithms is called Crossings Multiply test, which is an optimized variant.

Source code: CrossingsMultiplyTest (last function in the file)

An Overview is given in "Point in Polygon Strategies"

Use longitude for the x coordinate, and latitude for the y coordinate.

like image 114
AlexWien Avatar answered Sep 30 '22 07:09

AlexWien