source: subversion/applications/rendering/parking/osray/osray_server.py @ 22076

Last change on this file since 22076 was 22076, checked in by kdrangmeister, 10 years ago

osray: more highway types

  • Property svn:mime-type set to text/plain
File size: 5.3 KB
Line 
1# -*- coding: utf-8 -*-
2
3import string, cgi, time
4from os import curdir, sep
5from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
6import urlparse
7import osray
8import math
9
10options = {'height': 100, 'dsn': 'dbname=gis', 'width': 100, 'prefix': 'planet_osm', 'quick': False, 'hq': False}
11options['bbox']='9.94861 49.79293,9.96912 49.80629' # Europastern
12options['bbox']='9.92498 49.78816,9.93955 49.8002' # Innenstadt
13#options['bbox']='12.13344 54.08574,12.14387 54.09182' # Rostock
14#options['bbox']='12.03344 53.98574,12.24387 54.19182' # Rostock-xx
15#options['bbox']='2.822157 41.983275,2.827371 41.987123' # Girona River
16#options['bbox']='8.9771580802 47.2703623267,13.8350427083 50.5644529365' # bayern
17#options['bbox']='-43.1806 -22.91442,-43.17034 -22.90488' # Rio
18# for tile names and coordinates:
19# http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
20
21def avg(a,b): return (a+b)/2.0
22
23class osrayHandler(BaseHTTPRequestHandler):
24
25
26    def scale_bbox(self,old_bbox,scale):
27        old_bbox = old_bbox.replace(' ',',')
28        print "old_bbox ",old_bbox
29        pointlist = map(lambda coord: float(coord), old_bbox.split(','))
30        print "pointlist ",pointlist
31        xmin = pointlist[0]
32        ymin = pointlist[1]
33        xmax = pointlist[2]
34        ymax = pointlist[3]
35        xmid = avg(xmin,xmax)
36        ymid = avg(ymin,ymax)
37        xradius = xmid-xmin
38        yradius = ymid-ymin
39        xradius *= scale
40        yradius *= scale
41        xmin = xmid-xradius
42        xmax = xmid+xradius
43        ymin = ymid-yradius
44        ymax = ymid+yradius
45        return str(xmin)+" "+str(ymin)+","+str(xmax)+" "+str(ymax)
46
47    def num2deg(xtile, ytile, zoom):
48        n = 2.0 ** zoom
49        lon_deg = xtile / n * 360.0 - 180.0
50        lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
51        lat_deg = math.degrees(lat_rad)
52        return(lat_deg, lon_deg)
53
54    def num2bbox(xtile, ytile, zoom):
55        pass
56        return 0
57
58    def do_GET(self):
59        try:
60            theurl = self.path
61            if theurl == "/favicon.ico":
62                self.send_response(200)
63                self.end_headers()
64                self.wfile.write("")
65                return
66            urlcomponents = urlparse.urlparse(theurl)
67            print urlcomponents
68            baseurl = urlcomponents[2]
69            print "parse=",urlparse.urlparse(theurl)
70            print "base=",baseurl
71            urlqs = urlparse.urlparse(theurl)[4]
72            print "URL qs:", urlqs
73            queryparams = urlparse.parse_qs(urlqs)
74            print queryparams
75
76            options['bbox']=self.scale_bbox(options['bbox'],float(2**-1))
77
78            if baseurl.startswith("povtile"):
79                pass
80                # PLEASE IMPLEMENT HERE - FIXME
81           
82            if baseurl.endswith(".png"):
83                if queryparams.has_key('width'):
84                    options['width']=queryparams['width'][0]
85                if queryparams.has_key('height'):
86                    options['height']=queryparams['height'][0]
87                if queryparams.has_key('hq'):
88                    options['hq']=(str(queryparams['hq'][0])=='1')
89                print "--- calling osray"
90                osray.main(options)
91                print "--- calling osray ends"
92                f = open(curdir + sep + 'scene-osray.png')
93                print "--- send_response"
94                self.send_response(200)
95                print "--- send_header"
96                self.send_header('Content-type','image/png')
97                print "--- send_end_headers"
98                self.end_headers()
99                print "--- send_write"
100                self.wfile.write(f.read())
101                print "--- close"
102                f.close()
103                return
104            print "URL was ", theurl
105            urlqs = urlparse.urlparse(theurl)[4]
106            print "URL qs:", urlqs
107            queryparams = urlparse.parse_qs(urlqs)
108            print queryparams
109            self.send_response(200)
110            self.send_header('Content-type', 'text/html')
111            self.end_headers()
112            self.wfile.write("hey, today is the" + str(time.localtime()[7]))
113            self.wfile.write(" day in the year " + str(time.localtime()[0]))
114            return
115        except IOError:
116            self.send_error(404, 'File Not Found: %s' % self.path)
117
118
119"""
120    def do_POST(self):
121        global rootnode
122        try:
123            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
124            if ctype == 'multipart/form-data':
125                query = cgi.parse_multipart(self.rfile, pdict)
126            self.send_response(301)
127           
128            self.end_headers()
129            upfilecontent = query.get('upfile')
130            print "filecontent", upfilecontent[0]
131            self.wfile.write("<HTML>POST OK.<BR><BR>");
132            self.wfile.write(upfilecontent[0]);
133           
134        except :
135            pass
136"""
137
138def main():
139    try:
140        server = HTTPServer(('', 8087), osrayHandler)
141        print 'started osray server...'
142        server.serve_forever()
143    except KeyboardInterrupt:
144        print 'shutting down server'
145        server.socket.close()
146
147if __name__ == '__main__':
148    main()
Note: See TracBrowser for help on using the repository browser.