43
|
1 '''
|
|
2 Created on 2.9.2010
|
|
3
|
|
4 @author: casties, fknauft
|
|
5 '''
|
|
6
|
|
7 from OFS.Folder import Folder
|
|
8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
|
|
9 from Products.ZSQLExtend import ZSQLExtend
|
|
10 import logging
|
|
11 import re
|
|
12 import json
|
|
13 import time
|
|
14
|
|
15 from RestDbInterface import *
|
|
16
|
|
17
|
|
18 gisToSqlTypeMap = {
|
|
19 "text": "text",
|
|
20 "number": "numeric",
|
|
21 "id": "text",
|
|
22 "gis_id": "text",
|
|
23 "coord_lat": "numeric",
|
|
24 "coord_lon": "numeric"
|
|
25 }
|
|
26
|
55
|
27 def kmlEncode(s):
|
|
28 """returns string encoded for displaying in KML attribute"""
|
|
29 res = s.replace("'","__Apostroph__")
|
|
30 res = res.replace('"','__DoubleApostroph__')
|
|
31 res = res.replace(';','__$$__')
|
|
32 res = res.replace('&','&')
|
|
33 return res
|
|
34
|
|
35
|
43
|
36 class RestDbGisApi(RestDbInterface):
|
|
37 """Object for RESTful GIS database queries
|
|
38 path schema: /db/{schema}/{table}/
|
|
39 omitting table gives a list of schemas
|
|
40 omitting table and schema gives a list of schemas
|
|
41 """
|
|
42
|
|
43 meta_type="RESTgis"
|
|
44
|
|
45 # data templates
|
|
46 GIS_schema_table = PageTemplateFile('zpt/GIS_schema_table', globals())
|
55
|
47 KML_schema_table = PageTemplateFile('zpt/KML_schema_table', globals())
|
|
48
|
44
|
49 # and scripts
|
|
50 def KML_URL_schema_table(self,schema,table):
|
|
51 """KML_URL table function"""
|
|
52 self.REQUEST.RESPONSE.setHeader("Content-Type", "text/plain")
|
|
53 id = self.REQUEST.get('id',[])
|
|
54 doc = self.REQUEST.get('doc',None)
|
55
|
55 return self.getLiveKmlUrl(schema=schema,table=table,id=id,doc=doc)
|
44
|
56
|
43
|
57
|
|
58 def checkTableMetaPermission(self,action,schema,table,user=None):
|
|
59 """returns if the requested action on the table is allowed"""
|
|
60 logging.debug("checktablemetapermissions action=%s schema=%s table=%s user=%s"%(action,schema,table,user))
|
|
61 if user is None:
|
|
62 user = self.REQUEST.get('AUTHENTICATED_USER',None)
|
|
63 logging.debug("user=%s"%user)
|
|
64 # TODO: what now?
|
|
65 return True
|
|
66
|
|
67 def setTableMetaTypes(self,schema,table,fields):
|
|
68 """sets the GIS meta information for table"""
|
|
69 logging.debug("settablemetatypes schema=%s, table=%s, fields=%s"%(schema,table,fields))
|
|
70 gisIdField = None
|
|
71 latField = None
|
|
72 lonField = None
|
|
73 for f in fields:
|
|
74 t = f['type']
|
|
75 if t == 'gis_id':
|
|
76 gisIdField = f['name']
|
|
77 elif t == 'coord_lat':
|
|
78 latField = f['name']
|
|
79 elif t == 'coord_lon':
|
|
80 lonField = f['name']
|
|
81
|
|
82 res = self.executeSQL("select * from public.metadata where tablename=%s", (table,))
|
|
83 if len(res['rows']) > 0:
|
|
84 # meta record exists
|
|
85 if gisIdField is not None:
|
|
86 self.executeSQL('update public.metadata set "attribute with gis_id" = %s where tablename = %s', (gisIdField,table), hasResult=False)
|
|
87
|
|
88 else:
|
|
89 # new meta record
|
|
90 if gisIdField is not None:
|
|
91 self.executeSQL('insert into public.metadata ("tablename", "attribute with gis_id") values (%s, %s)', (table,gisIdField), hasResult=False)
|
|
92
|
|
93
|
|
94 def showTable(self,resultFormat='XML',schema='public',table=None,REQUEST=None,RESPONSE=None):
|
|
95 """returns PageTemplate with tables"""
|
|
96 logging.debug("showtable")
|
|
97 if REQUEST is None:
|
|
98 REQUEST = self.REQUEST
|
|
99
|
|
100 # should be cross-site accessible
|
|
101 if RESPONSE is None:
|
|
102 RESPONSE = self.REQUEST.RESPONSE
|
|
103
|
|
104 RESPONSE.setHeader('Access-Control-Allow-Origin', '*')
|
44
|
105
|
43
|
106 # everything else has its own template
|
|
107 pt = getattr(self.template, '%s_schema_table'%resultFormat, None)
|
|
108 if pt is None:
|
|
109 return "ERROR!! template %s_schema_table not found"%resultFormat
|
|
110
|
44
|
111 #data = self.getTable(schema,table)
|
|
112 # templates have to get their own data
|
|
113 return pt(schema=schema,table=table)
|
43
|
114
|
|
115
|
|
116 def createEmptyTable(self,schema,table,fields):
|
|
117 """create a table with the given fields
|
|
118 returns list of created fields"""
|
|
119 logging.debug("createEmptyTable")
|
|
120 sqlFields = []
|
|
121 for f in fields:
|
|
122 if isinstance(f,dict):
|
|
123 # {name: XX, type: YY}
|
|
124 name = sqlName(f['name'])
|
|
125 type = f['type']
|
|
126 sqltype = gisToSqlTypeMap[type]
|
|
127
|
|
128 else:
|
|
129 # name only
|
|
130 name = sqlName(f)
|
|
131 type = 'text'
|
|
132 sqltype = 'text'
|
|
133
|
|
134 sqlFields.append({'name':name, 'type':type, 'sqltype':sqltype})
|
|
135
|
|
136 if self.checkTableMetaPermission("create", schema, table):
|
|
137 self.executeSQL('drop table if exists "%s"."%s"'%(schema,table),hasResult=False)
|
|
138 fieldString = ", ".join(['"%s" %s'%(f['name'],f['sqltype']) for f in sqlFields])
|
|
139 sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString)
|
|
140 logging.debug("createemptytable: SQL=%s"%sqlString)
|
|
141 self.executeSQL(sqlString,hasResult=False)
|
|
142 self.setTableMetaTypes(schema,table,sqlFields)
|
|
143 return sqlFields
|
|
144 else:
|
|
145 logging.warning("create table not allowed!")
|
|
146 # throw exception?
|
|
147 return None
|
|
148
|
|
149
|
|
150 def getKmlUrl(self,schema='chgis',table='mpdl',id=[],doc=None):
|
|
151 logging.debug("getKmlUrl")
|
|
152 data = self.getDataForGoogleMap(schema,table,id,doc)
|
|
153 kml=self.getKMLname(data=data,table=table)
|
|
154 baseUrl = self.absolute_url()
|
|
155 return "%s/daten/%s"%(baseUrl,kml)
|
|
156
|
55
|
157 def getLiveKmlUrl(self,schema='chgis',table='mpdl',id=None,doc=None):
|
|
158 logging.debug("getLiveKmlUrl")
|
|
159 baseUrl = self.absolute_url()
|
|
160 return "%s/db/%s/%s?format=KML"%(baseUrl,schema,table)
|
|
161
|
|
162 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=None,doc=None):
|
43
|
163 logging.debug("getDataForGoogleMap")
|
|
164 qstr="SELECT * FROM "+schema+"."+table
|
|
165 try:
|
55
|
166 if id is not None:
|
43
|
167 qstr=qstr+" WHERE "
|
|
168 for id_item in id.split(","):
|
|
169 if table=='mpdl':
|
|
170 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
|
|
171 else:
|
|
172 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
|
|
173 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
|
|
174 data=self.ZSQLSimpleSearch(qstr)
|
|
175 return data
|
|
176 except:
|
|
177 return qstr
|
|
178
|
55
|
179 def getKmlData(self, schema, table, ids=None, gisIdField=None, latField=None, lonField=None):
|
|
180 """returns data structure for KML template"""
|
|
181 logging.debug("getKMLdata")
|
|
182 # Mapping a set of points from table-based SQL-query:
|
|
183 qstr='SELECT * FROM "%s"."%s"'%(schema,table)
|
|
184 idList = None
|
|
185 if ids is not None:
|
|
186 qstr += ' WHERE '
|
|
187 if table=='mpdl':
|
|
188 qstr += 'mpdl_xmlsource_id IN ('
|
|
189 else:
|
|
190 qstr += 'CAST(id AS text) IN ('
|
|
191
|
|
192 idList = ids.split(",")
|
|
193 qstr += ','.join(['%s' for i in idList])
|
|
194 qstr += ')'
|
|
195
|
|
196 data = self.executeSQL(qstr,idList)
|
|
197
|
|
198 fieldMap = self.getFieldNameMap(data['fields'])
|
|
199
|
|
200 if (gisIdField is None) and (latField is None or lonField is None):
|
|
201 # no fields given - choose automagically
|
|
202 # gis id in metadata first
|
|
203 SQL='SELECT "attribute with gis_id" FROM public.metadata WHERE tablename = %s'
|
|
204 res = self.executeSQL(SQL, (table,))
|
|
205 if len(res['rows']) > 0:
|
|
206 gisIdField = res['rows'][0][0]
|
|
207 else:
|
|
208 logging.warning("no entry in metadata table for table %s"%table)
|
|
209 # try field names
|
|
210 if 'latitude' in fieldMap and 'longitude' in fieldMap:
|
|
211 latField = 'latitude'
|
|
212 lonField = 'longitude'
|
|
213 elif 'x_coord' in fieldMap and 'y_coord' in fieldMap:
|
|
214 latField = 'x_coord'
|
|
215 lonField = 'y_coord'
|
|
216 else:
|
|
217 logging.error("getKMLdata unable to find position fields")
|
|
218 return None
|
|
219
|
|
220 # convert field names to row indexes
|
|
221 gisIdIdx = fieldMap.get(gisIdField,None)
|
|
222 latIdx = fieldMap.get(latField,None)
|
|
223 lonIdx = fieldMap.get(lonField,None)
|
|
224 logging.debug("gisidfield=%s idx=%s"%(gisIdField,gisIdIdx))
|
|
225
|
|
226 # convert data
|
|
227 kmlData = []
|
|
228 for dataset in data['rows']:
|
|
229 if gisIdIdx is not None:
|
|
230 gisID = dataset[gisIdIdx]
|
|
231 coords=self.getPoint4GISid(gisID)
|
|
232 if coords!=None:
|
|
233 xCoord=coords[0]
|
|
234 yCoord=coords[1]
|
|
235
|
|
236 elif latIdx is not None:
|
|
237 xCoord = dataset[lonIdx]
|
|
238 yCoord = dataset[latIdx]
|
|
239
|
|
240 else:
|
|
241 logging.error("getKMLdata unable to find position")
|
|
242 return None
|
|
243
|
|
244 if float(xCoord) == 0.0:
|
|
245 continue
|
|
246
|
|
247 if float(yCoord) == 0.0:
|
|
248 continue
|
|
249
|
|
250 kmlPlace = {}
|
|
251
|
|
252 # description
|
|
253 desc = ''
|
|
254 i = -1
|
|
255 for value in dataset:
|
|
256 i += 1
|
|
257 name = data['fields'][i][0]
|
|
258 logging.debug("value=%s"%value)
|
|
259 if value != None:
|
|
260 if name.find('name')>-1:
|
|
261 desc += "<name>%s</name>\n"%value
|
|
262 continue
|
|
263 elif name.find('place')>-1:
|
|
264 desc += "<name>%s</name>\n"%value
|
|
265 continue
|
|
266
|
|
267 val = "%s: %s"%(name, value)
|
|
268 if val.find('http')>-1:
|
|
269 val ='<a href="' + val + '" target="_blank">' + val + '</a>'
|
|
270
|
|
271 desc += kmlEncode(val)
|
|
272 desc += '<br/>\n'
|
|
273
|
|
274 kmlPlace['description'] = "<![CDATA[%s]]>"%desc
|
|
275 kmlPlace['icon'] = '#marker_icon'
|
|
276 kmlPlace['coord_x'] = str(xCoord)
|
|
277 kmlPlace['coord_y'] = str(yCoord)
|
|
278 kmlPlace['coord_z'] = '0'
|
|
279 kmlData.append(kmlPlace)
|
|
280
|
|
281 #logging.debug("kmlData=%s"%(repr(kmlData)))
|
|
282 return kmlData
|
|
283
|
|
284
|
|
285
|
43
|
286 def getKMLname(self,data=[],table=""):
|
|
287 logging.debug("getKMLname")
|
|
288 #session=context.REQUEST.SESSION
|
|
289 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"
|
|
290 initializeStringForGoogleMaps=""
|
|
291 #doLine=container.getVar('doLine')
|
|
292 # Mapping a set of points from table-based SQL-query:
|
|
293 if data!=None:
|
|
294 try:
|
|
295 SQL='SELECT "attribute with gis_id" FROM public.metadata WHERE tablename = %s'
|
|
296 res = self.executeSQL(SQL, (table,))
|
|
297 gisIDattribute = res['rows'][0][0]
|
|
298 except:
|
|
299 return "table not registered within metadata"
|
|
300
|
|
301 for dataset in data:
|
|
302 try:
|
|
303 xCoord=getattr(dataset,'longitude')
|
|
304 yCoord=getattr(dataset,'latitude')
|
|
305 except:
|
|
306 try:
|
|
307 xCoord=getattr(dataset,'x_coord')
|
|
308 yCoord=getattr(dataset,'y_coord')
|
|
309 except:
|
|
310 #try:
|
|
311 gisID=getattr(dataset,gisIDattribute)
|
|
312 coords=self.getPoint4GISid(gisID)
|
|
313 if coords!=None:
|
|
314 xCoord=coords[0]
|
|
315 yCoord=coords[1]
|
|
316 # except:
|
|
317 # return "no coordinates found"
|
|
318
|
|
319 if float(xCoord)!=0:
|
|
320 if float(yCoord)!=0:
|
|
321 kml4Marker=kml4Marker+"<Placemark>"
|
|
322 kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
|
|
323 for values in dataset:
|
55
|
324 #logging.debug("values=%s"%repr(values))
|
43
|
325 if values != (None, None):
|
|
326 if str(values).find('name')>-1:
|
|
327 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
|
|
328 continue
|
|
329 elif str(values).find('place')>-1:
|
|
330 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
|
|
331 continue
|
|
332
|
|
333 kml4Marker=kml4Marker+str(values)+": "
|
|
334 attribute_string=str(values).replace("'","__Apostroph__")
|
|
335 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
|
|
336 attribute_string=str(attribute_string).replace(';','__$$__')
|
|
337 attribute_string=str(attribute_string).replace('&','&')
|
|
338 if str(attribute_string).find('http')>-1:
|
|
339 attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>'
|
|
340 kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
|
|
341
|
|
342 kml4Marker=kml4Marker+"]]></description>\n"
|
|
343 kml4Marker=kml4Marker+"<styleURL>#marker_icon</styleURL>\n"
|
|
344 kml4Marker=kml4Marker+"<Point>"
|
|
345
|
|
346 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
|
|
347 kml4Marker=kml4Marker+"</Point>\n"
|
|
348 kml4Marker=kml4Marker+"</Placemark>\n"
|
|
349
|
|
350 kml4Marker=kml4Marker+"</Document>\n</kml>"
|
|
351 kmlFileName="marker"+str(time.time())+".kml"
|
|
352
|
|
353 #kml4Marker=str(kml4Marker).replace('&','$$')
|
|
354 #kml4Marker=str(kml4Marker).replace(';','__$$__')
|
|
355 #kml4Marker=str(kml4Marker).replace('#','__SHARP__')
|
|
356 isLoadReady='false'
|
|
357 while isLoadReady=='false':
|
|
358 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
|
|
359
|
|
360 return kmlFileName
|
|
361
|
44
|
362 # def getGoogleMapString(self,kml):
|
|
363 # logging.debug("getGoogleMapString")
|
|
364 # printed= '<body %s> '%kml +"""\n <div id="map_canvas" style="width: 98%; height: 95%"> </div> \n </body>" \n </html>"""
|
|
365 # return printed
|
43
|
366
|
|
367 def getPoint4GISid(self,gis_id):
|
|
368 j=0
|
|
369 coords=(0,0)
|
|
370 if gis_id != None:
|
|
371 while (True):
|
|
372 j=j+1
|
|
373 if (j>100): # FJK: just to prevent endless loops
|
|
374 break
|
|
375 if (gis_id.isdigit()): # FJK: regular exit from while-loop
|
|
376 break
|
|
377 else:
|
|
378 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters
|
|
379 gis_id=gis_id.strip() # FJK: to strip all whitespaces
|
|
380 resultpoint = [0,0]
|
|
381 results = None
|
|
382 try:
|
|
383 if int(gis_id)>0:
|
|
384 SQL="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE cast("+ str(gis_id) +" as text);"
|
|
385 results=self.ZSQLSimpleSearch(SQL)
|
|
386 #print results
|
|
387 if results != None:
|
|
388 for result in results:
|
|
389 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))]
|
|
390 if resultpoint !=[0,0]:
|
|
391 return resultpoint
|
|
392 else:
|
|
393 coords=self.getCoordsFromREST_gisID(joinid)
|
|
394 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;"
|
|
395 returnstring=self.ZSQLSimpleSearch(SQL)
|
|
396 return coords[0]
|
|
397 except:
|
|
398 return "gis_id not to interpretable:"+str(gis_id)
|
|
399 else:
|
|
400 return coords[0]
|
|
401
|
|
402 def getCoordsFromREST_gisID(self,gis_id):
|
|
403 coordlist=[]
|
|
404 i=0
|
|
405 while (i<5 and coordlist==[]):
|
|
406
|
|
407 urlresponse=container.urlFunctions.zUrlopenParseString(container.urlFunctions.zUrlopenRead("http://chgis.hmdc.harvard.edu/xml/id/"+gis_id))
|
|
408 baseDocElement=container.urlFunctions.zUrlopenDocumentElement(urlresponse)
|
|
409 childnodes=container.urlFunctions.zUrlopenChildNodes(baseDocElement)
|
|
410 itemnodes=container.urlFunctions.zUrlopenGetElementsByTagName(baseDocElement,'item')
|
|
411
|
|
412 for i in range(0,container.urlFunctions.zUrlopenLength(itemnodes)):
|
|
413 itemnode=container.urlFunctions.zUrlopenGetItem(itemnodes,i)
|
|
414 itemspatialnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemnode,'spatial')
|
|
415 for j in range(0,container.urlFunctions.zUrlopenLength(itemspatialnodes)):
|
|
416 coord=[]
|
|
417 itemspatialnode= container.urlFunctions.zUrlopenGetItem(itemspatialnodes,j)
|
|
418 itemspatiallatnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_latitude')
|
|
419 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallatnodes)):
|
|
420 itemspatiallatnode= container.urlFunctions.zUrlopenGetItem(itemspatiallatnodes,k)
|
|
421 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallatnode))
|
|
422 itemspatiallngnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_longitude')
|
|
423 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallngnodes)):
|
|
424 itemspatiallngnode= container.urlFunctions.zUrlopenGetItem(itemspatiallngnodes,k)
|
|
425 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallngnode))
|
|
426 coordlist.append(coord)
|
|
427 gis_id= "_"+gis_id
|
|
428 return coordlist
|
|
429
|
|
430 # End for GoogleMaps creation
|
|
431
|
|
432 def RESTwrite2File(self,datadir, name,text):
|
|
433 logging.debug("RESTwrite2File datadir=%s name=%s"%(datadir,name))
|
|
434 try:
|
|
435 import cStringIO as StringIO
|
|
436 except:
|
|
437 import StringIO
|
|
438
|
|
439 # make filehandle from string
|
|
440 textfile = StringIO.StringIO(text)
|
|
441 fileid=name
|
|
442 if fileid in datadir.objectIds():
|
|
443 datadir.manage_delObjects(fileid)
|
|
444 fileInZope=datadir.manage_addFile(id=fileid,file=textfile)
|
|
445 return "Write successful"
|
|
446
|
|
447 def manage_editRestDbGisApi(self, title=None, connection_id=None,
|
|
448 REQUEST=None):
|
|
449 """Change the object"""
|
|
450 if title is not None:
|
|
451 self.title = title
|
|
452
|
|
453 if connection_id is not None:
|
|
454 self.connection_id = connection_id
|
|
455
|
|
456 #checkPermission=getSecurityManager().checkPermission
|
|
457 REQUEST.RESPONSE.redirect('manage_main')
|
|
458
|
|
459
|
|
460 manage_addRestDbGisApiForm=PageTemplateFile('zpt/addRestDbGisApi',globals())
|
|
461
|
|
462 def manage_addRestDbGisApi(self, id, title='', label='', description='',
|
|
463 createPublic=0,
|
|
464 createUserF=0,
|
|
465 REQUEST=None):
|
|
466 """Add a new object with id *id*."""
|
|
467
|
|
468 ob=RestDbGisApi(str(id),title)
|
|
469 self._setObject(id, ob)
|
|
470
|
|
471 #checkPermission=getSecurityManager().checkPermission
|
|
472 REQUEST.RESPONSE.redirect('manage_main')
|
|
473
|
|
474
|