def get(self, region=None, lon=None, lat=None, id=None): args = self.parsers["get"].parse_args() if is_coord(id): lon, lat = get_lon_lat(id) regions = self._get_regions(region=region, lon=lon, lat=lat) params = self._get_args(id=id, lon=lon, lat=lat) args.update(params) self._register_interpreted_parameters(args) for r in regions: if all([lon, lat, is_coord(id)]): response = self._response_from_place_uri(r, args) if response: response.update({"regions": [r]}) break else: response = self._response_from_places_nearby(r, args) if response: response.update({"regions": [r]}) break if response: return response, 200 return { "regions": regions, "message": "No address for these coords" }, 404
def get_exclusion_areas(self, request): """ Retreive and adapt exclusion parameters for Here API. See the Here doc for avoidAreas option https://developer.here.com/documentation/routing/dev_guide/topics/resource-calculate-matrix.html """ _exclusion_areas = request.get('_here_exclusion_area[]', None) if _exclusion_areas == None: return None else: boxes = "" if len(_exclusion_areas) > HERE_MAX_LIMIT_AVOID_AREAS: self.log.error( 'Here parameters _here_exclusion_area[] is limited to 20 exclusion areas. truncate list !' ) for idx, exclusion_area in enumerate( _exclusion_areas[:HERE_MAX_LIMIT_AVOID_AREAS]): if exclusion_area.count('!') == 1: coord_1, coord_2 = exclusion_area.split('!') if is_coord(coord_1) and is_coord(coord_2): lon_1, lat_1 = get_lon_lat(coord_1) lon_2, lat_2 = get_lon_lat(coord_2) # The superior latitude has to be the first coord for Here API # https://developer.here.com/documentation/routing/dev_guide/topics/resource-param-type-bounding-box.html if lat_1 > lat_2: boxes += "{},{};{},{}".format( lat_1, lon_1, lat_2, lon_2) else: boxes += "{},{};{},{}".format( lat_2, lon_2, lat_1, lon_1) if idx < (len(_exclusion_areas) - 1): boxes += "!" else: self.log.error( 'Here parameters _here_exclusion_area[]={} is badly formated. Exclusion box is skipped' .format(exclusion_area)) else: self.log.error( 'Here parameters _here_exclusion_area[]={} is badly formated. Exclusion box is skipped' .format(exclusion_area)) return {"avoidAreas": boxes}
def get_exclusion_areas(self, request): """ Retreive and adapt exclusion parameters for Here API. See the Here doc for avoid option https://developer.here.com/documentation/matrix-routing-api/api-reference-swagger.html """ _exclusion_areas = request.get('_here_exclusion_area[]', None) if _exclusion_areas == None: return None else: boxes = 'bbox:' if len(_exclusion_areas) > HERE_MAX_LIMIT_AVOID_AREAS: self.log.error( 'Here parameters _here_exclusion_area[] is limited to 20 exclusion areas. truncate list !' ) for idx, exclusion_area in enumerate( _exclusion_areas[:HERE_MAX_LIMIT_AVOID_AREAS]): if exclusion_area.count('!') == 1: coord_1, coord_2 = exclusion_area.split('!') if is_coord(coord_1) and is_coord(coord_2): lon_1, lat_1 = get_lon_lat(coord_1) lon_2, lat_2 = get_lon_lat(coord_2) # The superior latitude has to be the second coord for Here API if lat_1 < lat_2: boxes += "{},{},{},{}".format( lon_1, lat_1, lon_2, lat_2) else: boxes += "{},{},{},{}".format( lon_2, lat_2, lon_1, lat_1) if idx < (len(_exclusion_areas) - 1): boxes += "|" else: self.log.error( 'Here parameters _here_exclusion_area[]={} is badly formated. Exclusion box is skipped' .format(exclusion_area)) else: self.log.error( 'Here parameters _here_exclusion_area[]={} is badly formated. Exclusion box is skipped' .format(exclusion_area)) return {'avoid': boxes}
def _get_args(self, lon=None, lat=None, id=None): args = {"uri": "{};{}".format(lon, lat), "_current_datetime": datetime.datetime.utcnow()} if all([lon, lat, is_coord(id)]): return args args.update( { "count": 1, "distance": 200, "type[]": ["address"], "depth": 1, "start_page": 0, "filter": "", "count": 1, } ) return args
def place(self, place): req = request_pb2.Request() req.requested_api = type_pb2.place_uri req.place_uri.uri = place req.place_uri.depth = 2 response = self.instance.send_and_receive(req) if response.places: return response.places[0] if utils.is_coord(place): # In some cases, the given "from" is not always a findable address by kraken # we just forward the given coord and return a pt object lon, lat = utils.get_lon_lat(place) p = type_pb2.PtObject() p.uri = place p.embedded_type = type_pb2.ADDRESS p.address.coord.lon = lon p.address.coord.lat = lat p.address.uri = place return p return None