comparison RestDbGisApi.py @ 261:5b38b50052e4

Display Polygons and Lines via chgis_id Reload button alternative changed to "R"
author fknauft
date Fri, 07 Oct 2011 15:58:07 +0200
parents e9a1ac7d2ab2
children 6613b9204bda
comparison
equal deleted inserted replaced
260:e9a1ac7d2ab2 261:5b38b50052e4
191 bad_luck=True 191 bad_luck=True
192 else: 192 else:
193 bad_luck=False 193 bad_luck=False
194 194
195 fieldMap = self.getFieldNameMap(data['fields']) 195 fieldMap = self.getFieldNameMap(data['fields'])
196 geomdata=None
196 if (geomField!="point"): 197 if (geomField!="point"):
197 try: 198 # first check if the file is registered as geo-dataset (which then should have an attribute "the_geom")
198 geomstr='select astext(st_simplify(transform(the_geom,4326),0.05)) from "%s"."%s"'%(schema,table) 199 requeststring="select f_geometry_column from geometry_columns where f_table_schema=%s and f_table_name=%s "
199 geomdata=self.executeSQL(geomstr) 200 geocolumn_res=self.executeSQL(requeststring,(schema,table))
200 teststr=geomdata.values()[1][0] 201 if len(geocolumn_res['rows'])>0:
201 if (teststr == (u'MULTIPOLYGON EMPTY',)): 202 geocolumn=geocolumn_res['rows'][0][0]
202 geomstr='select astext(st_simplify(transform(the_geom,2333),0.05)) from "%s"."%s"'%(schema,table) 203 try:
204 geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
203 geomdata=self.executeSQL(geomstr) 205 geomdata=self.executeSQL(geomstr)
204 206 teststr=geomdata.values()[1][0]
205 except: 207 if (teststr == (u'MULTIPOLYGON EMPTY',)):
206 try: 208 geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
207 geomstr='select chgis.astext(chgis.st_simplify(chgis.transform(the_geom,4326),0.05)) from "%s"."%s"'%(schema,table) 209 geomdata=self.executeSQL(geomstr)
208 geomdata=self.executeSQL(geomstr) 210
209 except: 211 except:
210 geomdata=None 212 try:
211 213 geomstr="select chgis.astext(chgis.st_simplify(chgis.transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
214 geomdata=self.executeSQL(geomstr)
215 except:
216 geomdata=None
217
212 if (gisIdField is None) and (latField is None or lonField is None) and geomField=='point': 218 if (gisIdField is None) and (latField is None or lonField is None) and geomField=='point':
213 # no fields given - choose automagically 219 # no fields given - choose automagically
214 sql = 'SELECT field_name FROM public.gis_table_meta_rows WHERE table_name = %s and gis_type = %s' 220 sql = "SELECT field_name FROM public.gis_table_meta_rows WHERE table_name = %s and gis_type = %s"
215 # gis id in metadata first 221 # gis id in metadata first
216 res = self.executeSQL(sql, (table,'gis_id')) 222 res = self.executeSQL(sql, (table,'gis_id'))
217 if len(res['rows']) > 0: 223 if len(res['rows']) > 0:
218 gisIdField = res['rows'][0][0] 224 gisIdField = res['rows'][0][0]
219 # latitude in metadata 225 # latitude in metadata
247 logging.debug("gisidfield=%s idx=%s"%(gisIdField,gisIdIdx)) 253 logging.debug("gisidfield=%s idx=%s"%(gisIdField,gisIdIdx))
248 254
249 # convert data 255 # convert data
250 kmlData = [] 256 kmlData = []
251 geom_list = {} 257 geom_list = {}
252 try: 258 # try:
253 if geomField=='poly' or geomField=='line': 259 # if geomField=='poly' or geomField=='line':
254 geom_list=geomdata.values()[1] 260 # geom_list=geomdata.values()[1]
255 except: 261 # except:
256 return "no geomdata in RestDbGisApi Line 254" 262 # return "no geomdata in RestDbGisApi Line 254"
263
257 data_list=data['rows'] 264 data_list=data['rows']
258 for k in range (len(data_list)): 265 for k in range (len(data_list)):
259 dataset = data_list[k] 266 dataset = data_list[k]
260 if len(geom_list)>k: 267 if len(geom_list)>k:
261 geom_value = geom_list[k] 268 geom_value = geom_list[k]
262 269
263 270
264 271 if gisIdIdx != None and geomField!='point':
272 gisID = dataset[gisIdIdx]
273 geomdata = self.getLineForChGisId(gisID)
274
265 # for dataset in data['rows']: 275 # for dataset in data['rows']:
266 xCoord = 0.0 276 xCoord = 0.0
267 yCoord = 0.0 277 yCoord = 0.0
268 if gisIdIdx != None and geomField=='point' : 278 if gisIdIdx != None and geomField=='point' :
269 gisID = dataset[gisIdIdx] 279 gisID = dataset[gisIdIdx]
343 kmlPlace['icon'] = '#marker_icon_'+colorField 353 kmlPlace['icon'] = '#marker_icon_'+colorField
344 kmlPlace['coord_x'] = str(xCoord) 354 kmlPlace['coord_x'] = str(xCoord)
345 kmlPlace['coord_y'] = str(yCoord) 355 kmlPlace['coord_y'] = str(yCoord)
346 kmlPlace['coord_z'] = '50' 356 kmlPlace['coord_z'] = '50'
347 kmlData.append(kmlPlace) 357 kmlData.append(kmlPlace)
348 if (geomField=='poly' or geomField=='line') and len(geomdata)>0: 358 if (geomField=='poly' or geomField=='line') and geomdata is not None:
349 polys=str(geom_value).split('(') 359 polys=str(geomdata).split('(')
350 aaa=len(polys) 360 aaa=len(polys)
351 for poly in polys: 361 for poly in polys:
352 kmlPlace = {} 362 kmlPlace = {}
353 kmlPlace['description'] = desc 363 kmlPlace['description'] = desc
354 coords=poly.replace(')','').replace("'","").split(',') 364 coords=poly.replace(')','').replace("'","").split(',')
383 #logging.error("error on sql:",sql%(str(str_gis_id),)) 393 #logging.error("error on sql:",sql%(str(str_gis_id),))
384 return None 394 return None
385 395
386 if gis_id is None or gis_id == "": 396 if gis_id is None or gis_id == "":
387 return None 397 return None
388 if len(gis_id) < 4: 398 if len(str(gis_id)) < 4:
389 return None 399 return None
390 400
391 # try gis_id 401 # try gis_id
392 coords = getPoint(gis_id) 402 coords = getPoint(gis_id)
393 if coords is None: 403 if coords is None:
421 #logging.error("SQL results now:", str(coords_test)) 431 #logging.error("SQL results now:", str(coords_test))
422 return coords[0] 432 return coords[0]
423 433
424 return coords 434 return coords
425 435
436
437 def getLineForChGisId(self, gis_id):
438 """returns line/poly coordinates for given gis_id""" # gets called by getKml
439 def getLine(id):
440 str_gis_id=str(id).split('.')[0]
441 sql="SELECT astext(st_simplify(transform(the_geom,4326),0.05)) FROM chgis.chgis_linesandpolys WHERE gis_id LIKE CAST('%s' AS text)"%(str_gis_id)
442 # logging.error("sql:",sql)
443 # res = self.executeSQL(sql, (str(str_gis_id),))
444 res = self.executeSQL(sql)
445 if len(res['rows']) > 0:
446 return res['rows'][0]
447 else:
448 #logging.error("error on sql:",sql%(str(str_gis_id),))
449 return None
450
451 if gis_id is None or gis_id == "":
452 return None
453 if len(str(gis_id)) < 4:
454 return None
455
456 # try gis_id
457 line_coords = getLine(gis_id)
458 if line_coords is None:
459 # try to clean gis_id...
460 gis_id_short = re.sub(r'[^0-9]','',gis_id)
461 # try again
462 line_coords = getLine(gis_id_short)
463 if line_coords is None:
464 logging.error("CH-GIS ID %s not found!"%(gis_id_short))
465
466
467 return line_coords
426 468
427 ## legacy methods... 469 ## legacy methods...
428 470
429 def getKmlUrl(self,schema='chgis',table='mpdl',args={'doc':None,'id':None}): 471 def getKmlUrl(self,schema='chgis',table='mpdl',args={'doc':None,'id':None}):
430 logging.debug("getKmlUrl") 472 logging.debug("getKmlUrl")