def getSgPolyFromArr(self, arr): allEdges = [] for e in arr.halfedges: edge = [e.source().point(), e.target().point()] edgeRev = [e.target().point(), e.source().point()] if not edgeRev in allEdges and not edge in allEdges: allEdges.append([e.source().point(), e.target().point()]) polyPts = [] prevEndPt = allEdges[0][1] polyPts.append(allEdges[0][0]) allEdges.pop(0) while len(allEdges) > 0: # print(len(allEdges)) for e in allEdges: if prevEndPt == e[0]: polyPts.append(prevEndPt) prevEndPt = e[1] allEdges.remove(e) elif prevEndPt == e[1]: polyPts.append(prevEndPt) prevEndPt = e[0] allEdges.remove(e) break poly = sg.Polygon(polyPts) return poly
def create_polygon(points, pixel, shape_id=0, use_convex_hull=True): sgPoints = [] for p0 in points: sgPoints.append(sg.Point2(p0[0], p0[1])) if use_convex_hull: # the following make sure the inside has +1, and outside has -1 # but not work for L-shape, I guess. chull_points = sg.convex_hull.graham_andrew(sgPoints) poly = sg.Polygon(chull_points) else: # for L-shape, please define points counter-clock-wise print( "Please define the points counter-clock-wise to make sure inner has flag of +1 and outer has -1" ) poly = sg.Polygon(sgPoints) draw(sgPoints) return poly
def _extract_simple_polygons_skgeom(coords, orientation=None): coords = _without_closing_point(coords) assert coords[0] != coords[-1] arr = sg.arrangement.Arrangement() for a, b in zip(coords, coords[1:] + [coords[0]]): arr.insert(sg.Segment2(sg.Point2(*a), sg.Point2(*b))) polygons = [] for _, boundary in geometry.face_boundaries(arr): polygons.append( sg.Polygon(list(reversed(_without_closing_point(boundary))))) if len(polygons) > 1 and orientation is not None: polygons = sorted(polygons, key=lambda p: np.dot(p.coords[0], orientation)) return polygons
def _shapely_to_skgeom(polygon): pts = _without_closing_point(list(polygon.exterior.coords)) return sg.Polygon(list(reversed(pts)))
writer = csv.writer(file) add = [["id", "wkt"]] writer.writerows(add) with fiona.open('river_bodies/bbg_one_river_done.shp') as rivers: count = 0 for feature in rivers: count += 1 print("started on new river", count, "length ", len(feature['geometry']['coordinates'][0][0]), len(feature['geometry']['coordinates'][0])) if count >= 1: coord = feature['geometry']['coordinates'] if len(feature['geometry']['coordinates'][0][0])> 2: line = LineString(coord[0][0]) array_coord = np.array(coord[0][0][1:]) square = sg.Polygon(array_coord) area= square.area() print(area) hole_list = [] for hole in coord[0][1:]: tri = sg.Polygon(hole) area = tri.area() print(area) hole_list.append(hole[1:][::-1]) poly = sg.PolygonWithHoles(array_coord[::-1],hole_list) else: line = LineString(coord[0]) array_coord = np.array(coord[0][1:-1]) poly = sg.Polygon(array_coord)
p = Segment2(c, d) print(type(2)) print(barycenter(a, 2, b, 12)) points = [ Point2(rnd.uniform(-10, 10), rnd.uniform(-10, 10)) for i in range(0, 100) ] draw(points) cvh = skgeom.convex_hull.graham_andrew(points) draw(Polygon(cvh)) plt.axis('equal') plt.show() print(skgeom.intersection(s, p)) poly = skgeom.Polygon([a, c, b, d]) print(poly) print("Poly a simple: ", poly.is_simple()) print(poly.vertices) for v in poly.vertices: print(v) h = [Point2(1, 1), Point2(1, 2), Point2(2, 1)] for v in poly.vertices: print(v) print(list(poly.vertices))