def test_poly(self): poly = Polygon( LinearRing([ Point(0, 0), Point(10, 0), Point(9, 1), Point(5, 5), Point(0, 0) ])) assert loads(dumps(poly)) == poly
def __init__(self, bbox, border_face_id=None): self.bbox = bbox self.vertices = [] self.vertex_ids = {} self._add_vertex(Point(bbox.xmin, bbox.ymin)) self._add_vertex(Point(bbox.xmin, bbox.ymax)) self._add_vertex(Point(bbox.xmax, bbox.ymin)) self._add_vertex(Point(bbox.xmax, bbox.ymax)) self.clipped = [] self.new_edge_id = -1 self.border_face_id = border_face_id self.original_edges = []
def densify(ln, small=10): new = LineString() stop = len(ln) last = stop - 1 for j in range(1, stop): pt0, pt1 = ln[j - 1], ln[j] inclusive = False if j == last: inclusive = True divide(pt0, pt1, new, small, inclusive) # copy first and last back into line # (to not break any dependencies in topology) new[0] = Point(*ln[0]) new[len(new) - 1] = Point(*ln[len(ln) - 1]) return new
def recs(): # external edge # edge_id, start_node, end_node, left_face, right_face, geom external = [ # The true/false for a node represents whether it touches the boundary of the polygon # if i recall correctly... #eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf, (460, 297,True, 297, 290,False,290, 156, 100, LineString([Point(x=557446.4253, y=5898732.40073, srid=32632), Point(x=557446.70749, y=5898730.76438, srid=32632), Point(x=557461.85466, y=5898714.30195, srid=32632), Point(x=557464.67384, y=5898708.97349, srid=32632), Point(x=557466.21351, y=5898703.77497, srid=32632)], srid=32632)) ,(461, 286,False,286, 274,True ,274, 156, 100, LineString([Point(x=557473.26864, y=5898683.87537, srid=32632), Point(x=557473.3975, y=5898672.80469, srid=32632), Point(x=557492.3793, y=5898654.93696, srid=32632), Point(x=557488.42572, y=5898639.29629, srid=32632), Point(x=557497.15022, y=5898610.43875, srid=32632), Point(x=557500.27595, y=5898593.43783, srid=32632), Point(x=557502.22223, y=5898581.20087, srid=32632), Point(x=557502.90194, y=5898579.89022, srid=32632)], srid=32632)) ] # polygon edges polygon = [ (446, 290, 286, 156, 96, LineString([Point(x=557466.21351, y=5898703.77497, srid=32632), Point(x=557473.48144, y=5898694.01782, srid=32632), Point(x=557475.37026, y=5898691.28081, srid=32632), Point(x=557475.67801, y=5898690.69341, srid=32632), Point(x=557475.53941, y=5898689.31825, srid=32632), Point(x=557473.26864, y=5898683.87537, srid=32632)], srid=32632)) ,(447, 286, 290, 100, 96, LineString([Point(x=557473.26864, y=5898683.87537, srid=32632), Point(x=557446.03958, y=5898687.12991, srid=32632), Point(x=557443.51385, y=5898692.41333, srid=32632), Point(x=557466.21351, y=5898703.77497, srid=32632)], srid=32632)) ] return external, polygon
def make_graph(external, visitor, new_edge_id, universe_id, srid): """ Returns a graph representation of the segments and external edges """ skeleton = TopoMap(universe_id, srid) # add outside edges (the external chains) for he in external: eid, sn, en, lf, rf, geom, = he skeleton.add_edge(eid, sn, en, lf, rf, geom, attrs = {'external':True}) # add segments from inner rings ("bridge" connectors), these segments have # the correct face to be propagated for i, segment in enumerate(visitor.ext_segments, start = new_edge_id+1): v0, v1, lf, rf = segment ln = LineString(srid=srid) ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) skeleton.add_edge(i, v0.info.vertex_id, v1.info.vertex_id, lf, rf, ln, attrs = {'external':True} ) # prevent overlapping edge identifiers new_edge_id = i # add all segments which do not have a face left/right for i, segment in enumerate(visitor.segments, start = new_edge_id+1): v0, v1, = segment ln = LineString(srid=srid) ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) start_node_id = v0.info.vertex_id if start_node_id is None: # note, not an int but tuple to prevent duplicate with external ids start_node_id = (id(v0), ) end_node_id = v1.info.vertex_id if end_node_id is None: # note, not an int but tuple to prevent duplicate with external ids end_node_id = (id(v1), ) skeleton.add_edge(i, start_node_id, end_node_id, None, None, ln, attrs = {'external':False}) return skeleton, new_edge_id
def _test(): poly = Polygon() assert poly.wkt == "POLYGON EMPTY" lr = LinearRing([(0, 0), (12, 0), (5, 10), (0, 0)]) print(lr) poly = Polygon(lr) print(poly) print(poly.envelope) assert poly.geom_type == "Polygon" lr0 = LinearRing([(0, 0, 10), (10, 0, 2), (5, 10, 2), (0, 0, 10)]) lr1 = LinearRing([(0, 0, 10), (10, 0, 2), (5, 10, 2), (0, 0, 10)]) lr2 = LinearRing([(0, 0, 10), (10, 0, 2), (5, 10, 2), (0, 0, 10)]) assert lr0.geom_type == "LinearRing" assert lr0 == lr1 assert lr0 == lr2 lri = LinearRing([(100, 0, 10), (10, 0, 2), (5, 10, 2), (100, 0, 10)]) try: print(lri.area) except NotImplementedError: pass print(lri.signed_area()) poly = Polygon(lr, [lri]) print(poly.envelope) try: print(poly.area) except NotImplementedError: pass lr = LinearRing([(0, 0), (10, 0), (10, 10), (0, 0)]) print(lr) assert lr.signed_area() == 50. poly = Polygon(lr) assert poly.representative_point() == Point(7.5, 5.0) lr = LinearRing([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]) print(lr) assert lr.signed_area() == 100. lr = LinearRing([(0, 0), (10, 0), (5, 10), (0, 0)]) assert lr.signed_area() == +50. assert lr.is_ccw lr.reverse() assert lr.signed_area() == -50. assert not lr.is_ccw print(poly.wkb) lr = LinearRing([(0, 0), (10, 0), (10, 10), (0, 0)]) assert lr.signed_area() == 50. lr = LinearRing([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]) assert lr.signed_area() == 100. lr.reverse() assert lr.signed_area() == -100.
def divide(pt0, pt1, new, eps, inclusive_last=False): no = floor(dist(pt0, pt1) / eps) no = max(no, 1) dx = (pt1[0] - pt0[0]) / no dy = (pt1[1] - pt0[1]) / no total = int(no) if inclusive_last: total += 1 for i in range(total): new.append(Point(pt0[0] + i * dx, pt0[1] + i * dy))
def example(): from sink import Field, Schema, Index, Layer from simplegeom.geometry import Point from random import randint import timeit # -- let's define the structure for a little table to hold points SRID = 28992 gid = Field("gid", "numeric") geo_point = Field("geo_point", "point") i_gid_pkey = Index([gid], primary_key=True) i_geo_point = Index([geo_point], cluster=True) schema = Schema([gid, geo_point], [i_gid_pkey, i_geo_point]) layer = Layer(schema, "test_table", SRID) # -- make the schema with AsyncLoader(workers=1) as loader: loader.load_schema(layer) # -- load the data in chunks with AsyncLoader() as loader: prev = timeit.default_timer() for i in range(1, int(5e5)): layer.append(i, Point(randint(0, 1e8), randint(0, 1e6), SRID)) # check every ten points, whether the layer has more than 50000 features # if so, dump the data and clear the layer if (i % 10000 == 0) and len(layer) >= 50000: now = timeit.default_timer() # print now - prev, len(layer) prev = now loader.load_data(layer) layer.clear() # in case of remaining data, also load this data if len(layer) > 0: loader.load_data(layer) layer.clear() with AsyncLoader(workers=1) as loader: # -- indexes loader.load_indexes(layer) # -- statistics loader.load_statistics(layer)
def _test(): print dumps(Point(1, 2)) ln = LineString([(0, 0, 3), (10, 10, 3)]) print dumps(ln) print dumps(ln) fh = StringIO() dump(ln, fh) print fh.getvalue() ln = LineString([(0, 0), (10, 10)]) print dumps(ln) print str(loads(dumps(ln))), str(ln) print str(loads(dumps(ln))) == str(ln) print loads( "010100006040710000000000000000F03F00000000000000400000000000001040") print loads( "01020000A04071000002000000000000000000000000000000000000000000000000000840000000000000000000000000000008400000000000002440" ) print loads( "01020000A00000000002000000000000000000000000000000000000000000000000000840000000000000000000000000000008400000000000002440" ) print loads( "01020000A00000000002000000000000000000000000000000000000000000000000000840000000000000000000000000000008400000000000002440" ) print dumps(Polygon()) print loads("01030000200000000000000000") print loads(dumps(Polygon())) lr = LinearRing([(0, 0), (10, 0), (5, 10), (0, 0)]) print lr lr = LinearRing([(0, 0, 10), (10, 0, 2), (5, 10, 2), (0, 0, 10)]) print lr print loads(dumps(Polygon(lr, [lr])))
def test_square(self): conv = ToPointsAndSegments() outside_edges, polygon = recs() for edge_id, start_node_id, end_node_id, _, _, geom in polygon: face = None weights = [] for i, pt in enumerate(geom): if i == len(geom) - 1: # last pt node = end_node_id tp = 1 weights = [0, 10] elif i == 0: # first pt node = start_node_id tp = 1 weights = [0, 10] else: # intermediate pt node = None tp = 0 if edge_id == 1001: weights = [10] elif edge_id == 1002: weights = [0] else: raise ValueError('encountered unknown edge') conv.add_point((pt.x, pt.y), VertexInfo(tp, face, node)) for (start, end) in zip(geom[:-1], geom[1:]): (sx, sy) = start (ex, ey) = end conv.add_segment((sx, sy), (ex, ey)) points, segments, infos = conv.points, conv.segments, conv.infos dt = triangulate(points, infos, segments) # for v in dt.vertices: # print v.info visitor = EdgeEdgeHarvester([t for t in InteriorTriangleIterator(dt)]) visitor.skeleton_segments() # with open("/tmp/skel0_unweighted.wkt", "w") as fh: # fh.write("wkt\n") # for seg in visitor.segments: # fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) # # with open("/tmp/skel1_unweighted.wkt", "w") as fh: # fh.write("wkt\n") # for lst in visitor.bridges.itervalues(): # for seg in lst: # fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) pick = ConnectorPicker(visitor) pick.pick_connectors() skeleton, new_edge_id = make_graph(outside_edges, visitor, new_edge_id=10000, universe_id=0, srid=-1) label_sides(skeleton) # lines = ["id;geometry\n"] # for he in skeleton.half_edges.itervalues(): # lines.append("{};{}\n".format(he.id, he.geometry)) # with open("/tmp/edges.wkt", "w") as fh: # fh.writelines(lines) # -- remove unwanted skeleton parts (same face id on both sides) prune_branches(skeleton) groups = define_groups(skeleton) new_edges, new_edge_id = make_new_edges(groups, new_edge_id) assert new_edge_id == 10000 assert len(new_edges) == 1 assert new_edges[0] == (10000, 5, 4, -1, 501, LineString([ Point(x=10.0, y=120.0, srid=0), Point(x=10.0, y=110.0, srid=0), Point(x=7.5, y=104.5, srid=0), Point(x=2.5, y=104.5, srid=0), Point(x=0.0, y=110.0, srid=0), Point(x=0.0, y=120.0, srid=0) ], srid=0))
from simplegeom.geometry import Point if __name__ == "__main__": empty = Point() assert empty.is_empty assert empty.geom_type == "Point" pt = Point(1, 2) assert pt.x == 1 assert pt.y == 2 assert pt.area == 0. pt[0] = 5 pt[1] = 6 assert pt.x == 5 assert pt.y == 6 print(pt.wkb) assert str( pt.wkb) == str(b'01010000200000000000000000000014400000000000001840')
def test(): # Test: # ===== # Triangle with *touching* hole # FIXME: # unnecessary node which is the start of the inner ring remains # as edges around this node are flagged as external conv = ToPointsAndSegments() a, b, c, = (0,0), (10,20), (20,0) d, e = (10,25), (25,0) g, h = (10,16), (16,2) alpha3 = angle(a, b) alpha2 = angle(a, g) alpha1 = angle(a, h) alpha0 = angle(a, c) # node sector # (he.twin.face.id, he.twin.next.id, he.twin.next.angle, he.id, he.angle) sectors = [ ("Z", 9001, alpha0, 9002, alpha1), ("C", 9002, alpha1, 9002, alpha2), ("Z", 9002, alpha2, 9003, alpha3), ("B", 9003, alpha3, 9001, alpha0) # ("Z", alpha1, 9002, alpha0, 9001), # ("C", alpha2, 9002, alpha1, 9002), # ("Z", alpha3, 9003, alpha2, 9002), # ("B", alpha0, 9001, alpha3, 9003) ] conv.add_point(b, info = VertexInfo(1, None, 1001)) conv.add_point(c, info = VertexInfo(0, None, 1002)) conv.add_point(a, info = VertexInfo(3, sectors, 1003)) conv.add_point(g, info = VertexInfo(0, None, None)) conv.add_point(h, info = VertexInfo(0, None, None)) conv.add_segment(a, b) conv.add_segment(b, c) conv.add_segment(c, a) conv.add_segment(a, g) conv.add_segment(g, h) conv.add_segment(h, a) points, segments, infos = conv.points, conv.segments, conv.infos pprint(points) pprint(segments) pprint(infos) dt = triangulate(points, infos, segments) for vertex in dt.vertices: print vertex, vertex.info with open("/tmp/alltris.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh) with open("/tmp/allvertices.wkt", "w") as fh: output_vertices(dt.vertices, fh) with open("/tmp/interiortris.wkt", "w") as fh: output_triangles([t for t in InteriorTriangleIterator(dt)], fh) with open("/tmp/hull.wkt", "w") as fh: output_triangles([t for t in ConvexHullTriangleIterator(dt)], fh) visitor = EdgeEdgeHarvester([t for t in InteriorTriangleIterator(dt)]) visitor.skeleton_segments() with open("/tmp/skel0.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.segments: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) with open("/tmp/skel1.wkt", "w") as fh: fh.write("wkt\n") for lst in visitor.bridges.itervalues(): print lst for seg in lst: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) visitor.pick_connectors() with open("/tmp/skel2.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.ext_segments: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) outside_edges = [ # #eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf (4000, LineString([d, b],), 5001, False, 5001, 1001, False, 1001, "A", "B" ), (4001, LineString([e, c],), 5002, False, 5002, 1002, False, 1002, "B", "A" ), ] # outside_edges = [] skeleton = SkeletonGraph() print """ ADDING OUTSIDE EDGES """ # first add outside edges for outside_edge in outside_edges: eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf, = outside_edge skeleton.add_segment(geom, external = True, edge_id = eid, left_face_id = lf, right_face_id = rf, start_node_id = sn, end_node_id = en, start_vertex_id = svtxid, end_vertex_id = evtxid, start_external = sn_ext, end_external = en_ext ) print """ BRIDGE CONNECTORS """ # # add nodes from inner rings ("bridge" connectors) for i, segment in enumerate(visitor.ext_segments): print segment v0, v1, lf, rf = segment ln = LineString() ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) skeleton.add_segment(ln, external = True, edge_id = i, start_vertex_id = v0.info.vertex_id, end_vertex_id = v1.info.vertex_id, left_face_id = lf, right_face_id = rf, # start_external = True, # end_external = True ) print """ UNLABELED EDGES """ # # then add all segments which are unlabeled for i, segment in enumerate(visitor.segments, start = len(visitor.ext_segments)): v0, v1, = segment ln = LineString() ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) start_vertex_id = v0.info.vertex_id if start_vertex_id is None: start_vertex_id = (id(v0), ) # note, not an int but tuple to prevent duplicate with external ids end_vertex_id = v1.info.vertex_id if end_vertex_id is None: end_vertex_id = (id(v1), ) # note, not an int but tuple to prevent duplicate with external ids print start_vertex_id print end_vertex_id skeleton.add_segment(ln, start_vertex_id = start_vertex_id, end_vertex_id = end_vertex_id, external = False, edge_id = i) with open('/tmp/edges.wkt', 'w') as fh: fh.write("geometry;leftface;rightface;sn;en\n") for edge in skeleton.edges: print >> fh, edge.geometry,";", edge.left_face_id,";", edge.right_face_id, ";", edge.start_node.id,";", edge.end_node.id skeleton.label_sides() skeleton.prune_branches() skeleton.find_new_edges(new_edge_id=90000, new_node_id=80000) skeleton.visualize_nodes() with open("/tmp/edges_new.wkt", "w") as fh: fh.write("eid;sn;en;lf;rf;length;geom\n") for eid, sn, en, lf, rf, length, geom, in skeleton.new_edges: print >> fh, eid, ";", sn,";", en, ";", lf, ";", rf, ";", length,";", geom
def test(): wkt = """ POLYGON ((0 0, 9 1, 10 10, 1 9, 0 0)) """ poly = loads(wkt) print poly conv = ToPointsAndSegments() conv.add_point((0, 0), info=VertexInfo(0, None, None)) conv.add_point((9, 1), info=VertexInfo(1, None, 1001)) conv.add_point((10, 10), info=VertexInfo(0, None, None)) conv.add_point((1, 9), info=VertexInfo(1, None, 1002)) conv.add_point((0, 0), info=VertexInfo(0, None, None)) conv.add_segment((0, 0), (9, 1)) conv.add_segment((9, 1), (10, 10)) conv.add_segment((10, 10), (1, 9)) conv.add_segment((1, 9), (0, 0)) points, segments, infos = conv.points, conv.segments, conv.infos pprint(points) pprint(segments) pprint(infos) dt = triangulate(points, infos, segments) for vertex in dt.vertices: print vertex, vertex.info with open("/tmp/alltris.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh) with open("/tmp/allvertices.wkt", "w") as fh: output_vertices(dt.vertices, fh) with open("/tmp/interiortris.wkt", "w") as fh: output_triangles([t for t in InteriorTriangleIterator(dt)], fh) with open("/tmp/hull.wkt", "w") as fh: output_triangles([t for t in ConvexHullTriangleIterator(dt)], fh) # #if DEBUG: print poly # ln = [] # for ring in poly: # for vtx in ring: # ln.append(vtx) # # ev = poly.envelope # #if DEBUG: print ev # eps = 10000 # half_dx = (ev.xmax - ev.xmin) / 2.0 # dy = (ev.ymax - ev.ymin) # # top - middle # top_y = ev.ymax + dy + eps # top_x = ev.xmin + half_dx # # bottom - left # left_x = ev.xmin - half_dx - eps # left_y = ev.ymin - eps # # bottom - right # right_x = ev.xmax + half_dx + eps # right_y = ev.ymin - eps # # bnd = [Vertex(left_x,left_y), # Vertex(right_x,right_y), # Vertex(top_x,top_y)] # # return # mesh = Mesh(boundary = bnd) # # mesh = Mesh() # prev_pt = None # seed("ab") # for i, pt in enumerate(ln): # vtx = Vertex(pt.x, pt.y) # # if i == 2: # vtx.flag = 1 # ext_end = Point(pt.x, pt.y) # # elif i == 60: # vtx.flag = 1 # ext_end2 = Point(pt.x, pt.y) # else: # vtx.flag = 0 # int(randint(0, 10) in (5, )) # # vtx = mesh.insert(vtx) # vtx.gid = i # if i == 2: # ext_end_id = vtx.gid # elif i == 60: # ext_end2_id = vtx.gid # # if i > 0: # mesh.add_constraint( prev_pt, vtx ) # prev_pt = vtx # # fh = open('/tmp/tris.wkt', 'w') # fh.write("geometry\n") # MeshVisualizer(mesh).list_triangles_wkt(fh) # fh.close() visitor = EdgeEdgeHarvester([t for t in InteriorTriangleIterator(dt)]) visitor.skeleton_segments() with open("/tmp/skel0.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.segments: fh.write( "LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format( seg)) visitor.pick_connectors() # visitor = MidpointHarvester([t for t in InteriorTriangleIterator(dt)]) # visitor.skeleton_segments() # with open("/tmp/skel.wkt", "w") as fh: # fh.write("wkt\n") # for seg in visitor.segments: # fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) # with open("/tmp/centres.wkt", "w") as fh: # fh.write("wkt\n") # for t, point in visitor.triangle_point.iteritems(): # fh.write("POINT({0})\n".format(point)) # visitor = TriangleVisitor(dt) # visitor.skeleton_segments() # visitor.pick_connectors() # # visitor.list_table_sg() # visitor.list_segments_pg() # visitor.list_connectors_pg() # visitor.list_pg() # visualizer = MeshVisualizer(mesh) # visualizer.list_pg() # make artificial external edges # ext_start = Point(1747940, 5136656) # ln = LineString() # ln.append(ext_start) # ln.append(ext_end) # outside_edges = [ # (500000, ln, 100, True, -1, 101, False, ext_end_id, 200, 201)] # #(eid, geom, sn, sn_ext, en, en_ext, lf, rf) # # ext_start2 = Point(1748550, 5136537) # ln = LineString() # ln.append(ext_start2) # ln.append(ext_end2) # outside_edges.append((600000, ln, 200, True, -2, 201, False, ext_end2_id, 201, 200)) # outside_edges = [ #eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf (4000, LineString([(1, 9), (0, 20), (20, 20), (20, 1), (9, 1)], ), 1002, False, 1002, 1001, False, 1001, "C", "B"), (4001, LineString([(0, 9), (1, 9)], ), 5002, False, 5002, 1002, False, 1002, "C", "D"), (4002, LineString([(9, 0), (9, 1)], ), 5001, False, 5001, 1001, False, 1001, "D", "C"), ] # outside_edges = [] skeleton = SkeletonGraph() print """ ADDING OUTSIDE EDGES """ # first add outside edges for outside_edge in outside_edges: eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf, = outside_edge skeleton.add_segment(geom, external=True, edge_id=eid, left_face_id=lf, right_face_id=rf, start_node_id=sn, end_node_id=en, start_vertex_id=svtxid, end_vertex_id=evtxid, start_external=sn_ext, end_external=en_ext) # print """ # # BRIDGE CONNECTORS # """ # # add nodes from inner rings ("bridge" connectors) # for i, segment in enumerate(visitor.ext_segments): # v0, v1, = segment # ln = LineString() # ln.append(Point(*v0.point)) # ln.append(Point(*v1.point)) # skeleton.add_segment(ln, # external = True, # edge_id = i, # start_vertex_id = v0.gid, # end_vertex_id = v1.gid, # left_face_id = v0.label, # right_face_id = v0.label, # # start_external = True, # # end_external = True # ) # # print """ # # UNLABELED EDGES # """ # # then add all segments which are unlabeled for i, segment in enumerate(visitor.segments): v0, v1, = segment ln = LineString() ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) start_vertex_id = v0.info.vertex_id if start_vertex_id is None: start_vertex_id = ( id(v0), ) # note, not an int but tuple to prevent duplicate with external ids end_vertex_id = v1.info.vertex_id if end_vertex_id is None: end_vertex_id = ( id(v1), ) # note, not an int but tuple to prevent duplicate with external ids print start_vertex_id print end_vertex_id skeleton.add_segment(ln, start_vertex_id=start_vertex_id, end_vertex_id=end_vertex_id, external=False, edge_id=i + len(visitor.ext_segments)) with open('/tmp/edges.wkt', 'w') as fh: fh.write("geometry;leftface;rightface;sn;en\n") for edge in skeleton.edges: print >> fh, edge.geometry, ";", edge.left_face_id, ";", edge.right_face_id, ";", edge.start_node.id, ";", edge.end_node.id skeleton.visualize_nodes() skeleton.label_sides() skeleton.prune_branches() skeleton.find_new_edges(new_edge_id=90000, new_node_id=80000) with open("/tmp/edges_new.wkt", "w") as fh: fh.write("eid;sn;en;lf;rf;length;geom\n") for eid, sn, en, lf, rf, length, geom, in skeleton.new_edges: print >> fh, eid, ";", sn, ";", en, ";", lf, ";", rf, ";", length, ";", geom
def clip_edge(self, edge_id, start_node_id, end_node_id, left_face_id, right_face_id, geometry, attrs={}): # PRECONDITION: # bounding box of segment overlaps clipping window # # caller should do something like this # # """ #if clip_window.intersects(geometry.envelope) and \ # not clip_window.contains(geometry.envelope): # """ self.original_edges.append(geometry) left, bottom, right, top = self.bbox.xmin, self.bbox.ymin, \ self.bbox.xmax, self.bbox.ymax segment_ct = len(geometry) - 1 start_node = None end_node = None clipped_geometry = LineString(srid=geometry.srid) new_sn_id = None new_en_id = None for s in range(segment_ct): first_segment = False last_segment = False t = s + 1 if s == 0: # first segment first_segment = True if t == segment_ct: # last segment last_segment = True x1, y1 = geometry[s] # start point of segment x2, y2 = geometry[t] # end point # simple tests to reject segment, completely outside if ((max(x1, x2) < left) or (max(y1, y2) < bottom) or (min(x1, x2) > right) or (min(y1, y2) > top)): continue # simple tests to accept segment # completely inside elif (left <= x1 <= right and left <= x2 <= right and bottom <= y1 <= top and bottom <= y2 <= top): # find if segment is fully on border # we ignore it, but we add nodes on the border if ((x1 == left or x1 == right) and x1 == x2) or \ ((y1 == bottom or y1 == top) and y1 == y2): node = Point(x1, y1) self._add_vertex(node) node = Point(x2, y2) self._add_vertex(node) # segment is inside, # but can still have one or two points # on the border of clipping window else: pt = Point(x1, y1) clipped_geometry.append(pt) if first_segment == True: start_node = pt if last_segment == True: end_node = Point(x2, y2) clipped_geometry.append(end_node) # start of segment is on border of clipping window if (x1 == left or x1 == right) or \ (y1 == top or y1 == bottom): start_node = Point(x1, y1) new_sn_id = self._add_vertex(start_node) # end of segment is on border of clipping window if (x2 == left or x2 == right) or \ (y2 == top or y2 == bottom): end_node = Point(x2, y2) new_en_id = self._add_vertex(end_node) # we only add the point if there will be more parts if not last_segment: clipped_geometry.append(end_node) # segment not fully inside, # nor fully outside # -> clip else: clipped_segment = self._clip_segment(x1, y1, x2, y2) if clipped_segment is None: # segment is fully outside clipping window # but bbox of segment overlaps clipping window continue # collapsed to one point, rejecting -- but adding to border if clipped_segment[0] == clipped_segment[1]: node = Point(clipped_segment[0][0], clipped_segment[0][1]) self._add_vertex(node) # segment is still a segment else: # different start node after clip if clipped_segment[0][0] != x1 or \ clipped_segment[0][1] != y1: start_node = Point(clipped_segment[0][0], clipped_segment[0][1]) clipped_geometry.append(start_node) new_sn_id = self._add_vertex(start_node) else: clipped_geometry.append(Point(x1, y1)) # different end node after clip if clipped_segment[1][0] != x2 or \ clipped_segment[1][1] != y2: end_node = Point(clipped_segment[1][0], clipped_segment[1][1]) new_en_id = self._add_vertex(end_node) clipped_geometry.append(end_node) # if this is the first segment and we do not have # a start_node yet, we should set it as such if first_segment == True and start_node is None: start_node = Point(x1, y1) # if this is the last segment of the polyline # and we do not have a end node yet, take # the end of the segment as the end node elif last_segment == True and end_node is None: end_node = Point(x2, y2) clipped_geometry.append(end_node) if start_node is not None and end_node is not None: # found a clipped piece # add it to the `clipped' list sn_id = start_node_id en_id = end_node_id if new_sn_id is not None: sn_id = new_sn_id if new_en_id is not None: en_id = new_en_id attrs['locked'] = True attrs['clipped'] = True new_edge = (self.new_edge_id, sn_id, en_id, left_face_id, right_face_id, clipped_geometry, attrs) self.clipped.append(new_edge) # reset for a next part that eventually is inside clipped_geometry = LineString(srid=geometry.srid) start_node = None end_node = None new_sn_id = None new_en_id = None self.new_edge_id -= 1
# return StreamingLayer(hoogteverschil_schema, PREFIX+"hoogteverschil", srid = srid, phase = phase, stream = fh) # #def iso_hoogte(fh, phase = Phase.ALL): # return StreamingLayer(iso_hoogte_schema, PREFIX+"iso_hoogte", srid = srid, phase = phase, stream = fh) # #def kade_of_wal(fh, phase = Phase.ALL): # return StreamingLayer(kade_of_wal_schema, PREFIX+"kade_of_wal", srid = srid, phase = phase, stream = fh) # #def overig_relief(fh, phase = Phase.ALL): # return StreamingLayer(overig_relief_schema, PREFIX+"overig_relief", srid = srid, phase = phase, stream = fh) from sink import dumps from simplegeom.geometry import Polygon, LineString, LinearRing, Point from datetime import datetime outer = LinearRing([Point(0, 0), Point(10, 0), Point(5, 10), Point(0, 0)]) inner = LinearRing([Point(1, 1), Point(9, 1), Point(5, 9), Point(1, 1)]) defn = [ gid, tdn_code, object_begin_tijd, bronactualiteit, brontype, geometrie_vlak, geometrie_lijn, geometrie_punt ] lyr = Layer(Schema(defn, []), "wazaa") lyr.append(10, 1003, datetime.now(), datetime.now(), "a beautiful product", Polygon(outer, [inner]), LineString([Point(0, 0), Point(10, 0), Point(5, 10), Point(0, 0)]), Point(100, 100)) print dumps(lyr)
def recs(): # external edge # edge_id, start_node, end_node, left_face, right_face, geom external = [ # The true/false for a node represents whether it touches the boundary of the polygon # if i recall correctly... # eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf, (252, 135, 130, 54, 45, LineString([ Point(x=560850.10666, y=5897605.99348, srid=32632), Point(x=560741.10142, y=5897548.82285, srid=32632), Point(x=560721.14836, y=5897552.58643, srid=32632) ], srid=32632)), (253, 130, 141, 74, 45, LineString([ Point(x=560721.14836, y=5897552.58643, srid=32632), Point(x=560722.71918, y=5897569.99831, srid=32632), Point(x=560693.83728, y=5897603.2688, srid=32632), Point(x=560756.86961, y=5897619.73413, srid=32632), Point(x=560777.04418, y=5897623.14318, srid=32632), Point(x=560797.7284, y=5897626.93212, srid=32632), Point(x=560848.93159, y=5897639.2497, srid=32632) ], srid=32632)), (275, 146, 149, 74, 51, LineString([ Point(x=560852.35323, y=5897649.45567, srid=32632), Point(x=560817.76284, y=5897681.17662, srid=32632), Point(x=560795.42893, y=5897701.01108, srid=32632), Point(x=560832.65749, y=5897740.48761, srid=32632), Point(x=560836.92066, y=5897740.48056, srid=32632), Point(x=560861.8534, y=5897713.28732, srid=32632), Point(x=560861.9305, y=5897709.23867, srid=32632), Point(x=560894.51146, y=5897674.45864, srid=32632) ], srid=32632)), (290, 142, 166, 74, 54, LineString([ Point(x=560923.98887, y=5897644.8424, srid=32632), Point(x=560924.54319, y=5897645.13689, srid=32632), Point(x=560951.61553, y=5897659.51281, srid=32632), Point(x=561160.70759, y=5897771.29099, srid=32632), Point(x=561206.42491, y=5897795.92407, srid=32632), Point(x=561218.48727, y=5897804.06678, srid=32632) ], srid=32632)) ] # polygon edges polygon = [(251, 141, 135, 48, 45, LineString([ Point(x=560848.93159, y=5897639.2497, srid=32632), Point(x=560863.31688, y=5897626.61676, srid=32632), Point(x=560850.10666, y=5897605.99348, srid=32632) ], srid=32632)), (263, 142, 135, 54, 48, LineString([ Point(x=560923.98887, y=5897644.8424, srid=32632), Point(x=560923.98066, y=5897644.78827, srid=32632), Point(x=560909.5673, y=5897637.17918, srid=32632), Point(x=560850.10666, y=5897605.99348, srid=32632) ], srid=32632)), (264, 141, 146, 74, 48, LineString([ Point(x=560848.93159, y=5897639.2497, srid=32632), Point(x=560853.92697, y=5897645.61066, srid=32632), Point(x=560852.35323, y=5897649.45567, srid=32632) ], srid=32632)), (265, 146, 149, 51, 48, LineString([ Point(x=560852.35323, y=5897649.45567, srid=32632), Point(x=560876.41272, y=5897659.30315, srid=32632), Point(x=560877.61799, y=5897658.14633, srid=32632), Point(x=560894.51146, y=5897674.45864, srid=32632) ], srid=32632)), (266, 149, 142, 74, 48, LineString([ Point(x=560894.51146, y=5897674.45864, srid=32632), Point(x=560923.98887, y=5897644.8424, srid=32632) ], srid=32632))] return external, polygon
def test_line(self): line = LineString([Point(0, 0), Point(10, 0)]) assert loads(dumps(line)) == line
def test(): # Test: # ===== # - straight forward split over 2 neighbours # outcome: 1 new edge conv = ToPointsAndSegments() outside_edges, polygon = recs() for _, start_node_id, end_node_id, _, _, geom in polygon: for i, pt in enumerate(geom): if i == len(geom) - 1: # last pt node = end_node_id tp = 1 elif i == 0: # first pt node = start_node_id tp = 1 else: # intermediate pt node = None tp = 0 conv.add_point((pt.x, pt.y), VertexInfo(tp, None, node)) for (start, end) in zip(geom[:-1],geom[1:]): (sx, sy) = start (ex, ey) = end conv.add_segment((sx, sy), (ex,ey)) points, segments, infos = conv.points, conv.segments, conv.infos pprint(points) pprint(segments) pprint(infos) dt = triangulate(points, infos, segments) for vertex in dt.vertices: print "POINT(", vertex.x, vertex.y, ")" #vertex.info with open("/tmp/alltris.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh) with open("/tmp/allvertices.wkt", "w") as fh: output_vertices(dt.vertices, fh) with open("/tmp/interiortris.wkt", "w") as fh: output_triangles([t for t in InteriorTriangleIterator(dt)], fh) with open("/tmp/hull.wkt", "w") as fh: output_triangles([t for t in ConvexHullTriangleIterator(dt)], fh) visitor = EdgeEdgeHarvester([t for t in InteriorTriangleIterator(dt)]) visitor.skeleton_segments() with open("/tmp/skel0.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.segments: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) with open("/tmp/skel1.wkt", "w") as fh: fh.write("wkt\n") for lst in visitor.bridges.itervalues(): print lst for seg in lst: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) pick = ConnectorPicker(visitor) pick.pick_connectors() with open("/tmp/skel2.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.ext_segments: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) # outside_edges = [ # # #eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf # (4000, # LineString([d, b],), # 5001, False, 5001, # 1001, False, 1001, # "A", "B" # ), # (4001, # LineString([e, c],), # 5002, False, 5002, # 1002, False, 1002, # "B", "A" # ), # ] # outside_edges = [] skeleton = SkeletonGraph() skeleton.srid = 32632 print """ ADDING OUTSIDE EDGES """ # first add outside edges for outside_edge in outside_edges: eid, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf, geom, = outside_edge skeleton.add_segment(geom, external = True, edge_id = eid, left_face_id = lf, right_face_id = rf, start_node_id = sn, end_node_id = en, start_vertex_id = svtxid, end_vertex_id = evtxid, start_external = sn_ext, end_external = en_ext ) print """ BRIDGE CONNECTORS """ # # add nodes from inner rings ("bridge" connectors) for i, segment in enumerate(visitor.ext_segments): print segment v0, v1, lf, rf = segment ln = LineString(srid=skeleton.srid) ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) skeleton.add_segment(ln, external = True, edge_id = i, start_vertex_id = v0.info.vertex_id, end_vertex_id = v1.info.vertex_id, left_face_id = lf, right_face_id = rf, # start_external = True, # end_external = True ) print """ UNLABELED EDGES """ # # then add all segments which are unlabeled for i, segment in enumerate(visitor.segments, start = len(visitor.ext_segments)): v0, v1, = segment ln = LineString(srid=skeleton.srid) ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) start_vertex_id = v0.info.vertex_id if start_vertex_id is None: start_vertex_id = (id(v0), ) # note, not an int but tuple to prevent duplicate with external ids end_vertex_id = v1.info.vertex_id if end_vertex_id is None: end_vertex_id = (id(v1), ) # note, not an int but tuple to prevent duplicate with external ids print start_vertex_id print end_vertex_id skeleton.add_segment(ln, start_vertex_id = start_vertex_id, end_vertex_id = end_vertex_id, external = False, edge_id = i) with open('/tmp/edges.wkt', 'w') as fh: fh.write("geometry;leftface;rightface;sn;en\n") for edge in skeleton.edges: print >> fh, edge.geometry,";", edge.left_face_id,";", edge.right_face_id, ";", edge.start_node.id,";", edge.end_node.id skeleton.label_sides() skeleton.prune_branches() skeleton.find_new_edges(new_edge_id=90000, new_node_id=80000) skeleton.visualize_nodes() with open("/tmp/edges_new.wkt", "w") as fh: fh.write("eid;sn;en;lf;rf;length;geom\n") for eid, sn, en, lf, rf, length, geom, in skeleton.new_edges: print >> fh, eid, ";", sn,";", en, ";", lf, ";", rf, ";", length,";", geom
def test(): conv = ToPointsAndSegments() a, b, c, d, e = (0, 0), (10, 20), (20, 0), (10, 25), (25, 0) conv.add_point(a, info=VertexInfo(0, None, None)) conv.add_point(b, info=VertexInfo(1, None, 1001)) conv.add_point(c, info=VertexInfo(0, None, 1002)) conv.add_segment(a, b) conv.add_segment(b, c) conv.add_segment(c, a) points, segments, infos = conv.points, conv.segments, conv.infos pprint(points) pprint(segments) pprint(infos) dt = triangulate(points, infos, segments) for vertex in dt.vertices: print vertex, vertex.info with open("/tmp/alltris.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh) with open("/tmp/allvertices.wkt", "w") as fh: output_vertices(dt.vertices, fh) with open("/tmp/interiortris.wkt", "w") as fh: output_triangles([t for t in InteriorTriangleIterator(dt)], fh) with open("/tmp/hull.wkt", "w") as fh: output_triangles([t for t in ConvexHullTriangleIterator(dt)], fh) visitor = EdgeEdgeHarvester([t for t in InteriorTriangleIterator(dt)]) visitor.skeleton_segments() with open("/tmp/skel0.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.segments: fh.write( "LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format( seg)) visitor.pick_connectors() outside_edges = [ # #eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf (4000, LineString([d, b], ), 5001, False, 5001, 1001, False, 1001, "A", "B"), (4001, LineString([e, c], ), 5002, False, 5002, 1002, False, 1002, "B", "A"), ] # outside_edges = [] skeleton = SkeletonGraph() print """ ADDING OUTSIDE EDGES """ # first add outside edges for outside_edge in outside_edges: eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf, = outside_edge skeleton.add_segment(geom, external=True, edge_id=eid, left_face_id=lf, right_face_id=rf, start_node_id=sn, end_node_id=en, start_vertex_id=svtxid, end_vertex_id=evtxid, start_external=sn_ext, end_external=en_ext) # print """ # # BRIDGE CONNECTORS # """ # # add nodes from inner rings ("bridge" connectors) # for i, segment in enumerate(visitor.ext_segments): # v0, v1, = segment # ln = LineString() # ln.append(Point(*v0.point)) # ln.append(Point(*v1.point)) # skeleton.add_segment(ln, # external = True, # edge_id = i, # start_vertex_id = v0.gid, # end_vertex_id = v1.gid, # left_face_id = v0.label, # right_face_id = v0.label, # # start_external = True, # # end_external = True # ) # # print """ # # UNLABELED EDGES # """ # # then add all segments which are unlabeled for i, segment in enumerate(visitor.segments): v0, v1, = segment ln = LineString() ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) start_vertex_id = v0.info.vertex_id if start_vertex_id is None: start_vertex_id = ( id(v0), ) # note, not an int but tuple to prevent duplicate with external ids end_vertex_id = v1.info.vertex_id if end_vertex_id is None: end_vertex_id = ( id(v1), ) # note, not an int but tuple to prevent duplicate with external ids print start_vertex_id print end_vertex_id skeleton.add_segment(ln, start_vertex_id=start_vertex_id, end_vertex_id=end_vertex_id, external=False, edge_id=i + len(visitor.ext_segments)) with open('/tmp/edges.wkt', 'w') as fh: fh.write("geometry;leftface;rightface;sn;en\n") for edge in skeleton.edges: print >> fh, edge.geometry, ";", edge.left_face_id, ";", edge.right_face_id, ";", edge.start_node.id, ";", edge.end_node.id skeleton.visualize_nodes() skeleton.label_sides() skeleton.prune_branches() skeleton.find_new_edges(new_edge_id=90000, new_node_id=80000) with open("/tmp/edges_new.wkt", "w") as fh: fh.write("eid;sn;en;lf;rf;length;geom\n") for eid, sn, en, lf, rf, length, geom, in skeleton.new_edges: print >> fh, eid, ";", sn, ";", en, ";", lf, ";", rf, ";", length, ";", geom
def test(): # from brep.io import geom_from_text wkt = """ POLYGON ((1748051.29733583 5136615.82947196 773.937, 1748047.48 5136606.14 774.133, 1748046.06 5136603.88 773.969, 1748042.35 5136597.96 773.537, 1748047.67 5136593.68 773.537, 1748062.48 5136584.98 773.537, 1748067.52 5136582.04 773.537, 1748078.22 5136575.79 773.537, 1748087.88 5136571.84 773.537, 1748100.04 5136568.43 773.537, 1748114.52 5136567.96 773.537, 1748123.7 5136568.71 773.537, 1748126.1 5136568.9 773.537, 1748139.61 5136568.29 773.537, 1748149.57 5136565.95 773.537, 1748151.93 5136555.15 773.537, 1748149.21 5136548.6 773.537, 1748145.32 5136546.63 773.537, 1748142.02 5136544.95 773.537, 1748131.75 5136545.4 773.537, 1748127.61 5136546.26 773.537, 1748103.12 5136551.32 773.537, 1748099.8 5136552.01 773.537, 1748097.94 5136550.26 773.537, 1748104.5 5136544.95 773.537, 1748110.82 5136539.96 773.537, 1748115.85 5136536 773.537, 1748120.75 5136530 773.537, 1748122.84 5136527.43 773.537, 1748130.56 5136525.53 773.529, 1748137.16 5136523.9 773.522, 1748181.68 5136506.95 773.582, 1748242.81 5136487.67 772.94, 1748259.51 5136493.28 772.94, 1748258.84 5136498.26 772.94, 1748301.42 5136484.25 772.94, 1748297.81 5136473.28 771.782, 1748340.02 5136463.62 771.782, 1748354.35 5136457.53 771.782, 1748368.83 5136450.68 771.782, 1748369.05 5136450.98 771.788, 1748372.99 5136456.65 771.892, 1748377.35 5136461.37 772.002, 1748382.3 5136465.89 772.112, 1748386.44 5136469.71 772.222, 1748392.7 5136473.98 772.332, 1748394.36 5136475.02 772.362, 1748398.85 5136477.82 772.442, 1748404.63 5136480.54 772.552, 1748412.49 5136483.25 772.662, 1748419.53 5136484.98 772.772, 1748426.23 5136486.04 772.882, 1748433.03 5136486.76 772.992, 1748441.07 5136486.75 773.102, 1748448.67 5136486.1 773.212, 1748454.72 5136485.04 773.342, 1748455.57 5136484.81 773.342, 1748455.03 5136493.07 773.342, 1748451.87 5136500.92 773.342, 1748448.06 5136508.98 773.342, 1748443.03 5136515.29 773.342, 1748435.89 5136523.68 773.342, 1748426.88 5136534.43 773.342, 1748419.43 5136547.01 773. 342, 1748412.45 5136556.31 773.342, 1748403.46 5136567.93 773.342, 1748395.46 5136580.18 773.342, 1748392.94 5136593.95 773.342, 1748395.02 5136601.5 773.342, 1748393.12 5136622.45 773.342, 1748390.53 5136630.67 773.129, 1748384.75 5136630.93 773.129, 1748376.07 5136635.84 773.129, 1748375.44 5136643.04 773.129, 1748369.59 5136648.43 773.129, 1748367.26 5136649.46 773.129, 1748363.05 5136651.32 773.129, 1748358.76 5136653.21 773.129, 1748356.79 5136656.19 773.129, 1748354.07 5136660.32 773.129, 1748348.53 5136665.18 773.129, 1748333.14 5136668.96 773.129, 1748328.14 5136669.42 773.129, 1748320.61 5136670.1 773.129, 1748315.97 5136670.48 773.129, 1748315.3 5136670.54 773.129, 1748304.63 5136671.4 773.129, 1748292.43 5136678.1 773.129, 1748284.63 5136689.93 773.129, 1748282.24 5136693.79 773.129, 1748278.53 5136699.76 773.129, 1748266.66 5136710.23 773.129, 1748257.66 5136711.9 773.129, 1748252.92 5136710.38 773.129, 1748245.92 5136708.12 773.129, 1748233.48 5136705.31 773.129, 1748223.28 5136703 773.129, 1748211.97 5136693.03 773.129, 1748206.17 5136686 773.114, 1748187.06 5136659.51 773.114, 1748185.87 5136658.15 773.101, 1748181.55 5136653.23 773.054, 1748173.2 5136636.95 773.054, 1748169.79 5136630.29 773.054, 1748157.99 5136613.96 773.054, 1748145.41 5136605.64 773.069, 1748137.18 5136604.48 773.084, 1748133.63 5136604.66 773.084, 1748122.27 5136605.23 773.084, 1748112.05 5136608.23 773.084, 1748096.84 5136610.21 773.084, 1748083.65 5136610.56 773.069, 1748070.37 5136611.7 773.174, 1748054.86 5136614.9 773.384, 1748051.29733583 5136615.82947196 773.937)) """ poly = loads(wkt) print poly points, segments = polygon_as_points_and_segments(poly) pprint(points) pprint(segments) dt = triangulate(points, segments) with open("/tmp/alltris.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh) with open("/tmp/allvertices.wkt", "w") as fh: output_vertices(dt.vertices, fh) with open("/tmp/interiortris.wkt", "w") as fh: output_triangles([t for t in InteriorTriangleIterator(dt)], fh) with open("/tmp/hull.wkt", "w") as fh: output_triangles([t for t in ConvexHullTriangleIterator(dt)], fh) # #if DEBUG: print poly # ln = [] # for ring in poly: # for vtx in ring: # ln.append(vtx) # # ev = poly.envelope # #if DEBUG: print ev # eps = 10000 # half_dx = (ev.xmax - ev.xmin) / 2.0 # dy = (ev.ymax - ev.ymin) # # top - middle # top_y = ev.ymax + dy + eps # top_x = ev.xmin + half_dx # # bottom - left # left_x = ev.xmin - half_dx - eps # left_y = ev.ymin - eps # # bottom - right # right_x = ev.xmax + half_dx + eps # right_y = ev.ymin - eps # # bnd = [Vertex(left_x,left_y), # Vertex(right_x,right_y), # Vertex(top_x,top_y)] # # return # mesh = Mesh(boundary = bnd) # # mesh = Mesh() # prev_pt = None # seed("ab") # for i, pt in enumerate(ln): # vtx = Vertex(pt.x, pt.y) # # if i == 2: # vtx.flag = 1 # ext_end = Point(pt.x, pt.y) # # elif i == 60: # vtx.flag = 1 # ext_end2 = Point(pt.x, pt.y) # else: # vtx.flag = 0 # int(randint(0, 10) in (5, )) # # vtx = mesh.insert(vtx) # vtx.gid = i # if i == 2: # ext_end_id = vtx.gid # elif i == 60: # ext_end2_id = vtx.gid # # if i > 0: # mesh.add_constraint( prev_pt, vtx ) # prev_pt = vtx # # fh = open('/tmp/tris.wkt', 'w') # fh.write("geometry\n") # MeshVisualizer(mesh).list_triangles_wkt(fh) # fh.close() visitor = EdgeEdgeHarvester([t for t in InteriorTriangleIterator(dt)]) visitor.skeleton_segments() with open("/tmp/skel0.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.segments: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) visitor = MidpointHarvester([t for t in InteriorTriangleIterator(dt)]) visitor.skeleton_segments() with open("/tmp/skel.wkt", "w") as fh: fh.write("wkt\n") for seg in visitor.segments: fh.write("LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".format(seg)) with open("/tmp/centres.wkt", "w") as fh: fh.write("wkt\n") for t, point in visitor.triangle_point.iteritems(): fh.write("POINT({0})\n".format(point)) # sys.exit() # visitor = TriangleVisitor(dt) # visitor.skeleton_segments() # visitor.pick_connectors() # # visitor.list_table_sg() # visitor.list_segments_pg() # visitor.list_connectors_pg() # visitor.list_pg() # visualizer = MeshVisualizer(mesh) # visualizer.list_pg() # make artificial external edges # ext_start = Point(1747940, 5136656) # ln = LineString() # ln.append(ext_start) # ln.append(ext_end) # outside_edges = [ # (500000, ln, 100, True, -1, 101, False, ext_end_id, 200, 201)] # #(eid, geom, sn, sn_ext, en, en_ext, lf, rf) # # ext_start2 = Point(1748550, 5136537) # ln = LineString() # ln.append(ext_start2) # ln.append(ext_end2) # outside_edges.append((600000, ln, 200, True, -2, 201, False, ext_end2_id, 201, 200)) # outside_edges = [] skeleton = SkeletonGraph() print """ ADDING OUTSIDE EDGES """ # first add outside edges for outside_edge in outside_edges: eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf, = outside_edge skeleton.add_segment(geom, external = True, edge_id = eid, left_face_id = lf, right_face_id = rf, start_node_id = sn, end_node_id = en, start_vertex_id = svtxid, end_vertex_id = evtxid, start_external = sn_ext, end_external = en_ext ) print """ BRIDGE CONNECTORS """ # add nodes from inner rings ("bridge" connectors) for i, segment in enumerate(visitor.ext_segments): v0, v1, = segment ln = LineString() ln.append(Point(*v0.point)) ln.append(Point(*v1.point)) skeleton.add_segment(ln, external = True, edge_id = i, start_vertex_id = v0.gid, end_vertex_id = v1.gid, left_face_id = v0.label, right_face_id = v0.label, # start_external = True, # end_external = True ) print """ UNLABELED EDGES """ # then add all segments which are unlabeled for i, segment in enumerate(visitor.segments): v0, v1, = segment ln = LineString() ln.append(Point(v0.x, v0.y)) ln.append(Point(v1.x, v1.y)) skeleton.add_segment(ln, start_vertex_id = v0.gid, end_vertex_id = v1.gid, external = False, edge_id = i + len(visitor.ext_segments)) fh = open('/tmp/edges.wkt', 'w') fh.write("geometry;leftface;rightface;sn;en\n") for edge in skeleton.edges: print >> fh, edge.geometry,";", edge.left_face_id,";", edge.right_face_id, ";", edge.start_node.id,";", edge.end_node.id fh.close() skeleton.visualize_nodes() skeleton.label_sides() skeleton.prune_branches() skeleton.find_new_edges() fh = open("/tmp/edges_new.wkt", "w") fh.write("eid;sn;en;lf;rf;length;geom\n") for eid, sn, en, lf, rf, length, geom, in skeleton.new_edges: print >> fh, eid, ";", sn,";", en, ";", lf, ";", rf, ";", length,";", geom fh.close()
def test_linear(self): line = LinearRing( [Point(0, 0), Point(10, 0), Point(5, 5), Point(0, 0)]) assert loads(dumps(line)) == line
def recs(): # external edge # edge_id, start_node, end_node, left_face, right_face, geom external = \ [[1004953, 2, 1004476, 1006293, 1006100, LineString([Point(x=-201.894, y=-305.203, srid=0), Point(x=-202.667, y=-311.323, srid=0), Point(x=-193.394, y=-315.9925, srid=0), Point(x=-193.564, y=-316.7255, srid=0)], srid=0)], [1010199, 1004476, 1007143, 1006293, 1006100, LineString([Point(x=-193.564, y=-316.7255, srid=0), Point(x=-203.348, y=-314.259, srid=0), Point(x=-203.594, y=-337.973, srid=0), Point(x=-202.365, y=-347.802, srid=0), Point(x=-194.872, y=-383.102, srid=0), Point(x=-187.366, y=-420.966, srid=0), Point(x=-183.5145, y=-423.9925, srid=0)], srid=0)], [1010646, 1331, 1333, 1004425, 1006293, LineString([Point(x=-13.523, y=-314.854, srid=0), Point(x=-11.0355, y=-313.1515, srid=0), Point(x=-11.158, y=-311.596, srid=0), Point(x=-14.004, y=-308.74, srid=0), Point(x=-20.565, y=-309.257, srid=0)], srid=0)]] # edges defining the polygon to be split polygon = \ [[1903, 1333, 1332, 1004425, 1006033, LineString([Point(x=-20.565, y=-309.257, srid=0), Point(x=-20.292, y=-312.717, srid=0), Point(x=-20.084, y=-315.361, srid=0)], srid=0)], [1005389, 1332, 1331, 1004425, 1006033, LineString([Point(x=-20.084, y=-315.361, srid=0), Point(x=-17.239, y=-315.141, srid=0), Point(x=-13.523, y=-314.854, srid=0)], srid=0)], [1009934, 1004476, 1333, 1006293, 1006033, LineString([Point(x=-193.564, y=-316.7255, srid=0), Point(x=-183.78, y=-319.192, srid=0), Point(x=-181.459, y=-319.701, srid=0), Point(x=-176.668, y=-319.862, srid=0), Point(x=-156.915, y=-320.53, srid=0), Point(x=-96.624, y=-314.452, srid=0), Point(x=-51.233, y=-311.671, srid=0), Point(x=-20.565, y=-309.257, srid=0)], srid=0)], [1010437, 1331, 1004476, 1006293, 1006033, LineString([Point(x=-13.523, y=-314.854, srid=0), Point(x=-14.227, y=-324.443, srid=0), Point(x=-16.649, y=-328.969, srid=0), Point(x=-22.207, y=-341.633, srid=0), Point(x=-30.853, y=-351.518, srid=0), Point(x=-40.735, y=-356.769, srid=0), Point(x=-55.247, y=-359.549, srid=0), Point(x=-94.772, y=-361.402, srid=0), Point(x=-119.344, y=-362.725, srid=0), Point(x=-154.431, y=-362.655, srid=0), Point(x=-169.850800842, y=-363.79366208, srid=0), Point(x=-173.407, y=-363.87, srid=0), Point(x=-175.696, y=-363.142, srid=0), Point(x=-183.532, y=-358.662, srid=0), Point(x=-186.713, y=-353.063, srid=0), Point(x=-190.159, y=-346.818, srid=0), Point(x=-192.098, y=-338.851, srid=0), Point(x=-191.236, y=-332.391, srid=0), Point(x=-184.571, y=-322.8, srid=0), Point(x=-193.7425, y=-317.5375, srid=0), Point(x=-193.564, y=-316.7255, srid=0)], srid=0)], [1010852, 1332, 1332, 1006033, 1006189, LineString([Point(x=-20.084, y=-315.361, srid=0), Point(x=-19.737, y=-321.247, srid=0), Point(x=-24.369, y=-332.676, srid=0), Point(x=-32.089, y=-344.105, srid=0), Point(x=-37.955, y=-349.665, srid=0), Point(x=-45.366, y=-351.827, srid=0), Point(x=-56.483, y=-353.062, srid=0), Point(x=-82.112, y=-353.371, srid=0), Point(x=-117.623, y=-356.46, srid=0), Point(x=-165.611, y=-359.953, srid=0), Point(x=-173.148, y=-358.231, srid=0), Point(x=-181.115, y=-353.494, srid=0), Point(x=-185.853, y=-346.818, srid=0), Point(x=-186.929, y=-340.143, srid=0), Point(x=-184.561, y=-333.683, srid=0), Point(x=-176.811, y=-324.08, srid=0), Point(x=-156.583, y=-325.007, srid=0), Point(x=-127.194, y=-324.027, srid=0), Point(x=-76.245, y=-319.702, srid=0), Point(x=-20.084, y=-315.361, srid=0)], srid=0)]] return external, polygon
def test_pt(self): pt = Point(0, 0) assert loads(dumps(pt)) == pt
def recs(): # external edge # edge_id, start_node, end_node, left_face, right_face, geom external = [] # polygon edges polygon = [(331, 199, 199, 74, 67, LineString([ Point(x=561248.71376, y=5898194.94024, srid=32632), Point(x=561266.86318, y=5898180.95802, srid=32632), Point(x=561265.84957, y=5898172.80328, srid=32632), Point(x=561264.9987, y=5898171.09707, srid=32632), Point(x=561262.3693, y=5898140.07594, srid=32632), Point(x=561227.39909, y=5898123.38065, srid=32632), Point(x=561210.85369, y=5898130.78853, srid=32632), Point(x=561180.97198, y=5898161.24993, srid=32632), Point(x=561176.4612, y=5898169.25525, srid=32632), Point(x=561172.95066, y=5898180.10562, srid=32632), Point(x=561173.61721, y=5898180.74667, srid=32632), Point(x=561187.34796, y=5898187.46103, srid=32632), Point(x=561201.76614, y=5898190.8415, srid=32632), Point(x=561243.65464, y=5898195.51012, srid=32632), Point(x=561248.71376, y=5898194.94024, srid=32632) ], srid=32632))] return external, polygon