|
@@ -16,6 +16,8 @@ from osgeo import osr
|
|
|
from osgeo import ogr
|
|
from osgeo import ogr
|
|
|
import OWSExceptions
|
|
import OWSExceptions
|
|
|
from owslib import crs as CRS
|
|
from owslib import crs as CRS
|
|
|
|
|
+import shutil
|
|
|
|
|
+from xml.etree import ElementTree
|
|
|
|
|
|
|
|
class OWS:
|
|
class OWS:
|
|
|
|
|
|
|
@@ -35,13 +37,6 @@ class OWS:
|
|
|
|
|
|
|
|
def __init__(self,url=None,qstring=None,configFile=None):
|
|
def __init__(self,url=None,qstring=None,configFile=None):
|
|
|
self.requestUrl = url
|
|
self.requestUrl = url
|
|
|
- if url:
|
|
|
|
|
- self.url = url
|
|
|
|
|
- logging.debug("OWS.py::__init__()::url: '%s'" % url)
|
|
|
|
|
- self.__getCapabilities()
|
|
|
|
|
- if qstring:
|
|
|
|
|
- self.qstring = qstring
|
|
|
|
|
-
|
|
|
|
|
configFiles = [ os.path.join(os.path.dirname(__file__),"config.cfg") ]
|
|
configFiles = [ os.path.join(os.path.dirname(__file__),"config.cfg") ]
|
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
if sys.platform == "win32":
|
|
@@ -55,12 +50,22 @@ class OWS:
|
|
|
self.config = ConfigParser.ConfigParser()
|
|
self.config = ConfigParser.ConfigParser()
|
|
|
self.config.read(configFiles)
|
|
self.config.read(configFiles)
|
|
|
|
|
|
|
|
- logging.debug("Creating cachedir for %s in %s" % (self.url,self.config.get("OWSProxy","cachedir")))
|
|
|
|
|
|
|
+ logging.debug("Creating cachedir for %s in %s" % (self.url,self.config.get("Proxy4OWS","cachedir")))
|
|
|
logging.debug("%s initialized"%self.service)
|
|
logging.debug("%s initialized"%self.service)
|
|
|
|
|
|
|
|
|
|
+ if url:
|
|
|
|
|
+ self.url = url
|
|
|
|
|
+ logging.debug("OWS.py::__init__()::url: '%s'" % url)
|
|
|
|
|
+ self.__getCapabilities()
|
|
|
|
|
+ if qstring:
|
|
|
|
|
+ self.qstring = qstring
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def __getCapabilities(self):
|
|
def __getCapabilities(self):
|
|
|
|
|
+ self.__getCacheDir()
|
|
|
logging.debug("OWS.py::__getCapabilities::self.url: '%s'" % self.url)
|
|
logging.debug("OWS.py::__getCapabilities::self.url: '%s'" % self.url)
|
|
|
self.parsedUrl = urlparse.urlparse(self.url)
|
|
self.parsedUrl = urlparse.urlparse(self.url)
|
|
|
|
|
+
|
|
|
if self.service == "WFS":
|
|
if self.service == "WFS":
|
|
|
from owslib.wfs import WebFeatureService
|
|
from owslib.wfs import WebFeatureService
|
|
|
try:
|
|
try:
|
|
@@ -73,6 +78,9 @@ class OWS:
|
|
|
from owslib.wcs import WebCoverageService
|
|
from owslib.wcs import WebCoverageService
|
|
|
self.capabilities = WebCoverageService(url=self.url,version="1.0.0")
|
|
self.capabilities = WebCoverageService(url=self.url,version="1.0.0")
|
|
|
|
|
|
|
|
|
|
+ # cache capabilities document
|
|
|
|
|
+ open(os.path.join(self.cachedir,"capabilities.xml"),"w").write(ElementTree.tostring(self.capabilities._capabilities))
|
|
|
|
|
+
|
|
|
def getParams(self):
|
|
def getParams(self):
|
|
|
params = urlparse.parse_qs(self.qstring)
|
|
params = urlparse.parse_qs(self.qstring)
|
|
|
|
|
|
|
@@ -120,12 +128,20 @@ class OWS:
|
|
|
|
|
|
|
|
def __getCacheDir(self):
|
|
def __getCacheDir(self):
|
|
|
|
|
|
|
|
- self.cachedir = tempfile.mkdtemp(prefix="%s-%s"%(self.service,
|
|
|
|
|
- md5.new(self.url).hexdigest()),
|
|
|
|
|
- dir=self.config.get("OWSProxy","cachedir"))
|
|
|
|
|
- os.chmod(self.cachedir, 0777)
|
|
|
|
|
- logging.debug("Cachedir %s created" % self.cachedir)
|
|
|
|
|
- open(os.path.join(self.cachedir,"url.txt"),"w").write(self.url)
|
|
|
|
|
|
|
+ dirname = os.path.join(self.config.get("Proxy4OWS","cachedir"),
|
|
|
|
|
+ "%s-%s" % (self.service, md5.new(self.url).hexdigest()))
|
|
|
|
|
+
|
|
|
|
|
+ # get existing cache dir
|
|
|
|
|
+ if not os.path.isdir(dirname):
|
|
|
|
|
+ os.mkdir(dirname)
|
|
|
|
|
+ logging.debug("Cachedir %s created" % dirname)
|
|
|
|
|
+ os.chmod(dirname, 0777)
|
|
|
|
|
+ open(os.path.join(self.cachedir,"url.txt"),"w").write(self.url)
|
|
|
|
|
+ else:
|
|
|
|
|
+ logging.debug("Cachedir %s found" % dirname)
|
|
|
|
|
+
|
|
|
|
|
+ self.cachedir = dirname
|
|
|
|
|
+
|
|
|
return self.cachedir
|
|
return self.cachedir
|
|
|
|
|
|
|
|
def dispatch(self):
|
|
def dispatch(self):
|
|
@@ -166,11 +182,37 @@ class OWS:
|
|
|
|
|
|
|
|
def getMapObj(self,mapfilename=None):
|
|
def getMapObj(self,mapfilename=None):
|
|
|
|
|
|
|
|
- self.__getCacheDir()
|
|
|
|
|
|
|
|
|
|
if self.url is not None and self.capabilities is None:
|
|
if self.url is not None and self.capabilities is None:
|
|
|
self.__getCapabilities()
|
|
self.__getCapabilities()
|
|
|
|
|
|
|
|
|
|
+ # nothing has changed in the capabilities document since last
|
|
|
|
|
+ # request ?
|
|
|
|
|
+ if os.path.exists(os.path.join(self.cachedir,"capabilities.xml")):
|
|
|
|
|
+ oldCapsFile = open(os.path.join(self.cachedir,"capabilities.xml"))
|
|
|
|
|
+ oldCaps = oldCapsFile.read()
|
|
|
|
|
+ oldCapsFile.close()
|
|
|
|
|
+ # the capabilities document is up-to-date, load existing
|
|
|
|
|
+ # mapfile
|
|
|
|
|
+ newXml = ElementTree.tostring(self.capabilities._capabilities)
|
|
|
|
|
+ if md5.new(oldCaps).hexdigest() == md5.new(newXml).hexdigest():
|
|
|
|
|
+ newCapsFile = open(os.path.join(self.cachedir,"capabilities.xml"),"w")
|
|
|
|
|
+ newCapsFile.write(newXml)
|
|
|
|
|
+ logging.debug("Capabilities unchanged, using existing mapfile %s" % self.getMapfileLocation())
|
|
|
|
|
+ return mapscript.mapObj(self.getMapfileLocation())
|
|
|
|
|
+
|
|
|
|
|
+ # finally
|
|
|
|
|
+
|
|
|
|
|
+ # clear existing cached files
|
|
|
|
|
+ logging.debug("Cached capabilities document is outdated, creating new one")
|
|
|
|
|
+ shutil.rmtree(self.cachedir)
|
|
|
|
|
+ # reate new one
|
|
|
|
|
+ self.__getCacheDir()
|
|
|
|
|
+ return self._createNewMapObj(mapfilename)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def _createNewMapObj(self,mapfilename=None):
|
|
|
|
|
+
|
|
|
mapobj = mapscript.mapObj()
|
|
mapobj = mapscript.mapObj()
|
|
|
|
|
|
|
|
mapobj.setMetaData("wms_onlineresource",self.getOnlineResource(self.config.get("MapServer","onlineresource"),mapfilename))
|
|
mapobj.setMetaData("wms_onlineresource",self.getOnlineResource(self.config.get("MapServer","onlineresource"),mapfilename))
|
|
@@ -246,13 +288,16 @@ class OWS:
|
|
|
if time:
|
|
if time:
|
|
|
time = "<DefaultTime>"+time+"</DefaultTime>"
|
|
time = "<DefaultTime>"+time+"</DefaultTime>"
|
|
|
|
|
|
|
|
- open(defFileName,'w').write(
|
|
|
|
|
- Template(open(templatefile).read()).substitute(
|
|
|
|
|
- dict(url= layerurl,
|
|
|
|
|
- name=name,time=time,extras="&BAND=1,2,3")))
|
|
|
|
|
- # FIXME always takes band 1,2,3 ^^
|
|
|
|
|
|
|
+ if not os.path.isfile(defFileName):
|
|
|
|
|
+ open(defFileName,'w').write(
|
|
|
|
|
+ Template(open(templatefile).read()).substitute(
|
|
|
|
|
+ dict(url= layerurl,
|
|
|
|
|
+ name=name,time=time,extras="&BAND=1,2,3")))
|
|
|
|
|
+ # FIXME always takes band 1,2,3 ^^
|
|
|
|
|
|
|
|
- logging.debug("Created %s layer definition file" % defFileName)
|
|
|
|
|
|
|
+ logging.debug("Created %s layer definition file" % defFileName)
|
|
|
|
|
+ else:
|
|
|
|
|
+ logging.debug("Using existing layer definition file %s" % defFileName)
|
|
|
|
|
|
|
|
return defFileName
|
|
return defFileName
|
|
|
|
|
|