Geo-location Point Inside Area
I was curious to detect if a known point is inside a polygon. Turns out that the algorithm is far simpler than I thought. Now think of this point as a person inside a building. Is the guy really inside the building?
Here is the test which can be used as an example use case:
- import unittest
-
- from insidePolygon import Point, is_inside_polygon
-
- class insidePolyTest(unittest.TestCase):
-
- def test_defaults(self):
- p = Point()
- self.assertEquals(p.x, 0)
- self.assertEquals(p.y, 0)
-
- def test_defaults(self):
- p = Point(3, 4)
- self.assertEquals(p.x, 3)
- self.assertEquals(p.y, 4)
-
- def test_is_inside_poly(self):
- poly = [Point(1,5), Point(5,5), Point(3,2), Point(1,5)]
- outside_point = Point(1,2)
- self.assertFalse(is_inside_polygon(poly, outside_point))
- inside_point = Point(3,4)
- self.assertTrue(is_inside_polygon(poly, inside_point))
-
- def test_geo_data(self):
- london = Point(51.501249,-0.126271)
- berlin = Point(52.540227,13.420944)
- madrid = Point(40.427994,-3.70182)
- rome = Point(41.922444,12.48436)
- paris = Point(48.860589,2.350674)
- bucharest = Point(44.443945,26.101341)
- # triangle
- poly = [london, berlin, madrid, london]
- self.assertTrue(is_inside_polygon(poly, paris))
- self.assertFalse(is_inside_polygon(poly, bucharest))
- # rectangle
- poly = [london, berlin, rome, madrid, london]
- self.assertTrue(is_inside_polygon(poly, paris))
- self.assertFalse(is_inside_polygon(poly, bucharest))
-
- unittest.main()
Ok. So Paris is inside the triangle made by London, Berlin and Madrid. Bucharest isn't.

Might not be a deal-breaker for some, but I'm really disappointed by "check-in" applications. First because just stating that I'm present at a certain point on the globe is not fun. Secondly, if the application associates that point to a venue, it should better check if the person checking-in is really at that venue, maybe with a small margin of error, like a 500m radius. Why am I allowed to check-in 15.000 km away and get points for that? Yes, I'm talking about Foursquare-ish apps.
Here's the Python code for calculating this:
- class Point:
- x = 0
- y = 0
-
- def __init__(self, x = 0, y = 0):
- self.x = x
- self.y = y
-
- def __str__(self):
- return "(%s, %s)" % (self.x, self.y)
-
- def max(a, b):
- if a > b:
- return a
- return b
-
- def min(a, b):
- if a < b:
- return a
- return b
-
- def is_inside_polygon(poly, point):
- n = len(poly) - 1
- counter = 0
-
- p1 = poly[0]
- for i in range(0, n + 1):
- p2 = poly[i % n]
- if point.y > min(p1.y, p2.y):
- if point.y <= max(p1.y, p2.y):
- if point.x <= max(p1.x, p2.x):
- if p1.y != p2.y:
- xinters = (point.y - p1.y) * (p2.x - p1.x)/(p2.y - p1.y) + p1.x
- if p1.x == p2.x or point.x < xinters:
- counter += 1
- p1 = p2
- if counter % 2 == 0:
- return False
- return True
If Elvis was using Foursquare or Koprol - the cool new Geolocation Social Network from Yahoo - we would have known when he left the building.



Discussion
..or use GeoDjango ;)
Hi, do you have the code in C#?
Thanks a lot, my mail is javica18@hotmail.com
Leave a Comment :
Leave a Comment