Skip to main content

Blazingly fast spatio-temporal database library

Project description

Spatio for Python

Fast spatial database for Python, powered by Rust.

PyPI Python 3.9+ License: MIT

What is it?

Spatio is an embedded database for location data. Store points on a map, query "what's nearby", track movement over time - all with minimal setup.

Install

pip install spatio

Supports: Linux (x86_64, aarch64), macOS (Intel, Apple Silicon), Python 3.9-3.13.

Windows not supported - use WSL2 or Docker.

Quick Start

import spatio

db = spatio.Spatio.memory()

nyc = spatio.Point(-74.0060, 40.7128)
db.insert_point("cities", nyc, b"New York")

# Find nearby (within 100km)
nearby = db.query_within_radius("cities", nyc, 100000, 10)
for point, data, distance in nearby:
    print(f"{data.decode()}: {distance/1000:.1f}km away")

Examples

Basic Operations

import spatio

# Create database
db = spatio.Spatio.memory()                # In-memory
db = spatio.Spatio.open("data.db")         # Persistent

# Store key-value data
db.insert(b"user:123", b"John Doe")
data = db.get(b"user:123")
db.delete(b"user:123")

# Store points
point = spatio.Point(-74.0060, 40.7128)
db.insert_point("pois", point, b"Restaurant")

# Query spatial
nearby = db.query_within_radius("pois", point, 1000.0, 10)
count = db.count_within_radius("pois", point, 1000.0)
exists = db.contains_point("pois", point, 1000.0)

Distance Calculations

nyc = spatio.Point(-74.0060, 40.7128)
london = spatio.Point(-0.1278, 51.5074)

# Haversine (fast, spherical Earth)
metric = spatio.DistanceMetric("haversine")
distance = db.distance_between(nyc, london, metric)
print(f"{distance/1000:.0f}km")  # ~5570km

# Geodesic (accurate, ellipsoidal Earth - Karney 2013)
metric = spatio.DistanceMetric("geodesic")
distance = db.distance_between(nyc, london, metric)

# Also available: "rhumb", "euclidean"

K-Nearest Neighbors

center = spatio.Point(-74.0, 40.7)

# Find 5 nearest within 50km
metric = spatio.DistanceMetric("haversine")
nearest = db.knn("cities", center, 5, 50000.0, metric)

for point, data, distance in nearest:
    print(f"{data.decode()}: {distance/1000:.1f}km")

Bounding Box Queries

# Find all points in rectangle
results = db.find_within_bounds(
    "cities",
    min_lat=40.0,
    min_lon=-75.0,
    max_lat=41.0,
    max_lon=-73.0,
    limit=100
)

# Check if any exist in box
has_points = db.intersects_bounds("cities", 40.0, -75.0, 41.0, -73.0)

Polygon Queries

# Points within polygon boundary
polygon = [
    (-74.0, 40.7),
    (-73.9, 40.7),
    (-73.9, 40.8),
    (-74.0, 40.8),
]
inside = db.query_within_polygon("pois", polygon, 100)

Trajectories

import time

# Track movement
trajectory = [
    (spatio.Point(-74.00, 40.71), int(time.time())),
    (spatio.Point(-74.01, 40.72), int(time.time()) + 60),
    (spatio.Point(-74.02, 40.73), int(time.time()) + 120),
]
db.insert_trajectory("vehicle:truck001", trajectory)

# Query movement history
start = int(time.time()) - 3600
end = int(time.time())
path = db.query_trajectory("vehicle:truck001", start, end)

for point, timestamp in path:
    print(f"At ({point.lon}, {point.lat}) at {timestamp}")

TTL (Auto-Expiration)

import spatio

db = spatio.Spatio.memory()

# Expire after 5 minutes
opts = spatio.SetOptions.with_ttl(300.0)
db.insert(b"session:abc", b"user data", opts)

# Returns None if expired
data = db.get(b"session:abc")

# Clean up expired items manually
removed = db.cleanup_expired()
print(f"Removed {removed} expired items")

Important: TTL is lazy. Expired items stay in memory until you call cleanup_expired() or they're overwritten. For long-running apps, clean up periodically to avoid memory leaks.

Configuration

config = spatio.Config()
db = spatio.Spatio.memory_with_config(config)

# That's it - most defaults are sensible
# See Rust docs for advanced options

API Reference

Spatio Class

# Create
Spatio.memory()
Spatio.open(path)
Spatio.memory_with_config(config)
Spatio.open_with_config(path, config)

