예제 #1
0
    def filter(self, osmdb, *args):
        g = Graph()
        t0 = time.time()

        vertices = {}
        print "load vertices into memory"
        for row in osmdb.execute("SELECT id from nodes"):
            g.add_vertex(str(row[0]))
            vertices[str(row[0])] = 0

        print "load ways into memory"
        for way in osmdb.ways():
            g.add_edge(way.nds[0], way.nds[-1], Link())
            g.add_edge(way.nds[-1], way.nds[0], Link())

        t1 = time.time()
        print "populating graph took: %f" % (t1 - t0)
        t0 = t1

        iteration = 1
        c = osmdb.cursor()
        while True:
            #c.execute("SELECT id from nodes where id not in (SELECT node_id from graph_nodes) LIMIT 1")
            try:
                vertex, dummy = vertices.popitem()
            except:
                break
            spt = g.shortest_path_tree(vertex, None, State(1, 0))
            for v in spt.vertices:
                vertices.pop(v.label, None)
                c.execute("INSERT into graph_nodes VALUES (?, ?)",
                          (iteration, v.label))
            spt.destroy()

            t1 = time.time()
            print "pass %s took: %f" % (iteration, t1 - t0)
            t0 = t1
            iteration += 1
        c.close()

        osmdb.conn.commit()
        g.destroy()
        # audit
        for gnum, count in osmdb.execute(
                "SELECT graph_num, count(*) FROM graph_nodes GROUP BY graph_num"
        ):
            print "FOUND: %s=%s" % (gnum, count)
예제 #2
0
    def filter(self, osmdb, *args):
        g = Graph()
        t0 = time.time()
        
        vertices = {}
        print "load vertices into memory"
        for row in osmdb.execute("SELECT id from nodes"):
            g.add_vertex(str(row[0]))
            vertices[str(row[0])] = 0

        print "load ways into memory"
        for way in osmdb.ways():
            g.add_edge(way.nds[0], way.nds[-1], Link())
            g.add_edge(way.nds[-1], way.nds[0], Link())

        t1 = time.time()
        print "populating graph took: %f"%(t1-t0)
        t0 = t1
        
        iteration = 1
        c = osmdb.cursor()
        while True:
            #c.execute("SELECT id from nodes where id not in (SELECT node_id from graph_nodes) LIMIT 1")
            try:
                vertex, dummy = vertices.popitem()
            except:
                break
            spt = g.shortest_path_tree(vertex, None, State(1,0))
            for v in spt.vertices:
                vertices.pop(v.label, None)
                c.execute("INSERT into graph_nodes VALUES (?, ?)", (iteration, v.label))
            spt.destroy()
            
            t1 = time.time()
            print "pass %s took: %f"%(iteration, t1-t0)
            t0 = t1
            iteration += 1
        c.close()
        
        osmdb.conn.commit()
        g.destroy()
        # audit
        for gnum, count in osmdb.execute("SELECT graph_num, count(*) FROM graph_nodes GROUP BY graph_num"):
            print "FOUND: %s=%s" % (gnum, count)
예제 #3
0
 def test_basic(self):
     g = Graph()
     g.add_vertex("A")
     g.add_vertex("B")
     g.add_edge("A", "B", Link())
     g.add_edge("A", "B", Street("foo", 20.0))
     gdb_file = os.path.dirname(__file__) + "unit_test.db"
     if os.path.exists(gdb_file):
         os.remove(gdb_file)        
     gdb = GraphDatabase(gdb_file)
     gdb.populate(g)
     
     list(gdb.execute("select * from resources"))
     assert "A" in list(gdb.all_vertex_labels())
     assert "B" in list(gdb.all_vertex_labels())
     assert glen(gdb.all_edges()) == 2
     assert glen(gdb.all_outgoing("A")) == 2
     assert glen(gdb.all_outgoing("B")) == 0
     assert glen(gdb.all_incoming("A")) == 0
     assert glen(gdb.all_incoming("B")) == 2
     assert glen(gdb.resources()) == 0
     assert gdb.num_vertices() == 2
     assert gdb.num_edges() == 2
     
     g.destroy()
     g = gdb.incarnate()
     
     list(gdb.execute("select * from resources"))
     assert "A" in list(gdb.all_vertex_labels())
     assert "B" in list(gdb.all_vertex_labels())
     assert glen(gdb.all_edges()) == 2
     assert glen(gdb.all_outgoing("A")) == 2
     assert glen(gdb.all_outgoing("B")) == 0
     assert glen(gdb.all_incoming("A")) == 0
     assert glen(gdb.all_incoming("B")) == 2
     assert glen(gdb.resources()) == 0
     assert gdb.num_vertices() == 2
     assert gdb.num_edges() == 2
     
     os.remove( gdb_file )
