changeset 38:f6d9a3caf986

fixed errors from merging fixed kml generation with local url setting metadata works for gis_id
author casties
date Wed, 01 Sep 2010 19:58:16 +0200
parents b356b86773a1
children 0c769ba7957c fbd3ce67c821
files RestDbInterface.py
diffstat 1 files changed, 76 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbInterface.py	Wed Sep 01 18:47:06 2010 +0200
+++ b/RestDbInterface.py	Wed Sep 01 19:58:16 2010 +0200
@@ -188,9 +188,16 @@
             elif t == 'coord_lon':
                 lonField = f['name']
                 
-        res = self.executeSQL("select * from metadata where tablename=%s", table)
-        #if len(res["rows"]) > 0:
-        # TODO: what now?
+        res = self.executeSQL("select * from public.metadata where tablename=%s", (table,))
+        if len(res['rows']) > 0:
+            # meta record exists
+            if gisIdField is not None:
+                self.executeSQL('update public.metadata set "attribute with gis_id" = %s where tablename = %s', (gisIdField,table), hasResult=False)
+                
+        else:
+            # new meta record
+            if gisIdField is not None:
+                self.executeSQL('insert into public.metadata ("tablename", "attribute with gis_id") values (%s, %s)', (table,gisIdField), hasResult=False)
     
     def publishTraverse(self,request,name):
         """change the traversal"""
@@ -305,15 +312,25 @@
     def showTable(self,resultFormat='XML',schema='public',table=None,REQUEST=None,RESPONSE=None):
         """returns PageTemplate with tables"""
         logging.debug("showtable")
+        if REQUEST is None:
+            REQUEST = self.REQUEST
+            
         # should be cross-site accessible 
         if RESPONSE is None:
             RESPONSE = self.REQUEST.RESPONSE
+            
         RESPONSE.setHeader('Access-Control-Allow-Origin', '*')
+        
         # GIS gets special treatment
         if resultFormat=="GIS":
             id = REQUEST.get('id',[])           
             doc = REQUEST.get('doc',None)
-            return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc)
+            return self.showGoogleMap(schema=schema,table=table,id=id,doc=doc)
+        
+        elif resultFormat=="KML_URL":
+            id = REQUEST.get('id',[])           
+            doc = REQUEST.get('doc',None)
+            return self.getKmlUrl(schema=schema,table=table,id=id,doc=doc)
 
         # everything else has its own template
         pt = getattr(self.template, '%s_schema_table'%resultFormat, None)
@@ -580,7 +597,8 @@
         logging.debug("getKmlUrl")
         data = self.getDataForGoogleMap(schema,table,id,doc)
         kml=self.getKMLname(data=data,table=table)
-        return """http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kml
+        baseUrl = self.absolute_url()
+        return "%s/daten/%s"%(baseUrl,kml)
 
     def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None):
         logging.debug("getDataForGoogleMap")
@@ -608,66 +626,68 @@
         # Mapping a set of points from table-based SQL-query:
         if data!=None:
             try:
-             SQL="""SELECT \"attribute with gis_id\" FROM public.metadata WHERE tablename LIKE '"""+table+"""'"""
-             gisIDattribute=self.ZSQLSimpleSearch(SQL)
+                SQL="""SELECT \"attribute with gis_id\" FROM public.metadata WHERE tablename LIKE '"""+table+"""'"""
+                gisIDattribute=self.ZSQLSimpleSearch(SQL)
             except:
                 return "table not registered within metadata"
+            
             for dataset in data:
