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.1.8-cp313-cp313-manylinux_2_35_x86_64.whl (365.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

spatio-0.1.8-cp313-cp313-macosx_11_0_arm64.whl (319.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

spatio-0.1.8-cp313-cp313-macosx_10_12_x86_64.whl (344.2 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

spatio-0.1.8-cp312-cp312-manylinux_2_35_x86_64.whl (365.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ x86-64

spatio-0.1.8-cp312-cp312-macosx_11_0_arm64.whl (319.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

spatio-0.1.8-cp312-cp312-macosx_10_12_x86_64.whl (344.2 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

spatio-0.1.8-cp311-cp311-manylinux_2_35_x86_64.whl (367.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ x86-64

spatio-0.1.8-cp311-cp311-macosx_11_0_arm64.whl (320.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

spatio-0.1.8-cp311-cp311-macosx_10_12_x86_64.whl (345.1 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

spatio-0.1.8-cp310-cp310-manylinux_2_35_x86_64.whl (370.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

spatio-0.1.8-cp310-cp310-macosx_11_0_arm64.whl (322.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

spatio-0.1.8-cp310-cp310-macosx_10_12_x86_64.whl (347.0 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

spatio-0.1.8-cp39-cp39-manylinux_2_35_x86_64.whl (369.5 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.35+ x86-64

spatio-0.1.8-cp39-cp39-macosx_11_0_arm64.whl (322.0 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

spatio-0.1.8-cp39-cp39-macosx_10_12_x86_64.whl (346.4 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 304990a2b53264b50f08ddebbb7deca79844b38efb98cbf7036b7bee2392c26f
MD5 feaa8e9c8f3d5f16185b30996de5ecab
BLAKE2b-256 5d7ecf527319225d26689a0a187a9958f30dee5ab74134c040d8398bb086e93c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ffb25a5a894da1b2d7e23c29d056b0fa5bd0e96f1851c158f7e87b52af236811
MD5 631d7470003eff4de6fd3732e20e25c1
BLAKE2b-256 01a99454581c6f7830b223e2a9e649a78e5cd2b5ba8545fe7e29909133a57be8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 25c45751da31ed3dcea42d255c4e76de5d0d3032003204cd9baf7185f9fb74d2
MD5 935fec2575a7236c2244e836e22aa135
BLAKE2b-256 8c114d704ea7868e213fd2fb21f0132af649a2f1445b40f044e1de81468dbb6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 28bb82aa8c4382542cae172053bd60aa051f4d9effd651e8097f012363ffce48
MD5 57ddc40cece45fac214bb96162ba4de5
BLAKE2b-256 d70d542d7652f14be5f27b8c4234fded5bb2415e873a195bff157197be3819e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3d7384ddcd242cabbff0125e0e02c3bd5b729a51a5d016458a497b6ec4245cd6
MD5 875d18dbbf6a7c735a94f8a59f4bf580
BLAKE2b-256 6875f98abcb98d04eff1a35d581b5efd6dffbc9b6bd9c95d1c8511fd5f0fbbb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5a69a85074dd8bc173793d8968b1de5e1e07be22fff9eec1602c1a78e790204d
MD5 64d0c7f82a5aafe8aa3725b115492256
BLAKE2b-256 550d943654c203cc4046daaf3232dcbdcd1f537b3720d239f14d3b5bfe100413

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 4986e196cc19d49b1b4900414b05ab3f1a56248549bdb5ee146a00dd04cf2d6e
MD5 8e911842a52f74dc8b008313e82ec9df
BLAKE2b-256 2006858d0c3830386101007c4aff7aefcd179fa01ddec668f89e9029d305faa3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0eaa778980a24884317074b1f229825adcda110790b8b5b5e4c48b65a5796e90
MD5 35f1ac8bb7e87f830e63f764aa56fb0a
BLAKE2b-256 7004b9303124320892d4abb16d7e0e2ae2927941ad8efcd04ae637bdfc9c4805

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a7797299d3fa3a6174e27778ad0d13814f4dbc16068651f7f39c629a9559d0c4
MD5 8569d6b3c93e710021e08ffe10af5b38
BLAKE2b-256 8da3a9ed5d71ec677afbf4e8cecf3f6ec10a2ad5083dbfea6ca858fff84718e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 dfe5ac2361ae5c6a5d04ed080b21dc274b1aa6329038de236b645503347233f0
MD5 2a209b0c2fb0c238c60ed2a67bd81665
BLAKE2b-256 9a47d749b9faede06bfee2f550541544a36816f9e2ad8845499bd40470ae7dd4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c2e1c60cd1066d0ff5b44547a01645589b18db482d2c8d699ccb01784064456c
MD5 fc71f2aa7c01436eb76f22bbee0df8f6
BLAKE2b-256 e41e21d8b3fd28dd8178f450c6a3ae8dbfae361093ef0999d9df8a76c5bee464

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c9c6195e937da6c9b6c3ea458d6556af98f5038cea8c5b6cd2615e4fe188eb4d
MD5 633ae152e895f28e46ea8fd7a244f452
BLAKE2b-256 6fc373ff246c0b6f763be2b98ee4adb874c40207ce40fda5e8b553ff4480e402

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 cc24ad806134287507fe01e2dc932495b995a04506c9514d5ffa1e3f95ff3e08
MD5 0afca5b7d229b1f234643371feda0d3f
BLAKE2b-256 c8c8e0508c546946e9b70444018aeb2d3ccdd72c3e46ef19bacc9135378d8771

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 148a0e4b223e5ef3fbf6f7120aa3ba96e0bd4bb4f7e4156b40ed875803e5ea4d
MD5 ee97ef153be6ba304e3543441a27e2b2
BLAKE2b-256 35959e88f62e54953c4eba56d8be38b2d883fd1c1c7d3234bbb4cb8e6fbb038b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.8-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 35d2c0c25638d47eb99bcf1699a7ee6d2e82f3a74d9657bbdcd64c6b347d1a2b
MD5 78884b688cb10dd8a4079b8cd2a52190
BLAKE2b-256 5761c155caa01e0c87008b207506345102f22be206eb659bb61505d61f5deee3

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