예제 #4
0
    def test_basic(self):
        g = Graph()
        g.add_vertex("A")
        g.add_vertex("B")
        g.add_edge("A", "B", Link())
        g.add_edge("A", "B", Street("foo", 20.0))
        gdb_file = os.path.dirname(__file__) + "unit_test.db"
        if os.path.exists(gdb_file):
            os.remove(gdb_file)
        gdb = GraphDatabase(gdb_file)
        gdb.populate(g)

        list(gdb.execute("select * from resources"))
        assert "A" in list(gdb.all_vertex_labels())
        assert "B" in list(gdb.all_vertex_labels())
        assert glen(gdb.all_edges()) == 2
        assert glen(gdb.all_outgoing("A")) == 2
        assert glen(gdb.all_outgoing("B")) == 0
        assert glen(gdb.all_incoming("A")) == 0
        assert glen(gdb.all_incoming("B")) == 2
        assert glen(gdb.resources()) == 0
        assert gdb.num_vertices() == 2
        assert gdb.num_edges() == 2

        g.destroy()
        g = gdb.incarnate()

        list(gdb.execute("select * from resources"))
        assert "A" in list(gdb.all_vertex_labels())
        assert "B" in list(gdb.all_vertex_labels())
        assert glen(gdb.all_edges()) == 2
        assert glen(gdb.all_outgoing("A")) == 2
        assert glen(gdb.all_outgoing("B")) == 0
        assert glen(gdb.all_incoming("A")) == 0
        assert glen(gdb.all_incoming("B")) == 2
        assert glen(gdb.resources()) == 0
        assert gdb.num_vertices() == 2
        assert gdb.num_edges() == 2

        os.remove(gdb_file)
