def endElement(self, name): global PoiCounter if name == "node": if self.epoi and self.isPoi: # look for existing entity qepoi = Epoi.all() qepoi.filter("osm_id =", self.epoi.osm_id) epoi = qepoi.fetch(1) if len(epoi) > 0: epoi = epoi[0] # node is already in storage. check version. if epoi.version < self.epoi.version: # overwrite existing point, keep attributes # which don't come from OSM self.epoi.pic = epoi.pic else: # unchanged, skip it return # find in Osmtag table and add there if new for key, value in self.osm_tags: qosmtag = Osmtag.all() qosmtag.filter("k =", key) qosmtag.filter("v =", value) osmtag = qosmtag.fetch(1) if len(osmtag) < 1: # store new osmtag osmtag = Osmtag(k=key, v=value) osmtag.put() logging.debug("Stored osmtag %s=%s" % (key, value)) else: osmtag = osmtag[0] # update reference in epoi entity if not (osmtag.key() in self.epoi.osm_tags): self.epoi.osm_tags.append(osmtag.key()) # This is needed for GeoModel location cells update self.epoi.update_location() self.epoi.put() PoiCounter = PoiCounter + 1 logging.debug( "Stored epoi osm_id: %d name: %s tags %s" % ( self.epoi.osm_id, self.epoi.name, ", ".join([("%s=%s") % (Osmtag.get(key).k, Osmtag.get(key).v) for key in self.epoi.osm_tags]), ) ) elif name == "way": pass elif name == "relation": pass elif name == "osm": pass else: pass
def get(self): bbox = self.request.get("bbox", "0,0,0,0").split(",") minlat = float(bbox[0]) minlon = float(bbox[1]) maxlat = float(bbox[2]) maxlon = float(bbox[3]) if self.request.get("cache", "") == "False": cache = False else: cache = True max_results = int(self.request.get("max_results", 60)) logging.info( "populate bbox minlon: %3.6f maxlon: %3.6f minlat: %3.6f maxlat: %3.6f cache: %d" % (minlon, maxlon, minlat, maxlat, cache) ) return # some checks if minlon > maxlon or minlat > maxlat: logging.error("coordinate parameters in wrong order") self.error(500) return # calculate center point of box and from there the surrounding # grid boxes for download center_lat = minlat + (maxlat - minlat) / 2 center_lon = minlon + (maxlon - minlon) / 2 bboxes = nineBoxes(center_lat, center_lon) # # Check if area is already in storage and if not, download # for box in bboxes: # geotype box __init__ parameters are: north, east, south, west # checks if there is at least one point in this box in the storage results = Epoi.bounding_box_fetch(Epoi.all(), box, max_results=11) if len(results) == 0 or not cache: # load box from osm api apiDownload(box) else: logging.debug( "skip bbox for download; west: %3.3f south: %3.3f east: %3.3f north: %3.3f" % (box.west, box.south, box.east, box.north) ) # # Build result object GeoJSON # result = {"type": "FeatureCollection", "bbox": [minlon, minlat, maxlon, maxlat]} res = Epoi.bounding_box_fetch( Epoi.all(), geo.geotypes.Box(maxlon, maxlat, minlon, minlat), max_results=max_results ) features = [] feature = {} for epoi in res: feature["type"] = "Feature" feature["geometry"] = {"type": "Point", "coordinates": [epoi.location.lon, epoi.location.lat]} properties = {} for key in epoi.osm_tags: prop = Osmtag.get(key) properties[prop.k] = prop.v feature["properties"] = properties features.append(feature) result["features"] = features self.response.headers["Content-Type"] = "application/json" self.response.out.write(json.dumps(result))