X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FGeometry.py;h=2bee60addc97ad27f6cde94d860336b1bf0d105d;hb=d797c850a8493c0027b1e8b8dbc4d3e78840d4d9;hp=89a6cdb929e87bc787574d62396c0fdea21df372;hpb=27ce45a6f4e2844f3d7bc5d570f3cb06e087bb5a;p=dead%2Fcensus-tools.git diff --git a/src/Geometry.py b/src/Geometry.py index 89a6cdb..2bee60a 100644 --- a/src/Geometry.py +++ b/src/Geometry.py @@ -56,7 +56,23 @@ class TwoVector: # If v is neither a TwoVector nor a tuple, we ain't it. return False + + def __add__(self, other_vector): + """ + Add other_vector to this vector, and return a new TwoVector + containing the result. + """ + return TwoVector(self.x + other_vector.x, self.y + other_vector.y) + + def __sub__(self, other_vector): + """ + Subtract other_vector from this vector. Return a new + TwoVector. + """ + return TwoVector(self.x - other_vector.x, self.y - other_vector.y) + + def __str__(self): """ Print the contents of our ordered pair. @@ -85,15 +101,7 @@ class TwoVector: return (self.x * other_vector.x) + (self.y * other_vector.y) - def add(self, other_vector): - """ - Add other_vector to this vector, and return a new TwoVector - containing the result. - """ - return TwoVector(self.x + other_vector.x, self.y + other_vector.y) - - class Polygon: """ Wraps shapely.geometry.Polygon. @@ -145,10 +153,19 @@ class Polygon: def __str__(self): """ - Write out the well-known text representation of this polygon. + Write out the well-known text representation of this polygon, + as well as the class name, which helps distinguish + Geometry.Polygon from shapely.geometry.Polygon.. """ return "Geometry.Polygon<%s>" % self._shapely_polygon.to_wkt() + + def wkt(self): + """ + Return just the well-known text for this Polygon. + """ + return self._shapely_polygon.to_wkt() + @classmethod def from_shapely(self, shapely_polygon): @@ -187,7 +204,7 @@ class Polygon: Translate the self polygon by the TwoVector v. """ regular_coords = self.coords() - f = (lambda p: p.add(v)) + f = (lambda p: p + v) translated_coords = map(f, regular_coords) return Polygon(translated_coords) @@ -225,8 +242,9 @@ class Polygon: # Ex: POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2)) if (text.lower().find('polygon') == -1): return None - - return shapely.wkt.loads(text) + + sp = shapely.wkt.loads(text) + return Polygon.from_shapely(sp) def drag_rectangle(self, v): @@ -241,8 +259,8 @@ class Polygon: initial_dv = self.drag_vertices(v) terminal_dv = [0, 0] # Just initializing it. - terminal_dv[0] = initial_dv[0].add(v) - terminal_dv[1] = initial_dv[1].add(v) + terminal_dv[0] = initial_dv[0] + v + terminal_dv[1] = initial_dv[1] + v return Polygon( (initial_dv[1], initial_dv[0], @@ -252,9 +270,9 @@ class Polygon: def drag(self, v): """ - Drag this polygon along the line v = (x2-x1, y2-y1), and + Drag this polygon along the vector v = (x2-x1, y2-y1), and return the new polygon consisting of the union of all points - that I've contained along the way. + that we've contained along the way. """ terminal_p = self.translate(v) dv_rect = self.drag_rectangle(v)