Beispiel #1
0
def geocoding(query, forward, raw, display):
    """
    OSM's Nominatim geocoding service.
    \f

    :param query: A string to represent address query for geocoding.
    :param forward: A boolean flag for forward/reverse geocoding.
    :param raw: A boolean flag to show api response as it is.
    :return: None.
    """
    geolocator = Nominatim(user_agent=f"maps-cli/{__version__}")
    if forward:
        location = geolocator.geocode(query)
        if raw:
            click.secho(json.dumps(location.raw, indent=2), fg="green")
        elif display:
            feature = get_feature_from_lat_lon(location.latitude,
                                               location.longitude)
            geo_display(feature)
        else:
            result = {"lat": location.latitude, "lon": location.longitude}
            click.secho(json.dumps(result, indent=2), fg="green")
    else:
        location = geolocator.reverse(query)
        if raw:
            click.secho(json.dumps(location.raw, indent=2), fg="green")
        else:
            click.secho(location.address, fg="green")
Beispiel #2
0
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")
Beispiel #3
0
def geocoding(ctx, query, apikey, forward, raw, display):
    """
    Open Route Service geocoding service.
    \f

    :param ctx: A context dictionary.
    :param query: A string to represent address query for geocoding.
    :param apikey: An API key for authentication.
    :param forward: A boolean flag for forward/reverse geocoding.
    :param raw: A boolean flag to show api response as it is.
    :param display: A boolean flag to show result in web browser.
    :return: None.
    """
    apikey = apikey or os.environ.get("ORS_APIKEY")
    if apikey is None:
        raise ApiKeyNotFoundError(
            "Please pass Open Route Service API KEY as --apikey or set it as environment "
            "variable in ORS_APIKEY ")
    ctx.obj["apikey"] = apikey
    geolocator = opnrs.Client(key=ctx.obj["apikey"])
    if forward:
        geocode = geolocator.pelias_search(text=query)
        if raw:
            click.secho(json.dumps(geocode, indent=2), fg="green")
        elif display:
            geocode.pop("geocoding")
            geo_display(json.dumps(geocode))
        else:
            for feature in geocode["features"]:
                coords = feature["geometry"]["coordinates"]
                result = {"lat": coords[1], "lon": coords[0]}
                click.secho(json.dumps(result, indent=2), fg="green")
    else:
        coordinate = query.split(",")
        reverse = geolocator.pelias_reverse(point=coordinate, validate=False)
        if raw:
            for result in reverse["features"]:
                click.secho(json.dumps(result, indent=2), fg="green")
        else:
            for result in reverse["features"]:
                click.secho(result["properties"]["label"], fg="green")
Beispiel #4
0
def geocoding(ctx, query, apikey, forward, raw, display):
    """
    MapBox's geocoding service.
    \f

    :param ctx: A context dictionary.
    :param query: A string to represent address query for geocoding.
    :param apikey: An API key for authentication.
    :param forward: A boolean flag for forward/reverse geocoding.
    :param raw: A boolean flag to show api response as it is.
    :param display: A boolean flag to show result in web browser.
    :return: None.
    """
    apikey = apikey or os.environ.get("MAPBOX_APIKEY")
    if apikey is None:
        raise ApiKeyNotFoundError(
            "Please pass MAPBOX API KEY as --apikey or set it as environment "
            "variable in MAPBOX_APIKEY ")
    ctx.obj["apikey"] = apikey
    geolocator = MapBox(api_key=ctx.obj["apikey"])
    if forward:
        location = geolocator.geocode(query)
        if raw:
            click.secho(json.dumps(location.raw, indent=2), fg="green")
        elif display:
            feature = get_feature_from_lat_lon(location.latitude,
                                               location.longitude)
            geo_display(feature)
        else:
            result = {"lat": location.latitude, "lon": location.longitude}
            click.secho(json.dumps(result, indent=2), fg="green")
    else:
        location = geolocator.reverse(query)
        if raw:
            click.secho(json.dumps(location.raw, indent=2), fg="green")
        else:
            click.secho(location.address, fg="green")
Beispiel #5
0
def isochrone(
    ctx,
    profile,
    coordinates,
    contours_minutes,
    contours_colors,
    polygons,
    apikey,
    display,
):
    """
    An isochrone, from the Greek root words iso (equal) and chrone (time), is a line that connects
    points of equal travel time around a given location.

    The Mapbox Isochrone API computes areas that are reachable within a specified amount of time
    from a location, and returns the reachable regions as contours of polygons or lines that
    you can display on a map.
    \f

    :param ctx: A context dictionary.
    :param profile: A Mapbox Directions routing profile ID. Options are mapbox/driving for travel
        times by car, mapbox/walking for pedestrian and hiking travel times, and mapbox/cycling for
        travel times by bicycle. For more detailed descriptions of these routing profiles, see the
        Directions API documentation.
    :param coordinates: A {longitude,latitude} coordinate pair around which to center the isochrone
        lines.
    :param contours_minutes: The times in minutes to use for each isochrone contour. You can
        specify up to four contours. Times must be in increasing order. The maximum time that can
        be specified is 60 minutes.
    :param contours_colors: The colors to use for each isochrone contour, specified as hex values
        without a leading # (for example, ff0000 for red). If this parameter is used, there must
        be the same number of colors as there are entries in contours_minutes.
        If no colors are specified, the Isochrone API will assign a default rainbow color scheme
        to the output.
    :param polygons: Specify whether to return the contours as GeoJSON polygons (true) or
        linestrings (false, default). When polygons=true, any contour that forms a ring is
        returned as a polygon.
    :param apikey: An API key for authentication.
    :param display: A boolean flag to show result in web browser.
    :return: None.
    """
    apikey = apikey or os.environ.get("MAPBOX_APIKEY")
    if apikey is None:
        raise ApiKeyNotFoundError(
            "Please pass MAPBOX API KEY as --apikey or set it as environment "
            "variable in MAPBOX_APIKEY ")
    ctx.obj["apikey"] = apikey
    client = MapBoxApi(base_url="https://api.mapbox.com", credentials=apikey)
    resp = client.isochrone(
        profile=profile,
        coordinates=coordinates.split(","),
        contours_minutes=contours_minutes.split(","),
        contours_colors=contours_colors.split(",")
        if contours_colors else contours_colors,
        polygons=polygons,
    )
    if display:
        lon, lat = [float(c) for c in coordinates.split(",")]
        center = get_feature_from_lat_lon(lat, lon)
        feature_collection = resp.json()
        feature_collection["features"].append(center)
        geo_display(json.dumps(feature_collection, indent=2))
    else:
        click.secho(json.dumps(resp.json(), indent=2), fg="green")
Beispiel #6
0
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")