def restore_poly_from_path_str(path_str): """ restores a list of polygons from a SVG path string """ contours = path_str.split('Z') # last contour may be empty from Polygon import Polygon as Poly poly = Poly() for c_str in contours: if c_str.strip() != "": pts_str = c_str.strip()[1:].split("L") pts = [] for pt_str in pts_str: x, y = map(float, pt_str.split(',')) pts.append((x, y)) poly.addContour(pts, is_clockwise(pts)) return poly
def shape_center(shape): """ computes the center of gravity of a shapefile multi-polygon """ from Polygon import Polygon parts = shape.parts[:] parts.append(len(shape.points)) # check for countries that cross the 180° longitude far_east = False far_west = False for i in range(len(parts)-1): pts = shape.points[parts[i]:parts[i+1]] if len(pts) == 0: continue if pts[0][0] < -90: far_west = True if pts[0][0] > 90: far_east = True poly = Polygon() for i in range(len(parts)-1): pts = shape.points[parts[i]:parts[i+1]] if far_east and far_west: # correct points for j in range(len(pts)): if pts[j][0] < 0: pts[j][0] += 360 poly.addContour(pts) return poly.center()
def test_polygon(dxf_file): # 从dxf文件中提取数据 datas = find_shape_from_dxf(dxf_file) shapes = list() for i in range(0, len(datas)): p = Polygon() shapes.append(p) p.addContour(datas[i]) p.shift(0,10) x, y = p.center() p.rotate(math.pi, x, y) return shapes
def apply_contours(self, contours): """ constructs a Polygon from contours """ self.contours = contours from Polygon import Polygon as GPCPoly poly = GPCPoly() skip = 0 for pts_ in contours: pts = [] for pt in pts_: if 'deleted' in pt and pt.deleted is True: skip += 1 continue pts.append((pt[0], pt[1])) ishole = utils.is_clockwise(pts) if len(pts) > 2: poly.addContour(pts, ishole) self.poly = poly
def generate_polygon(coords, scale): """ Generate a polygon that encompasses a set of grid coordinates. :param coords: List of coordinates to draw polygon around :param scale: Area around each grid square (1/scale in degrees is area of each point) :return: Polygon of area representing the set of points :return: Polygon of area representing the set of points """ out_poly = Polygon() h_step = 0.51/scale for pos in coords: # sum up each polygon out_poly.addContour([ (pos[0] - h_step, pos[1] - h_step), (pos[0] + h_step, pos[1] - h_step), (pos[0] + h_step, pos[1] + h_step), (pos[0] - h_step, pos[1] + h_step) ]) out_poly.simplify() return out_poly
HEX_HEIGH = 24 f1,f2 = 19, 25 fname = sys.argv[1] img = Image.open(fname) img.putalpha(0xFFFFFF) poly = Polygon() poly.addContour([ (0,HEX_HEIGH), (f1,HEX_HEIGH*2), (f1+f2,HEX_HEIGH*2), (f1+f1+f2,HEX_HEIGH), (f1+f2,0), (f1,0) ]) w,h = img.size for x in range(w): for y in range(h): if poly.isInside(x,y): continue img.putpixel((x, y), 0)