Distance Matrix¶
DistanceMatrix
¶
DistanceMatrix(maps: Map)
Google distance matrix client.
Purpose
Calculates distance and duration matrices and presents route comparisons.
Attributes:
| Name | Type | Description |
|---|---|---|
_maps |
Map
|
Google Maps gateway used for distance requests. |
modes |
List[str]
|
Supported Google travel modes. |
data |
Dict[str, Any] | None
|
Most recent raw matrix response. |
rows |
List[Dict[str, Any]] | None
|
Most recent flattened matrix results. |
origins |
List[AddressOrCoord] | None
|
Normalized origins for the last request. |
destinations |
List[AddressOrCoord] | None
|
Normalized destinations for the last request. |
mode |
str | None
|
Travel mode for the last matrix request. |
departure_time |
str | None
|
Last requested departure-time value. |
params |
Dict[str, str] | None
|
Query sent to the Google Distance Matrix API. |
Initialize supported travel modes and response state.
Purpose
Initialize supported travel modes and response state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maps
|
Map
|
Initialized Google Maps API gateway. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Initializes instance state. |
Source code in mapping.py
Methods:¶
normalize_mode
¶
Purpose
Validate and normalize a Google Distance Matrix travel mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Travel mode: driving, walking, bicycling, or transit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Normalized travel mode. |
Source code in mapping.py
normalize_inputs
¶
normalize_inputs(
values: (
List[AddressOrCoord]
| Tuple[AddressOrCoord, ...]
| AddressOrCoord
),
) -> List[AddressOrCoord]
Purpose
Normalize one or many origins/destinations into a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
List[AddressOrCoord] | AddressOrCoord
|
Single address/coordinate or a list/tuple of them. |
required |
Returns:
| Type | Description |
|---|---|
List[AddressOrCoord]
|
List[AddressOrCoord]: Normalized input list. |
Source code in mapping.py
convert_distance
¶
Purpose
Convert meters into kilometers and miles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meters
|
int | float | None
|
Distance in meters. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, float | None]
|
Dict[str, float | None]: Distance conversions. |
Source code in mapping.py
convert_duration
¶
Purpose
Convert seconds into minutes and hours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
int | float | None
|
Duration in seconds. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, float | None]
|
Dict[str, float | None]: Duration conversions. |
Source code in mapping.py
flatten_element
¶
flatten_element(
origin: str,
destination: str,
mode: str,
element: Dict[str, Any],
) -> Dict[str, Any]
Purpose
Flatten one Distance Matrix element into a row-friendly dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
str
|
Resolved origin label from the API. |
required |
destination
|
str
|
Resolved destination label from the API. |
required |
mode
|
str
|
Travel mode used for the request. |
required |
element
|
Dict[str, Any]
|
One Distance Matrix element. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Flattened route row. |
Source code in mapping.py
matrix
¶
matrix(
origins: (
List[AddressOrCoord]
| Tuple[AddressOrCoord, ...]
| AddressOrCoord
),
destinations: (
List[AddressOrCoord]
| Tuple[AddressOrCoord, ...]
| AddressOrCoord
),
mode: str = "driving",
departure_time: str = "",
) -> List[Dict[str, Any]]
Purpose
Execute a Distance Matrix request for one or more origins and one or more destinations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origins
|
List[AddressOrCoord] | AddressOrCoord
|
Origins. |
required |
destinations
|
List[AddressOrCoord] | AddressOrCoord
|
Destinations. |
required |
mode
|
str
|
Travel mode: driving, walking, bicycling, or transit. |
'driving'
|
departure_time
|
str
|
Optional departure time. Use 'now' for traffic-aware driving. |
''
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: Flattened route rows. |
Source code in mapping.py
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | |
summary
¶
Purpose
Return a compact dict with meters/seconds and human text fields for one origin, one destination, and one travel mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
AddressOrCoord
|
Origin address string or (lat, lng) tuple. |
required |
destination
|
AddressOrCoord
|
Destination address string or (lat, lng) tuple. |
required |
mode
|
str
|
Travel mode: driving, walking, bicycling, transit. |
'driving'
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
Distance and duration summary. |
Source code in mapping.py
compare_modes
¶
compare_modes(
origin: AddressOrCoord,
destination: AddressOrCoord,
modes: List[str] | None = None,
departure_time: str = "",
) -> List[Dict[str, Any]]
Purpose
Compare route distance and duration across multiple travel modes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
AddressOrCoord
|
Origin address string or coordinate tuple. |
required |
destination
|
AddressOrCoord
|
Destination address string or coordinate tuple. |
required |
modes
|
List[str] | None
|
Optional travel modes. Defaults to all supported modes. |
None
|
departure_time
|
str
|
Optional departure time. Use 'now' for traffic-aware driving. |
''
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: One flattened route summary per travel mode. |
Source code in mapping.py
to_dataframe
¶
Purpose
Convert route rows into a pandas DataFrame for Streamlit display, export, or downstream analytics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
List[Dict[str, Any]] | None
|
Optional rows. Defaults to the most recent matrix rows. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Route results as a DataFrame. |