This Python script sorts unsorted geographical data (from a CSV file) based on its proximity to a predefined GPX track. It generates a sorted "roadbook" by ordering places in the sequence they are encountered along the route. Ideal for cyclists, hikers, and geospatial analysts working with Overpass API results or similar unsorted data sources.
- Align Places to GPX Track: Matches CSV places (e.g., towns, landmarks) to the nearest points on a GPX track.
- Unsorted Input Handling: Works with raw, unsorted input CSV data.
- Smart Sorting: Orders places by their position along the GPX track.
- Logging and Progress Bar: Provides detailed progress updates and logs for a user-friendly experience.
- GPX Simplification: Optimizes GPX points for faster processing.
- Python 3.7 or higher
- Required Python libraries:
gpxpy
pandas
geopy
rdp
tqdm
Install the dependencies using:
pip install gpxpy pandas geopy rdp tqdm
- A GPX file defining the route (
example_route.gpx
). - A CSV file containing places (
places.csv
) with the following required columns:- Y: Latitude of the place.
- X: Longitude of the place.
Execute the script with your file paths:
# Example paths
gpx_file = r'example_route.gpx'
csv_file = r'places.csv'
output_file = r'sorted_places.csv'
# Process the CSV and generate the sorted roadbook
process_csv_with_smart_sorting(csv_file, gpx_points, output_file)
The output is a CSV file (sorted_places.csv
) with places sorted in the order they occur along the GPX track.
Name | Y (Latitude) | X (Longitude) |
---|---|---|
Gunnison | 38.5458 | -106.9287 |
San Francisco | 37.7749 | -122.4194 |
Blanding | 37.6240 | -109.4780 |
Name | Y (Latitude) | X (Longitude) | GPX Index |
---|---|---|---|
San Francisco | 37.7749 | -122.4194 | 1 |
Blanding | 37.6240 | -109.4780 | 2 |
Gunnison | 38.5458 | -106.9287 | 3 |
- GPX Simplification: Reduces the number of GPX points using the Ramer-Douglas-Peucker (RDP) algorithm for better performance.
- Nearest GPX Point: Finds the closest GPX point for each place in the CSV file.
- Sorting by Index: Orders the places based on their position along the GPX track.
- Output Roadbook: Outputs a CSV file with the sorted order.
The script provides:
- Logs: Detailed messages for each processing step (e.g., file loading, GPX simplification, sorting).
- Progress Bar: A real-time progress bar for processing places.
Example log output:
2025-01-02 12:00:00 - INFO - Loading GPX file: example_route.gpx
2025-01-02 12:00:01 - INFO - GPX file loaded with 10,000 points.
2025-01-02 12:00:01 - INFO - Starting GPX simplification with epsilon=0.001.
2025-01-02 12:00:02 - INFO - GPX simplification complete: 500 points (from 10,000).
2025-01-02 12:00:02 - INFO - Finding nearest GPX point for each place...
Processing places: 100%|████████████████████| 100/100 [00:02<00:00, 50.00it/s]
2025-01-02 12:00:24 - INFO - Roadbook saved successfully to sorted_places.csv.
This script is licensed under the Creative Commons Attribution (CC-BY) license. You are free to use, share, and adapt it, provided that appropriate credit is given.
Created by Christian Prior-Mamulyan.
For questions or feedback, email me at [email protected].