Explorar o código

Merge branch 'master' of git+ssh://git.ccss.cz/hsrs/owsproxy

Conflicts:
	OWS.py
	owsproxy.cgi
	wfs/__init__.py
Michal Šrédl %!s(int64=14) %!d(string=hai) anos
pai
achega
7693e10518
Modificáronse 5 ficheiros con 114 adicións e 20 borrados
  1. 31 12
      OWS.py
  2. 24 0
      OWSExceptions.py
  3. 1 1
      config.cfg-template
  4. 7 5
      owsproxy.cgi
  5. 51 2
      wfs/__init__.py

+ 31 - 12
OWS.py

@@ -13,7 +13,7 @@ import mapscript
 from string import Template
 from osgeo import osr
 from osgeo import ogr
-
+import OWSExceptions
 
 class OWS:
 
@@ -135,10 +135,14 @@ class OWS:
             self.setFilter(mapobj,request)
 
         # mapobj.getLayerByName(request.getValueByName("layers")).metadata.get("wfs_filter")
-        # here is still fine
+        # here is still fine, but it fails on dispatch:
 
-        # but it fails here:
-        mapobj.OWSDispatch(request)
+        res = mapobj.OWSDispatch(request)
+        if mapscript.MS_DONE == res:
+            raise OWSExceptions.NoValidRequest("No valid OWS Request")
+        elif mapscript.MS_FAILURE == res:
+            pass
+            #raise OWSExceptions.RequestFailed("Request failed")
 
     def getMapObj(self,mapfilename=None):
 
@@ -284,14 +288,29 @@ def getService():
     qstring = os.environ["QUERY_STRING"]
     params = urlparse.parse_qs(qstring)
 
+    for p in params:
+        if p.lower() == "owsurl":
+            v = params[p]
+            del params[p]
+            params["owsUrl"] = v
+
+        if p.lower() == "owsservice":
+            v = params[p]
+            del params[p]
+            params["owsService"] = v
+
     
-    owsUrl = urllib.unquote(params["owsUrl"][0])
-
-    if params["owsService"][0].lower() == "wfs":
-        from wfs import WFS
-        return WFS(owsUrl,qstring)
-    elif params["owsService"][0].lower() == "wcs":
-        from wcs import WCS
-        return WCS(owsUrl,qstring)
+    if "owsUrl" in params.keys() and\
+        "owsService" in params.keys():
+        owsUrl = urllib.unquote(params["owsUrl"][0])
+
+        if params["owsService"][0].lower() == "wfs":
+            from wfs import WFS
+            return WFS(owsUrl,qstring)
+        elif params["owsService"][0].lower() == "wcs":
+            from wcs import WCS
+            return WCS(owsUrl,qstring)
+    else:
+        raise OWSExceptions.MissingParameterValue("""owsUrl or owsService""")
     
 

+ 24 - 0
OWSExceptions.py

@@ -0,0 +1,24 @@
+class OWSException(Exception):
+    pass
+
+class WMSException(OWSException):
+    def toXml(self):
+        print  """Content-type: text/xml\n"""
+
+        print """<?xml version="1.0" ?>
+        <ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wms/1.3.0/exceptions_1_3_0.xsd">
+        <ServiceException code="%s" locator="%s"><![CDATA[
+        %s
+        ]]></ServiceException>
+        </ServiceExceptionReport>""" % \
+                (self.__class__.__name__,self.message,self.args)
+
+class MissingParameterValue(WMSException):
+    pass
+
+class RequestFailed(WMSException):
+    pass
+class NoValidRequest(WMSException):
+    pass

+ 1 - 1
config.cfg-template

@@ -9,5 +9,5 @@ tempdir=/tmp/
 errorfile=stderr
 imagepath=/tmp/mapserv/
 onlineresource=http://localhost/cgi-bin/owsproxy.cgi
-srs=EPSG:4326 EPSG:102067 EPSG:900913 EPSG:3035
+srs=EPSG:4326 EPSG:102067 EPSG:900913 EPSG:3035 EPSG:3857 EPSG:900913
 

+ 7 - 5
owsproxy.cgi

@@ -3,12 +3,14 @@
 #debugger
 #import rpdb2; rpdb2.start_embedded_debugger("lucerna")
 
-import sys
-sys.path.append("/home/jachym/usr/src/hsrs/hslayers/trunk/source/scripts/hslframework/owsviewer/")
-
 import OWS
 import logging
+import OWSExceptions
+
 logging.basicConfig(level=logging.DEBUG)
 
