Skip to content

Instantly share code, notes, and snippets.

@aidenprice
Last active August 29, 2015 14:03
Show Gist options
  • Save aidenprice/9bde42e75b7b024aa4d0 to your computer and use it in GitHub Desktop.
Save aidenprice/9bde42e75b7b024aa4d0 to your computer and use it in GitHub Desktop.
A Python class to describe a user polygon with a series of APLocationObjects. Can determine whether a test point (also an APLocationObject) lies within the polygon using the ray-casting algorithm. Created for my Curtin University, Masters of Geospatial Science, Spatial Computations 100, second assignment, BoreholeWrangler V3.0.
from APLocationObject import LocationObject
class PolygonObject:
# Class properties
lVertices = []
iNumberOfVertices = None
# Class methods
def __init__(self, lPoints):
self.iNumberOfVertices = len(lPoints)
if self.iNumberOfVertices == 0: # Polygon must have at least three points to describe an area.
raise ValueError("Zero points provided to the PolygonObject.")
elif 1 <= self.iNumberOfVertices <= 2:
raise ValueError("Not enough points provided to create an area. Three points needed for an area (at a "
"minimum).")
else:
for location in lPoints:
if isinstance(location, LocationObject):
self.lVertices.append(location)
else:
raise TypeError("An object of a class other than LocationObject passed to the PolygonObject.")
def determineIfPointLiesWithinPolygon(self, locationToTest):
# SEE NEXT SNIPPET FOR IMPLEMENTATION
def describePolygon(self):
print "Number of vertices = " + str(self.iNumberOfVertices)
for point in self.lVertices:
iPointNumber = self.lVertices.index(point) + 1
descriptionString = "Point #{0}:\t{1}\n".format(str(iPointNumber), point.describeLocation(False))
print descriptionString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment