comparison RestDbGisApi.py @ 58:5ed0769f5ad3

more cleanup in kml generation points can be sorted now
author casties
date Thu, 21 Oct 2010 19:38:41 +0200
parents b47314bcff37
children a5f2550a5b44
comparison
equal deleted inserted replaced
57:b47314bcff37 58:5ed0769f5ad3
182 data=self.ZSQLSimpleSearch(qstr) 182 data=self.ZSQLSimpleSearch(qstr)
183 return data 183 return data
184 except: 184 except:
185 return qstr 185 return qstr
186 186
187 def getKmlData(self, schema, table, ids=None, gisIdField=None, latField=None, lonField=None): 187 def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None):
188 """returns data structure for KML template""" 188 """returns data structure for KML template"""
189 logging.debug("getKMLdata") 189 logging.debug("getKMLdata")
190 # Mapping a set of points from table-based SQL-query: 190 # Mapping a set of points from table-based SQL-query:
191 qstr='SELECT * FROM "%s"."%s"'%(schema,table) 191 qstr='SELECT * FROM "%s"."%s"'%(schema,table)
192 idList = None 192 idList = None
199 199
200 idList = ids.split(",") 200 idList = ids.split(",")
201 qstr += ','.join(['%s' for i in idList]) 201 qstr += ','.join(['%s' for i in idList])
202 qstr += ')' 202 qstr += ')'
203 203
204 data = self.executeSQL(qstr,idList) 204 if sortBy:
205 # add sort clause
206 qstr += " ORDER BY %s"
207 if idList is None:
208 idList = []
209 idList.append(sortBy)
210 data = self.executeSQL(qstr,idList)
211 else:
212 data = self.executeSQL(qstr,idList)
205 213
206 fieldMap = self.getFieldNameMap(data['fields']) 214 fieldMap = self.getFieldNameMap(data['fields'])
207 215
208 if (gisIdField is None) and (latField is None or lonField is None): 216 if (gisIdField is None) and (latField is None or lonField is None):
209 # no fields given - choose automagically 217 # no fields given - choose automagically
234 # convert data 242 # convert data
235 kmlData = [] 243 kmlData = []
236 for dataset in data['rows']: 244 for dataset in data['rows']:
237 if gisIdIdx is not None: 245 if gisIdIdx is not None:
238 gisID = dataset[gisIdIdx] 246 gisID = dataset[gisIdIdx]
239 coords=self.getPoint4GISid(gisID) 247 coords=self.getPointForChGisId(gisID)
240 if coords!=None: 248 if coords!=None:
241 xCoord=coords[0] 249 xCoord=coords[0]
242 yCoord=coords[1] 250 yCoord=coords[1]
243 251
244 elif latIdx is not None: 252 elif latIdx is not None:
261 desc = '' 269 desc = ''
262 i = -1 270 i = -1
263 for value in dataset: 271 for value in dataset:
264 i += 1 272 i += 1
265 name = data['fields'][i][0] 273 name = data['fields'][i][0]
266 logging.debug("value=%s"%value) 274 #logging.debug("value=%s"%value)
267 if value != None: 275 if value != None:
268 #if name.find('name')>-1: 276 #if name.find('name')>-1:
269 # desc += "<name>%s</name>\n"%value 277 # desc += "<name>%s</name>\n"%value
270 # continue 278 # continue
271 #elif name.find('place')>-1: 279 #elif name.find('place')>-1:
288 kmlPlace['coord_z'] = '0' 296 kmlPlace['coord_z'] = '0'
289 kmlData.append(kmlPlace) 297 kmlData.append(kmlPlace)
290 298
291 #logging.debug("kmlData=%s"%(repr(kmlData))) 299 #logging.debug("kmlData=%s"%(repr(kmlData)))
292 return kmlData 300 return kmlData
301
302 def getPointForChGisId(self, gis_id):
303 """returns coordinate pair for given gis_id"""
304 if gis_id is None or gis_id == "":
305 return None
306
307 sql="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id = %s"
308 res = self.executeSQL(sql, (gis_id,))
309 if len(res['rows']) > 0:
310 coords = res['rows'][0]
311 return coords
312
313 else:
314 # try to clean gis_id...
315 gis_id = re.sub(r'[^0-9]','',gis_id)
316 # try again
317 sql="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id = %s"
318 res = self.executeSQL(sql, (gis_id,))
319 if len(res['rows']) > 0:
320 coords = res['rows'][0]
321 return coords
322
323 else:
324 logging.error("CH-GIS ID %s not found!"%repr(gis_id))
325
326 # TODO: do we need the getCoordsFromREST_gisID stuff?
327
328 return None
293 329
294 330
295 331
296 def getKMLname(self,data=[],table=""): 332 def getKMLname(self,data=[],table=""):
297 logging.debug("getKMLname") 333 logging.debug("getKMLname")