Simple OSRM routing and Folium map drawing toolkit
Project description
cartons
cartons is a lightweight Python toolkit for routing and interactive map visualization.
It combines the power of OSRM (Open Source Routing Machine) for route calculation with Folium for rendering beautiful interactive maps in HTML. With only a few lines of Python, you can calculate a route between two coordinates, extract route data such as geometry, distance, and duration, and display the result on a slippy map.
Why cartons?
cartons is made for people who want to work with routes without building everything from scratch.
It gives you a clean and simple way to:
- calculate a route between two coordinates
- retrieve the full route object from OSRM
- draw the route on an interactive map
- save the map as an HTML file
- customize route color, line thickness, tiles, and attribution
- work with precomputed coordinate lists for fast visualization
It is intentionally lightweight, easy to understand, and easy to integrate into scripts, notebooks, demos, and small GIS projects.
Preview
Geneva → Zürich
A long-distance route rendered across Switzerland.
Bern → Zürich
A shorter intercity route, useful for quick demos and examples.
Zoomed-in route detail
A close-up example showing that the route geometry stays clear and useful even at high zoom levels.
Features
Routing
- Route calculation between two geographic coordinates
- Uses an OSRM routing server through
routingpy - Returns the full route object, not just a line
- Route object can expose useful data such as:
- geometry
- distance
- duration
Visualization
- Draw routes on interactive Folium maps
- Save maps as
.html - Open generated maps in a browser
- Zoom and pan freely
- Use custom tile providers
- Use custom route color and line weight
Lightweight API
- Small, simple public API
- Minimal setup
- Easy to learn
- Good for scripts, prototypes, notebooks, and teaching
Fast drawing mode
- Draw an already existing coordinate list directly
- Useful when you already have route geometry and do not need to query OSRM again
Installation
Install from PyPI:
pip install cartons
Dependencies
cartons is built on top of:
Quick Start
import cartons
import webbrowser
# Bern → Zürich
m = cartons.draw(
"https://router.project-osrm.org",
7.4442153, 46.94686,
8.5431302, 47.3668725,
col="red",
weight=5,
tiles="CartoDB Positron",
attribution="© CartoDB Positron",
transport="car"
)
filename = "route.html"
m.save(filename)
webbrowser.open(filename)
This creates an interactive HTML map with the calculated route drawn on top.
How cartons works
The workflow is simple:
Coordinates
↓
cartons.route()
↓
OSRM route calculation
↓
route object returned
↓
cartons.draw()
↓
Folium map
↓
Interactive HTML output
Public API
The package exposes these functions:
cartons.route(...)cartons.draw(...)cartons.simpledraw(...)
These are imported in the package root, so they are available directly after import cartons.
API Reference
route()
Calculates a route using an OSRM server and returns the full route object.
cartons.route(base_url, lon1, lat1, lon2, lat2, transport="car")
Parameters
| Parameter | Description |
|---|---|
base_url |
URL of the OSRM routing server |
lon1 |
Longitude of the start point |
lat1 |
Latitude of the start point |
lon2 |
Longitude of the destination |
lat2 |
Latitude of the destination |
transport |
Routing profile, such as car, bike, or foot |
Returns
A route object returned by routingpy / OSRM.
What you can read from the returned route
Typical useful properties include:
route.geometryroute.distanceroute.duration
Example
import cartons
route = cartons.route(
"https://router.project-osrm.org",
7.4442153, 46.94686,
8.5431302, 47.3668725,
transport="car"
)
print("Distance (m):", route.distance)
print("Duration (s):", route.duration)
print("Number of geometry points:", len(route.geometry))
Friendly formatting example
print(f"Distance: {route.distance / 1000:.2f} km")
print(f"Duration: {route.duration / 60:.1f} min")
draw()
Calculates a route and immediately draws it on a Folium map.
cartons.draw(
base_url,
lon1,
lat1,
lon2,
lat2,
col="blue",
weight=5,
tiles="CartoDB Positron",
attribution="© CartoDB Positron",
transport="car"
marker=True
)
Parameters
| Parameter | Description |
|---|---|
base_url |
URL of the OSRM routing server |
lon1 |
Longitude of the start point |
lat1 |
Latitude of the start point |
lon2 |
Longitude of the destination |
lat2 |
Latitude of the destination |
col |
Route line color |
weight |
Route line thickness |
tiles |
Tile source used by Folium |
attribution |
Attribution text for the tile provider |
transport |
Routing profile used by OSRM |
marker |
Boolean used if you want Markers at start/end coords. |
Returns
A folium.Map object.
What it does internally
- calls
route(...) - reads
route.geometry - converts OSRM coordinate order into Folium coordinate order
- draws the route as a
folium.PolyLine - returns the map object
simpledraw()
Draws a route directly from an existing list of coordinates. The pre-existing coordinates should be formatted like this:python [lat,lon],[lat,lon],[lat,lon]and so on.
cartons.simpledraw(
coordslatlon,
col="blue",
weight=5,
tiles="CartoDB Positron",
attribution="© CartoDB Positron"
)
Parameters
| Parameter | Description |
|---|---|
coordslatlon |
List of coordinates in [lat, lon] format |
col |
Route line color |
weight |
Route line thickness |
tiles |
Tile source used by Folium |
attribution |
Attribution text for the tile provider |
Returns
A folium.Map object.
When to use simpledraw()
Use simpledraw() when:
- you already have route coordinates
- you want to visualize custom paths
- you do not want to call the routing server again
- you are drawing preprocessed or cached geometry
Examples
simpleroute()
Calculates a route and immediately draws it on a Folium map but with less customisation and therefore less code.
cartons.draw(
base_url,
lon1,
lat1,
lon2,
lat2,
transport="car"
)
Parameters
| Parameter | Description |
|---|---|
base_url |
URL of the OSRM routing server |
lon1 |
Longitude of the start point |
lat1 |
Latitude of the start point |
lon2 |
Longitude of the destination |
lat2 |
Latitude of the destination |
transport |
Routing profile used by OSRM |
Returns
A folium.Map object.
What it does internally
- calls
route(...) - reads
route.geometry - converts OSRM coordinate order into Folium coordinate order
- draws the route as a
folium.PolyLine - returns the map object
1. Calculate a route and inspect metadata
import cartons
route = cartons.route(
"https://router.project-osrm.org",
6.143158, 46.204391, # Geneva
8.541694, 47.376887, # Zürich
transport="car"
)
print(f"Distance: {route.distance / 1000:.2f} km")
print(f"Duration: {route.duration / 3600:.2f} h")
2. Draw a route on an interactive map
import cartons
m = cartons.draw(
"https://router.project-osrm.org",
6.143158, 46.204391, # Geneva
8.541694, 47.376887, # Zürich
col="red",
weight=6,
transport="car"
)
m.save("geneva_zurich.html")
3. Draw precomputed coordinates
import cartons
coords = [
[46.94686, 7.4442153],
[47.0, 7.7],
[47.15, 8.0],
[47.3668725, 8.5431302]
]
m = cartons.simpledraw(coords, col="red", weight=5)
m.save("custom_route.html")
Use Cases
cartons is useful for:
- route visualization
- travel maps
- mobility demos
- logistics prototypes
- educational GIS examples
- interactive notebook demonstrations
- displaying routes in small web or data projects
- working with route geometry in Python
Design Goals
This project is designed to be:
- simple
- readable
- practical
- lightweight
- easy to integrate
- easy to extend
Instead of being a huge GIS framework, cartons focuses on doing a small number of routing and visualization tasks well.
Project Structure
cartons/
├── __init__.py
├── routing.py
├── simpleroute.py
└── display.py
routing.py
Handles route calculation through OSRM.
display.py
Handles map creation and route drawing through Folium.
simpleroute.py
Same as display.py but for simpleroute.
__init__.py
Exports the public API.
Development
Clone the repository:
git clone https://github.com/AndPan3/cartons.git
cd cartons
Install in editable mode:
pip install -e .
Notes
cartonsdepends on access to an OSRM server- the public demo server is great for testing, but production use may require your own server
- returned route data depends on the routing backend and profile used
draw()is best when you want a full route calculation and visualization in one stepsimpledraw()is best when you already have coordinatessimpleroute()is best for easy routing and map display.
Roadmap Ideas
now availiable under otherfiles/next.txt
Contributing
Contributions, suggestions, and improvements are welcome.
If you find a bug or want to improve the project:
- Open an issue
- Submit a pull request
License
MIT License
Author
AndPan3
Acknowledgements
This project uses:
- OSRM for route computation
- routingpy as the Python routing interface
- Folium for map rendering
- CartoDB Positron / compatible tile providers for basemaps
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 cartons-1.2.0.tar.gz.
File metadata
- Download URL: cartons-1.2.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c97658879366dbb1c1cd3a0ca1fb6dd52579a45dbc7dd52e1852238ff9e488b9
|
|
| MD5 |
bec8dc2b4ac5e04ca1962e77e96c0150
|
|
| BLAKE2b-256 |
42cea823c41d148a315f69ad1cb7124757fa78302ce2b1a513df61891b5954a3
|
Provenance
The following attestation bundles were made for cartons-1.2.0.tar.gz:
Publisher:
python-publish.yml on AndPan3/cartons
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cartons-1.2.0.tar.gz -
Subject digest:
c97658879366dbb1c1cd3a0ca1fb6dd52579a45dbc7dd52e1852238ff9e488b9 - Sigstore transparency entry: 1348377169
- Sigstore integration time:
-
Permalink:
AndPan3/cartons@42f63ff2331ec8bc9b65b2dc0cde8f8b76a54e22 -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/AndPan3
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@42f63ff2331ec8bc9b65b2dc0cde8f8b76a54e22 -
Trigger Event:
release
-
Statement type:
File details
Details for the file cartons-1.2.0-py3-none-any.whl.
File metadata
- Download URL: cartons-1.2.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
924e38e70d73eec221625c3965bf41744cb73eea82abe842f1237d0988eeed10
|
|
| MD5 |
fb7467e0baa767d80d8a27460921a1d9
|
|
| BLAKE2b-256 |
73fa0bd1285d8cdb9657da190e102dd1367b382e44e3c917bfb8c5745b511ba1
|
Provenance
The following attestation bundles were made for cartons-1.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on AndPan3/cartons
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cartons-1.2.0-py3-none-any.whl -
Subject digest:
924e38e70d73eec221625c3965bf41744cb73eea82abe842f1237d0988eeed10 - Sigstore transparency entry: 1348377259
- Sigstore integration time:
-
Permalink:
AndPan3/cartons@42f63ff2331ec8bc9b65b2dc0cde8f8b76a54e22 -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/AndPan3
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@42f63ff2331ec8bc9b65b2dc0cde8f8b76a54e22 -
Trigger Event:
release
-
Statement type: