def main(self): pts_segs = ToPointsAndSegments() pts_segs.add_polygon([[(0, 0), (10, 0), (5, 10), (0, 0)], [(0, 0), (8, 2), (6, 4), (5, 7), (0, 0)]], ) #pts_segs.add_polygon([[(10,0), (15,10), (5,10), (10,0)], #[(2,2), (8,2), (6,4), (5,7), (2,2)] #], #) dt = triangulate(pts_segs.points, pts_segs.infos, pts_segs.segments) with open("/tmp/alltris.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh) with open("/tmp/path.wkt", "w") as fh: fh.write("i;group;depth;wkt\n") it = RegionatedTriangleIterator(dt) for i, (g, d, t) in enumerate(it, start=1): fh.write("{0};{1};{2};{3}\n".format(i, g, d, t)) with open("/tmp/later.wkt", "w") as fh: fh.write("wkt\n") for t in it.later: fh.write("{0}\n".format(t))
def main(): with open('curvy.geojson') as fh: t = json.load(fh) rings = [] for ring in t['features'][0]['geometry']['coordinates']: rings.append( #densify( [tuple(pt) for pt in ring[0]] # ) ) conv = ToPointsAndSegments() conv.add_polygon(rings) dt = triangulate(conv.points, conv.infos, conv.segments) with open("/tmp/alltris.wkt", "w") as fh: output_triangles([t for t in TriangleIterator(dt)], fh) visitor = NarrowPartFinder([t for t in ConvexHullTriangleIterator(dt)]) visitor.find_narrows() with open("/tmp/filtered.wkt", "w") as fh: output_triangles(visitor.filtered, fh) with open("/tmp/remaining.wkt", "w") as fh: l = [] filtered = frozenset(visitor.filtered) for t in ConvexHullTriangleIterator(dt): if t not in filtered: l.append(t) output_triangles(l, fh) with open("/tmp/path.wkt", "w") as fh: fh.write("i;group;depth;wkt\n") it = RegionatedTriangleIterator(dt) for i, (g, d, t) in enumerate(it, start=1): fh.write("{0};{1};{2};{3}\n".format(i, g, d, t)) it = RegionatedTriangleIterator(dt) zero_tris = [] for (g, d, t) in it: if g == 1: for i in xrange(3): if t.constrained[i]: break else: zero_tris.append(t) else: break with open("/tmp/outside_zero.wkt", "w") as fh: output_triangles(zero_tris, fh) visitor = EdgeEdgeHarvester([t for t in ConvexHullTriangleIterator(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))
def main(): logging.basicConfig(filename='test_logging.log', level=logging.DEBUG) pts_segs = ToPointsAndSegments() pts_segs.add_polygon([[(0,0), (10,0), (5,10), (0,0)], [(0,0), (8,2), (6,4), (5,7), (0,0)] ], ) dt = triangulate(pts_segs.points, pts_segs.infos, pts_segs.segments)
from tri import ToPointsAndSegments from grassfire import calc_skel #from simplegeom.wkt import loads """Centre in Goes """ if True: import logging import sys root = logging.getLogger() root.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') formatter = logging.Formatter('%(asctime)s - %(message)s') ch.setFormatter(formatter) root.addHandler(ch) conv = ToPointsAndSegments() polygon = [[(0, 3), (0.5, 5), (9, 8.5), (10, 2), (6, 4), (3.5, 1), (0, 3)]] conv.add_polygon(polygon) skel = calc_skel(conv, pause=True, output=True, shrink=True) # skeletonize / offset
def test_replace_kvertex(self): class QueueMock(object): """Mock for the priority queue """ def add(self, one): pass def remove(self, one): pass def discard(self, one): pass # init skeleton structure conv = ToPointsAndSegments() polygon = [[(-2, -1), (-1, 0), (1, 0), (1.5, -.5), (1.2, .7), (.4, 1.2), (-.6, 1.1), (-1.7, .7), (-2, -1)]] conv.add_polygon(polygon) dt = triangulate(conv.points, None, conv.segments) output_dt(dt) skel = init_skeleton(dt) # queue = QueueMock() newv = KineticVertex() newv.origin = (1, 0) newv.velocity = (0.5, 0.5) found = None for t in skel.triangles: if t.finite: if [v.position_at(0) for v in t.vertices] == [(1.0, 0.0), (0.4, 1.2), (-1.0, 0.0)]: found = t break assert found is not None a, b, c = None, None, None for v in t.vertices: if v.position_at(0) == (-1, 0): a = v if v.position_at(0) == (1, 0): b = v if v.position_at(0) == (0.4, 1.2): c = v assert a is not None assert b is not None assert c is not None # precondition: # no vertex equal to the new kinetic vertex for t in skel.triangles: assert newv not in t.vertices replace_kvertex(found, b, newv, 0, cw, queue) # postcondition # we should have replaced 3 vertices ct = 0 for t in skel.triangles: if newv in t.vertices: ct += 1 assert ct == 3 newv = KineticVertex() newv.origin = (-1, 0) newv.velocity = (-0.5, 0.5) # precondition: # no vertex equal to the new kinetic vertex for t in skel.triangles: assert newv not in t.vertices replace_kvertex(found, a, newv, 0, ccw, queue) # we should have replaced 4 vertices ct = 0 for t in skel.triangles: if newv in t.vertices: ct += 1 assert ct == 4
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)