24 |
|
|
25 |
getErrorMsg() |
getErrorMsg() |
26 |
|
|
27 |
fetchCapabilities (*resource) |
fetchCapabilities(resource) |
28 |
saveCapabilities (filename) |
saveCapabilities(filename) |
29 |
loadCapabilities (filename) |
loadCapabilities(filename) |
30 |
printCapabilities () |
printCapabilities() |
31 |
|
|
32 |
Requirements: |
Requirements: |
33 |
- PyOGCLib <http://www.sourceforge.net/projects/pyogclib> |
- PyOGCLib <http://www.sourceforge.net/projects/pyogclib> |
54 |
|
|
55 |
# Assume the PyOGCLib to be checked out next to the thuban main directory |
# Assume the PyOGCLib to be checked out next to the thuban main directory |
56 |
pyogclib = "../../../PyOGCLib" |
pyogclib = "../../../PyOGCLib" |
57 |
if os.path.isdir (pyogclib) and os.path.isdir (pyogclib + "/ogclib"): |
if os.path.isdir(pyogclib) and os.path.isdir(pyogclib + "/ogclib"): |
58 |
path.insert (0, pyogclib) |
path.insert(0, pyogclib) |
59 |
|
|
60 |
# We use gettext, so we need to import it and hence need to adjust the |
# We use gettext, so we need to import it and hence need to adjust the |
61 |
# path again |
# path again |
62 |
if __name__ == "__main__": |
if __name__ == "__main__": |
63 |
path.insert (0, "../../") |
path.insert(0, "../../") |
64 |
|
|
65 |
# ---------------------------------------------------------------------- |
# ---------------------------------------------------------------------- |
66 |
|
|
68 |
|
|
69 |
from ogclib.WMSClient import WMSClient |
from ogclib.WMSClient import WMSClient |
70 |
|
|
71 |
class WMSCapabilities (WMSClient): |
class WMSCapabilities(WMSClient): |
72 |
""" |
""" |
73 |
Thuban class to maintain capabilities. This class provides |
Thuban class to maintain capabilities. This class provides |
74 |
methods to fetch, save and load capabilities as well as methods to |
methods to fetch, save and load capabilities as well as methods to |
85 |
errorMsg = None |
errorMsg = None |
86 |
wmsVersion = None |
wmsVersion = None |
87 |
|
|
88 |
def __init__ (self, *parm): |
def __init__(self, *parm): |
89 |
""" |
""" |
90 |
Initialises Capabilities with one optional parameter |
Initialises Capabilities with one optional parameter |
91 |
|
|
92 |
|
param can be either a URL or a filename: |
93 |
|
|
94 |
filename -- load capabilities from file |
filename -- load capabilities from file |
95 |
url -- fetch capabilities from network |
url -- fetch capabilities from network |
96 |
""" |
""" |
97 |
|
|
98 |
if parm and parm[0]: |
if parm and parm[0]: |
99 |
if os.path.isfile (parm[0]): |
if os.path.isfile(parm[0]): |
100 |
self.loadCapabilities (parm[0]) |
self.loadCapabilities(parm[0]) |
101 |
else: |
else: |
102 |
if parm[0].find ("http://",0) == 0: |
if parm[0].find("http://", 0) == 0: |
103 |
self.fetchCapabilities (parm[0]) |
self.fetchCapabilities(parm[0]) |
104 |
else: |
else: |
105 |
self.errorMsg \ |
self.errorMsg \ |
106 |
= _("Resource '%s' is neither local file nor URL") |
= _("Resource '%s' is neither local file nor URL") \ |
107 |
% parm[0] |
% parm[0] |
108 |
|
|
109 |
|
|
110 |
def getErrorMsg (self): |
def getErrorMsg(self): |
111 |
return self.errorMsg |
return self.errorMsg |
112 |
|
|
113 |
|
|
114 |
def fetchCapabilities (self, resource): |
def fetchCapabilities(self, resource): |
115 |
"""Fetches the WMS capabilities from an Internet resource""" |
"""Fetches the WMS capabilities from an Internet resource""" |
116 |
|
|
117 |
|
xml = None |
118 |
self.wmsVersion = "1.1" |
self.wmsVersion = "1.1" |
119 |
self.capabilities = self.getCapabilities(resource, self.wmsVersion) |
xml = self.getCapabilities(resource, self.wmsVersion) |
120 |
if not self.capabilities: |
if not self.capabilities: |
121 |
self.wmsVersion = "1.0" |
self.wmsVersion = "1.0" |
122 |
self.capabilities = self.getCapabilities(resource, self.wmsVersion) |
xml = self.getCapabilities(resource, self.wmsVersion) |
123 |
|
|
124 |
|
|
125 |
def saveCapabilities (self, fname): |
def saveCapabilities(self, fname): |
126 |
"""Save capabilities to local file""" |
"""Save capabilities to local file""" |
127 |
|
|
128 |
if self.capabilities is None: |
if self.capabilities is None: |
129 |
self.errorMsg = _("No capabilities available") |
self.errorMsg = _("No capabilities available") |
130 |
else: |
else: |
131 |
try: |
try: |
132 |
out = open (fname, "w") |
out = open(fname, "w") |
133 |
out.write (self.capabilities) |
out.write(self.capabilities) |
134 |
out.close() |
out.close() |
135 |
except: |
except IOError: |
136 |
self.errorMsg = _("Can't open file '%s' for writing") % fname |
self.errorMsg = _("Can't open file '%s' for writing") % fname |
137 |
|
|
138 |
|
|
139 |
def loadCapabilities (self, fname): |
def loadCapabilities(self, fname): |
140 |
"""Load capabilities from a local file""" |
"""Load capabilities from a local file""" |
141 |
|
|
142 |
try: |
try: |
143 |
input = open (fname, "r") |
input = open(fname, "r") |
144 |
self.capabilities = input.read () |
xml = input.read() |
145 |
input.close() |
input.close() |
146 |
except: |
except IOError: |
147 |
self.errorMsg = _("Can't open file '%s' for reading") % fname |
self.errorMsg = _("Can't open file '%s' for reading") % fname |
148 |
|
|
149 |
|
|
150 |
def printCapabilities (self): |
def printCapabilities(self): |
151 |
"""Prints capabilities to stdout""" |
"""Prints capabilities to stdout""" |
152 |
|
|
153 |
print self.capabilities |
print self.capabilities |
157 |
if __name__ == "__main__": |
if __name__ == "__main__": |
158 |
capabilities \ |
capabilities \ |
159 |
= WMSCapabilities("http://frida.intevation.org/cgi-bin/frida_wms?") |
= WMSCapabilities("http://frida.intevation.org/cgi-bin/frida_wms?") |
160 |
if capa.getErrorMsg() is None: |
if capabilities.getErrorMsg() is None: |
161 |
capa.saveCapabilities("frida_capabilities.xml") |
capabilities.saveCapabilities("frida_capabilities.xml") |
162 |
else: |
else: |
163 |
print "Error: " + capa.getErrorMsg() |
print "Error: " + capabilities.getErrorMsg() |