Skip to main content

Microsoft Azure Maps Route Client Library for Python

Project description

Azure Maps Route Package client library for Python

This package contains a Python SDK for Azure Maps Services for Route. Read more about Azure Maps Services here

Source code | API reference documentation | Product documentation

Disclaimer

Azure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691

Getting started

Prerequisites

If you use Azure CLI, replace <resource-group-name> and <account-name> of your choice, and select a proper pricing tier based on your needs via the <sku-name> parameter. Please refer to this page for more details.

az maps account create --resource-group <resource-group-name> --account-name <account-name> --sku <sku-name>

Install the package

Install the Azure Maps Service Route SDK.

pip install azure-maps-route

Create and Authenticate the MapsRouteClient

To create a client object to access the Azure Maps Route API, you will need a credential object. Azure Maps Route client also support two ways to authenticate.

1. Authenticate with a Subscription Key Credential

You can authenticate with your Azure Maps Subscription Key. Once the Azure Maps Subscription Key is created, set the value of the key as environment variable: AZURE_SUBSCRIPTION_KEY. Then pass an AZURE_SUBSCRIPTION_KEY as the credential parameter into an instance of AzureKeyCredential.

from azure.core.credentials import AzureKeyCredential
from azure.maps.route import MapsRouteClient

credential = AzureKeyCredential(os.environ.get("AZURE_SUBSCRIPTION_KEY"))

route_client = MapsRouteClient(
    credential=credential,
)

2. Authenticate with an Azure Active Directory credential

You can authenticate with Azure Active Directory (AAD) token credential using the Azure Identity library. Authentication by using AAD requires some initial setup:

After setup, you can choose which type of credential from azure.identity to use. As an example, DefaultAzureCredential can be used to authenticate the client:

Next, set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET

You will also need to specify the Azure Maps resource you intend to use by specifying the clientId in the client options. The Azure Maps resource client id can be found in the Authentication sections in the Azure Maps resource. Please refer to the documentation on how to find it.

from azure.maps.route import MapsRouteClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
route_client = MapsRouteClient(
    client_id="<Azure Maps Client ID>",
    credential=credential
)

Key concepts

The Azure Maps Route client library for Python allows you to interact with each of the components through the use of a dedicated client object.

Sync Clients

MapsRouteClient is the primary client for developers using the Azure Maps Route client library for Python. Once you initialized a MapsRouteClient class, you can explore the methods on this client object to understand the different features of the Azure Maps Route service that you can access.

Async Clients

This library includes a complete async API supported on Python 3.5+. To use it, you must first install an async transport, such as aiohttp. See azure-core documentation for more information.

Async clients and credentials should be closed when they're no longer needed. These objects are async context managers and define async close methods.

Examples

The following sections provide several code snippets covering some of the most common Azure Maps Route tasks, including:

Request and Get Route Directions

This service request returns a route between an origin and a destination, passing through waypoints if they are specified. The route will take into account factors such as current traffic and the typical road speeds on the requested day of the week and time of day.

from azure.maps.route import MapsRouteClient

route_directions_result = client.get_route_directions(route_points=[LatLon(47.60323, -122.33028), LatLon(53.2, -106)]);

Request and Get Route Range

This service will calculate a set of locations that can be reached from the origin point by given coordinates and based on fuel, energy, time or distance budget that is specified.

from azure.maps.route import MapsRouteClient

route_range_result = client.get_route_range(coordinates=LatLon(47.60323, -122.33028), time_budget_in_sec=6000);

Get Route Matrix

If the Matrix Route request was accepted successfully, the Location header in the response contains the URL to download the results of the request.

Retrieves the result of a previous route matrix request. The method returns a poller for retrieving the result.

from azure.maps.route import MapsRouteClient

route_matrix_result = client.begin_get_route_matrix_result(matrix_id="11111111-2222-3333-4444-555555555555");

Get Route Directions Batch

Retrieves the result of a previous route direction batch request. The method returns a poller for retrieving the result.

from azure.maps.route import MapsRouteClient

route_directions_batch_poller_result = client.begin_get_route_directions_batch_result(batch_id="11111111-2222-3333-4444-555555555555");

Troubleshooting

General

Maps Route clients raise exceptions defined in Azure Core.

This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the error_code attribute, i.e, exception.error_code.

Logging

This library uses the standard logging library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.

Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the logging_enable argument:

import sys
import logging
from azure.maps.route import MapsRouteClient

# Create a logger for the 'azure.maps.route' SDK
logger = logging.getLogger('azure.maps.route')
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

Additional

Still running into issues? If you encounter any bugs or have suggestions, please file an issue in the Issues section of the project.

Next steps

More sample code

Get started with our Maps Route samples (Async Version samples).

Several Azure Maps Route Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Maps Route

set AZURE_SUBSCRIPTION_KEY="<RealSubscriptionKey>"

pip install azure-maps-route --pre

python samples/sample_authentication.py
python sample/sample_get_route_range.py
python samples/sample_get_route_directions.py
python samples/sample_request_route_matrix.py
python samples/async_samples/sample_authentication_async.py
python samples/async_samples/sample_get_route_range_async.py
python samples/async_samples/sample_request_route_matrix_async.py
python samples/async_samples/sample_get_route_directions_async.py

Notes: --pre flag can be optionally added, it is to include pre-release and development versions for pip install. By default, pip only finds stable versions.

Further detail please refer to Samples Introduction

Additional documentation

For more extensive documentation on Azure Maps Route, see the Azure Maps Route documentation on docs.microsoft.com.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Release History

1.0.0b1 (2022-10-13)

  • Initial Release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-maps-route-1.0.0b1.zip (162.0 kB view details)

Uploaded Source

Built Distribution

azure_maps_route-1.0.0b1-py3-none-any.whl (132.8 kB view details)

Uploaded Python 3

File details

Details for the file azure-maps-route-1.0.0b1.zip.

File metadata

  • Download URL: azure-maps-route-1.0.0b1.zip
  • Upload date:
  • Size: 162.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.28.1 setuptools/58.1.0 requests-toolbelt/0.10.0 tqdm/4.64.1 CPython/3.9.14

File hashes

Hashes for azure-maps-route-1.0.0b1.zip
Algorithm Hash digest
SHA256 f558f85af5807e3cce7f624c8ab002fdd9203769e1896b63c57e8ff72b3cd29d
MD5 cc13bfe19c1b564afb960be8a991ce69
BLAKE2b-256 e51cd5b94852d14754d81c759abaadd8016361af3c9cb6363e4211856842b6b6

See more details on using hashes here.

File details

Details for the file azure_maps_route-1.0.0b1-py3-none-any.whl.

File metadata

  • Download URL: azure_maps_route-1.0.0b1-py3-none-any.whl
  • Upload date:
  • Size: 132.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.28.1 setuptools/58.1.0 requests-toolbelt/0.10.0 tqdm/4.64.1 CPython/3.9.14

File hashes

Hashes for azure_maps_route-1.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 e946a39c6f40cf2b02c15298b8325307c75274016f628cdcb9d2a5910f8f26c7
MD5 33752ee2c7452d031bdf41e5543b8b1a
BLAKE2b-256 21076ba1ce28039a4615d13c411c788e8fabd79526cb5cab63d6418f9e376b69

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page