Ejemplo n.º 1
0
    def test_polygon_with_hole(self):
        conv = ToPointsAndSegments()
        outside_edges, polygon = recs()
        for edge_id, start_node_id, end_node_id, _, _, geom in polygon:
            face = None
            if edge_id == 1003:  # interior ring
                face = -1
            for i, pt in enumerate(geom):
                if i == len(geom) - 1:  # last pt
                    node = end_node_id
                    tp = 1
                    if edge_id == 1003:  # we need a bridge vertex
                        tp = 2
                elif i == 0:  # first pt
                    node = start_node_id
                    tp = 1
                    if edge_id == 1003:
                        tp = 2
                else:  # intermediate pt
                    node = None
                    tp = 0
                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)

        visitor = EdgeEdgeHarvester([t for t in InteriorTriangleIterator(dt)])
        visitor.skeleton_segments()

        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)
        assert new_edge_id == 10001

        # -- 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 len(new_edges) == 1
        assert new_edge_id == 10001
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
    def test_polygon_with_hole(self):
        """Splitting of face with for which the skeleton is a cycle 
        There should be a bridge connector made at node 1332
        """
        conv = ToPointsAndSegments()
        outside_edges, polygon = recs()
        for edge_id, start_node_id, end_node_id, _, _, geom in polygon:
            face = None
            # loop edge: 1010852 (start node == end node)
            for i, pt in enumerate(geom):
                if i == len(geom) - 1:  # last pt
                    node = end_node_id
                    tp = 1
                    if node == 1332:  # we need a bridge vertex
                        face = 1006189
                        tp = 3
                        print "setting type to", tp, "face to", face
                elif i == 0:  # first pt
                    node = start_node_id
                    tp = 1
                    if node == 1332:
                        face = 1006189
                        tp = 3
                        print "setting type to", tp, "face to", face
                else:  # intermediate pt
                    node = None
                    tp = 0
                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)

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

        pick = ConnectorPicker(visitor)
        pick.pick_connectors()

        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():
                for seg in lst:
                    fh.write(
                        "LINESTRING({0[0].x} {0[0].y}, {0[1].x} {0[1].y})\n".
                        format(seg))

        skeleton, new_edge_id = make_graph(outside_edges,
                                           visitor,
                                           new_edge_id=10000,
                                           universe_id=0,
                                           srid=-1)
        label_sides(skeleton)
        #         assert new_edge_id == 10001

        # -- 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)
        from pprint import pprint
        pprint(new_edges)
        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
Ejemplo n.º 5
0
def test():

    #    Test:
    #    =====
    #    Triangle with *touching* hole

    #    FIXME:
    #    unnecessary nodes -->>> too short edges

    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
    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))

    with open("/tmp/skel1.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()

    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))

    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
Ejemplo n.º 6
0
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