changeset 261:5b38b50052e4

Display Polygons and Lines via chgis_id Reload button alternative changed to "R"
author fknauft
date Fri, 07 Oct 2011 15:58:07 +0200
parents e9a1ac7d2ab2
children 6613b9204bda
files RestDbGisApi.py gis_gui/blocks/template.pt
diffstat 2 files changed, 66 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbGisApi.py	Fri Oct 07 12:25:21 2011 +0200
+++ b/RestDbGisApi.py	Fri Oct 07 15:58:07 2011 +0200
@@ -193,25 +193,31 @@
                     bad_luck=False
             
         fieldMap = self.getFieldNameMap(data['fields'])
+        geomdata=None
         if (geomField!="point"):
-            try:
-                geomstr='select astext(st_simplify(transform(the_geom,4326),0.05)) from "%s"."%s"'%(schema,table)
-                geomdata=self.executeSQL(geomstr)
-                teststr=geomdata.values()[1][0]
-                if (teststr == (u'MULTIPOLYGON EMPTY',)):
-                    geomstr='select astext(st_simplify(transform(the_geom,2333),0.05)) from "%s"."%s"'%(schema,table)
+            # first check if the file is registered as geo-dataset (which then should have an attribute "the_geom")
+            requeststring="select f_geometry_column from geometry_columns where f_table_schema=%s and f_table_name=%s "
+            geocolumn_res=self.executeSQL(requeststring,(schema,table))
+            if len(geocolumn_res['rows'])>0:
+                geocolumn=geocolumn_res['rows'][0][0]
+                try:
+                    geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
                     geomdata=self.executeSQL(geomstr)
+                    teststr=geomdata.values()[1][0]
+                    if (teststr == (u'MULTIPOLYGON EMPTY',)):
+                        geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
+                        geomdata=self.executeSQL(geomstr)
+    
+                except:
+                    try:
+                        geomstr="select chgis.astext(chgis.st_simplify(chgis.transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
+                        geomdata=self.executeSQL(geomstr)                
+                    except:
+                        geomdata=None
 
-            except:
-                try:
-                    geomstr='select chgis.astext(chgis.st_simplify(chgis.transform(the_geom,4326),0.05)) from "%s"."%s"'%(schema,table)
-                    geomdata=self.executeSQL(geomstr)                
-                except:
-                    geomdata=None
-            
         if (gisIdField is None) and (latField is None or lonField is None) and geomField=='point':
             # no fields given - choose automagically
-            sql = 'SELECT field_name FROM public.gis_table_meta_rows WHERE table_name = %s and gis_type = %s'
+            sql = "SELECT field_name FROM public.gis_table_meta_rows WHERE table_name = %s and gis_type = %s"
             # gis id in metadata first
             res = self.executeSQL(sql, (table,'gis_id'))
             if len(res['rows']) > 0:
@@ -249,11 +255,12 @@
         # convert data
         kmlData = []
         geom_list = {}
-        try:
-            if geomField=='poly' or geomField=='line':
-                geom_list=geomdata.values()[1]
-        except:
-            return "no geomdata in RestDbGisApi Line 254"
+#        try:
+#            if geomField=='poly' or geomField=='line':
+#                geom_list=geomdata.values()[1]
+#        except:
+#            return "no geomdata in RestDbGisApi Line 254"
+
         data_list=data['rows']
         for k in range (len(data_list)):
             dataset = data_list[k]
@@ -261,7 +268,10 @@
                     geom_value = geom_list[k]
         
         
-        
+            if gisIdIdx != None and geomField!='point':
+                gisID = dataset[gisIdIdx]
+                geomdata = self.getLineForChGisId(gisID)
+
   #      for dataset in data['rows']:
             xCoord = 0.0
             yCoord = 0.0
@@ -345,8 +355,8 @@
                 kmlPlace['coord_y'] = str(yCoord)
                 kmlPlace['coord_z'] = '50'
                 kmlData.append(kmlPlace)
-            if (geomField=='poly' or geomField=='line') and len(geomdata)>0:
-                polys=str(geom_value).split('(')
+            if (geomField=='poly' or geomField=='line') and geomdata is not None:
+                polys=str(geomdata).split('(')
                 aaa=len(polys)
                 for poly in polys:
                     kmlPlace = {}
@@ -385,7 +395,7 @@
         
         if gis_id is None or gis_id == "":
             return None
-        if len(gis_id) < 4:
+        if len(str(gis_id)) < 4:
             return None
         
         # try gis_id
@@ -424,6 +434,38 @@
         return coords
 
 
+    def getLineForChGisId(self, gis_id):
+        """returns line/poly coordinates for given gis_id"""   # gets called by getKml
+        def getLine(id):
+            str_gis_id=str(id).split('.')[0]
+            sql="SELECT astext(st_simplify(transform(the_geom,4326),0.05))  FROM chgis.chgis_linesandpolys WHERE gis_id LIKE CAST('%s' AS text)"%(str_gis_id)
+  #          logging.error("sql:",sql)
+  #           res = self.executeSQL(sql, (str(str_gis_id),))
+            res = self.executeSQL(sql)
+            if len(res['rows']) > 0:
+                return res['rows'][0]
+            else:
+                #logging.error("error on sql:",sql%(str(str_gis_id),))
+                return None
+        
+        if gis_id is None or gis_id == "":
+            return None
+        if len(str(gis_id)) < 4:
+            return None
+        
+        # try gis_id
+        line_coords = getLine(gis_id)
+        if line_coords is None:
+            # try to clean gis_id...
+            gis_id_short = re.sub(r'[^0-9]','',gis_id)
+            # try again
+            line_coords = getLine(gis_id_short)
+            if line_coords is None:
+                logging.error("CH-GIS ID %s not found!"%(gis_id_short))
+                
+
+        return line_coords
+
     ## legacy methods...    
 
     def getKmlUrl(self,schema='chgis',table='mpdl',args={'doc':None,'id':None}):
--- a/gis_gui/blocks/template.pt	Fri Oct 07 12:25:21 2011 +0200
+++ b/gis_gui/blocks/template.pt	Fri Oct 07 15:58:07 2011 +0200
@@ -10,7 +10,7 @@
       <tal:block tal:condition="python:impermanent=='true'">
       	<div class="close_button"> x </div>
       	<!-- FJK: das soll ein Reload-Button werden -->
-        <div class="reload_button" ><img src="./icons/reload.png" alt="Reload" style="width: 20px;"></div>
+        <div class="reload_button" ><img src="./icons/reload.png" alt="R" style="width: 20px;"></div>
       </tal:block>
       <div class="type_icon"> <tal:block metal:define-slot="icon"></tal:block> </div>
     </div>