def test_car_route_extra_options(): """Test routing API for car route.""" place_options = PlaceOptions( course=ROUTE_COURSE.west, sideof_street_hint=[52.512149, 13.304076], match_sideof_street=ROUTE_MATCH_SIDEOF_STREET.always, radius=10, min_course_distance=10, ) via_waypoint_options = WayPointOptions(stop_duration=0, pass_through=True) dest_waypoint_options = WayPointOptions(stop_duration=10, pass_through=False) ls = LS(api_key=LS_API_KEY) _ = ls.car_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000), (52.123, 13.22), (52.343, 13.444)], origin_place_options=place_options, destination_place_options=place_options, via_place_options=place_options, via_waypoint_options=via_waypoint_options, destination_waypoint_options=dest_waypoint_options, return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], )
def test_car_route_extra_options(): """Test routing API for car route.""" place_options = PlaceOptions( course=ROUTE_COURSE.west, sideof_street_hint=[52.512149, 13.304076], match_sideof_street=ROUTE_MATCH_SIDEOF_STREET.always, radius=10, min_course_distance=10, ) assert json.loads(place_options.__str__()) == { "course": 270, "sideOfStreetHint": "52.512149,13.304076", "matchSideOfStreet": "always", "namehint": None, "radius": 10, "minCourseDistance": 10, } via_waypoint_options = WayPointOptions(stop_duration=0, pass_through=True) assert json.loads(via_waypoint_options.__str__()) == {"stopDuration": 0, "passThrough": True} dest_waypoint_options = WayPointOptions(stop_duration=10, pass_through=False) ls = LS(api_key=LS_API_KEY) _ = ls.car_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000), (52.123, 13.22), (52.343, 13.444)], origin_place_options=place_options, destination_place_options=place_options, via_place_options=place_options, via_waypoint_options=via_waypoint_options, destination_waypoint_options=dest_waypoint_options, return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], )
def test_car_route(): """Test routing API for car route.""" ls = LS(api_key=LS_API_KEY) avoid_areas = [ AvoidBoundingBox(68.1766451354, 7.96553477623, 97.4025614766, 35.4940095078) ] avoid_features = [ROUTING_AVOID_FEATURES.tollRoad] result = ls.car_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000)], return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], avoid_areas=avoid_areas, avoid_features=avoid_features, exclude=["IND", "NZL", "AUS"], ) assert result.response["routes"][0]["sections"][0]["departure"]["place"][ "location"] == { "lat": 52.5137479, "lng": 13.4246242, "elv": 76.0, } assert result.response["routes"][0]["sections"][1]["departure"]["place"][ "location"] == { "lat": 52.5242323, "lng": 13.4301462, "elv": 80.0, } assert type(result.to_geojson()) == FeatureCollection
def test_truck_route(): """Test routing API for truck route.""" ls = LS(api_key=LS_API_KEY) truck = Truck( shipped_hazardous_goods=[SHIPPED_HAZARDOUS_GOODS.explosive], gross_weight=100, weight_per_axle=10, height=10, width=10, length=10, tunnel_category="B", axle_count=4, ) avoid_areas = [ AvoidBoundingBox(68.1766451354, 7.96553477623, 97.4025614766, 35.4940095078) ] avoid_features = [ROUTING_AVOID_FEATURES.tollRoad] _ = ls.truck_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000)], return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], truck=truck, avoid_areas=avoid_areas, avoid_features=avoid_features, exclude=["IND", "NZL", "AUS"], )
def discover( ctx, query, coordinates, radius, country_codes, bounding_box, limit, lang, apikey, raw, display, ): """ Search places using free-form text query. There are multiple combination of inputs for restricting your search - ``center`` and ``country_code`` - ``center`` and ``radius`` - ``bounding_box`` \f :param ctx: :param query: :param coordinates: :param radius: :param country_codes: :param bounding_box: :param limit: :param lang: :param apikey: :param raw: :param display: :return: """ apikey = apikey or os.environ.get("HERE_APIKEY") if apikey is None: raise ApiKeyNotFoundError( "Please pass HERE API KEY as --apikey or set it as environment " "variable in HERE_APIKEY " ) ctx.obj["apikey"] = apikey ls = LS(api_key=apikey) result = ls.discover( query=query, center=coordinates.split(",")[::-1] if coordinates else coordinates, radius=radius, country_codes=country_codes.split(",") if country_codes else country_codes, bounding_box=bounding_box.split(",") if bounding_box else bounding_box, limit=limit, lang=lang, ) if raw: click.secho(json.dumps(result.response, indent=2), fg="green") elif display: geo_display(json.dumps(result.to_geojson(), indent=2)) else: click.secho(json.dumps(result.items, indent=2), fg="green")
def test_ls_reverse_geocoding(): """Test reverse geocoding.""" ls = LS(api_key=LS_API_KEY) resp = ls.reverse_geocode(lat=19.1646, lng=72.8493) address = resp.items[0]["address"]["label"] assert "Goregaon" in address resp1 = ls.reverse_geocode(lat=19.1646, lng=72.8493, limit=4) assert len(resp1.items) == 4
def test_matrix_route(): """Test Matrix routing.""" ls = LS(api_key=LS_API_KEY) origins = [ {"lat": 37.76, "lng": -122.42}, {"lat": 40.63, "lng": -74.09}, {"lat": 30.26, "lng": -97.74}, ] region_definition = WorldRegion() matrix_attributes = [MATRIX_ATTRIBUTES.distances, MATRIX_ATTRIBUTES.travelTimes] avoid_areas = AvoidBoundingBox(68.1766451354, 7.96553477623, 97.4025614766, 35.4940095078) assert json.loads(avoid_areas.__str__()) == { "type": "boundingBox", "north": 68.1766451354, "south": 7.96553477623, "west": 97.4025614766, "east": 35.4940095078, } truck = Truck( shipped_hazardous_goods=[SHIPPED_HAZARDOUS_GOODS.explosive], gross_weight=100, weight_per_axle=10, height=10, width=10, length=10, tunnel_category="B", axle_count=4, ) result = ls.matrix( origins=origins, region_definition=region_definition, destinations=origins, routing_mode=ROUTING_MODE.fast, departure_time=datetime.now(tz=pytz.utc), transport_mode=ROUTING_TRANSPORT_MODE.truck, avoid_features=[AVOID_FEATURES.tollRoad], avoid_areas=[avoid_areas], truck=truck, matrix_attributes=matrix_attributes, ) mat = result.matrix assert mat["numOrigins"] == 3 assert mat["numDestinations"] == 3 assert len(mat["distances"]) == 9 profile = PROFILE.carShort result2 = ls.matrix( origins=origins, region_definition=region_definition, matrix_attributes=matrix_attributes, profile=profile, ) mat2 = result2.matrix assert mat2["numOrigins"] == 3 assert mat2["numDestinations"] == 3 with pytest.raises(NotImplementedError): result2.to_geojson()
def test_ls_reverse_geocoding_exception(): """Test reverse geocoding api exception.""" ls = LS(api_key=LS_API_KEY) with pytest.raises(ValueError): ls.reverse_geocode(lat=91, lng=90) with pytest.raises(ValueError): ls.reverse_geocode(lat=19, lng=190) ls = LS(api_key="dummy") with pytest.raises(ApiError): ls.reverse_geocode(lat=19.1646, lng=72.8493)
def test_exception_requests_inalid(): """Raise exception via requests as response for invalid api_key.""" ls = LS(api_key="dummy") with pytest.raises(ApiError) as execinfo: resp = ls.geocode(query="abc") raise ApiError(resp) resp = execinfo.value.args[0] assert resp.status_code == 401 assert resp.reason == "Unauthorized" assert str(execinfo.value).startswith( '401, Unauthorized, {"error":"Unauthorized"')
def test_pedestrian_route(): """Test routing API for pedestrian route.""" ls = LS(api_key=LS_API_KEY) _ = ls.pedestrian_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000)], return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], )
def test_ls_reverse_geocoding(): """Test reverse geocoding.""" ls = LS(api_key=LS_API_KEY) resp = ls.reverse_geocode(lat=19.1646, lng=72.8493) address = resp.items[0]["address"]["label"] assert ( address == "Goregaon East Railway Station(East ENT), Goregaon West, Mumbai 400062, India" ) resp1 = ls.reverse_geocode(lat=19.1646, lng=72.8493, limit=4) assert len(resp1.items) == 4
def test_ls_geocoding(): """Test geocoding api. """ address = "200 S Mathilda Sunnyvale CA" ls = LS(api_key=LS_API_KEY) resp = ls.geocode(query=address, limit=2) assert isinstance(resp, GeocoderResponse) assert resp.__str__() assert resp.as_json_string() geo_json = resp.to_geojson() assert geo_json.type == "FeatureCollection" assert geo_json.features pos = resp.items[0]["position"] assert len(resp.items) == 2 assert pos == {"lat": 37.37634, "lng": -122.03405}
def test_isonline_routing_exception(): """Test isonline exceptions.""" ls = LS(api_key=LS_API_KEY) with pytest.raises(ValueError): ls.calculate_isoline( range="900", range_type="time", mode="fastest;car;", ) with pytest.raises(ValueError): ls.calculate_isoline( range="900", range_type="time", mode="fastest;car;", arrival="2020-05-04T17:00:00+02", start=[52.5, 13.4], ) with pytest.raises(ValueError): ls.calculate_isoline( range="900", range_type="time", mode="fastest;car;", departure="2020-05-04T17:00:00+02", destination=[52.5, 13.4], )
def test_matrix_route_async(): """Test Matrix routing.""" ls = LS(api_key=LS_API_KEY) origins = [ { "lat": 37.76, "lng": -122.42 }, { "lat": 40.63, "lng": -74.09 }, { "lat": 30.26, "lng": -97.74 }, ] region_definition = WorldRegion() matrix_attributes = [ MATRIX_ATTRIBUTES.distances, MATRIX_ATTRIBUTES.travelTimes ] avoid_areas = AvoidBoundingBox(68.1766451354, 7.96553477623, 97.4025614766, 35.4940095078) truck = Truck( shipped_hazardous_goods=[SHIPPED_HAZARDOUS_GOODS.explosive], gross_weight=100, weight_per_axle=10, height=10, width=10, length=10, tunnel_category="B", axle_count=4, ) result = ls.matrix( origins=origins, region_definition=region_definition, async_req=True, destinations=origins, routing_mode=ROUTING_MODE.fast, departure_time="any", transport_mode=ROUTING_TRANSPORT_MODE.truck, avoid_features=[AVOID_FEATURES.tollRoad], avoid_areas=[avoid_areas], truck=truck, matrix_attributes=matrix_attributes, ) mat = result.matrix assert mat["numOrigins"] == 3 assert mat["numDestinations"] == 3 assert len(mat["distances"]) == 9
def test_scooter_route(): """Test routing API for scooter route.""" ls = LS(api_key=LS_API_KEY) scooter = Scooter(allow_highway=True) _ = ls.scooter_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000)], return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], scooter=scooter, exclude=["IND", "NZL", "AUS"], )
def test_bicycle_route(): """Test routing API for car route.""" ls = LS(api_key=LS_API_KEY) avoid_areas = [AvoidBoundingBox(68.1766451354, 7.96553477623, 97.4025614766, 35.4940095078)] avoid_features = [ROUTING_AVOID_FEATURES.tollRoad] _ = ls.bicycle_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000)], return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], avoid_areas=avoid_areas, avoid_features=avoid_features, exclude=["IND", "NZL", "AUS"], )
def test_credentials_exception(): """Test exception if environment variable ``LS_API_KEY`` is not present.""" api_key = os.environ.get("LS_API_KEY") del os.environ["LS_API_KEY"] with pytest.raises(Exception): _ = LS() os.environ["LS_API_KEY"] = api_key
def test_ls_geocoding_exception(): """Test geocoding api exception.""" address = "Goregaon West, Mumbai 400062, India" ls = LS(api_key="dummy") with pytest.raises(ApiError): ls.geocode(query=address) with pytest.raises(ValueError): ls.geocode(query="") ls.geocode(query=" ")
def test_ls_browse(): ls = LS(api_key=LS_API_KEY) result = ls.browse( center=[19.1663, 72.8526], radius=9000, limit=5, categories=[ PLACES_CATEGORIES.historical_monument, PLACES_CATEGORIES.museum, PLACES_CATEGORIES.park_recreation_area, PLACES_CATEGORIES.leisure, PLACES_CATEGORIES.shopping_mall, ], lang="en", ) assert len(result.items) == 5 result2 = ls.browse( center=[19.1663, 72.8526], name="starbucks", country_codes=["IND"], limit=10, categories=[PLACES_CATEGORIES.restaurant], lang="en", ) assert len(result2.items) == 10 result3 = ls.browse( center=[19.1663, 72.8526], name="starbucks", bounding_box=[13.08836, 52.33812, 13.761, 52.6755], categories=[PLACES_CATEGORIES.restaurant], lang="en", ) assert len(result3.items) == 13 with pytest.raises(ApiError): ls2 = LS(api_key="dummy") ls2.browse( center=[19.1663, 72.8526], radius=9000, limit=5, categories=[ PLACES_CATEGORIES.historical_monument, PLACES_CATEGORIES.museum, PLACES_CATEGORIES.park_recreation_area, PLACES_CATEGORIES.leisure, PLACES_CATEGORIES.shopping_mall, ], )
def test_isonline_routing(): """Test isonline routing api.""" ls = LS(api_key=LS_API_KEY) result = ls.calculate_isoline( start=[52.5, 13.4], range="900", range_type="time", mode="fastest;car;", departure="2020-05-04T17:00:00+02", ) coordinates = result.isoline[0]["component"][0]["shape"] assert coordinates[0] geo_json = result.to_geojson() assert geo_json.type == "Feature" assert geo_json.geometry.type == "Polygon" result2 = ls.calculate_isoline( destination=[52.5, 13.4], range="900", range_type="time", mode="fastest;car;", arrival="2020-05-04T17:00:00+02", ) coordinates = result2.isoline[0]["component"][0]["shape"] assert coordinates[0] with pytest.raises(ValueError): ls.calculate_isoline( start=[52.5, 13.4], range="900", range_type="time", mode="fastest;car;", destination=[52.5, 13.4], ) with pytest.raises(ApiError): ls2 = LS(api_key="dummy") ls2.calculate_isoline( start=[52.5, 13.4], range="900", range_type="time", mode="fastest;car;", )
def test_ls_lookup(): ls = LS(api_key=LS_API_KEY) result = ls.lookup( location_id="here:pds:place:276u0vhj-b0bace6448ae4b0fbc1d5e323998a7d2", lang="en", ) assert result.response["title"] == "Frankfurt-Hahn Airport" with pytest.raises(ApiError): ls2 = LS(api_key="dummy") ls2.lookup(location_id="here:pds:place:276u0vhj-b0bace6448ae4b0fbc1d5e323998a7d2")
def test_ls_browse(): ls = LS(api_key=LS_API_KEY) result = ls.browse( center=[19.1663, 72.8526], radius=9000, limit=5, categories=[ "300-3000-0025", "300-3100.550-5510-0202", "500-5520,600-6100-0062" ], lang="en", ) assert len(result.items) == 5 result2 = ls.browse( center=[19.1663, 72.8526], name="starbucks", country_codes=["IND"], limit=10, categories=["100-1000-0000"], lang="en", ) assert len(result2.items) == 10 result3 = ls.browse( center=[19.1663, 72.8526], name="starbucks", bounding_box=[13.08836, 52.33812, 13.761, 52.6755], categories=["100-1000-0000"], lang="en", ) assert len(result3.items) == 20 with pytest.raises(ApiError): ls2 = LS(api_key="dummy") ls2.browse( center=[19.1663, 72.8526], radius=9000, limit=5, categories=[ "300-3000-0025", "300-3100.550-5510-0202", "500-5520,600-6100-0062" ], )
def test_matrix_route_exception(): """Test exceptions for Matrix routing.""" ls = LS(api_key=LS_API_KEY) origins = [ { "lat": 37.76, "lng": -122.42 }, { "lat": 40.63, "lng": -74.09 }, { "lat": 30.26, "lng": -97.74 }, { "lat": 40.63, "lng": -74.09 }, ] region_definition = CircleRegion(radius=1000, center={ "lat": 37.76, "lng": -122.42 }) matrix_attributes = [ MATRIX_ATTRIBUTES.distances, MATRIX_ATTRIBUTES.travelTimes ] profile = PROFILE.carFast truck = Truck( shipped_hazardous_goods=[SHIPPED_HAZARDOUS_GOODS.explosive], gross_weight=100, weight_per_axle=10, height=10, width=10, length=10, tunnel_category="B", axle_count=4, ) with pytest.raises(ValueError): ls.matrix( origins=origins, region_definition=region_definition, profile=profile, matrix_attributes=matrix_attributes, ) with pytest.raises(ValueError): ls.matrix( origins=origins, region_definition=region_definition, matrix_attributes=matrix_attributes, transport_mode=ROUTING_TRANSPORT_MODE.car, truck=truck, )
def test_car_route(): """Test routing API for car route.""" ls = LS(api_key=LS_API_KEY) result = ls.car_route( origin=[52.51375, 13.42462], destination=[52.52332, 13.42800], via=[(52.52426, 13.43000)], return_results=[ROUTING_RETURN.polyline, ROUTING_RETURN.elevation], departure_time=datetime.now(), spans=[ROUTING_SPANS.names], ) assert result.response["routes"][0]["sections"][0]["departure"]["place"][ "location"] == { "lat": 52.5137479, "lng": 13.4246242, "elv": 76.0, } assert result.response["routes"][0]["sections"][1]["departure"]["place"][ "location"] == { "lat": 52.5242323, "lng": 13.4301462, "elv": 80.0, } assert type(result.to_geojson()) == FeatureCollection
def route( ctx, transport_mode, origin, destination, via, routing_mode, alternatives, lang, apikey, raw, display, ): """ find route between two or more locations. """ apikey = apikey or os.environ.get("HERE_APIKEY") if apikey is None: raise ApiKeyNotFoundError( "Please pass HERE API KEY as --apikey or set it as environment " "variable in HERE_APIKEY " ) ctx.obj["apikey"] = apikey ls = LS(api_key=apikey) if transport_mode == "car": result = ls.car_route( origin=origin.split(","), destination=destination.split(","), via=via if via else None, routing_mode=routing_mode, alternatives=alternatives, lang=lang, return_results=[ ROUTING_RETURN.polyline, ROUTING_RETURN.elevation, ROUTING_RETURN.instructions, ROUTING_RETURN.actions, ], ) elif transport_mode == "truck": result = ls.truck_route( origin=origin.split(","), destination=destination.split(","), via=via if via else None, routing_mode=routing_mode, alternatives=alternatives, lang=lang, return_results=[ ROUTING_RETURN.polyline, ROUTING_RETURN.elevation, ROUTING_RETURN.instructions, ROUTING_RETURN.actions, ], ) elif transport_mode == "pedestrian": result = ls.pedestrian_route( origin=origin.split(","), destination=destination.split(","), via=via if via else None, routing_mode=routing_mode, alternatives=alternatives, lang=lang, return_results=[ ROUTING_RETURN.polyline, ROUTING_RETURN.elevation, ROUTING_RETURN.instructions, ROUTING_RETURN.actions, ], ) elif transport_mode == "bicycle": result = ls.bicycle_route( origin=origin.split(","), destination=destination.split(","), via=via if via else None, routing_mode=routing_mode, alternatives=alternatives, lang=lang, return_results=[ ROUTING_RETURN.polyline, ROUTING_RETURN.elevation, ROUTING_RETURN.instructions, ROUTING_RETURN.actions, ], ) elif transport_mode == "scooter": result = ls.scooter_route( origin=origin.split(","), destination=destination.split(","), via=via if via else None, routing_mode=routing_mode, alternatives=alternatives, lang=lang, return_results=[ ROUTING_RETURN.polyline, ROUTING_RETURN.elevation, ROUTING_RETURN.instructions, ROUTING_RETURN.actions, ], ) if raw: click.secho(json.dumps(result.response, indent=2), fg="green") elif display: geo_display(json.dumps(result.to_geojson(), indent=2)) else: click.secho(json.dumps(result.routes, indent=2), fg="green")
################################################# ### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ### ################################################# # file to edit: 04_los_here_api_utils.ipynb import json import os from datetime import datetime from here_location_services import LS from here_location_services.config.routing_config import ROUTING_RETURN # Get HERE api object LS_API_KEY = os.environ.get("HERE_API_KEY") # Get API KEY from environment. ls = LS(api_key=LS_API_KEY) def get_info(los_res): """ This function parse result from HERE route api, and return travel_time and trip length Parameters los_res: result from HERE rout api Return (trip_duration in seconds, trip_length in meters) """ try: res = los_res.routes[0]['sections'][0]['summary'] return res['duration'], res['length'] except: return 0, 0
def test_ls_discover(): ls = LS(api_key=LS_API_KEY) result = ls.discover(query="starbucks", center=[19.1663, 72.8526], radius=10000, lang="en") assert len(result.items) == 20 result2 = ls.discover( query="starbucks", center=[19.1663, 72.8526], country_codes=["IND"], limit=2, ) assert len(result2.items) == 2 result3 = ls.discover( query="starbucks", bounding_box=[13.08836, 52.33812, 13.761, 52.6755], ) assert len(result3.items) == 20 with pytest.raises(ValueError): ls.discover( query="starbucks", center=[52.5, 13.4], bounding_box=[13.08836, 52.33812, 13.761, 52.6755], ) with pytest.raises(ApiError): ls2 = LS(api_key="dummy") ls2.discover(query="starbucks", center=[19.1663, 72.8526], radius=10000, limit=10)