Mercurial > hg > ChinaGisRestApi
comparison RestDbInterface.py @ 36:b915bce65372
added kml url option
author | fknauft |
---|---|
date | Wed, 01 Sep 2010 18:24:01 +0200 |
parents | 76ac7a721273 |
children | b356b86773a1 |
comparison
equal
deleted
inserted
replaced
11:b8922e13e7b5 | 36:b915bce65372 |
---|---|
6 | 6 |
7 from OFS.Folder import Folder | 7 from OFS.Folder import Folder |
8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile | 8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
9 from Products.ZSQLExtend import ZSQLExtend | 9 from Products.ZSQLExtend import ZSQLExtend |
10 import logging | 10 import logging |
11 import time | |
11 | 12 |
12 from zope.interface import implements | 13 from zope.interface import implements |
13 from zope.publisher.interfaces import IPublishTraverse | 14 from zope.publisher.interfaces import IPublishTraverse |
14 from ZPublisher.BaseRequest import DefaultPublishTraverse | 15 from ZPublisher.BaseRequest import DefaultPublishTraverse |
15 #from zope.publisher.interfaces import NotFound | 16 #from zope.publisher.interfaces import NotFound |
51 self.title = title | 52 self.title = title |
52 # database connection id | 53 # database connection id |
53 self.connection_id = connection_id | 54 self.connection_id = connection_id |
54 # create template folder | 55 # create template folder |
55 self.manage_addFolder('template') | 56 self.manage_addFolder('template') |
57 # create data folder | |
58 #self.manage_addFolder('daten') | |
56 | 59 |
57 | 60 |
58 def getCursor(self): | 61 def getCursor(self): |
59 """returns fresh DB cursor""" | 62 """returns fresh DB cursor""" |
60 conn = getattr(self,"_v_database_connection",None) | 63 conn = getattr(self,"_v_database_connection",None) |
125 | 128 |
126 def index_html(self,REQUEST,RESPONSE): | 129 def index_html(self,REQUEST,RESPONSE): |
127 """index method""" | 130 """index method""" |
128 # ReST path was stored in request | 131 # ReST path was stored in request |
129 path = REQUEST.get('restdb_path',[]) | 132 path = REQUEST.get('restdb_path',[]) |
133 | |
130 # type and format are real parameter | 134 # type and format are real parameter |
131 format = REQUEST.get('format','HTML').upper() | 135 format = REQUEST.get('format','HTML').upper() |
132 type = REQUEST.get('type',None) | 136 type = REQUEST.get('type',None) |
137 | |
138 # id and doc are used for GoogleMaps content | |
139 id = REQUEST.get('id',[]) | |
140 doc = REQUEST.get('doc',None) | |
141 | |
133 logging.debug("index_html path=%s format=%s type=%s"%(path,format,type)) | 142 logging.debug("index_html path=%s format=%s type=%s"%(path,format,type)) |
143 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') | |
134 | 144 |
135 if type is not None: | 145 if type is not None: |
136 # non-empty type -- look for template | 146 # non-empty type -- look for template |
137 pt = getattr(self.template, "%s_%s"%(format,type), None) | 147 pt = getattr(self.template, "%s_%s"%(format,type), None) |
138 if pt is not None: | 148 if pt is not None: |
143 return self.showListOfSchemas(format=format) | 153 return self.showListOfSchemas(format=format) |
144 elif len(path) == 2: | 154 elif len(path) == 2: |
145 # list of tables | 155 # list of tables |
146 return self.showListOfTables(format=format,schema=path[1]) | 156 return self.showListOfTables(format=format,schema=path[1]) |
147 elif len(path) == 3: | 157 elif len(path) == 3: |
158 # GIS | |
159 if format=="GIS": | |
160 return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc) | |
161 if format=="KML_URL": | |
162 return self.getKmlUrl(schema=path[1],table=path[2],id=id,doc=doc) | |
148 # table | 163 # table |
149 return self.showTable(format=format,schema=path[1],table=path[2]) | 164 return self.showTable(format=format,schema=path[1],table=path[2]) |
150 | 165 |
151 # don't know what to do | 166 # don't know what to do |
152 return str(REQUEST) | 167 return str(REQUEST) |
206 logging.debug("getlistofschemas") | 221 logging.debug("getlistofschemas") |
207 # TODO: really look up schemas | 222 # TODO: really look up schemas |
208 data={'fields': (('schemas',),), 'rows': [('public',),]} | 223 data={'fields': (('schemas',),), 'rows': [('public',),]} |
209 return data | 224 return data |
210 | 225 |
226 # Methods for GoogleMaps creation | |
227 def showGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None): | |
228 logging.debug("showGoogleMap") | |
229 data = self.getDataForGoogleMap(schema,table,id,doc) | |
230 kmlFileName=self.getKMLname(data=data,table=table) | |
231 initializeStringForGoogleMaps="""onload=\"initialize(\'http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kmlFileName+"""\')\""""#+str(data) | |
232 initializeStringForGoogleMaps=initializeStringForGoogleMaps.replace("None","0") | |
233 googleMap_page=self.htmlHead()+str(self.getGoogleMapString(kml=initializeStringForGoogleMaps)) | |
234 return googleMap_page | |
235 | |
236 def getKmlUrl(self,schema='chgis',table='mpdl',id=[],doc=None): | |
237 logging.debug("getKmlUrl") | |
238 data = self.getDataForGoogleMap(schema,table,id,doc) | |
239 kml=self.getKMLname(data=data,table=table) | |
240 return """http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kml | |
241 | |
242 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None): | |
243 logging.debug("getDataForGoogleMap") | |
244 qstr="SELECT * FROM "+schema+"."+table | |
245 try: | |
246 if id!=[]: | |
247 qstr=qstr+" WHERE " | |
248 for id_item in id.split(","): | |
249 if table=='mpdl': | |
250 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR" | |
251 else: | |
252 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR" | |
253 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and " | |
254 data=self.ZSQLSimpleSearch(qstr) | |
255 return data | |
256 except: | |
257 return qstr | |
258 | |
259 def getKMLname(self,data=[],table=""): | |
260 logging.debug("getKMLname") | |
261 #session=context.REQUEST.SESSION | |
262 kml4Marker="<kml xmlns=\'http://www.opengis.net/kml/2.2\'><Document><Style id=\'marker_icon\'><IconStyle><scale>15</scale><Icon><href>http://chinagis.mpiwg-berlin.mpg.de/chinagis/images/dot_red.png</href></Icon></IconStyle></Style>\n" | |
263 initializeStringForGoogleMaps="" | |
264 #doLine=container.getVar('doLine') | |
265 # Mapping a set of points from table-based SQL-query: | |
266 if data!=None: | |
267 try: | |
268 SQL="""SELECT \"attribute with gis_id\" FROM public.metadata WHERE tablename LIKE '"""+table+"""'""" | |
269 gisIDattribute=self.ZSQLSimpleSearch(SQL) | |
270 except: | |
271 return "table not registered within metadata" | |
272 for dataset in data: | |
273 try: | |
274 xCoord=getattr(dataset,'longitude') | |
275 yCoord=getattr(dataset,'latitude') | |
276 except: | |
277 try: | |
278 xCoord=getattr(dataset,'x_coord') | |
279 yCoord=getattr(dataset,'y_coord') | |
280 except: | |
281 # try: | |
282 gisID=getattr(dataset,getattr(gisIDattribute[0],'attribute with gis_id')) | |
283 coords=self.getPoint4GISid(gisID) | |
284 if coords!=None: | |
285 xCoord=coords[0] | |
286 yCoord=coords[1] | |
287 # except: | |
288 # return "no coordinates found" | |
289 if float(xCoord)!=0: | |
290 if float(yCoord)!=0: | |
291 kml4Marker=kml4Marker+"<Placemark>" | |
292 kml4Marker=kml4Marker+"<description> <![CDATA[<b>" | |
293 for values in dataset: | |
294 if values != (None, None): | |
295 if str(values).find('name')>-1: | |
296 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n" | |
297 continue | |
298 elif str(values).find('place')>-1: | |
299 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n" | |
300 continue | |
301 | |
302 kml4Marker=kml4Marker+str(values)+": " | |
303 attribute_string=str(values).replace("'","__Apostroph__") | |
304 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__') | |
305 attribute_string=str(attribute_string).replace(';','__$$__') | |
306 attribute_string=str(attribute_string).replace('&','&') | |
307 if str(attribute_string).find('http')>-1: | |
308 attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>' | |
309 kml4Marker=kml4Marker+attribute_string+"</a><br>\n" | |
310 | |
311 kml4Marker=kml4Marker+"]]></description>\n" | |
312 kml4Marker=kml4Marker+"<styleURL>#marker_icon</styleURL>\n" | |
313 kml4Marker=kml4Marker+"<Point>" | |
314 | |
315 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n" | |
316 kml4Marker=kml4Marker+"</Point>\n" | |
317 kml4Marker=kml4Marker+"</Placemark>\n" | |
318 | |
319 kml4Marker=kml4Marker+"</Document>\n</kml>" | |
320 kmlFileName="marker"+str(time.time())+".kml" | |
321 | |
322 # kml4Marker=str(kml4Marker).replace('&','$$') | |
323 # kml4Marker=str(kml4Marker).replace(';','__$$__') | |
324 # kml4Marker=str(kml4Marker).replace('#','__SHARP__') | |
325 isLoadReady='false' | |
326 while isLoadReady=='false': | |
327 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker) | |
328 | |
329 return kmlFileName | |
330 | |
331 def getGoogleMapString(self,kml): | |
332 logging.debug("getGoogleMapString") | |
333 printed= '<body %s> '%kml +"""\n <div id="map_canvas" style="width: 98%; height: 95%"> </div> \n </body>" \n </html>""" | |
334 return printed | |
335 | |
336 def getPoint4GISid(self,gis_id): | |
337 j=0 | |
338 coords=(0,0) | |
339 if gis_id != None: | |
340 while (True): | |
341 j=j+1 | |
342 if (j>100): # FJK: just to prevent endless loops | |
343 break | |
344 if (gis_id.isdigit()): # FJK: regular exit from while-loop | |
345 break | |
346 else: | |
347 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters | |
348 gis_id=gis_id.strip() # FJK: to strip all whitespaces | |
349 resultpoint = [0,0] | |
350 results = None | |
351 try: | |
352 if int(gis_id)>0: | |
353 SQL="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE cast("+ str(gis_id) +" as text);" | |
354 results=self.ZSQLSimpleSearch(SQL) | |
355 #print results | |
356 if results != None: | |
357 for result in results: | |
358 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))] | |
359 if resultpoint !=[0,0]: | |
360 return resultpoint | |
361 else: | |
362 coords=self.getCoordsFromREST_gisID(joinid) | |
363 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;" | |
364 returnstring=self.ZSQLSimpleSearch(SQL) | |
365 return coords[0] | |
366 except: | |
367 return "gis_id not to interpretable:"+str(gis_id) | |
368 else: | |
369 return coords[0] | |
370 | |
371 def getCoordsFromREST_gisID(self,gis_id): | |
372 coordlist=[] | |
373 i=0 | |
374 while (i<5 and coordlist==[]): | |
375 | |
376 urlresponse=container.urlFunctions.zUrlopenParseString(container.urlFunctions.zUrlopenRead("http://chgis.hmdc.harvard.edu/xml/id/"+gis_id)) | |
377 baseDocElement=container.urlFunctions.zUrlopenDocumentElement(urlresponse) | |
378 childnodes=container.urlFunctions.zUrlopenChildNodes(baseDocElement) | |
379 itemnodes=container.urlFunctions.zUrlopenGetElementsByTagName(baseDocElement,'item') | |
380 | |
381 for i in range(0,container.urlFunctions.zUrlopenLength(itemnodes)): | |
382 itemnode=container.urlFunctions.zUrlopenGetItem(itemnodes,i) | |
383 itemspatialnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemnode,'spatial') | |
384 for j in range(0,container.urlFunctions.zUrlopenLength(itemspatialnodes)): | |
385 coord=[] | |
386 itemspatialnode= container.urlFunctions.zUrlopenGetItem(itemspatialnodes,j) | |
387 itemspatiallatnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_latitude') | |
388 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallatnodes)): | |
389 itemspatiallatnode= container.urlFunctions.zUrlopenGetItem(itemspatiallatnodes,k) | |
390 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallatnode)) | |
391 itemspatiallngnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_longitude') | |
392 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallngnodes)): | |
393 itemspatiallngnode= container.urlFunctions.zUrlopenGetItem(itemspatiallngnodes,k) | |
394 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallngnode)) | |
395 coordlist.append(coord) | |
396 gis_id= "_"+gis_id | |
397 return coordlist | |
398 | |
399 # End for GoogleMaps creation | |
400 | |
401 def RESTwrite2File(self,datadir, name,text): | |
402 # try: | |
403 fileid=name | |
404 if fileid in datadir.objectIds(): | |
405 datadir.manage_delObjects(fileid) | |
406 newfile=open(name,'w') | |
407 newfile.write(text) | |
408 newfile.close() | |
409 file4Read=open(name,'r') | |
410 fileInZope=datadir.manage_addFile(id=fileid,file=file4Read) | |
411 return "Write successful" | |
412 # except: | |
413 # return "Could not write" | |
414 | |
211 | 415 |
212 def manage_editRestDbInterface(self, title=None, connection_id=None, | 416 def manage_editRestDbInterface(self, title=None, connection_id=None, |
213 REQUEST=None): | 417 REQUEST=None): |
214 """Change the object""" | 418 """Change the object""" |
215 if title is not None: | 419 if title is not None: |
235 | 439 |
236 #checkPermission=getSecurityManager().checkPermission | 440 #checkPermission=getSecurityManager().checkPermission |
237 REQUEST.RESPONSE.redirect('manage_main') | 441 REQUEST.RESPONSE.redirect('manage_main') |
238 | 442 |
239 | 443 |
444 | |
445 # constructors = ( | |
446 # REST_test.manage_addRESTclassForm, | |
447 # REST_test.manage_addRESTclass | |
448 # ) | |
449 | |
450 | |
451 # self.registerClass( | |
452 # RestDbInterface.RestDbInterface, | |
453 # constructors = ( | |
454 # RestDbInterface.manage_addRestDbInterfaceForm, | |
455 # RestDbInterface.manage_addRestDbInterface | |
456 # ) | |
457 # ) |