def recs(): # external edge # edge_id, start_node, end_node, left_face, right_face, geom external = [(1005, 4, 1, 501, -1, LineString([(0, 120), (0, 110)])), (1004, 5, 2, -1, 501, LineString([(10, 120), (10, 110)]))] # polygon edges polygon = [ (1001, 1, 2, 501, 500, LineString([(0, 110), (5, 109), (10, 110)])), (1002, 1, 2, 500, -1, LineString([(0, 110), (0, 100), (10, 100), (10, 110)])), ] return external, polygon
def border_segments(self): """After clipping all edges, this property gives all edges that together form the rim of the clipping window """ segments = [] cx = (self.bbox.xmax + self.bbox.xmin) * .5 cy = (self.bbox.ymax + self.bbox.ymin) * .5 centre = (cx, cy) #radial sort around viewport clipped_coords = self.vertices[:] clipped_coords.sort( lambda x, y: cmp(-angle(centre, x), -angle(centre, y))) for i in range(-1, len(clipped_coords) - 1): self.new_edge_id -= 1 border_edge = (self.new_edge_id, self.vertex_ids[clipped_coords[i]], self.vertex_ids[clipped_coords[i + 1]], self.border_face_id, self.border_face_id, LineString( (clipped_coords[i], clipped_coords[i + 1])), { 'locked': True, 'border': True, 'clipped': True }) segments.append(border_edge) return segments
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 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
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(): 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 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 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 test2(): # 81996.029 454995.4185 82010.37 455005.837 # LINESTRING(81979.573 454987.807, 81981.688 454985.0, 81996.029 454995.4185, 82010.37 455005.837, 82009.463 455007.209) # POLYGON((81000.0 455000.0, 81000.0 456000.0, 82000.0 456000.0, 82000.0 455000.0, 81000.0 455000.0)) ec = EdgeClipper(bbox=Envelope(81000.0, 455000.0, 82000.0, 456000.0), border_face_id=None) ec.clip_edge(1, 1, 2, 3, 4, geometry=LineString( [[81979.573, 454987.807], [81981.688, 454985.0], [81996.029, 454995.4185], [82010.37, 455005.837], [82009.463, 455007.209]], srid=28992), attrs={})
def test(): 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 dt = triangulate(points, infos, 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) 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)) pick = ConnectorPicker(visitor) pick.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)) outside_edges = [ #eid, geom, sn, sn_ext, svtxid, en, en_ext, evtxid, lf, rf (4000, LineString([(0, 20), (1, 9)]), 5002, True, (0, 20), 1002, False, (1, 9), 10000, 10005), (4001, LineString([(20, 1), (9, 1)]), 5001, True, (20, 1), 1001, False, (9, 1), 10005, 10000), ] skeleton = make_graph(outside_edges, visitor) label_sides(skeleton) prune_branches(skeleton) groups = define_groups(skeleton) new_edges = make_new_edges(groups) with open("/tmp/edges_new.wkt", "w") as fh: fh.write("eid;sn;en;lf;rf;geom\n") for eid, sn, en, lf, rf, geom, in new_edges: print >> fh, eid, ";", sn, ";", en, ";", lf, ";", rf, ";", geom
def main(): import json # pts_segs = ToPointsAndSegments() # pts_segs.add_polygon([[(0,0), (10,0), (5,10), (0,0)], # #[(2,2), (8,2), (6,4), (5,7), (2,2)] # ], # ) # pts_segs.add_polygon([[(10,0), (15,10), (5,10), (10,0)], # #[(2,2), (8,2), (6,4), (5,7), (2,2)] # ], # ) # FIXME: does not work with this dataset yet, as the vertex density is not # high enough: should add more vertices (densify) with open('/home/martijn/workspace/splitarea/data/sandro/poly.geojson' ) as fh: c = json.loads(fh.read()) conv = ToPointsAndSegments() poly = c['features'][0]['geometry']['coordinates'] rings = [] for ring in poly: rings.append( # densify( [tuple(pt) for pt in ring] # , 5) ) del poly conv.add_polygon(rings) dt = triangulate(conv.points, conv.infos, conv.segments) trafo = VoronoiTransformer(dt) trafo.transform() with open("/tmp/vroni.wkt", "w") as fh: fh.write("wkt;start;end;left;right\n") for (start, end, lft, rgt) in trafo.segments: fh.write( "LINESTRING({0[0]} {0[1]}, {1[0]} {1[1]});{2};{3};{4};{5}\n". format(trafo.centers[start], trafo.centers[end], start, end, lft, rgt)) # FIXME: this should be part of the VoronoiTransformer ! tm = TopoMap() for i, (start, end, lft, rgt) in enumerate(trafo.segments, start=1): tm.add_edge(i, start, end, lft, rgt, LineString([trafo.centers[start], trafo.centers[end]])) find_loops(tm) with open("/tmp/geom.wkt", "w") as fh: fh.write("wkt\n") for face in tm.faces.itervalues(): try: fh.write("{0}\n".format(face.multigeometry()[0])) except: pass # 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/inside.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh)
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(): 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
# ], # [ # # loop # Edge(1, 1, 2, 1, 0, LineString([(0, 0), (3, 0)])), # Edge(2, 2, 3, 1, 0, LineString([(3, 0), (3, 2)])), # Edge(3, 3, 4, 1, 0, LineString([(3, 2), (0, 2)])), # Edge(4, 4, 1, 1, 0, LineString([(0, 2), (0, 0)])), # # dangle # Edge(5, 1, 5, 1, 1, LineString([(0, 0), (1,1)])), # Edge(6, 5, 6, 1, 1, LineString([(1,1), (1, 1.5)])), # Edge(7, 5, 7, 1, 1, LineString([(1,1), (1.5, 1)])) # #Edge(6, 3, 6, 1, 1, LineString([(3, 2), (2,1)])), # ], [ # loop Edge( 1, 1, 2, 1, 0, LineString([(0, 0), (3, 0)])), Edge( 2, 2, 3, 1, 0, LineString([(3, 0), (3, 2)])), Edge( 3, 3, 4, 1, 0, LineString([(3, 2), (0, 2)])), Edge( 4, 4, 1, 1, 0, LineString([(0, 2), (0, 0)])), Edge( 5, 1, 5, 1, 2, LineString([(0,0), (1,1.5), (1.5, 1.5)]) ), Edge( 6, 5, 1, 1, 2, LineString([(1.5, 1.5), (0,0)]) ), Edge( 7, 5, 6, 1, 3, LineString([(1.5, 1.5), (1.25, 1.8), (1.5, 1.8)]) ), Edge( 8, 6, 5, 1, 3, LineString([(1.5, 1.8), (1.5, 1.5)]) ), Edge( 9, 5, 7, 1, 4, LineString([(1.5, 1.5), (2, 1.5), (2, 0.5)]) ), Edge(10, 7, 5, 1, 4, LineString([(2, 0.5), (1.5, 1.5)]) ) ] ] faces = [ # [Face(1, 1)]
def test_line(self): line = LineString([Point(0, 0), Point(10, 0)]) assert loads(dumps(line)) == line
heading.append('geometry') print ";".join(map(str, heading)) break # select the edges that we need for the test case! ext = [1010199, 1004953, 1010646] needed = [1010852, 1010437, 1009934, 1005389, 1903] external = [] polygon = [] for feature in c['features']: props = [ feature['properties']['edgeId'], feature['properties']['startNodeId'], feature['properties']['endNodeId'], feature['properties']['leftFaceId'], feature['properties']['rightFaceId'] ] props.append(LineString(feature['geometry']['coordinates'])) edgeId = feature['properties']['edgeId'] if edgeId in ext: external.append(props) # print ";".join(map(str, props)) if edgeId in needed: polygon.append(props) # print ";".join(map(str, props)) print "external = \\" pprint(external) print "polygon = \\" pprint(polygon)
def geometry(self): """Constructs geometry for this Loop """ # caching of rings, as copying of geometry is quite heavy process # (could be optimised -> faster array mechanism under neath geometry?) # # MM disable caching of rings at all... # self.linear_rings = None # self.linestrings = None if self.linear_rings is None: # make linear rings out of this loop (tangency # can cause a loop to have multiple rings # Rings are tested for simplicity (no self-intersections) # so we have to separate rings that are self-tangent in one point # into more than 1 ring nodes = set() tangent_nodes = set() stack = [] visit_count = {} _tangent_nodes_add = tangent_nodes.add # ref to function _nodes_add = nodes.add # ref to function for edge in self.half_edges: #print edge.id, "origin", edge.origin, "face", edge.face if edge.origin not in nodes: _nodes_add(edge.origin) else: _tangent_nodes_add(edge.origin) if edge.origin not in visit_count: visit_count[edge.origin] = 1 visit_count[edge.origin] += 1 ring_is_flat = edge.twin.face is edge.face # if tangent_nodes: rings = [] linestrings = [] ring = LineString() first = True start_node = None end_node = None edge_seen = set() #_ring_extend = ring.extend # ref to function for edge in self.half_edges: is_flat = edge.twin.face is edge.face if not edge.id in edge_seen: if edge.anchor is not None: geom = edge.anchor.geometry step = 1 else: geom = edge.twin.anchor.geometry step = -1 if first: s = slice(None, None, step) first = False else: if step == -1: s = slice(-2, None, step) else: assert step == 1 s = slice(1, None, step) ring.extend(geom, s) if is_flat: edge_seen.add(edge.id) if start_node is None: start_node = edge.origin end_node = edge.twin.origin if end_node in tangent_nodes: visit_count[end_node] -= 1 # 3 cases: # stack what was made and open new one # finish what was made and open new one # finish what was made and continue previous one if start_node is end_node: # finish was what made if is_flat: linestrings.append(ring) else: rings.append(LinearRing(ring)) loops_to_make = visit_count[end_node] # FIXME: these conditions are a bit complex # can they be simplified ???? if loops_to_make > 0: # open new one start_node = None end_node = None ring = LineString() first = True elif stack: # continue previous one ring, start_node = stack.pop() first = False else: assert loops_to_make == 0 # open new one start_node = None end_node = None ring = LineString() first = True else: # stack what was made and open new one stack.append((ring, start_node)) start_node = None end_node = None ring = LineString() first = True if len(ring): assert start_node is end_node if is_flat: linestrings.append(ring) else: rings.append(LinearRing(ring)) # postcondition: stack should be empty now assert not stack, "stack should be empty now" self.linear_rings = rings self.linestrings = linestrings else: ring = LineString() first = True edge_seen = set() # No tangent node but some edge is still dead ends then whole cycle is empty. # _______ # / \ # | --- | # \_______/ # for edge in self.half_edges: if not edge.id in edge_seen: if edge.anchor is not None: geom = edge.anchor.geometry step = 1 else: geom = edge.twin.anchor.geometry step = -1 if first: s = slice(None, None, step) first = False else: if step == -1: s = slice(-2, None, step) else: assert step == 1 s = slice(1, None, step) ring.extend(geom, s) if ring_is_flat: edge_seen.add(edge.id) if ring_is_flat: # print "ring inside is flat" self.linestrings = [ring] else: self.linear_rings = [LinearRing(ring)] ## Expensive checks! # for ring in self.linear_rings: # assert is_linearring(ring) # assert is_ring_simple(ring) return self.linear_rings, self.linestrings
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 transform(dt): trafo = VoronoiTransformer(dt) trafo._transform_centers() # calculate circumcenter of all triangles # FIXME: this is naive: segments can be unneeded, if there are triangles that have the same circumcentre, or for which the center is exactly the same (unlikely under floating point precision -> angle calculation is also not possible then)... trafo._transform_segments() #with open("/tmp/vroni_segments.wkt", "w") as fh: # fh.write("id;wkt;start;end;left;right;dist\n") # for i, (start, end, lft, rgt) in enumerate(trafo.segments, start = 1): # fh.write("{6};LINESTRING({0[0]} {0[1]}, {1[0]} {1[1]});{2};{3};{4};{5};{7}\n".format(trafo.centers[start], trafo.centers[end], start, end, lft, rgt, i, dist(trafo.centers[start], trafo.centers[end]))) # by iterating over all vertices and all the triangles in their stars we # obtain the voronoi cells (polygons) cells = trafo._transform_cells() with open("/tmp/vroni_cells.wkt", "w") as fh: fh.write("wkt\n") for cell in cells: fh.write("{}\n".format(cell)) close_pairs = trafo.close_centers() ds = DisjointSet() for a in close_pairs: for b in close_pairs[a]: ds.add(a, b) # print ds.leader # print ds.group # trafo.transform() #with open("/tmp/allvertices.wkt", "w") as fh: # tri.delaunay.output_vertices(dt.vertices, fh) with open("/tmp/vroni_nodes.wkt", "w") as fh: fh.write("id;wkt\n") for i, geom in trafo.centers.iteritems(): txt = "{0};POINT({1[0]} {1[1]})\n".format(i, geom) fh.write(txt) #def dist(orig, dest): # dx = orig[0] - dest[0] # dy = orig[1] - dest[1] # return (dx * dx + dy * dy) ** 0.5 with open("/tmp/vroni_segments_culled.wkt", "w") as fh: fh.write("id;wkt;start;end;left;right;dist\n") for i, (start, end, lft, rgt) in enumerate(trafo.segments, start = 1): start = ds.leader.get(start, start) end = ds.leader.get(end, end) # skip segments that do not have length if start != end: fh.write("{6};LINESTRING({0[0]} {0[1]}, {1[0]} {1[1]});{2};{3};{4};{5};{7}\n".format(trafo.centers[start], trafo.centers[end], start, end, lft, rgt, i, dist(trafo.centers[start], trafo.centers[end]))) from topomap.topomap import TopoMap from topomap.loopfactory import find_loops from simplegeom.geometry import LineString tm = TopoMap(universe_id=0) for v in dt.vertices: if v.is_finite: tm.add_face(id(v), attrs = v.info) for i, (start, end, lft, rgt) in enumerate(trafo.segments, start = 1): start = ds.leader.get(start, start) end = ds.leader.get(end, end) if start != end: tm.add_edge(i, start, end, lft, rgt, LineString([trafo.centers[start], trafo.centers[end]])) find_loops(tm) with open("/tmp/geom.wkt", "w") as fh: fh.write("wkt;segment;height\n") for face in tm.faces.itervalues(): if not face.unbounded: fh.write("{0};{1[0]};{1[1]}\n".format(face.multigeometry()[0], face.attrs))
# 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 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))
def simplify(line, pp, tolerance=float("inf"), DEBUG=False): """Simplifies a polyline with Visvalingham Whyatt algorithm, i.e. bottom up """ # FIXME: Do we need an additional step to split a polyline # in 2 pieces when it is a loop? Will lead to different simplification at # least # prv = [None] * len(line) nxt = [None] * len(line) for i in range(len(line)): prv[i] = i - 1 nxt[i] = i + 1 # first does not have prv and # last does not have nxt prv[0] = None nxt[-1] = None # measures for points # first and last do not get any measure oseq = pqdict.PQDict() for i in range(1, len(line) - 1): size = area(line[prv[i]], line[i], line[nxt[i]]) base = dist(line[prv[i]], line[nxt[i]]) if base == 0: eps = float("inf") else: eps = size / (0.5 * base) oseq[i] = eps # FIXME: # loops can also be formed by 2 edges, # that can end up 'on top' of each other is_loop = line[0] == line[-1] remaining_ct = len(line) measure = float("inf") while oseq: idx, measure, = oseq.popitem() # print('try removing {}'.format(line[idx])) if measure > tolerance: break if is_loop and remaining_ct <= 4: # we need 4 points in a loop edge, no further simplification # set measure to +inf, preventing from picking this edge again for # simplification measure = float("inf") break # FIXME: temporary stop criterion, always keep 3 points in any edge # rationale: areas keep some size with 3 (non-collinear) points # although no guarantees on collinearity, reduces chance # on face collapsing into line (degeneracy) if remaining_ct <= 3: measure = float("inf") break # -- check if the triangle is empty ### print prv[idx], idx, nxt[idx] start, mid, end = line[prv[idx]], line[idx], line[nxt[idx]] if DEBUG: # True: with open("/tmp/triangle_pts.wkt", "w") as fh: fh.write("wkt\n") output_points([start, mid, end], fh) # rectangle that lies around this triangle xmin = min(start[0], min(mid[0], end[0])) xmax = max(start[0], max(mid[0], end[0])) ymin = min(start[1], min(mid[1], end[1])) ymax = max(start[1], max(mid[1], end[1])) rect = [(xmin, ymin), (xmax, ymax)] if DEBUG: # True: with open("/tmp/rect.wkt", "w") as fh: fh.write("wkt\n") fh.write( """POLYGON(({xmin} {ymin}, {xmax} {ymin}, {xmax} {ymax}, {xmin} {ymax}, {xmin} {ymin}))\n""" .format(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax)) # by means of the quadtree we find overlapping points # the number of overlapping points can already tell us something: # # if we find exactly 3, this triangle is empty, no need to check # the points # - however, it depends on the topological config with other edges # whether it is safe to remove the vertex -> end points that form # base of triangle, which also is an edge --> problematic to remove # (area collapse of triangle) -- two edges form a loop # # if we find less than 3, there is a problem # - either this is a degenerate line (with multiple vertices on same # location) -> should be solved while reading the data # - or there exists a problem/bug with the quadtree/keeping it up to date # -> point not added in the tree, or the intersection method is # not performing ok... # # if there is more than 3 points, we could potentially introduce # intersection between segments, check if these points lie on interior # of triangle that will be collapsed if we remove the point, if not # then it is safe to remove the vertex from the polyline # (postbox principle) skip = False overlapping_pts = pp.quadtree.range_search(rect) # overlapping_pts = pp.kdtree.range_search(*rect) if DEBUG: # def do_debug(): with open("/tmp/overlapping.wkt", "w") as fh: fh.write("wkt\n") output_points(overlapping_pts, fh) with open("/tmp/quadtree_pts.wkt", "w") as fh: fh.write("wkt\n") output_points([pt for pt in pp.quadtree], fh) do_debug() for pt in overlapping_pts: if ((pt[0] == start[0] and pt[1] == start[1]) or (pt[0] == mid[0] and pt[1] == mid[1]) or (pt[0] == end[0] and pt[1] == end[1])): continue elif on_triangle(pt, start, mid, end): skip = True break if DEBUG: # print(f"skip := {skip} ") input("debugging line") if skip: continue # -- really remove vertex from the line pp.quadtree.remove((mid[0], mid[1])) remaining_ct -= 1 # make sure we only simplify interior points, and not end points # get neighbouring points prvidx = prv[idx] nxtidx = nxt[idx] indices = [] if prvidx != 0: indices.append(prvidx) if nxtidx != len(line) - 1: indices.append(nxtidx) # # for idx in indices: # oseq.pop(idx) # link up previous and nxt vertex nxt[prvidx] = nxtidx prv[nxtidx] = prvidx # update measures for those left and right for i in indices: assert 0 < i < len(line), "out of bounds: {0} {1}".format( i, len(line)) size = area(line[prv[i]], line[i], line[nxt[i]]) base = dist(line[prv[i]], line[nxt[i]]) if base == 0: eps = float("inf") else: eps = size / (0.5 * base) oseq[i] = eps if DEBUG: input("paused after simplify") if remaining_ct == 2: measure = float("inf") simplified = [] simplified.append(line[0]) nxtidx = nxt[0] assert nxtidx is not None while nxtidx is not None: simplified.append(line[nxtidx]) nxtidx = nxt[nxtidx] return LineString(simplified, line.srid), measure
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
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
from simplegeom.geometry import LineString if __name__ == "__main__": empty = LineString() assert empty.is_empty print(empty) a = LineString([(0, 0), (1, 2)]) b = LineString([(0, 0), (1, 2)]) print(a.length) assert a == b lnz = LineString([(0, 0, 0), (1, 2, 3)]) print(lnz) lnm = LineString([(0, 0, 0), (1, 2, 3)]) assert lnz == lnm print(lnm[-1]) ln = LineString([(0, 0, 0, 4), (1, 2, 3, 4)]) print(ln) print(ln[1])
def simplify_reumann_witkam(polyline, pp, tolerance, DEBUG): # own_points = set([(pt[0], pt[1]) for pt in polyline]) p0 = 0 p1 = 1 # box = polyline.envelope # init_box(ring) # ring = [polyline[p0], polyline[p1]] # for i in range(2, len(polyline)): # increase_box(box, polyline[i]) # others = set(pp.quadtree.range_search([(box.xmin, box.ymin), (box.xmax, box.ymax)])) keep = [True] * len(polyline) is_loop = polyline[0] == polyline[-1] remaining_ct = len(polyline) # the first points defines the line to check the other points against p0 = 0 p1 = 1 pi = pj = p1 # keep first point keep[p0] = True ring = [polyline[p0], polyline[p1]] box = init_box(ring) measure = float("+inf") for i in range(2, len(polyline)): pi = pj pj += 1 # advance the index for the point to be checked if DEBUG: print("Debug info for RW-simplify") print(f"p0 := {p0}") print(f"p1 := {p1}") print(f" pi := {pi}") print(f" pj := {pj}") print("") ring.append(polyline[pj]) increase_box(box, polyline[pj]) dist = point_line_distance([polyline[p0], polyline[p1]], polyline[pj]) # the points of the line that we have kept so far / still need to check? (self intersect) own_points = set([(pt[0], pt[1]) for pt in ring]) # FIXME: # if we are going to keep this point (and shift the initial direction line/box), # we do not need the whole topology preserving circus # only, if the point is potentially to be removed, we need to check for violations # # at this moment this does not have the correct order of operations! if DEBUG: with open("/tmp/init_line.wkt", "w") as fh: lnstr = "LINESTRING({0[0][0]} {0[0][1]}, {0[1][0]} {0[1][1]})".format( [polyline[p0], polyline[p1]] ) fh.write("wkt\n") fh.write(lnstr) fh.write("\n") with open("/tmp/point__check.wkt", "w") as fh: lnstr = "POINT({0[0]} {0[1]})".format(polyline[pj]) fh.write("wkt\n") fh.write(lnstr) fh.write("\n") simplified = [] for preserve, pt in zip(keep, polyline): if preserve: simplified.append(pt) with open("/tmp/simplified_line.wkt", "w") as fh: fh.write("wkt\n") fh.write("{}".format(LineString(simplified, polyline.srid))) fh.write("\n") if len(ring) >= 3: with open("/tmp/keepfree_poly.wkt", "w") as fh: tmp = ring[:] tmp.append(ring[0]) fh.write("wkt\n") fh.write( "{}".format( Polygon( shell=LinearRing(tmp, polyline.srid), srid=polyline.srid ) ) ) fh.write("\n") # FIXME: next to the distance, we also should check whether the # polygon formed by the points from p0...pj is still empty of other # points # -- polygon should be formed here / to linear ring a point should # be added, and around this polygon we should query the quadtree # for points in the neighbourhood # then we should do a point in polygon test for points not being # part of the polygon, best if such a point in polygon test # is robust against polygons not being 'valid' (like bow ties) if pp is not None: # own = set((pt.x, pt.y) for pt in ring) # FIXME: deal with points of own line that we will also find # with the quadtree # FIXME: we could also do this search at the beginning # at the expense of doing some more iterations here # and also having to keep this set then updated others = pp.quadtree.range_search(box) if DEBUG: with open("/tmp/qt_box.wkt", "w") as fh: fh.write("wkt\n") fh.write(as_polygon(box)) fh.write("\n") with open("/tmp/close_points.wkt", "w") as fh: fh.write("wkt\n") for pt in others: fh.write(f"POINT({pt[0]} {pt[1]})") fh.write("\n") topology_problem = False for other in others: # FIXME: this is not okay, as self intersection potentially can happen if DEBUG: print(f"checking {other}") print(ring) print(other) if other in own_points: if DEBUG: print("- other part of line being simplified") # FIXME: potential self-intersection # - to fix, make set of ring object and use that continue if is_point_in_polygon(ring, other): if DEBUG: print("- other lies in sweep-poly (blocking!)") topology_problem = True break # ? else: if DEBUG: print("- other not in sweep-poly") if DEBUG: input(f"line written as debugging requested (DEBUG={DEBUG})") if topology_problem: # we keep the point at index pi keep[pi] = True # make a new line for checking the remaining points with p0 = pi p1 = pj ring = [polyline[p0], polyline[p1]] box = init_box(ring) # if dist < measure: # measure = dist # continue main loop, check remaining points continue if is_loop and remaining_ct <= 4: # we need 4 points in a loop edge, no further simplification # set measure to +inf, preventing from picking this edge again for # simplification measure = float("inf") break # FIXME: temporary stop criterion, always keep 3 points in any edge # rationale: areas keep some size with 3 (non-collinear) points # although no guarantees on collinearity, reduces chance # on face collapsing into line (degeneracy) if remaining_ct <= 3: measure = float("inf") break if dist <= tolerance: # do not keep this point # FIXME: which point to keep pi or pj? if pp is not None: pp.quadtree.remove((polyline[pi][X], polyline[pi][Y])) # others.discard((polyline[pi][X], polyline[pi][Y])) remaining_ct -= 1 keep[pi] = False continue else: # we want to keep the point at index pi keep[pi] = True # make a new line for checking the remaining points with p0 = pi p1 = pj ring = [polyline[p0], polyline[p1]] box = init_box(ring) if dist < measure: measure = dist # keep last point keep[-1] = True # first and last point should be kept assert keep[0] == True assert keep[len(polyline) - 1] == True # get the simplified polyline simplified = [] for preserve, pt in zip(keep, polyline): if preserve: simplified.append(pt) return ( LineString(simplified, polyline.srid), measure, ) # FIXME: check if new tolerance calculation is correct
def test(): import matplotlib.pyplot as plt ec = EdgeClipper(bbox=Envelope(0, 0, 10, 10), border_face_id=None) ec.clip_edge(1, 1, 2, 3, 4, geometry=LineString([(-5, 5), (2.5, 12.5)], srid=28992), attrs={}) ec.clip_edge(5, 6, 7, 8, 9, geometry=LineString([(-5, 5), (5, 5)], srid=28992), attrs={}) ec.clip_edge(10, 11, 12, 13, 14, geometry=LineString([(7.5, 5), (10, 5)], srid=28992), attrs={}) ec.clip_edge(15, 16, 17, 18, 19, geometry=LineString([(10, 6), (7.5, 6)], srid=28992), attrs={}) ec.clip_edge(20, 21, 22, 23, 24, geometry=LineString([(10, 7), (7.5, 7), (6.5, 7)], srid=28992), attrs={}) ec.clip_edge(25, 26, 27, 28, 29, geometry=LineString([(2.5, 12.5), (2.5, 10), (7.5, 10), (7.5, 12.5)], srid=28992), attrs={}) ec.clip_edge(30, 31, 32, 33, 34, geometry=LineString([(1, 7), (0, 7), (-1, 7)], srid=28992), attrs={}) ec.clip_edge(35, 36, 37, 38, 39, geometry=LineString([(-1, 8), (0, 8), (1, 8)], srid=28992), attrs={}) ec.clip_edge(40, 41, 42, 43, 44, geometry=LineString([(1, -1), (1, 1), (2, 1), (2, -1), (3, -1), (3, 1), (4, 1), (4, -1), (5, -1), (5, 1), (6, 1)], srid=28992), attrs={}) ec.clip_edge(45, 46, 47, 48, 49, geometry=LineString([(0, 11), (2, 9)], srid=28992), attrs={}) ec.clip_edge(50, 51, 52, 53, 54, geometry=LineString([(-1, 11), (1, 9)], srid=28992), attrs={}) ec.clip_edge(55, 56, 57, 58, 59, geometry=LineString([(9, 2), (12, 2)], srid=28992), attrs={}) ec.clip_edge(55, 56, 57, 58, 59, geometry=LineString([(-1, 3), (0, 3), (5, 4), (10, 3)], srid=28992), attrs={}) ec.clip_edge(60, 61, 62, 63, 64, geometry=LineString([(7, 2), (8, 0), (9, 1)], srid=28992), attrs={}) # - print clipped edges -------------------------------- for item in ec.clipped: print(item) # - plot -------------------------------- for item in ec.border_segments: geom = item[5] X, Y = [], [] [(X.append(vertex[0]), Y.append(vertex[1])) for vertex in geom] plt.plot(X, Y, 'b-', alpha=0.5) for item in ec.clipped: geom = item[5] X, Y = [], [] [(X.append(vertex[0]), Y.append(vertex[1])) for vertex in geom] plt.plot(X, Y, 'gH-', alpha=0.5, markersize=20) X, Y = [], [] [(X.append(vertex[0]), Y.append(vertex[1])) for vertex in ec.vertices] plt.plot(X, Y, 'ro') plt.axis([-1, 11, -1, 11]) plt.show()
def test_enlarge_by_unknown_type(self): e = Envelope(0, 0, 10, 10) ln = LineString([(0, 0), (20, 20)]) self.assertRaises(TypeError, e.enlarge_by, ln)
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()