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(): 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(): # 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(): 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 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(): # 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 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