# Key-value
insert(key, value, options=None)
get(key) -> bytes | None
delete(key) -> bytes | None

# Spatial
insert_point(prefix, point, value, options=None)
query_within_radius(prefix, center, radius_meters, limit)
count_within_radius(prefix, center, radius_meters)
contains_point(prefix, center, radius_meters)
find_within_bounds(prefix, min_lat, min_lon, max_lat, max_lon, limit)
intersects_bounds(prefix, min_lat, min_lon, max_lat, max_lon)
query_within_polygon(prefix, polygon_coords, limit)

# Distance
distance_between(point1, point2, metric)
knn(prefix, center, k, max_radius, metric)

# Trajectories
insert_trajectory(object_id, trajectory, options=None)
query_trajectory(object_id, start_time, end_time)

# Maintenance
cleanup_expired() -> int
stats() -> dict
close()

Point Class

Point(longitude, latitude, altitude=None)

# Properties
point.lon  # -180 to 180
point.lat  # -90 to 90
point.alt  # Always None (2D only for now)

# Methods
point.distance_to(other_point) -> float  # meters, Haversine

SetOptions Class

SetOptions.with_ttl(seconds)
SetOptions.with_expiration(unix_timestamp)

DistanceMetric Class

DistanceMetric("haversine")   # Fast spherical
DistanceMetric("geodesic")    # Accurate ellipsoidal
DistanceMetric("rhumb")       # Constant bearing
DistanceMetric("euclidean")   # Planar only

Performance Tips

  1. Batch inserts when possible
  2. Use namespaces to separate data types
  3. Set reasonable limits on queries
  4. Call cleanup_expired() periodically if using TTL
  5. Use persistent storage for data you can't lose

Building from Source

git clone https://github.com/pkvartsianyi/spatio
cd spatio/py-spatio

# Install maturin
pip install maturin

# Build and install
maturin develop

# Or build wheel
maturin build --release

Platform Support

Supported:

  • Linux: x86_64, aarch64 (manylinux)
  • macOS: x86_64 (Intel), arm64 (Apple Silicon)
  • Python: 3.9, 3.10, 3.11, 3.12, 3.13

Not supported:

  • Windows (use WSL2, Docker, or VM)

See main PLATFORMS.md for details.

Examples

Check the examples/ directory:

  • basic_usage.py - Getting started
  • performance_demo.py - Benchmarks
  • trajectory_tracking.py - Movement tracking

Links

License

MIT - see LICENSE

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

