This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathWMSTileServer.py
77 lines (67 loc) · 2.94 KB
/
WMSTileServer.py
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
from TileServer import *
from projections import *
try:
from pyproj import Proj
from xml.etree import ElementTree as ET
except:
pass
class WMSTileServer(TileServer):
'''Generic WMS tile server (see below for extending it to a specific provider)'''
def geturl(self, nx, ny, lx, ly, tilew, tileh, zoom, format, maptype):
tz = pow(2, zoom)
if self.customBounds:
ulx, uly, orx, ory = self.bounds
dx, dy = orx-ulx, ory-uly
x1 = ulx + dx * nx / tz
x2 = ulx + dx * (nx+1.0) / tz
y1 = uly + dy * (1.0 - (ny + 1.0) / tz)
y2 = uly + dy * (1.0 - (ny + 0.0) / tz)
return self.url + "&BBOX=%f,%f,%f,%f&WIDTH=256&HEIGHT=256&LAYERS=%s" % (x1, y1, x2, y2, maptype)
if self.isPLatLon:
y1, x1 = unit_to_p4326(1 - 2.0 * (ny) / tz, 2.0 * (nx) / tz - 1)
y2, x2 = unit_to_p4326(1 - 2.0 * (ny + 1.0) / tz, 2.0 * (nx + 1.0) / tz - 1)
else:
x1, y1 = unit_to_project(self.projection, 2.0 * (nx) / tz - 1, 1 - 2.0 * (ny) / tz)
x2, y2 = unit_to_project(self.projection, 2.0 * (nx + 1.0) / tz - 1, 1 - 2.0 * (ny + 1.0) / tz)
return self.url + "&BBOX=%f,%f,%f,%f&WIDTH=256&HEIGHT=256&LAYERS=%s" % (x1, y2, x2, y1, maptype)
def initFromGetCapabilities(self, host, baseurl, index = 0, srs = None, layer = None):
# GetCapabilities (Layers + SRS)
self.customBounds = False
capabilities = urlopen(host + baseurl + "?SERVICE=WMS&VERSION=1.1.1&Request=GetCapabilities").read().strip()
if layer == None or srs == None:
try:
tree = ET.fromstring(capabilities)
layers = tree.findall("Capability/Layer/Layer")
data = {}
for layer in layers:
name = layer.find("Name").text
srss = layer.findall("SRS")
data[name] = map(lambda x:x.text, srss)
#print name,data[name]
# Choose Layer and SRS by (alphabetical) index
layer = sorted(data.keys())[index]
if srs is None:
srs = sorted(data[layer])[0]
except:
pass
# generate tile URL and init projection by EPSG code
self.url = baseurl + "?SRS=%s&FORMAT=image/png&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap" % (srs)
self.isPLatLon = False
try:
if srs == "EPSG:4326": # android patches for common projections
self.isPLatLon = True
else:
self.projection = Proj(init=srs)
except:
pass
class OSMWMSTileServer(WMSTileServer):
'''Connect to OSM-WMS worldwide tile service
(i.e., we actually talk to a WMS-T server, but the syntax is WMS)
'''
provider_name = 'osmwms'
provider_host = '129.206.229.158'
available_maptype = dict(roadmap = 'Roadmap')
def __init__(self, **kwargs):
self.initFromGetCapabilities('http://129.206.229.158', '/cached/osm', index=1)
super(WMSTileServer, self).__init__(**kwargs)
TileServer.register(OSMWMSTileServer)