changeset 247:47bba001d718

show line and polygon-layer
author fknauft
date Wed, 14 Sep 2011 10:56:00 +0200
parents 83b5d3215c19
children fcb7876178d1
files RestDbGisApi.py urlFunctions.py
diffstat 2 files changed, 121 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbGisApi.py	Thu Sep 08 13:40:13 2011 +0200
+++ b/RestDbGisApi.py	Wed Sep 14 10:56:00 2011 +0200
@@ -12,6 +12,7 @@
 import time
 import datetime
 import urllib
+import urlFunctions
 
 from RestDbInterface import *
 
@@ -204,20 +205,21 @@
                     bad_luck=False
             
         fieldMap = self.getFieldNameMap(data['fields'])
-        try:
-            geomstr='select astext(st_simplify(transform(the_geom,4326),0.02)) 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.02)) from "%s"."%s"'%(schema,table)
+        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)
+                    geomdata=self.executeSQL(geomstr)
 
-        except:
-            try:
-                geomstr='select chgis.astext(chgis.st_simplify(chgis.transform(the_geom,4326),0.02)) from "%s"."%s"'%(schema,table)
-                geomdata=self.executeSQL(geomstr)                
             except:
-                geomdata=None
+                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
@@ -380,12 +382,16 @@
     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 = CAST(%s AS text)"
-            str_gis_id=str(gis_id).split('.')[0]
-            res = self.executeSQL(sql, (str_gis_id,))
+            str_gis_id=str(id).split('.')[0]
+            sql="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE CAST('%s' AS text)"%str(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]
-            return None
+            else:
+                #logging.error("error on sql:",sql%(str(str_gis_id),))
+                return None
         
         if gis_id is None or gis_id == "":
             return None
@@ -394,13 +400,34 @@
         coords = getPoint(gis_id)
         if coords is None:
             # try to clean gis_id...
-            gis_id = re.sub(r'[^0-9]','',gis_id)
+            gis_id_short = re.sub(r'[^0-9]','',gis_id)
             # try again
-            coords = getPoint(gis_id)
+            coords = getPoint(gis_id_short)
             if coords is None:
-                logging.error("CH-GIS ID %s not found!"%repr(gis_id))
-
-            # TODO: do we need the getCoordsFromREST_gisID stuff?
+                #logging.error("CH-GIS ID %s not found!"%str(gis_id_short))
+                
+                # this will ask the Harvard-Service for the Coords of this gis_id and write it into our coords-list
+                try:
+                    coords=self.getCoordsFromREST_gisID(gis_id)
+                    #logging.error("coords from REST"%str(coords))
+                except:
+                    logging.error("coords from REST did not work for "%str(gis_id))
+                if coords[0] is None:
+                    logging.error("CH-GIS ID %s not found in Harvard"%str(gis_id))
+                else:
+                    try:
+                        SQL="INSERT INTO chgis.chgis_coords (gis_id,x_coord,y_coord) VALUES (CAST(%s AS text),CAST(%s AS numeric),CAST(%s AS numeric))"
+                        SQL_analyze="ANALYZE chgis.chgis_coords"
+                        self.executeSQL(SQL,(str(gis_id_short),str(coords[0][1]),str(coords[0][0])),False)
+                        self.executeSQL(SQL_analyze,(),False)
+                    except:
+                        logging.error("Could not write into chgis_coords: ", str(gis_id_short))
+                        logging.error("coords[0][0]:"%coords[0][0])
+                        logging.error("coords[0][1]:"%coords[0][1])
+                        return coords[0]
+                    coords_test = getPoint(gis_id)
+                    #logging.error("SQL results now:", str(coords_test))
+                    return coords[0]
 
         return coords
 
@@ -550,8 +577,8 @@
                 if resultpoint !=[0,0]: 
                     return resultpoint
             else:
-                coords=self.getCoordsFromREST_gisID(joinid)
-                SQL="INSERT INTO chgis.chgis_coords (gis_id,x_coord,y_coord) VALUES (" +gis_id+ "," +coords[0][1]+ "," +coords[0][0]+ "); ANALYZE chgis.chgis_coords;"
+                coords=self.getCoordsFromREST_gisID(gis_id)
+                SQL="INSERT INTO chgis.chgis_coords (chgis_coords_pkey,gis_id,x_coord,y_coord) VALUES (default," +gis_id+ "," +coords[0][1]+ "," +coords[0][0]+ "); ANALYZE chgis.chgis_coords;"
                 returnstring=self.ZSQLSimpleSearch(SQL)
                 return coords[0]
          except:
@@ -560,31 +587,33 @@
             return coords[0]
 
     def getCoordsFromREST_gisID(self,gis_id):
+        gis_id=gis_id.strip()
         coordlist=[]
         i=0
         while (i<5 and coordlist==[]):
