source: subversion/applications/rendering/parking/osray/osray_geom.py @ 22353

Last change on this file since 22353 was 22340, checked in by kdrangmeister, 9 years ago

random tree height

  • Property svn:mime-type set to text/plain
File size: 4.9 KB
Line 
1# -*- coding: utf-8 -*-
2
3#import string, cgi, time
4#from os import curdir, sep
5#from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
6#import urlparse
7#import osray
8import math
9import random
10import re
11
12def avg(a,b): return (a+b)/2.0
13def swap(tuple): return (tuple[1],tuple[0])
14
15#
16# parsing postgis strings
17#
18
19def WKT_to_point(pointstring):
20    pointstring = pointstring[6:] # cut off the "POINT("
21    pointstring = pointstring[:-1] # cut off the ")"
22    latlon = pointstring.split(' ')
23    return (latlon[0],latlon[1])
24
25#
26# string stuff to handle bbox strings
27#
28def bbox_string(xmin,ymin,xmax,ymax):
29    return str(xmin)+" "+str(ymin)+","+str(xmax)+" "+str(ymax)
30
31def bbox_format_3_to_1_comma(bbox):
32    coords = bbox.split(',')
33    if len(coords)!=4:
34        raise 'wrong bbox format: '+bbox
35    return coords[0]+" "+coords[1]+","+coords[2]+" "+coords[3]
36   
37def coords_from_bbox(bbox):
38    bbox = bbox.replace(' ',',')
39    coordslist = map(lambda coord: float(coord), old_bbox.split(','))
40    return tuple(coordslist)
41
42def scale_coords(xmin,ymin,xmax,ymax,scale):
43    xmid = avg(xmin,xmax)
44    ymid = avg(ymin,ymax)
45    xradius = xmid-xmin
46    yradius = ymid-ymin
47    xradius *= scale
48    yradius *= scale
49    xmin = xmid-xradius
50    xmax = xmid+xradius
51    ymin = ymid-yradius
52    ymax = ymid+yradius
53    return (xmin,ymin,xmax,ymax)
54
55def scale_bbox(bbox,scale):
56    xmin,ymin,xmax,ymax = coords_from_bbox(bbox)
57    xmin,ymin,xmax,ymax = scale_coords(xmin,ymin,xmax,ymax,scale)
58    return bbox_string(xmin,ymin,xmax,ymax)
59
60#
61# mathematical calculations
62#
63def num2deg(xtile, ytile, zoom):
64    n = 2.0 ** zoom
65    lon_deg = xtile / n * 360.0 - 180.0
66    lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
67    lat_deg = math.degrees(lat_rad)
68    return(lat_deg, lon_deg)
69
70def num2bbox(xtile, ytile, zoom):
71    bottomleft = swap(num2deg(xtile,ytile+1,zoom))
72    topright = swap(num2deg(xtile+1,ytile,zoom))
73    bbox = str(bottomleft)[1:-1].replace(',',' ')+","+str(topright)[1:-1].replace(',',' ')
74    return bbox
75
76def shift_by_meters(lat, lon, brng, d):
77    R = 6371000.0 # earth's radius in m
78    lat=math.radians(lat)
79    lon=math.radians(lon)
80    lat2 = math.asin( math.sin(lat)*math.cos(d/R) +
81                      math.cos(lat)*math.sin(d/R)*math.cos(brng))
82    lon2 = lon + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat),
83                             math.cos(d/R)-math.sin(lat)*math.sin(lat2))
84    lat2=math.degrees(lat2)
85    lon2=math.degrees(lon2)
86    return [lat2,lon2]
87
88def calc_bearing(x1,y1,x2,y2,side):
89    Q = complex(x1,y1)
90    R = complex(x2,y2)
91    v = R-Q
92    if side=='left':
93        v=v*complex(0,1);
94    elif side=='right':
95        v=v*complex(0,-1);
96    else:
97        raise TypeError('side must be left or right')
98    #v=v*(1/abs(v)) # normalize
99    angl = angle(v) # angle (radians) (0°=right, and counterclockwise)
100    bearing = math.pi/2.0-angl # (0°=up, and clockwise)
101    return bearing
102
103def random_range_seed(a,b,s):
104    random.seed(s)
105    return random.uniform(a,b)
106
107#
108# parsing OSM strings
109#
110def parse_length_in_meters(length,default):
111    units={
112           'm':1.0, 'metres':1.0, 'meters':1.0, 'metre':1.0, 'meter':1.0,
113           'km':1000.0,
114           'ft':0.3,
115           'yd':1.1,
116           }
117    parsed = re.split('(\d*[,.]?\d+)',length)
118    if len(parsed)!=3:
119        print "### unparsable length '{0}', parsed: {1}".format(length,parsed)
120        return default
121    prefix = parsed[0].strip()
122    if prefix!='':
123        print "### unknown prefix '{0}' in length '{1}'".format(prefix,length)
124        return default
125    unit = parsed[2].strip().lower()
126    if unit=='': unit='m' # defaults to m
127    factor = units.get(unit,0.0)
128    if factor==0.0:
129        print "### unknown unit '{0}' in length '{1}'".format(unit,length)
130    meter = float(parsed[1])*factor
131    return meter
132
133def parse_int_safely(intstring,default):
134    """ parses a intstring and returns an int - any error: returns default """
135    if intstring==None:
136        return default
137    try:
138        layer = int(intstring)
139    except ValueError:
140        print "### unknown integer '{0}'".format(intstring)
141        layer = default
142    return layer
143
144def parse_float_safely(floatstring,default):
145    if floatstring==None:
146        return default
147    try:
148        f = float(floatstring)
149    except ValueError:
150        print "### unknown float '{0}'".format(floatstring)
151        f = default
152    return f
153
154def parse_yes_no_safely(boolstring,default):
155    if boolstring == None:
156        return default
157    boolstring = boolstring.upper()
158    if boolstring == 'YES':
159        return True
160    if boolstring == 'NO':
161        return False
162    if boolstring == '1':
163        return True
164    if boolstring == '0':
165        return False
166    if boolstring == 'TRUE':
167        return True
168    if boolstring == 'FALSE':
169        return False
170    return default
Note: See TracBrowser for help on using the repository browser.