From 5fd51896831bb1ee9f44a5c1b58e71d1ead682d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ma=C3=ABl=20M=C3=A9liani?= <m.meliani@neogeo-online.net>
Date: Wed, 5 Jun 2013 17:21:23 +0200
Subject: [PATCH] Changed method to get projection from a featureType or a
 coverage. To develop...

---
 src/mapfile.py |  9 +++++++++
 src/server.py  | 16 +++++++++-------
 src/stores.py  |  4 +---
 src/tools.py   | 23 +++++++++++++++++++++++
 4 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/src/mapfile.py b/src/mapfile.py
index 08a432d..3e4f893 100644
--- a/src/mapfile.py
+++ b/src/mapfile.py
@@ -301,6 +301,15 @@ class LayerModel(MetadataMixin):
                            mapscript.projectionObj('+init=epsg:4326'))
         return stores.Extent(rect.minx, rect.miny, rect.maxx, rect.maxy)
 
+    def get_proj4(self):
+        return self.ms.getProjection()
+
+    def get_wkt(self):
+        return tools.proj4_to_wkt(self.ms.getProjection())
+
+    def get_authority(self):
+        return tools.wkt_to_authority(self.get_wkt())
+
 
 class FeatureTypeModel(LayerModel):
     """
diff --git a/src/server.py b/src/server.py
index c04661a..be652bc 100755
--- a/src/server.py
+++ b/src/server.py
@@ -250,8 +250,8 @@ class featuretype(object):
                     "title": ft.get_mra_metadata("title", ft.name),
                     "abstract": ft.get_mra_metadata("abstract", None),
                     "keywords": ft.get_mra_metadata("keywords", []),
-                    "srs": dsft.get_projection(),
-                    "nativeCRS": dsft.get_native(),
+                    "srs": "%s:%s" % (ft.get_authority()[0], ft.get_authority()[1]),
+                    "nativeCRS": ft.get_wkt(),
                     "attributes": [{
                             "name": f.get_name(),
                             "minOccurs": 0 if f.is_nullable() else 1,
@@ -405,14 +405,15 @@ class coverage(object):
     @HTTPCompatible()
     def GET(self, map_name, ws_name, cs_name, c_name, format):
         mf, ws = get_mapfile_workspace(map_name, ws_name)
+
+        # with webapp.mightNotFound("coveragestore", workspace=ws_name):
+        #     cs = ws.get_coveragestore(cs_name)
+
         with webapp.mightNotFound("coverage", coveragestore=cs_name):
             c = ws.get_coveragemodel(c_name, cs_name)
 
-        with webapp.mightNotFound("coveragestore", workspace=ws_name):
-            cs = ws.get_coveragestore(cs_name)
-
         extent = c.get_extent()
-        latlon_extent = cs.get_latlon_extent()
+        latlon_extent = c.get_latlon_extent()
 
         return {"coverage": ({
                     "name": c.name,
@@ -424,7 +425,8 @@ class coverage(object):
                     "title": c.get_mra_metadata("title", c.name),
                     "abstract": c.get_mra_metadata("abstract", None),
                     "keywords": c.get_mra_metadata("keywords", []),
-                    "srs": cs.get_projection(),
+                    "srs": "%s:%s" % (c.get_authority()[0], c.get_authority()[1]),
+                    "nativeCRS": c.get_wkt(),
                     "nativeBoundingBox": {
                         "minx": extent.minX(),
                         "miny": extent.minY(),
diff --git a/src/stores.py b/src/stores.py
index 0000b7e..d96efea 100644
--- a/src/stores.py
+++ b/src/stores.py
@@ -404,9 +404,7 @@ class Coveragestore(object):
         return self.backend.GetProjection()
 
     def get_proj4(self):
-        proj = osr.SpatialReference()
-        proj.ImportFromWkt(self.get_projection())
-        return proj.ExportToProj4()
+        return tools.wkt_to_proj4(self.get_projection())
 
     def nbbands(self):
         return self.backend.RasterCount
diff --git a/src/tools.py b/src/tools.py
index 6e486cb..567f5e0 100644
--- a/src/tools.py
+++ b/src/tools.py
@@ -34,6 +34,7 @@ import pyxml
 import webapp
 
 import xml.etree.ElementTree as etree
+from osgeo import osr
 
 __config = None
 
@@ -156,4 +157,26 @@ def is_hidden(path):
     # MacOSX has at least four ways to hide files...
     return os.path.basename(path).startswith(".")
 
+def wkt_to_proj4(wkt):
+    srs = osr.SpatialReference()
+    srs.ImportFromWkt(wkt)
+    return srs.ExportToProj4()
+
+def proj4_to_wkt(proj4):
+    srs = osr.SpatialReference()
+    srs.ImportFromProj4(proj4)
+    return srs.ExportToWkt()
+
+def wkt_to_authority(wkt):
+    srs = osr.SpatialReference()
+    srs.ImportFromWkt(wkt)
+    
+    # Are there really no other with osgeo? Oo
+    
+    if srs.GetAuthorityCode('PROJCS') == None and srs.GetAuthorityCode('GEOGCS') != None :
+        return srs.GetAuthorityName('GEOGCS'), srs.GetAuthorityCode('GEOGCS')
+    if srs.GetAuthorityCode('PROJCS') != None:
+        return srs.GetAuthorityName('PROJCS'), srs.GetAuthorityCode('PROJCS')
+    else:
+        raise KeyError("Unable to get authority from %s" % wkt)
 
-- 
GitLab