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

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

osray: highway areas, area lights

  • Property svn:mime-type set to text/plain
File size: 5.2 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 do_GET(self):
55        try:
56            theurl = self.path
57            if theurl == "/favicon.ico":
58                self.send_response(200)
59                self.end_headers()
60                self.wfile.write("")
61                return
62            urlcomponents = urlparse.urlparse(theurl)
63            print urlcomponents
64            baseurl = urlcomponents[2]
65            print "parse=",urlparse.urlparse(theurl)
66            print "base=",baseurl
67            urlqs = urlparse.urlparse(theurl)[4]
68            print "URL qs:", urlqs
69            queryparams = urlparse.parse_qs(urlqs)
70            print queryparams
71
72            options['bbox']=self.scale_bbox(options['bbox'],float(2**-1))
73
74            if baseurl.startswith("povtile"):
75                pass
76                # PLEASE IMPLEMENT HERE - FIXME
77           
78            if baseurl.endswith(".png"):
79                if queryparams.has_key('width'):
80                    options['width']=queryparams['width'][0]
81                if queryparams.has_key('height'):
82                    options['height']=queryparams['height'][0]
83                if queryparams.has_key('hq'):
84                    options['hq']=(str(queryparams['hq'][0])=='1')
85                print "--- calling osray"
86                osray.main(options)
87                print "--- calling osray ends"
88                f = open(curdir + sep + 'scene-osray.png')
89                print "--- send_response"
90                self.send_response(200)
91                print "--- send_header"
92                self.send_header('Content-type','image/png')
93                print "--- send_end_headers"
94                self.end_headers()
95                print "--- send_write"
96                self.wfile.write(f.read())
97                print "--- close"
98                f.close()
99                return
100            print "URL was ", theurl
101            urlqs = urlparse.urlparse(theurl)[4]
102            print "URL qs:", urlqs
103            queryparams = urlparse.parse_qs(urlqs)
104            print queryparams
105            self.send_response(200)
106            self.send_header('Content-type', 'text/html')
107            self.end_headers()
108            self.wfile.write("hey, today is the" + str(time.localtime()[7]))
109            self.wfile.write(" day in the year " + str(time.localtime()[0]))
110            return
111        except IOError:
112            self.send_error(404, 'File Not Found: %s' % self.path)
113
114
115"""
116    def do_POST(self):
117        global rootnode
118        try:
119            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
120            if ctype == 'multipart/form-data':
121                query = cgi.parse_multipart(self.rfile, pdict)
122            self.send_response(301)
123           
124            self.end_headers()
125            upfilecontent = query.get('upfile')
126            print "filecontent", upfilecontent[0]
127            self.wfile.write("<HTML>POST OK.<BR><BR>");
128            self.wfile.write(upfilecontent[0]);
129           
130        except :
131            pass
132"""
133
134def main():
135    try:
136        server = HTTPServer(('', 8087), osrayHandler)
137        print 'started osray server...'
138        server.serve_forever()
139    except KeyboardInterrupt:
140        print 'shutting down server'
141        server.socket.close()
142
143if __name__ == '__main__':
144    main()
Note: See TracBrowser for help on using the repository browser.