changeset 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
files RestDbGisApi.py RestDbInterface.py
diffstat 2 files changed, 47 insertions(+), 8 deletions(-) [+]
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
         
 
 
--- a/RestDbInterface.py	Wed Oct 20 18:27:24 2010 +0200
+++ b/RestDbInterface.py	Thu Oct 21 19:38:41 2010 +0200
@@ -179,7 +179,7 @@
             data = cur.fetchall()
             cur.close()
             #logging.debug("fields: %s"%repr(fields))
-            logging.debug("rows: %s"%repr(data))
+            #logging.debug("rows: %s"%repr(data))
             return {"fields":fields, "rows":data}
         else:
             cur.close()
@@ -316,10 +316,13 @@
         #data = self.getTable(schema,table)
         return pt(schema=schema,table=table)
  
-    def getTable(self,schema='public',table=None,username='guest'):
+    def getTable(self,schema='public',table=None,sortBy=1,username='guest'):
         """return table data"""
         logging.debug("gettable")
-        data = self.executeSQL('select * from "%s"."%s"'%(schema,table))
+        if sortBy:
+            data = self.executeSQL('select * from "%s"."%s" order by %s'%(schema,table,sortBy))
+        else:
+            data = self.executeSQL('select * from "%s"."%s"'%(schema,table))
         return data
 
     def hasTable(self,schema='public',table=None,username='guest'):
@@ -351,7 +354,7 @@
         qstr="""SELECT c.relname AS tablename FROM pg_catalog.pg_class c
             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
             WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
-            AND pg_catalog.pg_table_is_visible(c.oid)"""
+            AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1"""
         #qstr="select attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) from pg_attribute, pg_class where attrelid = pg_class.oid and pg_attribute.attnum > 0"
         data=self.executeSQL(qstr)
         return data