forked from arenevier/rivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriver_output.py
136 lines (115 loc) · 6.5 KB
/
river_output.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# Copyright (C) 2010 Arnaud Renevier <[email protected]>
import os, sys, shutil, psycopg2, glob
from mako.template import Template
def createRiver(cursor, index, osm_id, name, sandre, parent=None):
print (("computing river %s - osm_id %s") % (name,osm_id))
river = River(osm_id, name, sandre=sandre, parent=parent)
index[osm_id] = river
sql = "SELECT r.osm_id, r.name, r.sandre FROM relations INNER JOIN tributaries ON relations.osm_id = tributaries.main_id INNER JOIN relations r ON tributaries.tributary_id = r.osm_id WHERE relations.osm_id = %s ORDER BY tributaries.id" % osm_id;
cursor.execute(sql, (osm_id,))
for (ch_osm_id, ch_name, ch_sandre) in cursor.fetchall():
if index.has_key(ch_osm_id):
sys.stderr.write("trying to insert an already present river: #%s\n" % (ch_osm_id))
continue
tributary = createRiver(cursor, index, ch_osm_id, ch_name, ch_sandre, river)
river.childs.append(tributary)
#sql = "WITH RECURSIVE t(geom) AS(SELECT (ST_Dump(geom)).geom AS geom FROM relations WHERE osm_id = %s UNION ALL SELECT ST_Union(f.geom, t.geom) FROM (SELECT (st_dump(geom)).geom AS geom FROM relations WHERE osm_id = %s) AS f, t WHERE ST_StartPoint(f.geom) = ST_EndPoint(t.geom)) SELECT max(ST_Length(ST_LineMerge(geom), TRUE)) FROM t" % (osm_id, osm_id);
sql = "SELECT (sum(ST_Length_Spheroid(geom,'SPHEROID[\"WGS 84\",6378137,298.257223563]'))/1000) FROM relations WHERE osm_id = %s" % osm_id;
cursor.execute(sql, (osm_id,osm_id))
river.length = float(cursor.fetchone()[0] or 0)
sql = "SELECT r1.osm_id, r1.name FROM relations r1 INNER JOIN relations r2 ON (ST_Intersects(r1.geom, r2.geom) AND r1.t = 'boundary') INNER JOIN (SELECT ST_DumpPoints(geom) AS geom FROM relations WHERE osm_id = %s) r3 ON ST_Intersects((r3.geom).geom, ST_MakePolygon(r1.geom)) WHERE r2.osm_id = %s ORDER BY (r3.geom).path" % (osm_id, osm_id)
cursor.execute(sql, (osm_id,))
seens = {}
for (ci_osm_id, ci_name) in cursor.fetchall():
if seens.has_key(ci_osm_id):
continue
river.cities.append((ci_osm_id, unicode(ci_name, 'utf-8')))
seens[ci_osm_id] = True;
sql = "SELECT w.osm_id, w.name FROM waysinrel INNER JOIN ways ON waysinrel.way_id = ways.osm_id INNER JOIN ways w ON ST_LineCrossingDirection(ways.geom, w.geom) != 0 WHERE w.t = 'bridge' AND waysinrel.rel_id = %s ORDER BY waysinrel.id, ST_Distance(w.geom, ST_StartPoint(ways.geom))" % osm_id
cursor.execute(sql, (osm_id,))
for (br_osm_id, br_name) in cursor.fetchall():
river.bridges.append((br_osm_id, unicode(br_name, 'utf-8')))
sql = "SELECT osm_id, name, lock_name, lock_ref, lock_height, cemt, motorboat FROM waysinrel INNER JOIN ways ON waysinrel.way_id = ways.osm_id WHERE t = 'lock' AND waysinrel.rel_id = %s ORDER BY waysinrel.id, ST_Distance(geom, ST_StartPoint(ways.geom));" % osm_id
cursor.execute(sql, (osm_id,))
for (lo_osm_id, lo_name, lo_lock_name, lo_lock_ref, lo_lock_height, lo_cemt, lo_motorboat) in cursor.fetchall():
#river.locks.append((lo_osm_id, unicode(lo_name, 'utf-8')))
if lo_lock_name != None:
lo_lock_name = unicode(lo_lock_name,'utf-8')
if river.locks != None:
river.locks.append(Lock(lo_osm_id, unicode(lo_name, 'utf-8'), lo_lock_name , lo_lock_ref, lo_lock_height, lo_cemt, lo_motorboat))
sql = "SELECT osm_id, name FROM waysinrel INNER JOIN ways ON waysinrel.way_id = ways.osm_id WHERE t = 'tunnel' AND waysinrel.rel_id = %s ORDER BY waysinrel.id, ST_Distance(geom, ST_StartPoint(ways.geom));" % osm_id
cursor.execute(sql, (osm_id,))
for (lo_osm_id, lo_name) in cursor.fetchall():
river.tunnels.append((lo_osm_id, unicode(lo_name, 'utf-8')))
return river
class Lock(object):
def __init__(self,osm_id,name, lock_name=None, lock_ref=None, lock_height=None, cemt=None, motorboat=None):
self.osm_id = osm_id
self.name = name
self.lock_name = lock_name
self.lock_ref = lock_ref
self.lock_height = lock_height
self.cemt = cemt
self.motorboat = motorboat
class River(object):
def __init__(self, osm_id, name, sandre = None, childs=None, cities=None, bridges=None, locks=None, tunnels=None, length=0, parent=None):
self.osm_id = osm_id
self.name = unicode(name, 'utf-8')
self.length = length
if sandre != None:
self.sandre = unicode(sandre, 'utf-8')
else:
self.sandre = ""
self.parent = parent
if childs:
self.childs = childs
else:
self.childs = []
if cities:
self.cities = cities
else:
self.cities = []
if bridges:
self.bridges = bridges
else:
self.bridges = []
if locks:
self.locks = locks
else:
self.locks = []
if tunnels:
self.tunnels = tunnels
else:
self.tunnels = []
def outputriver(river):
template = Template(filename='templates/river.html', input_encoding="utf-8", output_encoding="utf-8", strict_undefined=True)
with open("htmloutput/%d.html" % (river.osm_id), "w") as fd:
print "output for river #%d - name %s - sandre %s" % (river.osm_id, river.name, river.sandre)
fd.write(template.render(river=river))
for tributary in river.childs:
outputriver(tributary)
if __name__ == '__main__':
indextemplate = Template(filename='templates/index.html', input_encoding="utf-8", output_encoding="utf-8", strict_undefined=True)
conn = psycopg2.connect("dbname='osm' user='osm' host='localhost' password='osm'")
cursor = conn.cursor()
cursor.execute("SELECT osm_id, name, sandre FROM roots ORDER BY name")
index = {}
roots = []
for (osm_id, name, sandre) in cursor.fetchall():
if index.has_key(osm_id):
sys.stderr.write("trying to insert an already present river: #%s\n" % (osm_id))
continue
river = createRiver(cursor, index, osm_id, name, sandre)
roots.append(river)
roots.sort(key=lambda r: r.length, reverse=True)
shutil.rmtree("htmloutput", True);
os.mkdir("htmloutput")
for fname in (glob.glob("templates/*.js") + glob.glob("templates/*.png")):
shutil.copy(fname, "htmloutput")
with open("htmloutput/index.html", "w") as fd:
print "generating main index"
fd.write(indextemplate.render(roots=roots))
for river in roots:
outputriver(river)
conn.close()