source: subversion/sites/wms.openstreetmap.de/wmsindex.wsgi @ 28349

Revision 28349, 3.8 KB checked in by giggls, 2 years ago (diff)

Always show bounding box in WGS84 even if raster has
another projection like google mercator.

This is done using the pyproj library

  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3#
4# generate index page for wms.openstreetmap.de dynamically
5#
6# from information provided by templates and mapfile information
7#
8# this way for adding new layers the mapfile is the only place we need to
9# make changes. No redundant information anymore :)
10#
11# this is not a very elegant script (intermixes content and code)
12# but serves its purpose
13#
14# (c) 2010 Sven geggus <sven-osm@geggus.net>
15#
16# GNU GPL Version 3 or later
17# http://www.gnu.org/copyleft/gpl.html
18
19header_template='/osm/wms/templates/index_header.html'
20footer_template='/osm/wms/templates/index_footer.html'
21wmsurl="http://wms.openstreetmap.de/wms"
22tmsurl="http://wms.openstreetmap.de/tms"
23mapurl="http://wms.openstreetmap.de/slippymap"
24# zoomlevel for potlatch URL
25zoom=17
26
27mapfile='/osm/wms/osmwms.map'
28
29import re,cgi,mapscript,os,pyproj
30
31# this will generate an extent in WGS84 projection
32# regardless of the actual layer projection
33def gen4326Extent(ms_extent,ms_srs):
34  ms_srs=ms_srs.lower()
35  if ms_srs == "espg:4326":
36    return ms_extent
37  else:
38    x0,y0,x1,y1=eval('['+ms_extent.replace(' ',',')+']')
39    proj4326 = pyproj.Proj(init='epsg:4326')
40    projlayer = pyproj.Proj({'init':ms_srs})
41    lon0,lat0 = pyproj.transform(projlayer,proj4326,x0,y0)
42    lon1,lat1 = pyproj.transform(projlayer,proj4326,x1,y1)
43    return "%f %f %f %f" % (lon0,lat0,lon1,lat1)
44
45def application(env, start_response):
46  status = '200 OK'
47  map = mapscript.mapObj(mapfile)
48
49  templ = open(header_template,'r')
50  index_html = templ.read()
51  templ.close()
52 
53  for i in range(0,map.numlayers):
54   layer = map.getLayer(i)
55   
56   zoom = layer.metadata.get('zoomlevels')
57   if zoom is None:
58    zoom = 19
59   zoom = int(zoom)+1
60   if layer.name != 'copyright':
61    extent = gen4326Extent(layer.metadata.get('wms_extent'),layer.metadata.get('wms_srs'))
62    index_html += "<hr />\n"
63    index_html += '<table border="0" width="100%">\n'
64    index_html += '<tr><td width="30%%"><b>Name:</b></td><td>%s</td></tr>' % layer.name
65    wiki = layer.metadata.get('wiki')
66    if wiki is not None:
67     index_html += '<tr><td width="30%%"><b>Wiki:</b></td><td><a href="%s">%s</a></td></tr>' % (wiki,wiki)
68    index_html += '<tr><td width="30%%"><b>Source:</b></td><td>%s</td></tr>' % layer.metadata.get('copyright')
69    if layer.metadata.get('-terms-of-use'):
70     index_html += '<tr><td width="30%%"><b>Terms of Use:</b></td><td>%s</td></tr>' % layer.metadata.get('-terms-of-use')
71    index_html += '<tr><td width="30%%"><b>Content:</b></td><td>%s</td></tr>' % layer.metadata.get('wms_title')
72    index_html += '<tr><td width="30%%"><b>Bounding Box:</b></td><td>%s</td></tr>' % extent
73    index_html += '<tr><td width="30%%"><b>TMS URL for JOSM Imagery:</b></td><td><b>tms:%s/%s (zoom %d)</b></td></tr>' % (tmsurl,layer.name,zoom)
74    index_html += '<tr><td width="30%%"><b>WMS URL (for JOSM/Merkaartor):</b></td><td>%s?layers=%s&</td></tr>' % (wmsurl,layer.name)
75    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)
76    latlon=extent.split()
77    lon1=float(latlon[0])
78    lat1=float(latlon[1])
79    lon2=float(latlon[2])
80    lat2=float(latlon[3])
81    lat=lat1+(lat2-lat1)/2
82    lon=lon1+(lon2-lon1)/2
83    index_html += '<tr><td width="30%%"><b>Online Editor URL:</b></td><td><a href="http://www.openstreetmap.org/edit?lat=%f&lon=%f&zoom=%s&tileurl=%s/%s/!/!/!.png">Potlatch %s</a></td></tr>' \
84    % (lat,lon,zoom,tmsurl,layer.name,layer.name)
85    index_html += '</table>\n'
86 
87  templ = open(footer_template,'r')
88  index_html += templ.read()
89  templ.close()
90
91  response_headers = [('Content-type', 'text/html'),
92                      ('Content-Length', str(len(index_html)))]
93                       
94  start_response(status, response_headers)
95  return [index_html]
96
97
98                                                 
Note: See TracBrowser for help on using the repository browser.