-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwms.wsgi
executable file
·205 lines (171 loc) · 6.03 KB
/
wms.wsgi
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Mapscript WMS server with default request parameters for josm
#
# to be used in conjunction with wsgi Interface
#
# (c) 2009-2010 Sven Geggus <[email protected]>
#
# Released under the GNU GENERAL PUBLIC LICENSE
# http://www.gnu.org/copyleft/gpl.html
#
# $Id$
#
debug = 1
mapfile="/osm/wms/osmwms.map"
tcopyright="copyrighted Material: OSM use only"
josmdefaults={'SERVICE': 'WMS', 'VERSION': '1.1.1', 'FORMAT':'image/png', 'REQUEST':'GetMap'}
import os,crypt,mapscript,sys,math,string,cgi
from time import *
from urllib.parse import parse_qs
# make our WMS also usable as TMS
def TileToMeters(tx, ty, zoom):
initialResolution = 20037508.342789244 * 2.0 / 256.0
originShift = 20037508.342789244
tileSize = 256.0
zoom2 = (2.0**zoom)
res = initialResolution / zoom2
mx = (res*tileSize*(tx+1))-originShift
my = (res*tileSize*(zoom2-ty))-originShift
return mx, my
# this will give the BBox Parameter from x,y,z
def TileToBBox(x,y,z):
x1,y1=TileToMeters(x-1,y+1,z)
x2,y2=TileToMeters(x,y,z)
return x1,y1,x2,y2
# generate a valid XML ServiceException message text
# from a simple textual error message
def SException(start_response,msg,code=""):
xml = '<ServiceExceptionReport '
xml += 'xmlns=\"http://www.opengis.net/ogc\" '
xml += 'xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance" '
xml += 'xsi:schemaLocation=\"http://www.opengis.net/ogc '
xml += 'http://schemas.opengis.net/wms/1.1.1/OGC-exception.xsd\">\n'
if code == "":
xml += ' <ServiceException>\n ' + msg
else:
xml += ' <ServiceException code=\"' + code +'\">\n ' + msg
xml += '\n </ServiceException>\n</ServiceExceptionReport>\n'
bxml=xml.encode()
status = '200 OK'
response_headers = [('Content-type', 'application/vnd.ogc.se_xml'),('Content-Length', str(len(bxml)))]
start_response(status, response_headers)
return bxml
# "main"
def application(env, start_response):
global debug
global tcopyright
# read apaceh environment variables
content_type = "text/plain"
response_headers = dict([('Content-type', content_type)])
if (env['SERVER_PORT'] == '80'):
proto="http"
else:
proto="https"
url = "%s://%s%s" % \
(proto,env['SERVER_NAME'],env['REQUEST_URI'])
os.chdir(env['DOCUMENT_ROOT'])
querystr = env['QUERY_STRING']
try:
uagent = env['HTTP_USER_AGENT']
except:
uagent = ''
qupper=querystr.upper()
# MAP=something in request is always an error
if "MAP=" in qupper:
return SException(start_response,'Invalid argument "MAP=..." in request')
# if tile=something is given we are in TMS mode
# parse QUERY_STRING
query = parse_qs(querystr)
query = dict(query)
msreq = mapscript.OWSRequest()
query_layers=''
tilemode=0
REQUEST=''
# ad QUERY items into mapscript request object
for key, value in query.items():
key=key.upper()
if (key == "DEBUG"):
debug = 1
if (key == "REQUEST"):
REQUEST = value
if (key == "LAYERS"):
query_layers=value
if ("," in query_layers):
return SException(start_response,'Multiple Layers are currently unsupported')
if key != "TILE":
msreq.setParameter(key,value[0])
else:
xyz=value[0].split(',')
if (len(xyz) != 3):
return SException(start_response,'Invalid argument "TILE=%s" in request, must be TILE=x,y,z' % value)
try:
x=int(xyz[0])
except:
return SException(start_response,'Invalid argument "TILE=%s" in request, must be TILE=x,y,z' % value)
try:
y=int(xyz[1])
except:
return SException(start_response,'Invalid argument "TILE=%s" in request, must be TILE=x,y,z' % value)
try:
z=int(xyz[2])
except:
return SException(start_response,'Invalid argument "TILE=%s" in request, must be TILE=x,y,z' % value)
tilemode=1
# we are in tilemode, lets add the required keys
if (tilemode):
msreq.setParameter("srs","EPSG:3857")
msreq.setParameter("bbox","%f,%f,%f,%f" % TileToBBox(x,y,z))
msreq.setParameter("width","256")
msreq.setParameter("height","256")
# add default QUERY items for use with JOSM
# this will allow for easy to remember WMS Request-URLs
if ("JOSM" in uagent) or (tilemode):
for d in josmdefaults:
if d not in qupper:
msreq.setParameter(d,josmdefaults[d])
map = mapscript.mapObj(mapfile)
map.web.metadata.set("wms_onlineresource",url.split("?")[0])
map.web.metadata.set("wms_allow_getmap_without_styles","true")
try:
layer=map.getLayerByName(query_layers[0])
cstring=layer.metadata.get('copyright')
except:
cstring=tcopyright
try:
wms_disabled=layer.metadata.get('-wms-disabled')
except:
wms_disabled='false'
if wms_disabled == 'true' and not tilemode:
return SException(start_response,'No WMS available for this layer, try TMS.')
clayer=map.getLayerByName("copyright")
cclass=clayer.getClass(0)
cclass.setText(cstring)
if (REQUEST == "GetCapabilities"):
map.removeLayer(clayer.index)
# write a mapfile for debugging purposes
if (debug):
os.umask(0o022)
map.save("/tmp/topomap.map")
# write mapserver results to buffer
mapscript.msIO_installStdoutToBuffer()
try:
res = map.OWSDispatch(msreq)
except:
return [SException(start_response,str(sys.exc_info()[1]))]
# adjust content-type
content_type = mapscript.msIO_stripStdoutBufferContentType()
output=mapscript.msIO_getStdoutBufferBytes()
# set Expire header to 100 days from now
expire = strftime('%a, %d %b %Y %H:%M:%S %z',gmtime(time()+3600*24*100))
response_headers = [('Content-type', content_type),
('Expires', expire),
('Content-Length', str(len(output)))]
status = '200 OK'
start_response(status, response_headers)
# write image data to stdout
return [output]
if __name__ == '__main__':
import wsgiref.handlers
wsgiref.handlers.CGIHandler().run(application)