-
Notifications
You must be signed in to change notification settings - Fork 1
/
wmsindex.wsgi
executable file
·123 lines (109 loc) · 4.95 KB
/
wmsindex.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# generate index page for wms.openstreetmap.de dynamically
#
# from information provided by templates and mapfile information
#
# this way for adding new layers the mapfile is the only place we need to
# make changes. No redundant information anymore :)
#
# this is not a very elegant script (intermixes content and code)
# but serves its purpose
#
# (c) 2010 Sven geggus <[email protected]>
#
# GNU GPL Version 3 or later
# http://www.gnu.org/copyleft/gpl.html
header_template='/osm/wms/templates/index_header.html'
footer_template='/osm/wms/templates/index_footer.html'
# zoomlevel for potlatch URL
zoom=17
mapfile='/osm/wms/osmwms.map'
import re,cgi,mapscript,os,pyproj
# this will generate an extent in WGS84 projection
# regardless of the actual layer projection
def gen4326Extent(ms_extent,ms_srs):
ms_srs=ms_srs.lower()
if ms_srs == "espg:4326":
return ms_extent
else:
x0,y0,x1,y1=eval('['+ms_extent.replace(' ',',')+']')
t = pyproj.Transformer.from_crs(ms_srs, "epsg:4326", always_xy=True)
lon0,lat0 = t.transform(x0,y0)
lon1,lat1 = t.transform(x1,y1)
return "%f %f %f %f" % (lon0,lat0,lon1,lat1)
def application(env, start_response):
wmsurl="://wms.openstreetmap.de/wms"
tmsurl="://wms.openstreetmap.de/tms"
mapurl="://wms.openstreetmap.de/slippymap"
status = '200 OK'
port = env.get('SERVER_PORT', '80')
if (port == '443'):
wmsurl='https'+wmsurl
tmsurl='https'+tmsurl
mapurl='https'+mapurl
else:
wmsurl='http'+wmsurl
tmsurl='http'+tmsurl
mapurl='http'+mapurl
map = mapscript.mapObj(mapfile)
templ = open(header_template,'r',encoding='utf8')
index_html = templ.read()
templ.close()
for i in range(0,map.numlayers):
layer = map.getLayer(i)
zoom = layer.metadata.get('zoomlevels')
if zoom is None:
zoom = 19
zoom = int(zoom)+1
if layer.name != 'copyright':
extent = gen4326Extent(layer.metadata.get('wms_extent'),layer.metadata.get('wms_srs'))
index_html += "<hr />\n"
index_html += '<table border="0" width="100%">\n'
index_html += '<tr><td width="30%%"><b>Name:</b></td><td id="%s"><b>%s</b></td></tr>' % (layer.name,layer.name)
wiki = layer.metadata.get('wiki')
if wiki is not None:
index_html += '<tr><td width="30%%"><b>Wiki:</b></td><td><a href="%s">%s</a></td></tr>' % (wiki,wiki)
index_html += '<tr><td width="30%%"><b>Source:</b></td><td>%s</td></tr>' % layer.metadata.get('copyright')
if layer.metadata.get('-terms-of-use'):
index_html += '<tr><td width="30%%"><b>Terms of Use:</b></td><td>%s</td></tr>' % layer.metadata.get('-terms-of-use')
index_html += '<tr><td width="30%%"><b>Content:</b></td><td>%s</td></tr>' % layer.metadata.get('wms_title')
index_html += '<tr><td width="30%%"><b>Bounding Box:</b></td><td>%s</td></tr>' % extent
index_html += '<tr><td width="30%%"><b>TMS URL for JOSM Imagery:</b></td><td><b>tms:%s/%s (zoom %d)</b> ' % (tmsurl,layer.name,zoom)
# https://josm.openstreetmap.de/wiki/Help/RemoteControlCommands#imagery
index_html += '<a href="http://localhost:8111/imagery?title=%s&type=tms&max_zoom=%s&url=%s/%s/{zoom}/{x}/{y}.png">Add via Remote control</a> ' \
% (layer.name,zoom,tmsurl,layer.name)
index_html += '</td></tr>'
if not layer.metadata.get('-wms-disabled'):
index_html += '<tr><td width="30%%"><b>WMS URL (for JOSM/Merkaartor):</b></td><td>%s?layers=%s&</td></tr>' % (wmsurl,layer.name)
index_html += '<tr><td width="30%%"><b>Interactive map:</b></td><td><a href="%s/%s">%s/%s</a></td></tr>' % (mapurl,layer.name,mapurl,layer.name)
latlon=extent.split()
lon1=float(latlon[0])
lat1=float(latlon[1])
lon2=float(latlon[2])
lat2=float(latlon[3])
lat=lat1+(lat2-lat1)/2
lon=lon1+(lon2-lon1)/2
index_html += '<tr><td width="30%%"><b>Online Editor URL:</b></td><td>'
# https://github.com/openstreetmap/iD/blob/master/API.md
# background=custom:https://{switch:a,b,c}.tile.openstreetmap.org/{zoom}/{x}/{y}.png
index_html += '<a href="https://www.openstreetmap.org/edit#background=custom:%s/%s/{zoom}/{x}/{y}.png">iD</a> ' \
% (tmsurl,layer.name)
index_html += '(<a href="https://www.openstreetmap.org/edit#background=custom:%s/%s/{zoom}/{x}/{y}.png&map=%s/%f/%f">center</a>), ' \
% (tmsurl,layer.name,zoom,lat,lon)
index_html += '<a href="https://www.openstreetmap.org/edit?lat=%f&lon=%f&zoom=%s&tileurl=%s/%s/!/!/!.png">Potlatch %s</a> ' \
% (lat,lon,zoom,tmsurl,layer.name,layer.name)
index_html += '</td></tr>'
index_html += '</table>\n'
templ = open(footer_template,'r',encoding='utf8')
index_html += templ.read()
templ.close()
index_html_utf=index_html.encode('utf8')
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(index_html_utf)))]
start_response(status, response_headers)
return [index_html_utf]
if __name__ == '__main__':
import wsgiref.handlers
wsgiref.handlers.CGIHandler().run(application)