diff RestDbGisApi.py @ 58:5ed0769f5ad3

more cleanup in kml generation points can be sorted now
author casties
date Thu, 21 Oct 2010 19:38:41 +0200
parents b47314bcff37
children a5f2550a5b44
line wrap: on
line diff
--- a/RestDbGisApi.py	Wed Oct 20 18:27:24 2010 +0200
+++ b/RestDbGisApi.py	Thu Oct 21 19:38:41 2010 +0200
@@ -184,7 +184,7 @@
         except:
             return qstr
 
-    def getKmlData(self, schema, table, ids=None, gisIdField=None, latField=None, lonField=None):
+    def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None):
         """returns data structure for KML template"""
         logging.debug("getKMLdata")
         # Mapping a set of points from table-based SQL-query:
@@ -201,7 +201,15 @@
             qstr += ','.join(['%s' for i in idList])
             qstr += ')'
 
-        data = self.executeSQL(qstr,idList)
+        if sortBy:
+            # add sort clause
+            qstr += " ORDER BY %s"
+            if idList is None:
+                idList = []
+            idList.append(sortBy)
+            data = self.executeSQL(qstr,idList)
+        else:
+            data = self.executeSQL(qstr,idList)
         
         fieldMap = self.getFieldNameMap(data['fields'])
         
@@ -236,7 +244,7 @@
         for dataset in data['rows']:
             if gisIdIdx is not None:
                 gisID = dataset[gisIdIdx]
-                coords=self.getPoint4GISid(gisID)
+                coords=self.getPointForChGisId(gisID)
                 if coords!=None:
                     xCoord=coords[0]
                     yCoord=coords[1]
@@ -263,7 +271,7 @@
             for value in dataset:
                 i += 1
                 name = data['fields'][i][0]
-                logging.debug("value=%s"%value)
+                #logging.debug("value=%s"%value)
                 if value != None:
                     #if name.find('name')>-1:
                     #    desc += "<name>%s</name>\n"%value
@@ -290,6 +298,34 @@
     
         #logging.debug("kmlData=%s"%(repr(kmlData)))
         return kmlData
+
+    def getPointForChGisId(self, gis_id):
+        """returns coordinate pair for given gis_id"""
+        if gis_id is None or gis_id == "":
+            return None
+        
+        sql="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id = %s"
+        res = self.executeSQL(sql, (gis_id,))
+        if len(res['rows']) > 0:
+            coords = res['rows'][0]
+            return coords
+        
+        else:
+            # try to clean gis_id...
+            gis_id = re.sub(r'[^0-9]','',gis_id)
+            # try again
+            sql="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id = %s"
+            res = self.executeSQL(sql, (gis_id,))
+            if len(res['rows']) > 0:
+                coords = res['rows'][0]
+                return coords
+            
+            else:
+                logging.error("CH-GIS ID %s not found!"%repr(gis_id))
+
+            # TODO: do we need the getCoordsFromREST_gisID stuff?
+
+        return None