-service = OWS.getService()
-service.performRequest()
+try:
+    service = OWS.getService()
+    service.performRequest()
+except OWSExceptions.OWSException,e:
+    e.toXml()

+ 51 - 2
wfs/__init__.py

@@ -9,6 +9,7 @@ import urllib
 import urlparse
 import logging
 from osgeo import ogr
+import pyproj
 import os
 import re
 
@@ -34,17 +35,27 @@ class WFS(OWS):
 
         self.setMapName(mapobj)
 
+        logging.debug(self.capabilities.contents)
+
+        srss = []
+
         for name in self.capabilities.contents:
+                
+            mapobj.setMetaData("wms_srs",self.config.get("MapServer","srs"))
+            mapobj.setMetaData("wfs_srs",self.config.get("MapServer","srs"))
             layer = self.capabilities.contents[name]
 
             logging.debug("Creating layer %s" % name)
 
+            srss = srss+filter(lambda y: not y in srss,layer.crsOptions)
+
             lyrobj = mapscript.layerObj(mapobj)
-            lyrobj.name = name.replace(":","_")
-            #lyrobj.name = name
+            #lyrobj.name = name.replace(":","_")
+            lyrobj.name = name
             lyrobj.title = layer.title
             if layer.title:
                 lyrobj.setMetaData("wms_title",layer.title)
+                lyrobj.setMetaData("wfs_title",layer.title)
             #if layer.abstract:
             #    lyrobj.setMetaData("ows_abstract",  layer.abstract)
             lyrobj.setMetaData("wfs_typename", name)
@@ -61,12 +72,15 @@ class WFS(OWS):
                 if extent:
                     lyrobj.setMetaData("wms_extent","%s %s %s %s" % \
                             (extent[0],extent[1],extent[2],extent[3]))
+                    lyrobj.setMetaData("wfs_extent","%s %s %s %s" % \
+                            (extent[0],extent[1],extent[2],extent[3]))
                 lyrobj.type = self._getLayerType(ogrLayer)
             else:
                 mapobj.removeLayer(mapobj.numlayers-1)
                 logging.debug("No ogrDataSource found")
                 continue
 
+            #lyrobj.setProjection(self.__getLayerCrs(layer.crsOptions))
             lyrobj.setProjection(layer.crsOptions[0])
 
             lyrobj.dump = mapscript.MS_TRUE 
@@ -78,6 +92,11 @@ class WFS(OWS):
             style.size=5
             style.width=5
 
+        ## overwrite already set SRSs
+        #if len(srss) > 0:
+        #    logging.debug("Overwriting SRS option")
+        #    mapobj.setMetaData("wms_srs"," ".join(srss))
+
         self.saveMapfile(mapobj,mapfilename)
         return mapobj
     
@@ -95,8 +114,10 @@ class WFS(OWS):
 
         if self.capabilities.identification.title:
             mapobj.setMetaData("wms_title",self.capabilities.identification.title)
+            mapobj.setMetaData("wfs_title",self.capabilities.identification.title)
         if self.capabilities.identification.abstract:
             mapobj.setMetaData("wms_abstract",self.capabilities.identification.abstract)
+            mapobj.setMetaData("wfs_abstract",self.capabilities.identification.abstract)
 
     def _getLayerType(self,layer):
         """Returns MS layer type based on ogr.Layer.GetGeomType
@@ -124,6 +145,13 @@ class WFS(OWS):
         """
 
         geomType = layer.GetGeomType()
+        if geomType == 0: # unknown
+            # brutal force way
+            f = layer.GetNextFeature()
+            if f:
+                gr = f.GetGeometryRef()
+                geomType = gr.GetGeometryType()
+
         if geomType in [ogr.wkbPolygon,
                         ogr.wkbMultiPolygon,
                         ogr.wkbLinearRing]:
@@ -133,3 +161,24 @@ class WFS(OWS):
                return mapscript.MS_LAYER_LINE
         else:
                return mapscript.MS_LAYER_POINT
+
+    def __getLayerCrs(self,crss):
+        """
+        Returns bests (non-degree) coordinate system of the layer, which is
+        available.
+
+        Ofcourse, sometimes, there is no other option, there EPSG:4326, but
+        take somethign else, if you can
+        """
+        for crs in crss:
+            try:
+                proj = pyproj.Proj("+init=%s"%crs)
+                if proj.is_latlon():
+                    continue
+                else:
+                    return crs
+            except:
+                pass
+        return crss[0]
+
+