7

I am looking for an algorithm through I can create a geofence and check if a device is entering/leaving the fence. I have looked at point in polygon algorithms (ray casting and winding number) but are there any algorithms which can be applied to circle and any irregular shapes as well? The important constraint is time efficiency.

Thank you.

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
Nilgiri
  • 71
  • 1
  • 1
  • 3

5 Answers5

4

Here is the c code algorithm that is simple to understand:

http://alienryderflex.com/polygon/

Josh C
  • 89
  • 1
  • 11
3

Well circles are pretty easy (at least if you are assuming a locally flat surface) - just absolute distance from a point.

The normal way if you need speed is a cascade where you check a circle first, or a square around the point, then a convex polygon, then a more detailed polygon if necessary.

How are you defining the irregular shape - if it's not a polygon?

ps see How to test if a point is inside of a convex polygon in 2D integer coordinates?

Community
  • 1
  • 1
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • N.B. Rather than a square, it is usually a rectangle and often called a "bounding box". – David Conrad Jun 01 '12 at 21:54
  • @DavidConrad - yes, I oversimplified. Alternatively it IS a square you just change your coordinates ;-) – Martin Beckett Jun 01 '12 at 22:42
  • 1
    Sure. :) I just wanted to give the OP the term "bounding box" and a little more info, which might be useful to search for, and didn't feel it was worth a separate answer, so I put it here, as a comment to your answer. Cheers! – David Conrad Jun 01 '12 at 23:13
2

I found this solution for python. This worked as far as I know.

from shapely.geometry import Point, Polygon
import matplotlib.path as mpltPath
from functools import reduce
import operator
import math
coords = [[ 12.934158,77.609316], [ 12.934796,77.609852],[ 12.934183,77.610646], [ 12.933551,77.610100], [12.934158,77.609316]]

#sorting the geofence coords in clockwise direction
center = tuple(map(operator.truediv, reduce(lambda x, y: map(operator.add, x, y), coords), [len(coords)] * 2))
coords = sorted(coords, key=lambda coord: (-135 - math.degrees(math.atan2(*tuple(map(operator.sub, coord, center))[::-1]))) % 360)

#Testing if a point inside or outside
poly = Polygon(coords)
point = Point(12.933556,77.609854)
print(coords)
if(point.within(poly)):
    print("Inside")
else:
    print("Outside")

Thank You!

R Nanthak
  • 344
  • 3
  • 17
1

Winding Number Method

But the bottom line is that for both geometric correctness and efficiency reasons, the Winding Number algorithm should always be preferred for determining the inclusion of a point in a polygon.

LucasM
  • 431
  • 4
  • 11
0

Take a look at quadtrees, spatial index, quadkeys and r-trees.

Micromega
  • 12,486
  • 7
  • 35
  • 72