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

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

spatio-0.1.9-cp313-cp313-macosx_11_0_arm64.whl (329.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

spatio-0.1.9-cp313-cp313-macosx_10_12_x86_64.whl (352.2 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

spatio-0.1.9-cp312-cp312-manylinux_2_35_x86_64.whl (377.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ x86-64

spatio-0.1.9-cp312-cp312-macosx_11_0_arm64.whl (329.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

spatio-0.1.9-cp312-cp312-macosx_10_12_x86_64.whl (352.1 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

spatio-0.1.9-cp311-cp311-manylinux_2_35_x86_64.whl (377.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ x86-64

spatio-0.1.9-cp311-cp311-macosx_11_0_arm64.whl (331.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

spatio-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl (354.7 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

spatio-0.1.9-cp310-cp310-manylinux_2_35_x86_64.whl (380.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

spatio-0.1.9-cp310-cp310-macosx_11_0_arm64.whl (333.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

spatio-0.1.9-cp310-cp310-macosx_10_12_x86_64.whl (357.4 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

spatio-0.1.9-cp39-cp39-manylinux_2_35_x86_64.whl (381.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.35+ x86-64

spatio-0.1.9-cp39-cp39-macosx_11_0_arm64.whl (334.1 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

spatio-0.1.9-cp39-cp39-macosx_10_12_x86_64.whl (358.6 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 52ba502ec3691e43fa1c73c379db8ae53dd8504f2ff75ca86a6d650d83467196
MD5 0884d55b65c2535433137a5b20dd9944
BLAKE2b-256 808d65dc29521ed6bf3af2e8430472dfd53b40ca51b6e7f4e0f79aab064fa2f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d01071b90b4fd1dde807ae9856d787030d2c8ada74470a6a8adc9bd33f8f563
MD5 d396bee1297912d7f2259a1eca0a86fb
BLAKE2b-256 8bc00bad8b57562fe903debd17e0142805e30e605e1b9f09b22f2ed08ec8a0ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 92b53e38721bfb3ecaf08ae506516e699a803009eb2fa15b8f915c715e996e9d
MD5 85236d89400b3bc961f853355408f528
BLAKE2b-256 c63c9625d527effc2725018785a64f20f6fdb86ff87ea11604490eac606dc069

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 e72c0503dbd3aaca978d83cc9684ab042f4bafa20ff6cb6a1b579386baff45c1
MD5 45e472201f0377e185ab2c2443495b6b
BLAKE2b-256 4e64f95c49e9c589c9ae9ba07d89eed597ea06ab3f8340de068ea5839624cd44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e58d6c72491a5fecbbbca7e5773449f628ba3fdeb55c435b12ab65d7eb142e4a
MD5 e75e1bc23b98eff0af6062a18a6f1366
BLAKE2b-256 421792c4566e49cd88e77795cc3c2526b9244a014f669295176ae131475b06f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2b0801ac5e4150812e1788b656cd227a16f8ffce68f6f722315077e14574d120
MD5 d8d018f27c1ea762985a09e5fb3ffd3a
BLAKE2b-256 4a3d9219581a03f133642fe8de7e48969a591b8998ac6adf9bd66891fc74623d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 6604f5c2922254ee8ea61ce4c09984d94d6257d09d9b630b858104d6f05053e5
MD5 f1c4dac6379b8d9b50985523e74d2cce
BLAKE2b-256 424497cdc92ec9d8b8b23d48a36dcaef3fcd59b03329133d3449c22d5643747f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5e2822e49504cffbd6bfcb859afdb40fb1052e2faf46e389c1f496a70fdf9366
MD5 c6ae300ca2d7e8880c5b51460153c204
BLAKE2b-256 9bced10af586f876c58adae11553fb518b8502e35e14013210ac832e290957a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3a87e894a804d89182348561e46a95a68b0fee9a536b1844f888185d5da19a24
MD5 e8df2580ed4a8ee4ab5ab8a9f55958f6
BLAKE2b-256 00b885eecc37a2d4c5584fb62a0063c91ddc19d73badb6ef31040dd72e2ca9f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 86b2dbdf2f4e61dec99587b062598715aacc556ed7804e02033038c6d7ffdcc5
MD5 0751485e45c0e470309ce26cfe59d031
BLAKE2b-256 6a553dda89d9d2024e57c3b36296764db6ea09e8a11070393386d1853244e96b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8a84cca1f6b6e94f86bfbb74c8ce062d00aeec5bb11af086fd50e514adde7d1d
MD5 30b7bdc6ef1c7c6f3a032563270586ba
BLAKE2b-256 f7c70d6a612c40d62972089e30f29c1975bd12449494d22d4ee4e46f9ad017ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cf3fba59b3b1ac3db6b47a0988f2397b0884ec1ce5bf6f5312bd9df99f587bc2
MD5 d062abf273c6127aaf0c111fd019abe3
BLAKE2b-256 40851d6a1a13209a43cae1a7f767cdd459dd544870b816b0e8ede43906c706f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 cf6d40b513879fd8c22d768163bde8a90c7d7a240c6942ea55c12bd1aceab1c3
MD5 ddaa25c303304fdcd31fae7d699a330a
BLAKE2b-256 4ecf4b021dcc52543493f5e0c827965c88109eaf394c64121040e0dfddb236cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c1839e5243de9430345771ac76545d58b24b0267d78ecf809f7c696f21dd4e45
MD5 d7091eb3ed2cf384213532a24aa21632
BLAKE2b-256 f17e602fe6229bfa9965e6728851dfb566fd61430494947f2ea2c1dd3e167c64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.1.9-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2b9c4c4a7a67fccecf688594f858209a27b5a8f2b71b7f52e737cb087bcb3ca6
MD5 a3e713363bf1d8971983b277348abb56
BLAKE2b-256 8d75ff0e407485ff67cac49ebb2630ceb3ce93790becbc09b013b364294be86c

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