# HG changeset patch # User fknauft # Date 1283358241 -7200 # Node ID b915bce653725bd0690605119fc9b3123253e560 # Parent b8922e13e7b59c11bfd81d57862abadff8362348 added kml url option diff -r b8922e13e7b5 -r b915bce65372 RestDbInterface.py --- a/RestDbInterface.py Wed Aug 11 15:16:36 2010 +0200 +++ b/RestDbInterface.py Wed Sep 01 18:24:01 2010 +0200 @@ -8,6 +8,7 @@ from Products.PageTemplates.PageTemplateFile import PageTemplateFile from Products.ZSQLExtend import ZSQLExtend import logging +import time from zope.interface import implements from zope.publisher.interfaces import IPublishTraverse @@ -53,6 +54,8 @@ self.connection_id = connection_id # create template folder self.manage_addFolder('template') + # create data folder + #self.manage_addFolder('daten') def getCursor(self): @@ -127,10 +130,17 @@ """index method""" # ReST path was stored in request path = REQUEST.get('restdb_path',[]) + # type and format are real parameter format = REQUEST.get('format','HTML').upper() type = REQUEST.get('type',None) + + # id and doc are used for GoogleMaps content + id = REQUEST.get('id',[]) + doc = REQUEST.get('doc',None) + logging.debug("index_html path=%s format=%s type=%s"%(path,format,type)) + RESPONSE.setHeader('Access-Control-Allow-Origin', '*') if type is not None: # non-empty type -- look for template @@ -145,6 +155,11 @@ # list of tables return self.showListOfTables(format=format,schema=path[1]) elif len(path) == 3: + # GIS + if format=="GIS": + return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc) + if format=="KML_URL": + return self.getKmlUrl(schema=path[1],table=path[2],id=id,doc=doc) # table return self.showTable(format=format,schema=path[1],table=path[2]) @@ -208,6 +223,195 @@ data={'fields': (('schemas',),), 'rows': [('public',),]} return data + # Methods for GoogleMaps creation + def showGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None): + logging.debug("showGoogleMap") + data = self.getDataForGoogleMap(schema,table,id,doc) + kmlFileName=self.getKMLname(data=data,table=table) + initializeStringForGoogleMaps="""onload=\"initialize(\'http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kmlFileName+"""\')\""""#+str(data) + initializeStringForGoogleMaps=initializeStringForGoogleMaps.replace("None","0") + googleMap_page=self.htmlHead()+str(self.getGoogleMapString(kml=initializeStringForGoogleMaps)) + return googleMap_page + + 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) + return """http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kml + + def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None): + logging.debug("getDataForGoogleMap") + qstr="SELECT * FROM "+schema+"."+table + try: + if id!=[]: + 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=""): + logging.debug("getKMLname") + #session=context.REQUEST.SESSION + kml4Marker="\n" + initializeStringForGoogleMaps="" + #doLine=container.getVar('doLine') + # 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) + 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" + + kml4Marker=kml4Marker+"
\n
" + kmlFileName="marker"+str(time.time())+".kml" + + # 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) + + return kmlFileName + + def getGoogleMapString(self,kml): + logging.debug("getGoogleMapString") + printed= ' '%kml +"""\n
\n " \n """ + return printed + + def getPoint4GISid(self,gis_id): + j=0 + coords=(0,0) + if gis_id != None: + while (True): + j=j+1 + if (j>100): # FJK: just to prevent endless loops + break + if (gis_id.isdigit()): # FJK: regular exit from while-loop + break + else: + gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters + gis_id=gis_id.strip() # FJK: to strip all whitespaces + resultpoint = [0,0] + results = None + try: + if int(gis_id)>0: + SQL="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE cast("+ str(gis_id) +" as text);" + results=self.ZSQLSimpleSearch(SQL) + #print results + if results != None: + for result in results: + resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))] + 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;" + returnstring=self.ZSQLSimpleSearch(SQL) + return coords[0] + except: + return "gis_id not to interpretable:"+str(gis_id) + else: + return coords[0] + + def getCoordsFromREST_gisID(self,gis_id): + 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 + return coordlist + +# End for GoogleMaps creation + + def RESTwrite2File(self,datadir, name,text): +# try: + fileid=name + if fileid in datadir.objectIds(): + datadir.manage_delObjects(fileid) + newfile=open(name,'w') + newfile.write(text) + newfile.close() + file4Read=open(name,'r') + fileInZope=datadir.manage_addFile(id=fileid,file=file4Read) + return "Write successful" +# except: +# return "Could not write" + def manage_editRestDbInterface(self, title=None, connection_id=None, REQUEST=None): @@ -237,3 +441,17 @@ REQUEST.RESPONSE.redirect('manage_main') + +# constructors = ( +# REST_test.manage_addRESTclassForm, +# REST_test.manage_addRESTclass +# ) + + +# self.registerClass( +# RestDbInterface.RestDbInterface, +# constructors = ( +# RestDbInterface.manage_addRestDbInterfaceForm, +# RestDbInterface.manage_addRestDbInterface +# ) +# )