Skip to main content

Simple OSRM routing and Folium map drawing toolkit

Project description

cartons

PyPI version Downloads

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.

Geneva to Zürich route

Bern → Zürich

A shorter intercity route, useful for quick demos and examples.

Bern to Zürich route

Zoomed-in route detail

A close-up example showing that the route geometry stays clear and useful even at high zoom levels.

Zoomed-in route detail


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.geometry
  • route.distance
  • route.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

  • cartons depends 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 step
  • simpledraw() is best when you already have coordinates
  • simpleroute()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:

  1. Open an issue
  2. 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


Download files

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

Source Distribution

cartons-1.1.8.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cartons-1.1.8-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file cartons-1.1.8.tar.gz.

File metadata

  • Download URL: cartons-1.1.8.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cartons-1.1.8.tar.gz
Algorithm Hash digest
SHA256 8642c77db77efe29ebb1eeb94a2b6a222c233b79c73d1e29233289950036c681
MD5 0605d7f30d825a25dabf3906712d30e8
BLAKE2b-256 987fb22bf5c46636d05193599a4488fac70fed4ebf25217a4d5d015d4056b358

See more details on using hashes here.

Provenance

The following attestation bundles were made for cartons-1.1.8.tar.gz:

Publisher: python-publish.yml on AndPan3/cartons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cartons-1.1.8-py3-none-any.whl.

File metadata

  • Download URL: cartons-1.1.8-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.7

File hashes

Hashes for cartons-1.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d12ecad27b2e92fd45dbd06e3c3293251700ac7682638bb663e69b8c51d077
MD5 4942d4c0947d94c4a219c85d610b03a9
BLAKE2b-256 9c9ce0beb2fb0b11f80a4ffe1d7f741ce82ebcbf36e70d7c24a5c66f29181922

See more details on using hashes here.

Provenance

The following attestation bundles were made for cartons-1.1.8-py3-none-any.whl:

Publisher: python-publish.yml on AndPan3/cartons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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