-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch.py
53 lines (42 loc) · 1.78 KB
/
fetch.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
import psycopg2
if __name__ == '__main__':
road_sections = {}
fileout = open("sections.txt", 'w')
print "Connecting to database ......"
conn_to = psycopg2.connect(host='osm-workspace-2.cfmyklmn07yu.us-west-2.rds.amazonaws.com', port='5432', database='osm', user='ds', password='ds2015')
if conn_to:
print "Connected."
cursor = conn_to.cursor()
sql = "select road_name, direction, from_postmile, to_postmile from ss_arterial_pattern"
cursor.execute(sql)
results = cursor.fetchall()
for road_name, direction, from_postmile, to_postmile in results:
if road_name not in road_sections:
road_sections[road_name] = {}
if direction not in road_sections[road_name]:
road_sections[road_name][direction] = []
if (from_postmile, to_postmile) not in road_sections[road_name][direction]:
road_sections[road_name][direction].append((from_postmile, to_postmile))
roads = []
keys = road_sections.keys()
keys.sort(key=lambda x:x)
for road in keys:
p = str(road)+" => array("
if road not in roads:
roads.append(road)
s = "road_name['"+road+"'] = [] \n"
fileout.write(s)
for direction in road_sections[road]:
s = "road_name['"+road+"']['" + str(direction)+"']=["
road_sections[road][direction].sort()
for (from_pm, to_pm) in road_sections[road][direction]:
s += str(from_pm)+','
if s[-1] == ',':
s = s[:-1] + '];'
s += '\n'
fileout.write(s)
p += str(direction)+','
p = p[:-1]+'),'
print p
print roads
fileout.close()