Skip to main content

Crate sqlitegis

Crate sqlitegis 

Source
Expand description

§SQLiteGIS

CI codecov crates.io docs.rs MSRV License

PostGIS-style spatial functions for SQLite in pure Rust, primarily a Diesel ORM integration. Geometries travel as EWKB BLOBs, matching the PostGIS wire format so queries port between SQLite and PostGIS without rewriting. The same functions are also exposed as a SQLite loadable extension (native and WebAssembly) for non-Rust consumers like the SQLite CLI. Try the live demo to run spatial SQL against 68k cities entirely in the browser.

§Quick start (Diesel)

use diesel::prelude::*;
use sqlitegis::diesel::functions::st_point;
use sqlitegis::diesel::prelude::*;

// Register the spatial functions on every new SqliteConnection.
sqlitegis::sqlite::register_on_every_new_connection();

diesel::table! {
    features (id) {
        id -> Integer,
        geom -> Nullable<sqlitegis::diesel::Geometry>,
    }
}

let mut conn = SqliteConnection::establish(":memory:").unwrap();
let nearby = features::table
    .filter(features::geom.st_dwithin(st_point(13.4, 52.5).nullable(), 1000.0).eq(true))
    .select(features::geom.st_astext());

CreateSpatialIndex and DropSpatialIndex are DDL helpers without typed wrappers, called through diesel::sql_query. R-tree-backed queries run 50 to 60x faster than the non-indexed equivalents (see Benchmarks).

§Without Diesel: pure-Rust geometry

If you only need the geometry algebra without SQL, the core functions are callable from regular Rust without any database at all.

use sqlitegis::core::functions::constructors::st_point;
use sqlitegis::core::functions::measurement::st_distance;

let a = st_point(0.0, 0.0, None).unwrap();
let b = st_point(3.0, 4.0, None).unwrap();
assert!((st_distance(&a, &b).unwrap() - 5.0).abs() < 1e-10);

§As a SQLite loadable extension

For non-Rust consumers (SQLite CLI, Datasette, the WebAssembly browser path) the same functions are available as a load_extension-style cdylib. Build it yourself with the sqlite-extension feature.

cargo build --release -p sqlitegis --features sqlite-extension
SELECT load_extension('./target/release/libsqlitegis');
SELECT ST_AsText(ST_Buffer(ST_Point(0, 0), 1.0));
SELECT ST_Distance(ST_GeomFromText('POINT(0 0)'), ST_GeomFromText('POINT(3 4)'));

§Notes

Geodesic functions (ST_DistanceSphere, ST_DistanceSpheroid, ST_LengthSphere, ST_Azimuth, ST_Project, ST_DWithinSphere, ST_DWithinSpheroid) require SRID=4326 non-empty Point inputs and reject anything else. ST_GeomFromGeoJSON defaults to SRID=4326. ST_DWithin* predicates require a finite, non-negative distance.

§Benchmarks

See BENCHMARKS.md for the full R-tree and SpatiaLite comparison reports. Headline: on a 50k-row dataset across 31 head-to-head workloads, sqlitegis wins 20 (geodesic family 3.7x to 8.6x faster, binary predicates 1.2x to 1.7x via an MBR-only fastpath, I/O parse paths 2x faster) and loses 9 (ST_Envelope, ST_AsBinary, and the per-row scalar accessors ST_X/ST_Y/ST_Area/ST_Perimeter go through full EWKB decode where SpatiaLite has thin-C-wrapper shortcuts).

§Contributing

See CONTRIBUTING.md.

§License

MIT OR Apache-2.0

§Crate layout

Modules are gated behind features so consumers only pay for what they ask for. See the [features] table in Cargo.toml for the full list. In short:

  • core is always available (pure-Rust geometry, EWKB I/O, function catalog, no SQLite or Diesel deps).
  • sqlite adds crate::sqlite::register_functions for in-process registration against a *mut sqlite3 connection.
  • sqlite-extension further adds the #[no_mangle] C entry points so the cdylib build is loadable via SQLite’s load_extension.
  • diesel adds backend-agnostic types (Geometry, Geography) plus GeometryExpressionMethods.
  • diesel-sqlite / diesel-postgres add the backend-specific impls.

Diesel users typically import via the prelude: use sqlitegis::prelude::*; (re-exported from crate::diesel::prelude).

Re-exports§

pub use diesel::prelude;

Modules§

core
Pure-Rust geometry primitives, EWKB I/O, and the canonical function catalog used by the SQLite and Diesel layers to generate their surfaces. No SQLite, Diesel, or wasm dependency at this level.
diesel
Diesel ORM integration. Backend-agnostic types and the GeometryExpressionMethods trait live here. Enable diesel-sqlite or diesel-postgres to compile the backend-specific impls.
sqlite
SQLite integration. Available under feature = "sqlite" for in-process registration against a raw *mut sqlite3, and under feature = "sqlite-extension" for the #[no_mangle] C entry points that make the cdylib loadable via SQLite’s load_extension.

Enums§

SqliteGisError
Errors returned by SQLiteGIS’s core, SQLite, and Diesel layers.

Type Aliases§

Result
Result alias used by every fallible function in the crate.