# HG changeset patch
# User casties
# Date 1283363896 -7200
# Node ID f6d9a3caf986700a0a06babb042c0f69b09ef33e
# Parent b356b86773a16891e0f26df55281f4de56ffa709
fixed errors from merging
fixed kml generation with local url
setting metadata works for gis_id
diff -r b356b86773a1 -r f6d9a3caf986 RestDbInterface.py
--- 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+""
- kml4Marker=kml4Marker+" "
- for values in dataset:
- if values != (None, None):
- if str(values).find('name')>-1:
- kml4Marker=kml4Marker+""+str(values[1])+"\n"
- continue
- elif str(values).find('place')>-1:
- kml4Marker=kml4Marker+""+str(values[1])+"\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('&','&')
- if str(attribute_string).find('http')>-1:
- attribute_string='' + str(attribute_string) + ''
- kml4Marker=kml4Marker+attribute_string+"
\n"
-
- kml4Marker=kml4Marker+"]]>\n"
- kml4Marker=kml4Marker+"#marker_icon\n"
- kml4Marker=kml4Marker+""
-
- kml4Marker=kml4Marker+""+str(xCoord)+","+str(yCoord)+",0\n"
- kml4Marker=kml4Marker+"\n"
- kml4Marker=kml4Marker+"\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+""
+ kml4Marker=kml4Marker+" "
+ for values in dataset:
+ if values != (None, None):
+ if str(values).find('name')>-1:
+ kml4Marker=kml4Marker+""+str(values[1])+"\n"
+ continue
+ elif str(values).find('place')>-1:
+ kml4Marker=kml4Marker+""+str(values[1])+"\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('&','&')
+ if str(attribute_string).find('http')>-1:
+ attribute_string='' + str(attribute_string) + ''
+ kml4Marker=kml4Marker+attribute_string+"
\n"
+
+ kml4Marker=kml4Marker+"]]>\n"
+ kml4Marker=kml4Marker+"#marker_icon\n"
+ kml4Marker=kml4Marker+""
+
+ kml4Marker=kml4Marker+""+str(xCoord)+","+str(yCoord)+",0\n"
+ kml4Marker=kml4Marker+"\n"
+ kml4Marker=kml4Marker+"\n"
kml4Marker=kml4Marker+"\n"
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