예제 #5
0
def FindDisjunctGraphs (dbname):
        db = OSMDB(dbname)
        
        #should really be done before simplifying and splitting
        #fuse_nodes(db)
        
        c = db.cursor()
        c.execute("DROP table if exists graph_nodes")
        c.execute("DROP table if exists graph_edges")
        c.execute("CREATE table graph_nodes (graph_num INTEGER, node_id TEXT, WKT_GEOMETRY TEXT)")
        c.execute("CREATE table graph_edges (graph_num INTEGER, edge_id TEXT, WKT_GEOMETRY TEXT)")
        c.execute("CREATE index graph_nodes_id_indx ON graph_nodes(node_id)")
        c.execute("CREATE index graph_edges_id_indx ON graph_edges(edge_id)")
        c.close()
      
        g = Graph()
        t0 = time.time()
        
        vertices = {}
        print "load vertices into memory"
        for row in db.execute("SELECT DISTINCT start_nd from edges"):
            g.add_vertex(str(row[0]))
            vertices[str(row[0])] = 0
            #print str(row[0])

        for row in db.execute("SELECT DISTINCT end_nd from edges"):
            g.add_vertex(str(row[0]))
            vertices[str(row[0])] = 0

        #k = vertices.keys()
        #k.sort()
        #print k, len(k)
        
        print "load edges into memory"
        for start_nd, end_nd in db.execute("SELECT start_nd, end_nd from edges"):
            g.add_edge(start_nd, end_nd, Link())
            g.add_edge(end_nd, start_nd, Link())
            #print start_nd, end_nd
            
        db.conn.commit()
        
        t1 = time.time()
        print "populating graph took: %f"%(t1-t0)
        t0 = t1
        
        print len(vertices)
        iteration = 1
        c = db.cursor()
        while True:
            #c.execute("SELECT id from nodes where id not in (SELECT node_id from graph_nodes) LIMIT 1")
            try:
                vertex, dummy = vertices.popitem()
                #print vertex
            except:
                break
            spt = g.shortest_path_tree(vertex, None, State(1,0))
            print spt.size
            for v in spt.vertices:
                lat, lon = c.execute("SELECT lat, lon from nodes where id=?", (v.label, )).next()
                c.execute("INSERT into graph_nodes VALUES (?, ?, ?)", (iteration, v.label, "POINT(%f %f)" % (lon, lat)))
                for e in v.outgoing: # this gives a wierd maze graph, should do for all edges outside loop.
                    lat1, lon1 = c.execute("SELECT lat, lon from nodes where id=?", (e.from_v.label, )).next()
                    lat2, lon2 = c.execute("SELECT lat, lon from nodes where id=?", (e.to_v.label, )).next()
                    c.execute("INSERT into graph_edges VALUES (?, ?, ?)", 
                        (iteration, e.from_v.label + '->' + e.to_v.label, "LINESTRING(%f %f, %f %f)" % (lon1, lat1, lon2, lat2)))
                #print v.label
                vertices.pop(v.label, None)
                g.remove_vertex(v.label, True, True)
                #print v.label
            spt.destroy()
            
            t1 = time.time()
            print "pass %s took: %f nvertices %d"%(iteration, t1-t0, len(vertices))
            t0 = t1
            iteration += 1
        c.close()
        
        db.conn.commit()
        g.destroy()
        # audit
        for gnum, count in db.execute("SELECT graph_num, count(*) FROM graph_nodes GROUP BY graph_num"):
            print "FOUND: %s=%s" % (gnum, count)
예제 #6
0
 def test_hello_world(self):
     g = Graph()
     
     g.add_vertex( "Seattle" )
     g.add_vertex( "Portland" )
     
     g.add_edge( "Seattle", "Portland", Street("I-5 south", 5000) )
     g.add_edge( "Portland", "Seattle", Street("I-5 north", 5500) )
     
     spt = g.shortest_path_tree( "Seattle", "Portland", State(g.numagencies,0), WalkOptions() )
     
     assert spt.get_vertex("Seattle").outgoing[0].payload.name == "I-5 south"
     
     g.add_vertex( "Portland-busstop" )
     g.add_vertex( "Seattle-busstop" )
     
     g.add_edge( "Seattle", "Seattle-busstop", Link() )
     g.add_edge( "Seattle-busstop", "Seattle", Link() )
     g.add_edge( "Portland", "Portland-busstop", Link() )
     g.add_edge( "Portland-busstop", "Portland", Link() )
     
     spt = g.shortest_path_tree( "Seattle", "Seattle-busstop", State(g.numagencies,0), WalkOptions() )
     assert spt.get_vertex("Seattle-busstop").incoming[0].payload.__class__ == Link
     spt.destroy()
     
     spt = g.shortest_path_tree( "Seattle-busstop", "Portland", State(g.numagencies,0), WalkOptions() )
     assert spt.get_vertex("Portland").incoming[0].payload.__class__ == Street
     spt.destroy()
     
     sc = ServiceCalendar()
     sc.add_period( 0, 86400, ["WKDY","SAT"] )
     tz = Timezone()
     tz.add_period( TimezonePeriod( 0, 86400, 0 ) )
     
     g.add_vertex( "Portland-busstop-onbus" )
     g.add_vertex( "Seattle-busstop-onbus" )
     
     tb = TripBoard("WKDY", sc, tz, 0)
     tb.add_boarding( "A", 10, 0 )
     tb.add_boarding( "B", 15, 0 )
     tb.add_boarding( "C", 400, 0 )
     
     cr = Crossing()
     
     al = TripAlight("WKDY", sc, tz, 0)
     al.add_alighting( "A", 10+20, 0 )
     al.add_alighting( "B", 15+20, 0 )
     al.add_alighting( "C", 400+20, 0 )
     
     g.add_edge( "Seattle-busstop", "Seattle-busstop-onbus", tb )
     g.add_edge( "Seattle-busstop-onbus", "Portland-busstop-onbus", cr )
     g.add_edge( "Portland-busstop-onbus", "Portland-busstop", al )
     
     spt = g.shortest_path_tree( "Seattle", "Portland", State(g.numagencies,0), WalkOptions() )
     
     assert spt.get_vertex( "Portland" ).incoming[0].from_v.incoming[0].from_v.incoming[0].from_v.incoming[0].from_v.incoming[0].from_v.label == "Seattle"
     
     spt = g.shortest_path_tree( "Seattle", "Portland", State(g.numagencies,0), WalkOptions() )
     vertices, edges = spt.path( "Portland" )
     
     assert [v.label for v in vertices] == ['Seattle', 'Seattle-busstop', "Seattle-busstop-onbus", "Portland-busstop-onbus", 'Portland-busstop', 'Portland']
     assert [e.payload.__class__ for e in edges] == [Link, TripBoard, Crossing, TripAlight, Link]
     
     spt.destroy()
     g.destroy()
