dist-along-route determines the distance of a point along a defined route, with both specified in GPS coords
Project description
dist-along-route
A Python library for computing distances along a route from latitude/longitude coordinates. This library efficiently calculates how far along a route any given point is. It is best used for calculating how far along transit vehicles are on their routes, but can be applied to any scenario where you need to measure distances along a predefined path.
Installation
pip install dist-along-route
Quick Start
import numpy as np
from dist_along_route import Route
# Define a route as lat/lon coordinates (in this case the path from the Lincoln Memorial to the US Capitol)
route_coords = np.array(
[
[38.88927893, -77.05017266],
[38.88981981, -77.00906834],
]
)
# Create a Route object
route = Route(route_coords, distance_unit="feet", precision=1)
# Test point (lat/lon)
p1 = np.array([38.89767634, -77.03653007]) # White House
p2 = np.array([38.88946913, -77.03524507]) # Washington Monument
test_points = np.array([p1, p2])
# Get distance from start of route
distances_from_start, perpendicular_distances = route.get_dist_from_start(test_points)
print(distances_from_start)
print(perpendicular_distances)
How it Works
Basics
- Route Initialization: The
Routeclass takes an array of latitude/longitude points defining the route. These are first converted to Cartesian coordinates for accurate distance calculations. - Path Subdivision: The route is subdivided into smaller segments such that no segment is larger than the specified precision.
- Distance Calculation: For each point in the route, the library computes the distance of that point from the start of the route.
- KDTree Search: A KDTree is built from the subdivided route points to allow for efficient nearest neighbor searches.
- Point Processing: The
get_dist_from_startmethod takes in test points, finds the nearest neighbor using the KDTree, then looks up the distance of that route point from the start of the route.
Subtleties
- Precision Control: The
precisionparameter controls how finely the route is subdivided. A smaller precision leads to more accurate distance calculations but increases computation time. - Distance Units: The library supports four distance units: meters, feet, miles, and kilometers. The choice of unit affects the output distances. This unit applies to the precision, distances from the start, and perpendicular distances.
- Off-Route Points: Test points whose nearest neighbor is the first or last point of the route are considered off-route and will return NaN for their distance from the start.
- Perpendicular Distance Filtering: The optional
max_dist_from_routeparameter allows users to classify points that are too far from the route as off-route, returning NaN for their distance from the start.
API Reference
Route Class
The main class for route distance calculations.
Constructor
Route(lat_lon_pts, precision=1.0, distance_unit="feet")
Parameters:
lat_lon_pts(np.ndarray): Array of shape (n, 2) containing lat/lon coordinatesprecision(float): Maximum distance between subdivided path points (default: 1.0)distance_unit(str): Unit for distance calculations - "meters", "feet", "miles", or "kilometers" (default: "feet")
Methods
get_dist_from_start()
get_dist_from_start(lat_lon_pt, max_dist_from_route=None)
Parameters:
lat_lon_pt(np.ndarray): Single point as [lat, lon] or array of points with shape (n, 2)max_dist_from_route(float, optional): Maximum perpendicular distance from route. Points beyond this distance will return NaN.
Returns:
- Tuple of (distances_from_start, distances_to_route) in the same unit as the route
Distance Units
Supported distance units:
"meters"- SI base unit"feet"- US customary unit (default)"miles"- US customary unit"kilometers"- SI unit
Precision Control
The precision parameter controls the accuracy of distance calculations:
- Lower values (e.g., 0.1) = Higher accuracy, slower processing
- Higher values (e.g., 10.0) = Lower accuracy, faster processing
Choose based on your accuracy requirements and performance needs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dist_along_route-0.1.0.tar.gz.
File metadata
- Download URL: dist_along_route-0.1.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb80216719b60f0dd22ec88a6c2bf28667e9f08bb89e99259f4884a85107e1b8
|
|
| MD5 |
966e9c2ceedb03e8c2b1dd5c52025515
|
|
| BLAKE2b-256 |
5386a3166e029a7dcae68716c8fac0b568664b58fc80e70f8f224ec7e6c2919b
|
Provenance
The following attestation bundles were made for dist_along_route-0.1.0.tar.gz:
Publisher:
python-publish.yml on ThisTemba/dist-along-route
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dist_along_route-0.1.0.tar.gz -
Subject digest:
eb80216719b60f0dd22ec88a6c2bf28667e9f08bb89e99259f4884a85107e1b8 - Sigstore transparency entry: 422395211
- Sigstore integration time:
-
Permalink:
ThisTemba/dist-along-route@d35b3d47b3c85a87ba9817de45b7dbd97a6fe629 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ThisTemba
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d35b3d47b3c85a87ba9817de45b7dbd97a6fe629 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dist_along_route-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dist_along_route-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f65c4d0f93c7e2ac22193353c4296b14a7f6a4eb0b3123448f42f063403e084e
|
|
| MD5 |
103d0401873d1536fd0a04901ae4cae0
|
|
| BLAKE2b-256 |
d2f567ef1099afda53184ca8e4c1d1ca4525fdfa5537ffa087573e70880b2492
|
Provenance
The following attestation bundles were made for dist_along_route-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on ThisTemba/dist-along-route
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dist_along_route-0.1.0-py3-none-any.whl -
Subject digest:
f65c4d0f93c7e2ac22193353c4296b14a7f6a4eb0b3123448f42f063403e084e - Sigstore transparency entry: 422395237
- Sigstore integration time:
-
Permalink:
ThisTemba/dist-along-route@d35b3d47b3c85a87ba9817de45b7dbd97a6fe629 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ThisTemba
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d35b3d47b3c85a87ba9817de45b7dbd97a6fe629 -
Trigger Event:
release
-
Statement type: