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, Docker, or a Linux VM.

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

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.2.2-cp313-cp313-manylinux_2_34_x86_64.whl (445.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

spatio-0.2.2-cp313-cp313-macosx_11_0_arm64.whl (382.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

spatio-0.2.2-cp312-cp312-manylinux_2_34_x86_64.whl (444.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

spatio-0.2.2-cp312-cp312-macosx_11_0_arm64.whl (382.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

spatio-0.2.2-cp311-cp311-manylinux_2_34_x86_64.whl (444.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

spatio-0.2.2-cp311-cp311-macosx_11_0_arm64.whl (383.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

spatio-0.2.2-cp310-cp310-manylinux_2_34_x86_64.whl (447.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

spatio-0.2.2-cp310-cp310-macosx_11_0_arm64.whl (384.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

spatio-0.2.2-cp39-cp39-manylinux_2_34_x86_64.whl (449.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

spatio-0.2.2-cp39-cp39-macosx_11_0_arm64.whl (387.4 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file spatio-0.2.2-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.2.2-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c0606636d676b84c916863c9a6ccc4966d89dac6f731eed1d6e229bb3170276e
MD5 dc9b72ddba9e6be5ad412141962f56f4
BLAKE2b-256 255928247bf6737424be0ddc16880482cc460923cd03580667f9c3ce836046c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e81d25d4511971ea3f20fd588ed1f6b9d8c5c19242552274a5783dd94fd3b4b
MD5 856dc066cf4f8d62afe67213172179eb
BLAKE2b-256 fa7ddebfe422e7ee8fea9e47913525e362c26e606ae335d84358cee361f20b01

See more details on using hashes here.

File details

Details for the file spatio-0.2.2-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.2.2-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1e0aeddabe3f8fb0fe8d48e33a1c879c965484144353e2005e4c730564cda937
MD5 35ddf65a20730f6e00fa451215ed0861
BLAKE2b-256 3d3c75f3c937ccaf6d0699517a37ea073c68eec399d7de67cae42d06fd04bab7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9a05d6495a6ee710c6eedc645dc1e103ee4294a388d0d48f6b5c0a1c67ec6ea8
MD5 4f691205ada55be0cf952e6116e71f88
BLAKE2b-256 13a81f2998162c68840777eacb70071b49d599ad87b1df17082453e3e87d7139

See more details on using hashes here.

File details

Details for the file spatio-0.2.2-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.2.2-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 ead8f0ebf7c44c2240fcd3c97869c8dc2b762c0be2815f28cbb2c7916bf6910a
MD5 196b1a663cb5579e434e23ebd048ea63
BLAKE2b-256 6cbe618eb6f9f4b703bacbb2998562ab97e0bf7923f4e6147b680cd5ceddadb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c4d0460c499f20aa383562506b3343a2e28c019ba855b1f9b682522c77b23dc
MD5 df6e70b950e717398f859c5437738efa
BLAKE2b-256 f45e2e9cc38ffed87e03e7deba83f95d8a887dc29db5e047539f1ed509fbbfe5

See more details on using hashes here.

File details

Details for the file spatio-0.2.2-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.2.2-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 3e632d2a088adaf192f83477434b2304d21ccd1064c7419189d0cbf0af17bb99
MD5 dd74789cc3fe14bb80ffae713aeddee7
BLAKE2b-256 24c2660e0519ed3384acc5049d8f4a69d7295490d61bc1104aa9eaa06f328ab0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 da619de7726a4d25d331bf489498abb32d10ae4d7a5860f02150a90936d8b382
MD5 3c37af770cd7daf5cd87431566be0d65
BLAKE2b-256 6d1fefa8fd0b183bd12f74ac1e2a707eaf731be04ab8c07bd4f69bff1ea71d6e

See more details on using hashes here.

File details

Details for the file spatio-0.2.2-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for spatio-0.2.2-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 224d29ec51b7a080f1f8877013ab43ae9fdfade71c4c2cb9e5c1a49301be02dc
MD5 b1f097c3de282d7ebc05d76137597477
BLAKE2b-256 aeffbb10beddb0cd83d47d7ca9fe20ae208a19f76a6ab7227bc82f01c44f912a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4122a2a5a21595716530c200d50035bc6261e1db22c0650317c045c75a059d6c
MD5 bdb239c650f4965d92bc950d364fe857
BLAKE2b-256 49a59168ab83d9e3bde4c86e4fe4812ccaf874a62385be15bc140d39b298c10d

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