H3 and NetworkX based route affinity graph toolkit.
Project description
sameer-graph-lib
sameer-graph-lib is an editable Python library for building H3-based route affinity graphs with NetworkX.
It turns H3 arrays, latitude/longitude sequences, and encoded polylines into connected hex chains, inserts them into a weighted graph, extracts high-affinity corridors, and decomposes the graph into a main trunk plus minor branches.
Editable install
python -m pip install -e ".[dev,plot]"
Because the install is editable, changes you make inside src/sameer_graph_lib are picked up immediately by Python without reinstalling.
If your machine uses uv, run commands through the managed environment:
uv run --extra dev --extra plot python -c "import sameer_graph_lib; print(sameer_graph_lib.__version__)"
Install
From PyPI after publication:
pip install sameer-graph-lib
With optional plotting and geospatial extras:
pip install "sameer-graph-lib[plot,geo]"
Quick start
from sameer_graph_lib import HexGraph
graph = HexGraph(hex_resolution=9)
route = graph.add_latlng_sequence([
(12.9716, 77.5946),
(12.9760, 77.5990),
])
print(graph.get_graph_stats())
selected = graph.get_appropriate_hexes(cutoff=0.8)
fig = graph.visualize_graph(title="80% compact cluster", highlight_hexes=selected)
print(graph.decompose_branches())
Main APIs
SpatialIngestor: converts H3 arrays, lat/lng sequences, and encoded polylines into contiguous H3 chains.AffinityGraph: NetworkX wrapper for array-based insertion, nearest attachment, affinity scoring, editing, and JSON persistence.CorridorExtractor: uses exact all-node Dijkstra selection to extract the most compact cluster covering a target percentage of graph traversal volume.TopologyAnalyzer: separates the main branch from residual minor branches.HexGraph: backwards-compatible convenience class for your original code style.
Graph creation follows the original per-node procedure: H3 arrays are normalized, then each hex is inserted with add_node/add_hex. Lat/lng sequences and encoded polylines are first converted into H3 arrays at the requested resolution, then inserted the same way.
For QC, use:
fig = graph.visualize_graph(highlight_hexes=selected)
fig.savefig("graph_qc.png", dpi=150, bbox_inches="tight")
fig = graph.visualize_step_by_step(route[:5], labels=["A", "B", "C", "D", "E"])
fig.savefig("insertion_steps.png", dpi=150, bbox_inches="tight")
To plot actual H3 hex boundaries as geospatial polygons:
from sameer_graph_lib import plot_h3_cells, plot_h3_cells_map
fig = plot_h3_cells("88618c4f29fffff", label_full_hex=True)
fig.savefig("single_h3_cell.png", dpi=150, bbox_inches="tight")
fig = graph.plot_h3_cells(highlight_hexes=selected, show_labels=False)
fig.savefig("h3_cell_footprint.png", dpi=150, bbox_inches="tight")
fig = plot_h3_cells_map(route, selected_cells=selected)
fig.savefig("h3_cell_basemap.png", dpi=150, bbox_inches="tight")
plot_h3_cells_map uses GeoPandas + Contextily. Install it with:
python -m pip install -e ".[plot,geo]"
uv run --extra plot --extra geo python -c "from sameer_graph_lib import plot_h3_cells_map"
To get H3 centers and a convex hull:
from sameer_graph_lib import getLatLng, h3_convex_hull
points = getLatLng(route) # [(lat, lng), ...]
hull = h3_convex_hull(route) # Shapely geometry in (lng, lat)
graph_hull = graph.convex_hull() # Same, using graph nodes
Useful commands
python -m pytest
python -m build
uv run --extra dev pytest -q
uv run --extra dev python -m build
uv run --extra dev python -m twine check dist/*
Build artifacts will appear in dist/ after python -m build.
Publish To PyPI
- Build the package:
uv run --extra dev python -m build
- Validate the package metadata:
uv run --extra dev python -m twine check dist/*
- Upload to PyPI:
uv run --extra dev python -m twine upload dist/*
After upload, users can install it with:
pip install sameer-graph-lib
Publish From GitHub
This repo also includes a Trusted Publishing workflow in .github/workflows/publish.yml.
To finish that setup:
- Create the project on PyPI, or reserve the name
sameer-graph-lib. - On PyPI, open the project settings and add a Trusted Publisher for:
owner:iams31repository:sameer_graph_libworkflow:publish.ymlenvironment:pypi - Create a GitHub Release, or run the workflow manually from the Actions tab.
After that, GitHub Actions can publish without storing a long-lived PyPI token.
Official references:
- PyPI Trusted Publishing: https://docs.pypi.org/trusted-publishers/
- Packaging guide upload flow: https://packaging.python.org/tutorials/packaging-projects/
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 sameer_graph_lib-0.2.0.tar.gz.
File metadata
- Download URL: sameer_graph_lib-0.2.0.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
665a1f4064d7b91f74cf43799a02b5f1e2b685ec2c0408142c80859a7b8d09d5
|
|
| MD5 |
0e919847913ca136d544a91876e06a68
|
|
| BLAKE2b-256 |
dbfced63b11731767c25c8f9ab00f987fe8b635fc0c481562323a407049acb25
|
Provenance
The following attestation bundles were made for sameer_graph_lib-0.2.0.tar.gz:
Publisher:
publish.yml on iams31/sameer_graph_lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sameer_graph_lib-0.2.0.tar.gz -
Subject digest:
665a1f4064d7b91f74cf43799a02b5f1e2b685ec2c0408142c80859a7b8d09d5 - Sigstore transparency entry: 1395127797
- Sigstore integration time:
-
Permalink:
iams31/sameer_graph_lib@8f959fae367dd2b094039eddc44c9cb49c467dbb -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/iams31
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f959fae367dd2b094039eddc44c9cb49c467dbb -
Trigger Event:
release
-
Statement type:
File details
Details for the file sameer_graph_lib-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sameer_graph_lib-0.2.0-py3-none-any.whl
- Upload date:
- Size: 25.7 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 |
e640fca4e8a2ed84b874a18f74aab1227c18d3881ee2debc4f4f0b7136af21ca
|
|
| MD5 |
86507742c45d267790e23dc2489d5656
|
|
| BLAKE2b-256 |
c7bb71d612fd0573ed64869f0849b0ba990926f79d575fb44e75833c7f1176b8
|
Provenance
The following attestation bundles were made for sameer_graph_lib-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on iams31/sameer_graph_lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sameer_graph_lib-0.2.0-py3-none-any.whl -
Subject digest:
e640fca4e8a2ed84b874a18f74aab1227c18d3881ee2debc4f4f0b7136af21ca - Sigstore transparency entry: 1395127901
- Sigstore integration time:
-
Permalink:
iams31/sameer_graph_lib@8f959fae367dd2b094039eddc44c9cb49c467dbb -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/iams31
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f959fae367dd2b094039eddc44c9cb49c467dbb -
Trigger Event:
release
-
Statement type: