-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdb_add_wkt.py
executable file
·83 lines (69 loc) · 3.03 KB
/
gdb_add_wkt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python2.6
from graphserver.ext.gtfs.gtfsdb import GTFSDatabase
from graphserver.ext.osm.osmdb import OSMDB
from graphserver.graphdb import GraphDatabase
from graphserver.core import Link
import sys
from optparse import OptionParser
def main():
usage = """usage: python wktize_gdb.py <graphdb_filename> <osmdb_filename> <gtfsdb_filename>"""
parser = OptionParser(usage=usage)
(options, args) = parser.parse_args()
if len(args) != 3:
parser.print_help()
exit(-1)
graphdb_filename = args[0]
osmdb_filename = args[1]
gtfsdb_filename = args[2]
gtfsdb = GTFSDatabase( gtfsdb_filename )
osmdb = OSMDB( osmdb_filename )
gdb = GraphDatabase( graphdb_filename )
def vertex_interesting(vlabel) :
return vlabel[0:4] == 'sta-' or vlabel[0:4] == 'osm-'
def vertex_lookup(vlabel) :
if vlabel[0:4] == 'sta-' :
id, name, lat, lon = gtfsdb.stop(vlabel[4:])
vclass = 'GTFS Stop'
elif vlabel[0:4] == 'osm-' :
id, tags, lat, lon, endnode_refs = osmdb.node(vlabel[4:])
vclass = 'OSM Node'
else :
lat = None
lon = None
vclass = None
return vclass, lat, lon
c = gdb.get_cursor()
c.execute( "CREATE TABLE IF NOT EXISTS geom_vertices (label TEXT UNIQUE ON CONFLICT IGNORE, class TEXT, WKT_GEOMETRY TEXT)" )
c.execute( "CREATE TABLE IF NOT EXISTS geom_edges (class TEXT, WKT_GEOMETRY TEXT)" )
gdb.commit()
num_vertices = gdb.num_vertices()
curr_vertex = 0
for vlabel in gdb.all_vertex_labels() :
curr_vertex += 1
if curr_vertex % 1000 == 0 :
sys.stdout.write( '\rVertex %i/%i' % (curr_vertex, num_vertices) )
sys.stdout.flush()
if not vertex_interesting(vlabel) : continue
vclass, lat, lon = vertex_lookup(vlabel)
c.execute("INSERT INTO geom_vertices VALUES (?, ?, ?)", (vlabel, vclass, "POINT(%s %s)" % (lon, lat)))
gdb.commit()
print ' '
num_edges = gdb.num_edges()
curr_edge = 0
edges = gdb.execute( "SELECT vertex1, vertex2, edgetype, edgestate FROM edges" )
for vertex1, vertex2, edgetype, edgestate in edges :
curr_edge += 1
if curr_edge % 1000 == 0 :
sys.stdout.write( '\rEdge %i/%i' % (curr_edge, num_edges) )
sys.stdout.flush()
if not (vertex_interesting(vertex1) and vertex_interesting(vertex2)) : continue
vclass1, lat1, lon1 = vertex_lookup(vertex1)
vclass2, lat2, lon2 = vertex_lookup(vertex2)
c.execute("INSERT INTO geom_edges VALUES (?, ?)", (edgetype, "LINESTRING(%s %s, %s %s)" % (lon1, lat1, lon2, lat2)))
gdb.commit()
print '\nIndexing...'
gdb.execute( "CREATE INDEX IF NOT EXISTS geom_vertices_label ON geom_vertices (label)" )
gdb.execute( "CREATE INDEX IF NOT EXISTS geom_vertices_class ON geom_vertices (class)" )
gdb.execute( "CREATE INDEX IF NOT EXISTS geom_edges_class ON geom_edges (class)" )
if __name__=='__main__':
main()