changeset 59:a5f2550a5b44

more cleanup in kml generation sorting works better more parameters
author casties
date Fri, 22 Oct 2010 19:37:55 +0200
parents 5ed0769f5ad3
children 9fdadb60529f
files RestDbGisApi.py zpt/KML_schema_table.zpt
diffstat 2 files changed, 58 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbGisApi.py	Thu Oct 21 19:38:41 2010 +0200
+++ b/RestDbGisApi.py	Fri Oct 22 19:37:55 2010 +0200
@@ -125,7 +125,6 @@
                 name = sqlName(f['name'])
                 type = f['type']
                 sqltype = gisToSqlTypeMap[type]
-            
             else:
                 # name only
                 name = sqlName(f)
@@ -148,45 +147,24 @@
             return None
     
     
-    def getKmlUrl(self,schema='chgis',table='mpdl',id=[],doc=None):
-        logging.debug("getKmlUrl")
-        data = self.getDataForGoogleMap(schema,table,id,doc)
-        kml=self.getKMLname(data=data,table=table)
-        baseUrl = self.absolute_url()
-        return "%s/daten/%s"%(baseUrl,kml)
-
-    def getLiveKmlUrl(self,schema,table,REQUEST=None):
+    def getLiveKmlUrl(self,schema,table,useTimestamp=True,REQUEST=None):
         if REQUEST is None:
             REQUEST = self.REQUEST
         logging.debug("getLiveKmlUrl")
         baseUrl = self.absolute_url()
         timestamp = time.time()
+        # filter parameters in URL and add to new URL
         params = [p for p in REQUEST.form.items() if p[0] not in ('format','timestamp')]
         params.append(('format','KML'))
-        params.append(('timestamp',timestamp))
+        if useTimestamp:
+            # add timestamp so URL changes every time
+            params.append(('timestamp',timestamp))
         paramstr = urllib.urlencode(params)
         return "%s/db/%s/%s?%s"%(baseUrl,schema,table,paramstr)
 
-    def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=None,doc=None):
-        logging.debug("getDataForGoogleMap")
-        qstr="SELECT * FROM "+schema+"."+table
-        try:
-            if id is not None:
-                qstr=qstr+" WHERE "
-                for id_item in id.split(","):
-                    if table=='mpdl':
-                        qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
-                    else:
-                        qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
-                qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
-            data=self.ZSQLSimpleSearch(qstr)
-            return data
-        except:
-            return qstr
-
     def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None):
         """returns data structure for KML template"""
-        logging.debug("getKMLdata")
+        logging.debug("getKMLdata gid=%s lat=%s lon=%s"%(gisIdField,latField,lonField))
         # Mapping a set of points from table-based SQL-query:
         qstr='SELECT * FROM "%s"."%s"'%(schema,table)
         idList = None
@@ -203,13 +181,13 @@
 
         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)
+            if sortBy == 1:
+                qstr += ' ORDER BY 1'
+            else:
+                # TODO: proper quoting for names
+                qstr += ' ORDER BY "%s"'%sortBy.replace('"','')
+                
+        data = self.executeSQL(qstr,idList)
         
         fieldMap = self.getFieldNameMap(data['fields'])
         
@@ -301,32 +279,56 @@
 
     def getPointForChGisId(self, gis_id):
         """returns coordinate pair for given gis_id"""
+        def getPoint(id):
+            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:
+                return res['rows'][0]
+            return None
+        
         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 gis_id
+        coords = getPoint(gis_id)
+        if coords is None:
             # 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:
+            coords = getPoint(gis_id)
+            if coords is None:
                 logging.error("CH-GIS ID %s not found!"%repr(gis_id))
 
             # TODO: do we need the getCoordsFromREST_gisID stuff?
 
-        return None
-        
+        return coords
+
+
+    ## legacy methods...    
+
+    def getKmlUrl(self,schema='chgis',table='mpdl',id=[],doc=None):
+        logging.debug("getKmlUrl")
+        data = self.getDataForGoogleMap(schema,table,id,doc)
+        kml=self.getKMLname(data=data,table=table)
+        baseUrl = self.absolute_url()
+        return "%s/daten/%s"%(baseUrl,kml)
+
+    def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=None,doc=None):
+        logging.debug("getDataForGoogleMap")
+        qstr="SELECT * FROM "+schema+"."+table
+        try:
+            if id is not None:
+                qstr=qstr+" WHERE "
+                for id_item in id.split(","):
+                    if table=='mpdl':
+                        qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
+                    else:
+                        qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
+                qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
+            data=self.ZSQLSimpleSearch(qstr)
+            return data
+        except:
+            return qstr
 
 
     def getKMLname(self,data=[],table=""):
--- a/zpt/KML_schema_table.zpt	Thu Oct 21 19:38:41 2010 +0200
+++ b/zpt/KML_schema_table.zpt	Fri Oct 22 19:37:55 2010 +0200
@@ -1,6 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
 <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:tal="http://xml.zope.org/namespaces/tal" 
-  tal:define="schema options/schema; table options/table; data python:here.getKmlData(schema=schema,table=table); 
+  tal:define="gisIdField python:request.get('gis_id_field',None);
+  latField python:request.get('lat_field',None); lonField python:request.get('lon_field',None);
+  sortBy python:request.get('sort_field',1);
+  schema options/schema; table options/table; 
+  data python:here.getKmlData(schema=schema,table=table,sortBy=sortBy,gisIdField=gisIdField,latField=latField,lonField=lonField); 
   withLine python:request.get('connect_line',None);">
   <Document>
     <Style id="marker_icon">