예제 #7
0
    def test_hello_world(self):
        g = Graph()

        g.add_vertex("Seattle")
        g.add_vertex("Portland")

        g.add_edge("Seattle", "Portland", Street("I-5 south", 5000))
        g.add_edge("Portland", "Seattle", Street("I-5 north", 5500))

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())

        assert spt.get_vertex(
            "Seattle").outgoing[0].payload.name == "I-5 south"

        g.add_vertex("Portland-busstop")
        g.add_vertex("Seattle-busstop")

        g.add_edge("Seattle", "Seattle-busstop", Link())
        g.add_edge("Seattle-busstop", "Seattle", Link())
        g.add_edge("Portland", "Portland-busstop", Link())
        g.add_edge("Portland-busstop", "Portland", Link())

        spt = g.shortest_path_tree("Seattle", "Seattle-busstop",
                                   State(g.numagencies, 0), WalkOptions())
        assert spt.get_vertex(
            "Seattle-busstop").incoming[0].payload.__class__ == Link
        spt.destroy()

        spt = g.shortest_path_tree("Seattle-busstop", "Portland",
                                   State(g.numagencies, 0), WalkOptions())
        assert spt.get_vertex(
            "Portland").incoming[0].payload.__class__ == Street
        spt.destroy()

        sc = ServiceCalendar()
        sc.add_period(0, 86400, ["WKDY", "SAT"])
        tz = Timezone()
        tz.add_period(TimezonePeriod(0, 86400, 0))

        g.add_vertex("Portland-busstop-onbus")
        g.add_vertex("Seattle-busstop-onbus")

        tb = TripBoard("WKDY", sc, tz, 0)
        tb.add_boarding("A", 10, 0)
        tb.add_boarding("B", 15, 0)
        tb.add_boarding("C", 400, 0)

        cr = Crossing()

        al = TripAlight("WKDY", sc, tz, 0)
        al.add_alighting("A", 10 + 20, 0)
        al.add_alighting("B", 15 + 20, 0)
        al.add_alighting("C", 400 + 20, 0)

        g.add_edge("Seattle-busstop", "Seattle-busstop-onbus", tb)
        g.add_edge("Seattle-busstop-onbus", "Portland-busstop-onbus", cr)
        g.add_edge("Portland-busstop-onbus", "Portland-busstop", al)

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())

        assert spt.get_vertex("Portland").incoming[0].from_v.incoming[
            0].from_v.incoming[0].from_v.incoming[0].from_v.incoming[
                0].from_v.label == "Seattle"

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())
        vertices, edges = spt.path("Portland")

        assert [v.label for v in vertices] == [
            'Seattle', 'Seattle-busstop', "Seattle-busstop-onbus",
            "Portland-busstop-onbus", 'Portland-busstop', 'Portland'
        ]
        assert [e.payload.__class__ for e in edges
                ] == [Link, TripBoard, Crossing, TripAlight, Link]

        spt.destroy()
        g.destroy()
