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

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

spatio-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (362.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

spatio-0.2.0-cp313-cp313-macosx_10_12_x86_64.whl (384.4 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

spatio-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl (418.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

spatio-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (361.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

spatio-0.2.0-cp312-cp312-macosx_10_12_x86_64.whl (383.8 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

spatio-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl (418.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

spatio-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (362.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

spatio-0.2.0-cp311-cp311-macosx_10_12_x86_64.whl (385.5 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

spatio-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl (421.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

spatio-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (364.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

spatio-0.2.0-cp310-cp310-macosx_10_12_x86_64.whl (388.0 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

spatio-0.2.0-cp39-cp39-manylinux_2_34_x86_64.whl (422.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

spatio-0.2.0-cp39-cp39-macosx_11_0_arm64.whl (366.5 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

spatio-0.2.0-cp39-cp39-macosx_10_12_x86_64.whl (388.9 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 04f043d79909fa1eaac566674ddf7187d0da7460abab3ce764a9a8891d054559
MD5 541688f1a8f1eea3681e77fd0ee54cec
BLAKE2b-256 c53374acc5fe0f974f107ba5d0d7c1b61e9f94cb412b1781898a5d599c7f5184

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c641142723f4bf0c64132f092e441e4394acda8521955ed07e7e14222dd6ebe4
MD5 7a2ef21986ec213d778a8f7db86df526
BLAKE2b-256 6302f3e0ecde3fb5538b296e42916fce4f3f2776d24624585fe74b1a8478d78c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c9582989ac2f2fa771bcf7e7dab0eab685c2dcca292c0ef313cea3a350a42512
MD5 78490d84992106b21989686665d7d32b
BLAKE2b-256 22d2aaf417ee10ea9cdde911cad5df197377758159630e7f32cea2a96d384a3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 3969a9a0911528788c0e13f6bf8d0c5330d0c7326ec0d6a3bb176479a5c601ad
MD5 7a5485d612d7e023895f390470afc5d1
BLAKE2b-256 8db5694a1c00bb6cc3d8851b5d5c6e753dcb5636e0785f07ed2e9d22883dfe0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1a514bcc4697fba16b827aceba5e1bf2bcdd242b8dc5a69fbf5fd0f81764fff8
MD5 eceb607c6f82c97d2ebe3370df98a0c8
BLAKE2b-256 b5f0d41ec945ef84ecdec6506e3c75cc8f4793754a53d0da3b5147a1a6d28d94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 53ba1b5ce25ed806377969c41081e0b7831194de026ad44aba820a340dfa1512
MD5 d556d438dc2ee0b854136884f92a5735
BLAKE2b-256 8e990d1ddbf5e24da432348b022cde577611a7c65a23c17abd48781e6dae808f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 9e4a24709e20a5055b9261e808a06b2f5bda6e356b0f796b85bdc3f2951ee512
MD5 c88c963477dd6e17b273fa67a071e16e
BLAKE2b-256 407e0e27ba6afc6ba7d2a5616764682070ff1db956dd11ab08f850466b88a893

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19326da20dd5cc4020a7778bdce496bce829e91e7eb1b0164df0057cbecaeef4
MD5 817a9a630dd4b2987db1490f1ee24005
BLAKE2b-256 94e1eecb59afc6357a88430606334653ea8e809854832563df43a065f00a8bc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cbdc15693ee22fa48cc2994293285f9e83983cb75177563396a49124dc47ea57
MD5 d27e8aeea671ced71479b377530b3fa4
BLAKE2b-256 f60da7cebdadc4668a23776c0c18f10df1007b021b9902100b8c1e23456251d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 6b0154ea2c4782ce38fa849395890bff8611d8229257f1eb6990c612c90abe19
MD5 75ed902749e7bdb5d8575287a506a338
BLAKE2b-256 77dcca6b47cb036ad97a1dda34fa5ef66ad84eb6d904dc08dc7149244b9dd6b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7a7033c407c59b2e6d5486fb7c105dba3da8113a8b6fb0c05237c275bc6ff923
MD5 14a57bcc09ac7b9d7605811db58e92d6
BLAKE2b-256 1aa8b0d33fedbfacd37367311239ca19e39071e52c96bd5a9fed78d8444e86ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 82c41e371971f9c07809f256ce6e6b9d63c1963373173566e33124b37638f9ca
MD5 9aa1baea7b0b5a4a235bcb7a536101e2
BLAKE2b-256 c017503683014784009b7f85744c1acb2796788586b1523cd091b4c3f1e4b3a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a55dd74c59b16d1146c9af9758f22f3a3562900e18d258cbe7b79a0ccd0f76a1
MD5 f55f451b460345ba999de6d48d34b5a7
BLAKE2b-256 bad625adf5e26c41cbc7a0739763baf5c888533ddd3c332145e9fe09d81b930c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b6cc419a01a0cb96dfc60f4bbb9607a97810cce16cc0cbb05ed37ce5bf805bb8
MD5 1a9b7cba81d56112efa3266b71b18476
BLAKE2b-256 9e81d394872165ee37a1e83d4a249dffcd973c726e7e8f711cef32ffda33d0fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for spatio-0.2.0-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2e3c0dfd1b17e139803eafd7408e73e0ee8bff2d3be9dab6580297694f0ff02b
MD5 fe4f97d2f4b3c89f936935c985782a01
BLAKE2b-256 3d372cd8cdf01413045e25814ce520c601a28d5e8a49c6aacced452c75b5aeaf

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