-              try:  
-                  xCoord=getattr(dataset,'longitude')
-                  yCoord=getattr(dataset,'latitude')
-              except:
-                  try:
-                      xCoord=getattr(dataset,'x_coord')
-                      yCoord=getattr(dataset,'y_coord')
-                  except:
-#                     try:
-                         gisID=getattr(dataset,getattr(gisIDattribute[0],'attribute with gis_id'))
-                         coords=self.getPoint4GISid(gisID)
-                         if coords!=None:
-                             xCoord=coords[0]
-                             yCoord=coords[1]
-#                     except:
-#                         return "no coordinates found"
-              if float(xCoord)!=0:
-                 if float(yCoord)!=0:
-                    kml4Marker=kml4Marker+"<Placemark>"
-                    kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
-                    for values in dataset:
-                      if values != (None, None):
-                        if str(values).find('name')>-1:
-                          kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
-                          continue
-                        elif str(values).find('place')>-1:
-                          kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
-                          continue
-                
-                        kml4Marker=kml4Marker+str(values)+": "
-                        attribute_string=str(values).replace("'","__Apostroph__")
-                        attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
-                        attribute_string=str(attribute_string).replace(';','__$$__')
-                        attribute_string=str(attribute_string).replace('&','&amp;')
-                        if str(attribute_string).find('http')>-1:
-                          attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>'
-                        kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
-                          
-                    kml4Marker=kml4Marker+"]]></description>\n"
-                    kml4Marker=kml4Marker+"<styleURL>#marker_icon</styleURL>\n"
-                    kml4Marker=kml4Marker+"<Point>"
-                          
-                    kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
-                    kml4Marker=kml4Marker+"</Point>\n"
-                    kml4Marker=kml4Marker+"</Placemark>\n"
+                try:  
+                    xCoord=getattr(dataset,'longitude')
+                    yCoord=getattr(dataset,'latitude')
+                except:
+                    try:
+                        xCoord=getattr(dataset,'x_coord')
+                        yCoord=getattr(dataset,'y_coord')
+                    except:
+                        #try:
+                        gisID=getattr(dataset,getattr(gisIDattribute[0],'attribute with gis_id'))
+                        coords=self.getPoint4GISid(gisID)
+                        if coords!=None:
+                            xCoord=coords[0]
+                            yCoord=coords[1]
+                        # except:
+                        #     return "no coordinates found"
+                        
+                if float(xCoord)!=0:
+                    if float(yCoord)!=0:
+                        kml4Marker=kml4Marker+"<Placemark>"
+                        kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
+                        for values in dataset:
+                            if values != (None, None):
+                                if str(values).find('name')>-1:
+                                    kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
+                                    continue
+                                elif str(values).find('place')>-1:
+                                    kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
+                                    continue
+                        
+                                kml4Marker=kml4Marker+str(values)+": "
+                                attribute_string=str(values).replace("'","__Apostroph__")
+                                attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
+                                attribute_string=str(attribute_string).replace(';','__$$__')
+                                attribute_string=str(attribute_string).replace('&','&amp;')
+                                if str(attribute_string).find('http')>-1:
+                                    attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>'
+                                kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
+                                  
+                        kml4Marker=kml4Marker+"]]></description>\n"
+                        kml4Marker=kml4Marker+"<styleURL>#marker_icon</styleURL>\n"
+                        kml4Marker=kml4Marker+"<Point>"
+                              
+                        kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
+                        kml4Marker=kml4Marker+"</Point>\n"
+                        kml4Marker=kml4Marker+"</Placemark>\n"
         
             kml4Marker=kml4Marker+"</Document>\n</kml>"
             kmlFileName="marker"+str(time.time())+".kml"
         
-        #    kml4Marker=str(kml4Marker).replace('&','$$')
-        #    kml4Marker=str(kml4Marker).replace(';','__$$__')
-        #    kml4Marker=str(kml4Marker).replace('#','__SHARP__')
+            #kml4Marker=str(kml4Marker).replace('&','$$')
+            #kml4Marker=str(kml4Marker).replace(';','__$$__')
+            #kml4Marker=str(kml4Marker).replace('#','__SHARP__')
             isLoadReady='false'
             while isLoadReady=='false':
-              isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
+                isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
 
         return kmlFileName