source: subversion/applications/rendering/parking/parkingicons/osm_parking_icons_trunk.py @ 28519

Last change on this file since 28519 was 26130, checked in by kdrangmeister, 8 years ago

renamed files to conform to python conventions

File size: 5.9 KB
Line 
1# -*- coding: utf-8 -*-
2# by kay - basic functions
3
4### config for Toolserver
5DSN = 'dbname=gis'
6openlayertextfilename = '/home/kayd/shellscripts/parkingicons.txt'
7
8import sys
9import psycopg2
10import csv
11from numpy import *
12
13def shift_by_meters(lat, lon, brng, d):
14    R = 6371000.0 # earth's radius in m
15    lat=math.radians(lat)
16    lon=math.radians(lon)
17    lat2 = math.asin( math.sin(lat)*math.cos(d/R) + 
18                      math.cos(lat)*math.sin(d/R)*math.cos(brng))
19    lon2 = lon + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat), 
20                             math.cos(d/R)-math.sin(lat)*math.sin(lat2))
21    lat2=math.degrees(lat2)
22    lon2=math.degrees(lon2)
23    return [lat2,lon2]
24
25def calc_bearing(x1,y1,x2,y2,side):
26    Q = complex(x1,y1)
27    R = complex(x2,y2)
28    v = R-Q
29    if side=='left':
30        v=v*complex(0,1);
31    elif side=='right':
32        v=v*complex(0,-1);
33    else:
34        raise TypeError('side must be left or right')
35    #v=v*(1/abs(v)) # normalize
36    angl = angle(v) # angle (radians) (0°=right, and counterclockwise)
37    bearing = math.pi/2.0-angl # (0°=up, and clockwise)
38    return bearing
39
40def unboth(list,sideindex):
41    """ Replace in a list all rows with 'both' with two rows with 'left' and 'right'
42    """
43    list_both = list[:]
44    list_both.reverse()
45    list = []
46    while len(list_both)>0:
47        row = list_both.pop()
48        #print "row=", row
49        side = row[sideindex]
50        if side=='both':
51            row_l = row[:]
52            row_l[sideindex] = 'left'
53            #print 'bothl:', row_l
54            list += [row_l]
55            row_r = row[:]
56            row_r[sideindex] = 'right'
57            #print 'bothr:', row_r
58            list += [row_r]
59        else:
60            #print side, ":", row
61            list += [row]
62    return list
63
64if len(sys.argv) == 3:
65    DSN = sys.argv[1]
66    openlayertextfilename = sys.argv[2]
67else:
68    print "usage: osm-parking-icons.py 'dbname=osm_mapnik host=sql-mapnik' '/home/kayd/parkingicons/parkingicons.txt'"
69    exit(0);
70
71print "Opening connection using dns:", DSN
72conn = psycopg2.connect(DSN)
73print "Encoding for this connection is", conn.encoding
74curs = conn.cursor()
75
76openlayertextfile = csv.writer(open(openlayertextfilename, 'w'), delimiter='\t',quotechar='"', quoting=csv.QUOTE_MINIMAL)
77openlayertextfile.writerow(['lat','lon','title','description','icon','iconSize','iconOffset'])
78
79latlon= "ST_Y(ST_Transform(ST_line_interpolate_point(way,0.5),4326)),ST_X(ST_Transform(ST_line_interpolate_point(way,0.5),4326))"
80coords= "ST_Y(ST_line_interpolate_point(way,0.5)) as py,ST_X(ST_line_interpolate_point(way,0.5)) as px,ST_Y(ST_line_interpolate_point(way,0.49)) as qy,ST_X(ST_line_interpolate_point(way,0.49)) as qx,ST_Y(ST_line_interpolate_point(way,0.51)) as ry,ST_X(ST_line_interpolate_point(way,0.51)) as rx"
81FW = "FROM planet_osm_line WHERE"
82
83### display disc - maxstay
84
85pc_disc_maxstay = []
86for side in ['left','right','both']:
87    curs.execute("SELECT osm_id,"+latlon+",\"parking:condition:"+side+":maxstay\","+coords+",'"+side+"' "+FW+" \"parking:condition:"+side+":maxstay\" is not NULL and \"parking:condition:"+side+"\"='disc'")
88    pc_disc_maxstay += curs.fetchall()
89
90for pc_dm in pc_disc_maxstay:
91    bearing = calc_bearing(pc_dm[7],pc_dm[6], pc_dm[9],pc_dm[8], pc_dm[10]) 
92    openlayertextfile.writerow(shift_by_meters(pc_dm[1],pc_dm[2],bearing,4.0)+['Disc parking','Maximum parking time:<br>'+pc_dm[3],'parkingicons/pi-disc.png','16,16','-8,-8'])
93
94### display vehicles
95
96pc_vehicles = []
97for side in ['left','right','both']:
98    curs.execute("SELECT osm_id,"+latlon+",\"parking:condition:"+side+":vehicles\","+coords+",'"+side+"' "+FW+" \"parking:condition:"+side+":vehicles\" is not NULL")
99    pc_vehicles += curs.fetchall()
100
101vehicle_icons = {"car":"parkingicons/pi-car.png" , "bus":"parkingicons/pi-bus.png" , "motorcycle":"parkingicons/pi-motorcycle.png"}
102
103for pc_v in pc_vehicles:
104    vehicle_icon = vehicle_icons.get(pc_v[3],"parkingicons/pi-unkn.png");
105    """
106    P = complex(pc_v[5],pc_v[4])
107    Q = complex(pc_v[7],pc_v[6])
108    R = complex(pc_v[9],pc_v[8])
109    v = R-Q
110    side = pc_v[10]
111    if side=='left':
112        v=v*complex(0,1);
113    elif side=='right':
114        v=v*complex(0,-1);
115    v=v*(1/abs(v)) # normalize
116    angl = angle(v) # angle (radians) (0°=right, and counterclockwise)
117    bearing = math.pi/2.0-angl # (0°=up, and clockwise)
118    #print P,v,angle(v,deg=True),side
119    """
120    bearing = calc_bearing(pc_v[7],pc_v[6], pc_v[9],pc_v[8], pc_v[10])   
121    #openlayertextfile.writerow([pc_v[1],pc_v[2],'Parking only for','Vehicle: '+pc_v[3]+'<br>'+str(math.degrees(angl))+'° '+side,vehicle_icon,'16,16','-8,-8'])
122    openlayertextfile.writerow(shift_by_meters(pc_v[1],pc_v[2],bearing,4.0)+['Parking only for','Vehicle: '+pc_v[3],vehicle_icon,'16,16','-8,-8'])
123
124conn.rollback()
125
126sys.exit(0)
127
128"""
129SELECT
130   ST_Y(way) AS lat_wgs84,
131   ST_X(way) AS lon_wgs84,
132   ST_X(transform(way, 31466)) AS KOORD_X,
133   ST_Y(transform(way, 31466)) AS KOORD_Y,
134   skeys(tags) AS key,
135   svals(tags) AS value,
136   osm_id
137FROM
138   dortmund_point
139WHERE
140   exist(tags, 'amenity')
141LIMIT 10;
142
143SELECT
144   ST_Y(way) AS lat_wgs84,
145   ST_X(way) AS lon_wgs84,
146   ST_X(transform(way, 31466)) AS KOORD_X,
147   ST_Y(transform(way, 31466)) AS KOORD_Y,
148   key,
149   value,
150   tags->'name' as name,
151   osm_id
152FROM
153   (SELECT
154       osm_id, way, tags,
155       (each(tags)).key,
156       (each(tags)).value
157    FROM
158       dortmund_point
159    WHERE
160       exist(tags, 'amenity') /*AND exist(tags, 'name')*/
161   ) AS sq
162WHERE
163   key = 'amenity'
164LIMIT 10;
165
166SELECT
167   ST_Y(way) AS lat_wgs84,
168   ST_X(way) AS lon_wgs84,
169   ST_X(transform(way, 31466)) AS KOORD_X,
170   ST_Y(transform(way, 31466)) AS KOORD_Y,
171   key,
172   value,
173   tags->'name' as name,
174   tags->'sport' as sport,
175   osm_id
176FROM
177   (SELECT
178       osm_id, tags,
179       ST_Centroid(way) as way,
180       (each(tags)).key,
181       (each(tags)).value
182    FROM
183       dortmund_polygon
184    WHERE
185       tags->'natural' = 'water'
186       AND
187       tags->'sport' = 'swimming'
188   ) AS sq
189WHERE
190   key = 'natural'
191LIMIT 10;
192"""
Note: See TracBrowser for help on using the repository browser.