def clip_to_rect(polygon, bbox): """ clips a polygon to a given bounding box takes in a gisutils.Polygon and gisutils.Bounds2D """ from Polygon.Shapes import Rectangle rect = Rectangle(bbox.width, bbox.height) rect.shift(bbox.left, bbox.top) return clip_to_poly(polygon, rect)
def sub_square_polygon(self, pos, width,height, angle=0): p1 = Rectangle(width,height) p1.shift(-width/2,-height/2) p1.rotate(angle) p1.shift(pos[0],pos[1]) if self.poly == None: self.poly = p1 return check = self.poly-p1 if (self.keepsimple and len(check)<2) or not self.keepsimple: self.poly = check
def operationsExample(): print('### Operations') # create a circle with a hole p1 = Circle(1.0) - Circle(0.5) # create a square p2 = Rectangle(0.7) # shift the square a little bit p2.shift(0.25, 0.35) plist = [p1, p2] slist = ['p1', 'p2'] # addition, the same as logical OR (p1 | p2) p = p1 + p2 p.shift(2.5, 0.0) plist.append(p) slist.append('p1 + p2 (OR)') # subtraction p = p1 - p2 p.shift(5.0, 0.0) plist.append(p) slist.append('p1 - p2') # subtraction p = p2 - p1 p.shift(7.5, 0.0) plist.append(p) slist.append('p2 - p1') # logical AND p = p2 & p1 p.shift(10.0, 0.0) plist.append(p) slist.append('p2 AND p1') # logical XOR p = p2 ^ p1 p.shift(12.5, 0.0) plist.append(p) slist.append('p2 XOR p1') # draw the results of the operations writeSVG('Operations.svg', plist, width=800, labels=slist, labels_centered=True)
def _merge_boxes( boxes ): ''' Merges a list of points specifying contiguous boxes into a single Polygon. Returns the polygon, list of points on the polygon. ''' # bboxArea = the union of all the bounding boxes on the route bboxArea = None # union all the boxes together for i in xrange(0,len(boxes),4): # Make a Rectangle out of the width/height of a bounding box # longitude = x, latitude = y theRect = Rectangle( abs(boxes[i] - boxes[i+2]), abs(boxes[i+1] - boxes[i+3]) ) theRect.shift( boxes[i+2], boxes[i+3] ) bboxArea = bboxArea + theRect if bboxArea else theRect # turn bboxArea into a list of points bboxContour = [list(t) for t in bboxArea.contour( 0 )] return bboxArea, bboxContour
def searchPoints( request ): postData = json.loads(request.raw_post_data) rectangles = postData['rectangles'] # bboxArea = the union of all the bounding boxes on the route bboxArea = None for i in xrange(0,len(rectangles),4): # Make a Rectangle out of the width/height of a bounding box # longitude = x, latitude = y theRect = Rectangle( abs(rectangles[i] - rectangles[i+2]), abs(rectangles[i+1] - rectangles[i+3]) ) theRect.shift( rectangles[i+2], rectangles[i+3] ) bboxArea = bboxArea + theRect if bboxArea else theRect bboxArea = [list(t) for t in bboxArea.contour( 0 )] # TODO: pull objects out of database using bboxArea points = { "points" : [p.position for p in Point.objects( position__within_polygon=bboxArea )] } return HttpResponse( json.dumps(points), mimetype="application/json" )
def operationsExample(): # create a circle with a hole p1 = Circle(1.0) - Circle(0.5) # create a square p2 = Rectangle(0.7) # shift the square a little bit p2.shift(0.25, 0.35) plist = [p1, p2] # addition, the same as logical OR (p1 | p2) p = p1 + p2 p.shift(2.5, 0.0) plist.append(p) # subtraction p = p1 - p2 p.shift(5.0, 0.0) plist.append(p) # subtraction p = p2 - p1 p.shift(7.5, 0.0) plist.append(p) # logical AND p = p2 & p1 p.shift(10.0, 0.0) plist.append(p) # logical XOR p = p2 ^ p1 p.shift(12.5, 0.0) plist.append(p) # draw the results of the operations writeSVG('Operations.svg', plist, width=800)
from Polygon import * from Polygon.Shapes import Circle, Star, Rectangle from Polygon.IO import writeSVG p = Circle(5.5, points=128) p -= Circle(4.4, points=128) p += Circle(3.3, points=128) p -= Circle(2.2, points=128) p += Star(1.1) l = Rectangle(4, 14) l.shift(-4.3, -10.5) r = p ^ l writeSVG('logo.svg', [r], width=800)