spatio-0.1.7-cp313-cp313-manylinux_2_35_x86_64.whl (356.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

spatio-0.1.7-cp313-cp313-macosx_11_0_arm64.whl (307.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

spatio-0.1.7-cp313-cp313-macosx_10_12_x86_64.whl (335.1 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

spatio-0.1.7-cp312-cp312-manylinux_2_35_x86_64.whl (356.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ x86-64

spatio-0.1.7-cp312-cp312-macosx_11_0_arm64.whl (307.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

spatio-0.1.7-cp312-cp312-macosx_10_12_x86_64.whl (335.1 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

spatio-0.1.7-cp311-cp311-manylinux_2_35_x86_64.whl (356.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ x86-64

spatio-0.1.7-cp311-cp311-macosx_11_0_arm64.whl (307.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

spatio-0.1.7-cp311-cp311-macosx_10_12_x86_64.whl (334.9 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

spatio-0.1.7-cp310-cp310-manylinux_2_35_x86_64.whl (356.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

spatio-0.1.7-cp310-cp310-macosx_11_0_arm64.whl (307.5 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

spatio-0.1.7-cp310-cp310-macosx_10_12_x86_64.whl (335.0 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

spatio-0.1.7-cp39-cp39-manylinux_2_35_x86_64.whl (356.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.35+ x86-64

spatio-0.1.7-cp39-cp39-macosx_11_0_arm64.whl (307.7 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

spatio-0.1.7-cp39-cp39-macosx_10_12_x86_64.whl (335.2 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

Details for the file spatio-0.1.7-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 c290bd28022ce80824938091dc847e9d4c4faa0e84c9071d5b49423e9fd9ac05
MD5 c5af2b94798bdd70e93bd1ff2eeca17b
BLAKE2b-256 45b8bdac9c9f982f25c2f8d734c81984828a39b947a326bd51ca30f433264645

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1ee830e29c332f35173bb75870e793f5f4864873bcac4832f95c01088559682d
MD5 dd91f6d72b6baa13c8180514b4dde6eb
BLAKE2b-256 9270ad35e09de6df33386248ae19f4018eaa6c8d2061281a944669e058e06837

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 31ee2daf0fef805ff1e8ae3f4269520558ea56eb030d511e3cf1268f50c03cc9
MD5 fe41874da3b00520a6532812716c1429
BLAKE2b-256 9f0a0df092ce14259e2d42f50104c245c62bcf5ded4047ed8cc4efa52ca5fdf6

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 b31a80e515793469192ef65c97c27ffcfc9bd5bb28961f582fc8fd95be125128
MD5 9baa1eb7ebb0c47b6412ebdb067a2807
BLAKE2b-256 1417762c61778d1dcf06422deaa14b1a27ef1f945f715f5ac77159570ea4da99

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5de83d1e02c0019990300bc546cbfc8ba1063b99bdce8888933c3886b6bc40e9
MD5 e14fcd809c4d34ce6c6a4926897b8fb6
BLAKE2b-256 c391864b1fd7305973b52e09e65a4c035d25ec219abac3c4db76fcd0a324f962

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d77b2ac12943a94609cb18f9c75277d09d7b38e2baf32b4e4d2ddc921313ee5b
MD5 586a9d00c7321a43ca5e7d506d3de088
BLAKE2b-256 4292974a398ed5dcad1a8cb992c09960d57a75596e014d9f351802f655cd8ff6

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp311-cp311-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 3a02d6e30e9e854f0ee234c6581907fa205dc4298543106cf76861c3395fb867
MD5 32ce01770a2621e10c96a6b9a63ceee8
BLAKE2b-256 9343188a82bcafd34dc75d9a3843b37b787cb6e925467b2a4198a30593821b57

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b42e2ce4950646e58e084d00b278c326abbf6d3ea1552c916d31f74a6af71b3
MD5 9333641edcd0c0bb506f0f716a656370
BLAKE2b-256 5e273efe51133385cb3fc6a6324af520df19f9511b0f9f61867a7405205a6280

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7ae125ee03935c57a7e0558c828b1b50e81769668379af24a9efdbe03fefaa0d
MD5 9a4910df5cb037140959142bdb3815f5
BLAKE2b-256 0877fbea8b80380a061ed54b6ab3f4a337bd929df07715796f57382ab4ebc3fd

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 45c5980deaa5882f8e8e7f4fafbcf13e650cc30c95d548800ba8d2d52e8994bc
MD5 6b2a8963098b1ff916057b6792e4145c
BLAKE2b-256 4e3a5fa70cd5880340bffb5fe94a0e222650bfe788befaf2d98606f3de4d4658

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 03b5a7aaa21f3050c3a50c9983c3449d48e1cc034a67590aa8211cbd856e7245
MD5 15e87ee0906ad87c89abeb7a0eec58f3
BLAKE2b-256 30763163317ff2e8674b672d52633cb04a0f9098af52a5d701deccb663750909

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e525fc0edfc2fd1be2f64fa0dd572398064e2aef57a63ffb5424a04aa484429b
MD5 d4d50861ab10ef5d312a502f8201b6cc
BLAKE2b-256 b81e54cd9d4561b9bb31d53b99bee502aad36553fa0e04266feede6bc848cc33

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp39-cp39-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 5c418352b4a7fa20eaf3896bb5568c9b121b40cad7f97fd401cdfe72a1a809ea
MD5 d7c5c3f1f709ecbf35eb25f288d0bf54
BLAKE2b-256 be98a11b448e2186c25992835220ebb36be82a4dc00f0b394f30c61af7402a2f

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a8a2ba3ce907faaca37b7dc3fad235bdcb955798af3a0cd3c7212f16e32229f9
MD5 49991fbc0efc3eb65a03e31aa141e542
BLAKE2b-256 fc65f0958889b2b56e1c2088c5afa2ec5a1cb567a8222ddf1a14c5fb6d1bf558

See more details on using hashes here.

File details

Details for the file spatio-0.1.7-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.1.7-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fd95774e39b7640568f87d33243c10f51fe932cf5c37ac51a0a28d9f612ebfcd
MD5 25f1d414a1803e8b926bf251a4c4190b
BLAKE2b-256 192659ea863b95214215b71d178c3f248f36e9ccc252d73bf4225e9fdab8712b

See more details on using hashes here.

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