Home » Odeon Blogs » Tudor, UI/UX Maestro »

Geo-location Point Inside Area

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:

  1. import unittest
  2. from insidePolygon import Point, is_inside_polygon
  3. class insidePolyTest(unittest.TestCase):
  4. def test_defaults(self):
  5. p = Point()
  6. self.assertEquals(p.x, 0)
  7. self.assertEquals(p.y, 0)
  8. def test_defaults(self):
  9. p = Point(3, 4)
  10. self.assertEquals(p.x, 3)
  11. self.assertEquals(p.y, 4)
  12. def test_is_inside_poly(self):
  13. poly = [Point(1,5), Point(5,5), Point(3,2), Point(1,5)]
  14. outside_point = Point(1,2)
  15. self.assertFalse(is_inside_polygon(poly, outside_point))
  16. inside_point = Point(3,4)
  17. self.assertTrue(is_inside_polygon(poly, inside_point))
  18. def test_geo_data(self):
  19. london = Point(51.501249,-0.126271)
  20. berlin = Point(52.540227,13.420944)
  21. madrid = Point(40.427994,-3.70182)
  22. rome = Point(41.922444,12.48436)
  23. paris = Point(48.860589,2.350674)
  24. bucharest = Point(44.443945,26.101341)
  25. # triangle
  26. poly = [london, berlin, madrid, london]
  27. self.assertTrue(is_inside_polygon(poly, paris))
  28. self.assertFalse(is_inside_polygon(poly, bucharest))
  29. # rectangle
  30. poly = [london, berlin, rome, madrid, london]
  31. self.assertTrue(is_inside_polygon(poly, paris))
  32. self.assertFalse(is_inside_polygon(poly, bucharest))
  33. unittest.main()

Ok. So Paris is inside the triangle made by London, Berlin and Madrid. Bucharest isn't.

Europe

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:

  1. class Point:
  2. x = 0
  3. y = 0
  4. def __init__(self, x = 0, y = 0):
  5. self.x = x
  6. self.y = y
  7. def __str__(self):
  8. return "(%s, %s)" % (self.x, self.y)
  9. def max(a, b):
  10. if a > b:
  11. return a
  12. return b
  13. def min(a, b):
  14. if a < b:
  15. return a
  16. return b
  17. def is_inside_polygon(poly, point):
  18. n = len(poly) - 1
  19. counter = 0
  20. p1 = poly[0]
  21. for i in range(0, n + 1):
  22. p2 = poly[i % n]
  23. if point.y > min(p1.y, p2.y):
  24. if point.y <= max(p1.y, p2.y):
  25. if point.x <= max(p1.x, p2.x):
  26. if p1.y != p2.y:
  27. xinters = (point.y - p1.y) * (p2.x - p1.x)/(p2.y - p1.y) + p1.x
  28. if p1.x == p2.x or point.x < xinters:
  29. counter += 1
  30. p1 = p2
  31. if counter % 2 == 0:
  32. return False
  33. 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.


Categories: curiosity iphone mobile sparks

Discussion

  1. ..or use GeoDjango ;)


  2. Hi, do you have the code in C#?
    Thanks a lot, my mail is javica18@hotmail.com




Leave a Comment :

(required)


(required)




(required)




(required)






Leave a Comment


Page generated in: 0.18s