-         
-            urlresponse=container.urlFunctions.zUrlopenParseString(container.urlFunctions.zUrlopenRead("http://chgis.hmdc.harvard.edu/xml/id/"+gis_id))
-            baseDocElement=container.urlFunctions.zUrlopenDocumentElement(urlresponse)
-            childnodes=container.urlFunctions.zUrlopenChildNodes(baseDocElement)
-            itemnodes=container.urlFunctions.zUrlopenGetElementsByTagName(baseDocElement,'item')
-        
-            for i in range(0,container.urlFunctions.zUrlopenLength(itemnodes)):
-                itemnode=container.urlFunctions.zUrlopenGetItem(itemnodes,i)
-                itemspatialnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemnode,'spatial')
-            for j in range(0,container.urlFunctions.zUrlopenLength(itemspatialnodes)):
-                coord=[]
-                itemspatialnode= container.urlFunctions.zUrlopenGetItem(itemspatialnodes,j)
-                itemspatiallatnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_latitude')
-                for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallatnodes)):
-                    itemspatiallatnode= container.urlFunctions.zUrlopenGetItem(itemspatiallatnodes,k)
-                    coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallatnode))
-            itemspatiallngnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_longitude')
-            for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallngnodes)):
-                itemspatiallngnode= container.urlFunctions.zUrlopenGetItem(itemspatiallngnodes,k)
-                coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallngnode))
-            coordlist.append(coord)
-            gis_id= "_"+gis_id
+            urlresponse=urlFunctions.zUrlopenParseString(self,urlFunctions.zUrlopenRead(self,"http://chgis.hmdc.harvard.edu/xml/id/"+gis_id))
+            baseDocElement= urlFunctions.zUrlopenDocumentElement(self,urlresponse)
+            childnodes=urlFunctions.zUrlopenChildNodes(self,baseDocElement)
+            itemnodes=urlFunctions.zUrlopenGetElementsByTagName(self,baseDocElement,'item')
+            itemspatialnodes=None
+            
+            for i in range(0,urlFunctions.zUrlopenLength(self,itemnodes)):
+                itemnode=urlFunctions.zUrlopenGetItem(self,itemnodes,i)
+                itemspatialnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemnode,'spatial')
+            if itemspatialnodes is not None:
+                for j in range(0,urlFunctions.zUrlopenLength(self,itemspatialnodes)):
+                    coord=[]
+                    itemspatialnode= urlFunctions.zUrlopenGetItem(self,itemspatialnodes,j)
+                    itemspatiallatnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemspatialnode,'degrees_latitude')
+                    for k in range(0,urlFunctions.zUrlopenLength(self,itemspatiallatnodes)):
+                        itemspatiallatnode= urlFunctions.zUrlopenGetItem(self,itemspatiallatnodes,k)
+                        coord.append(urlFunctions.zUrlopenGetTextData(self,itemspatiallatnode))
+                itemspatiallngnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemspatialnode,'degrees_longitude')
+                for k in range(0,urlFunctions.zUrlopenLength(self,itemspatiallngnodes)):
+                    itemspatiallngnode= urlFunctions.zUrlopenGetItem(self,itemspatiallngnodes,k)
+                    coord.append(urlFunctions.zUrlopenGetTextData(self,itemspatiallngnode))
+                coordlist.append(coord)
+                gis_id= "_"+gis_id
         return coordlist
        
 # End for GoogleMaps creation
@@ -631,4 +660,6 @@
         #checkPermission=getSecurityManager().checkPermission
         REQUEST.RESPONSE.redirect('manage_main')
 
-        
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/urlFunctions.py	Wed Sep 14 10:56:00 2011 +0200
@@ -0,0 +1,45 @@
+import xml.dom.minidom 
+#from xml.dom import utils
+from xml.dom.minidom import parseString
+import urllib
+
+def zUrlopenRead(self,url):
+    return urllib.urlopen(url).read()
+  
+def zUrlopenInfo(self,url):
+    return urllib.urlopen(url).info()
+    
+def zUrlopenParseString(self,string):
+    return parseString(string)
+
+def zUrlopenDocumentElement(self,doc):
+    return doc.documentElement
+
+def zUrlopenChildNodes(self,docnode):
+    return docnode.childNodes
+
+def zUrlopenGetElementsByTagName(self,docnodelist,name):
+    return docnodelist.getElementsByTagName(name)
+
+def zUrlopenLength(self,docnodelist):
+    return docnodelist.length
+
+def zUrlopenGetItem(self,docnodelist,number):
+    return docnodelist.item(number)
+    
+def zUrlopenGetAttribute(self,docnode,name):
+    return docnode.getAttribute(name)
+
+def zUrlopenGetTextData(self,docnode):
+#    return docnode.nodeName
+    return getText(docnode.childNodes)
+
+def getText(nodelist):
+    rc = ""
+    for node in nodelist:
+        if node.nodeType == node.TEXT_NODE:
+            rc = rc + node.data
+    return rc
+
+
+