rtree-geodetic
Python bindings for rstar_geodetic, a
geodetic (longitude/latitude) R-tree with great-circle and WGS84 nearest-neighbour and
radius queries over point, linestring, and polygon geometries.
Each (lon, lat) is mapped to a unit vector on the sphere and indexed in an R-tree, so the
antimeridian and the poles are ordinary interior points.
Coordinates are longitude first, latitude second, in degrees; distances are returned in
metres. Queries return integer input positions.
Install
pip install rtree-geodetic
Wheels are built for CPython 3.10 and newer (a single abi3 wheel per platform).
Quick start
A tree is built from any iterable of geometry-likes: (lon, lat) pairs, GeoJSON mappings,
objects exposing __geo_interface__ (shapely geometries), or a single object whose
__geo_interface__ is a collection (a geopandas GeoSeries, a shapely Multi*, a
GeometryCollection, or a FeatureCollection).
from rtree_geodetic import GeodeticPointTree
tree = GeodeticPointTree([
(-0.1278, 51.5074), # 0 London
(2.3522, 48.8566), # 1 Paris
(13.4050, 52.5200), # 2 Berlin
])
tree.nearest((4.9041, 52.3676)) # 0 (London)
tree.nearest_with_distance((4.9041, 52.3676)) # (0, 357888.0) metres
tree.within_distance((4.9041, 52.3676), 400_000.0) # [0, 1]
tree.within_distance((4.9041, 52.3676), 400_000.0, return_distance=True)
# [(0, 357888.0), (1, 430123.5)]
tree.geometry(0).__geo_interface__
# {'type': 'Point', 'coordinates': (-0.1278, 51.5074)}
Point trees also offer a longitude/latitude rectangle query. Order the corners west-then-east to cross the antimeridian (the GeoJSON RFC 7946 convention):
tree = GeodeticPointTree([(179.0, 0.0), (-178.0, 1.0), (0.0, 0.0)])
tree.in_rectangle((170.0, -10.0), (-170.0, 10.0)) # [0, 1] (the two near the seam)
Linestring and polygon trees measure the minimum great-circle distance to the geometry (zero for a query inside a polygon):
from rtree_geodetic import GeodeticLineStringTree, GeodeticPolygonTree
lines = GeodeticLineStringTree([[(0, 0), (1, 1), (2, 0)], [(10, 10), (11, 11)]])
lines.nearest((1.0, 0.5)) # 0
square = [[(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]]
polys = GeodeticPolygonTree([square])
polys.nearest_with_distance((5.0, 5.0)) # (0, 0.0) -- inside
shapely and geopandas
The geometry views expose __geo_interface__, so shapely.geometry.shape reconstructs
them, and trees accept shapely geometries and geopandas GeoSeries directly:
import geopandas as gpd
from shapely.geometry import Point, shape
from rtree_geodetic import GeodeticPointTree
series = gpd.GeoSeries([Point(-0.1278, 51.5074), Point(2.3522, 48.8566)])
tree = GeodeticPointTree(series)
index = tree.nearest(Point(2.0, 49.0)) # 1 (Paris)
geom = shape(tree.geometry(index).__geo_interface__) # a shapely Point
Distance semantics
Distances are great-circle metres. By default they use a spherical Earth (the GRS80 mean radius, 6 371 008.8 m); against an ellipsoid the error is at most about 0.5%.
GeodeticPointTree also offers exact WGS84-ellipsoid geodesic distances (Karney's method)
through nearest_wgs84, nearest_with_distance_wgs84, and within_distance_wgs84:
tree.nearest_with_distance_wgs84((4.9041, 52.3676)) # (0, 358968.7) geodesic metres
Invalid coordinates or geometry (an out-of-range longitude or latitude, a non-finite value,
too few vertices, an edge spanning half the sphere, or an unclosed ring) raise
GeodeticError, a subclass of ValueError.
Performance
Measured with benchmarks/bench.py
(uv run --group bench python benchmarks/bench.py; seeded synthetic data,
Apple M2 Pro, Python 3.14, shapely 2.1.2, rtree 1.4.1, release build).
One million points distributed uniformly on the sphere, 10,000 queries:
| Operation | rtree-geodetic | shapely STRtree | Rtree |
|---|---|---|---|
| Build | 0.79 s | 0.23 s | 1.63 s |
| Nearest neighbour, per call | 3.5 us | 7.9 us | 21.4 us |
| Nearest neighbour, WGS84 geodesic | 2.6 us | not offered | not offered |
| Within 50 km (radius query, ~16 hits) | 10.5 us | 10.3 us* | not offered |
100,000 small linestrings and polygons, 2,000 queries:
| Operation | rtree-geodetic | shapely STRtree |
|---|---|---|
| Linestring build | 0.66 s | 0.02 s |
| Linestring nearest, per call | 4.4 us | 11.1 us |
| Polygon build | 2.44 s | 0.02 s |
| Polygon nearest, per call | 7.8 us | 12.6 us |
[!NOTE] The planar libraries index raw degrees, so their distances are in degrees and their results degrade away from the equator: on this dataset the
STRtreenearest neighbour is geodesically wrong for 10.9% of queries, and the starred radius query (an equator-equivalent degree radius) returns the wrong set at other latitudes. Shapely's batchquery_nearestamortises the Python boundary to 5.7 us per query at 1M points; rtree-geodetic offers per-call queries only.
rtree-geodetic build times include coordinate validation and, for linestrings and polygons, per-edge great-circle envelopes; construction traverses Python objects, so a numpy / GeoArrow fast path is future work.
Licence
Licensed under either of Apache License, Version 2.0 or MIT licence at your option.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
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 rtree_geodetic-0.2.1.tar.gz.
File metadata
- Download URL: rtree_geodetic-0.2.1.tar.gz
- Upload date:
- Size: 188.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c7559baf38a1603449d7a94d03a6809f19ffae10ffd206c686c33bfe99f4da0
|
|
| MD5 |
62d497c1454f44335174248d5556b87f
|
|
| BLAKE2b-256 |
657dfe62f91d560ff421622baf1b88e5a21ca2ed7279a3257d61adbfc833adff
|
Provenance
The following attestation bundles were made for rtree_geodetic-0.2.1.tar.gz:
Publisher:
wheels.yml on urschrei/rstar_geodetic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rtree_geodetic-0.2.1.tar.gz -
Subject digest:
0c7559baf38a1603449d7a94d03a6809f19ffae10ffd206c686c33bfe99f4da0 - Sigstore transparency entry: 2580156944
- Sigstore integration time:
-
Permalink:
urschrei/rstar_geodetic@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Branch / Tag:
refs/tags/py-v0.2.1 - Owner: https://github.com/urschrei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
wheels.yml@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file rtree_geodetic-0.2.1-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: rtree_geodetic-0.2.1-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 246.0 kB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a261422ffbaf6b6def941c71ff06c51f57bf9c49159a57dab776b8bb86f6227
|
|
| MD5 |
f89b974b958feadb1f0978193451ade2
|
|
| BLAKE2b-256 |
0a73170adfbc270db39e951ba7142fc174462b59a167ccbff6f48256e5659667
|
Provenance
The following attestation bundles were made for rtree_geodetic-0.2.1-cp310-abi3-win_amd64.whl:
Publisher:
wheels.yml on urschrei/rstar_geodetic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rtree_geodetic-0.2.1-cp310-abi3-win_amd64.whl -
Subject digest:
6a261422ffbaf6b6def941c71ff06c51f57bf9c49159a57dab776b8bb86f6227 - Sigstore transparency entry: 2580156983
- Sigstore integration time:
-
Permalink:
urschrei/rstar_geodetic@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Branch / Tag:
refs/tags/py-v0.2.1 - Owner: https://github.com/urschrei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
wheels.yml@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file rtree_geodetic-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: rtree_geodetic-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 408.6 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3abdd11d0fe0d43f22bc4c9080a189ca29db40ad4ac6136d719b93d6397833ba
|
|
| MD5 |
044c328009d80750a3f5b4401cf4c859
|
|
| BLAKE2b-256 |
ee121d6439061face3d3059f1091720e6f2eebe20ecc9e318618ec5d1bcf0f23
|
Provenance
The following attestation bundles were made for rtree_geodetic-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
wheels.yml on urschrei/rstar_geodetic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rtree_geodetic-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
3abdd11d0fe0d43f22bc4c9080a189ca29db40ad4ac6136d719b93d6397833ba - Sigstore transparency entry: 2580157036
- Sigstore integration time:
-
Permalink:
urschrei/rstar_geodetic@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Branch / Tag:
refs/tags/py-v0.2.1 - Owner: https://github.com/urschrei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
wheels.yml@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file rtree_geodetic-0.2.1-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: rtree_geodetic-0.2.1-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 357.1 kB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6eebfcb91f2f7f64694c1bed8772803e710f4437529022a34a8cea2859b30586
|
|
| MD5 |
411f57652b055bb93f6e2ff9e1bd970d
|
|
| BLAKE2b-256 |
b2d61d180de1d25f21bcdc8facf2ba5b37607849310dae5f8aede75c2765610a
|
Provenance
The following attestation bundles were made for rtree_geodetic-0.2.1-cp310-abi3-macosx_11_0_arm64.whl:
Publisher:
wheels.yml on urschrei/rstar_geodetic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rtree_geodetic-0.2.1-cp310-abi3-macosx_11_0_arm64.whl -
Subject digest:
6eebfcb91f2f7f64694c1bed8772803e710f4437529022a34a8cea2859b30586 - Sigstore transparency entry: 2580157006
- Sigstore integration time:
-
Permalink:
urschrei/rstar_geodetic@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Branch / Tag:
refs/tags/py-v0.2.1 - Owner: https://github.com/urschrei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
wheels.yml@572709da19acafb8e36c0eac536dd5b8dfe172b4 -
Trigger Event:
push
-
Statement type: