Beispiel #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
Beispiel #2
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))
    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
Beispiel #4
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
Beispiel #5
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