예제 #8
0
class TestGraphSerialize(unittest.TestCase):
    def setUp(self):
        self.out1 = tempfile()
        self.out2 = tempfile()
        self.g1 = Graph()
        self.g2 = Graph()

    def tearDown(self):
        self.g1.destroy()
        self.g2.destroy()

        try: os.unlink(self.out1.name)
        except: pass
        try: os.unlink(self.out2.name)
        except: pass

    def inout(self):
        lgs.gSerialize(self.g1.soul, self.out1.name, self.out2.name)
        lgs.gDeserialize(self.g2.soul, self.out1.name, self.out2.name)
    

    def test_errors(self):
       self.assertRaises(IOError, self.g1.serialize, "/dev/asdfafsdaf")
       self.assertRaises(IOError, self.g1.serialize, "/dev/asdfafsdaf", True)
       self.g1.add_vertices(('a','b'))
       self.g1.add_edge('a','b', NoOpPyPayload(5))
       self.assertRaises(Exception, self.g1.serialize, self.out1.name)
       self.g2.add_vertices(('a','b'))
       self.g2.add_edge('a','b', Link())
       self.g2.serialize("cows")
       # bad sig
       with open("cows.gbin", 'r+b') as foo:
           foo.seek(1)
           foo.write("afdasdf")
       self.assertRaises(IOError, self.g1.deserialize, "cows")
       # bad file
       self.assertRaises(IOError, self.g1.deserialize, self.out2.name)

    def test_vertices(self):
        nv = 10
        for i in xrange(10):
            self.g1.add_vertex(str(i))
        self.inout()
        self.assertEquals(self.g2.size, nv);

    def test_link_edge(self):
        self.g1.add_vertex("A")
        self.g1.add_vertex("B")
        self.g1.add_edge("A","B", Link())
        self.inout()
        self.assertEquals(self.g2.size, 2)
        print self.g2.vertices
        self.assertTrue(self.g2.get_vertex("A") != None)
        self.assertEquals(len(self.g2.get_vertex("A").outgoing), 1)
        self.assertEquals(len(self.g2.get_vertex("B").outgoing), 0)
        self.assertEquals(len(self.g2.get_vertex("B").incoming), 1)
        self.assertEquals(self.g2.get_vertex("A").outgoing[0].payload.__class__, Link)

    def test_street_edge(self):
        self.g1.add_vertex("A")
        self.g1.add_vertex("B")
        s1_args = ("s1", 2, 2, 2, False)
        s2_args = ("s2", 3, 3, 3, True)
        self.g1.add_edge("A","B", Street(*s1_args))
        self.assertEquals(self.g1.get_vertex("A").outgoing[0].payload.reverse_of_source, False)
        
        self.g1.add_edge("B","A", Street(*s2_args))
        self.inout()
        self.assertEquals(self.g2.size, 2)
        print self.g2.vertices
        self.assertTrue(self.g2.get_vertex("A") != None)
        self.assertEquals(len(self.g2.get_vertex("A").outgoing), 1)
        self.assertEquals(len(self.g2.get_vertex("B").outgoing), 1)
        self.assertEquals(len(self.g2.get_vertex("B").incoming), 1)
        self.assertEquals(self.g2.get_vertex("A").outgoing[0].payload.__class__, Street)
        s1 = self.g2.get_vertex("A").outgoing[0].payload
        s2 = self.g2.get_vertex("B").outgoing[0].payload
        self.assertEquals(s1.name, "s1")
        for e,args in ((s1, s1_args), (s2, s2_args)):
            for f,v in zip(("name","length","rise","fall","reverse_of_source"),args):
                print f,v, getattr(e,f)
                self.assertEquals(getattr(e,f), v)