Skill v1.0.0
currentAutomated scan100/100version: "1.0.0" name: embedded-spatial-sqlite description: Use when adding geospatial capability to an embedded/local SQLite database — SpatiaLite, or plain SQLite (e.g. better-sqlite3 in an Electron app) without a full spatial extension. Covers when embedded spatial storage is appropriate and how to approximate spatial queries without PostGIS.
Embedded Spatial SQLite (SpatiaLite / better-sqlite3)
When to use this skill
- An Electron/desktop app storing local geospatial data (offline-first tools, field data collection apps)
- Deciding whether a local SQLite file needs full SpatiaLite or can get by with plain columns + application-level geometry handling
- "How do I do a bounding-box or nearest-neighbor query without a spatial index in SQLite"
Decision framework
| Situation | Approach | |
|---|---|---|
| Need real geometry types, spatial functions, spatial index, inside SQLite | Load the SpatiaLite extension (mod_spatialite) — gives ST_* functions and an R*Tree index, PostGIS-like semantics in an embedded file | |
| Simple point data, bbox-style filtering only, no complex geometry ops | Plain better-sqlite3 with lat/lon REAL columns + SQLite's built-in R*Tree virtual table module for indexed bbox queries — no native extension binary dependency | |
| Storing complex vector geometry but only ever reading it back for display (no server-side spatial queries) | Store geometry as a GeoJSON/WKB blob column; do spatial filtering client-side or via a lightweight JS geometry library (e.g. Turf.js) after a coarse bbox pre-filter from SQLite | |
| Electron app that needs to sync with a PostGIS backend | Keep the local schema geometry-representation-compatible (WKB or GeoJSON) so sync logic doesn't need translation |
Practical note for Electron + better-sqlite3: better-sqlite3 is a synchronous native binding; loading SpatiaLite as a runtime extension requires the platform-specific mod_spatialite shared library to be present and loadable via db.loadExtension() — this adds packaging/cross-platform build complexity (separate binaries per OS/arch) that plain RTree + JS-side geometry avoids entirely. Default to the RTree + Turf.js approach unless the app genuinely needs SpatiaLite's full spatial SQL surface.
Checklist
- [ ] Do you actually need spatial SQL (joins,
ST_Intersectsin a query), or just indexed nearby-lookup + JS-side rendering? Most Electron map apps only need the latter. - [ ] If using SQLite R*Tree: create the virtual table alongside a regular table holding the full record, keyed by rowid
- [ ] If using SpatiaLite: verify
mod_spatialiteships correctly for every target OS/architecture in the Electron build (this is the main source of "works on my machine" packaging bugs) - [ ] Store geometry in a stable interchange format (GeoJSON or WKB) so it's portable if the app later syncs to PostGIS
- [ ] For large local datasets, benchmark cold-start query time — embedded DBs have no connection pooling/caching layer to hide slow scans
Common pitfalls
- Shipping `mod_spatialite` cross-platform: forgetting Windows/macOS/Linux all need their own compiled extension binary, and Electron's packaging (electron-builder/forge) needs explicit
extraResourcesconfig to include it. - Using R*Tree without a companion data table: the R*Tree virtual table only stores bounding boxes and a rowid — always pair it with a regular table for the actual attributes/geometry.
- Assuming better-sqlite3 handles geometry types natively: it doesn't — geometry is just bytes/text unless SpatiaLite is loaded; plan the encoding (WKB/WKT/GeoJSON) deliberately.
- Blocking the Electron main process with large synchronous spatial queries —
better-sqlite3is synchronous by design; move heavy queries to a worker thread or the renderer's background work to avoid freezing the UI.
Reference snippets
// better-sqlite3 + SQLite R*Tree virtual table for indexed bbox queriesimport Database from 'better-sqlite3';const db = new Database('local.db');db.exec(`CREATE TABLE IF NOT EXISTS sites (id INTEGER PRIMARY KEY, name TEXT, lat REAL, lon REAL);CREATE VIRTUAL TABLE IF NOT EXISTS sites_rtree USING rtree(id, minLon, maxLon, minLat, maxLat);`);// Indexed bbox queryconst results = db.prepare(`SELECT s.* FROM sites sJOIN sites_rtree r ON s.id = r.idWHERE r.minLon <= ? AND r.maxLon >= ? AND r.minLat <= ? AND r.maxLat >= ?`).all(maxLon, minLon, maxLat, minLat);
// Loading SpatiaLite (when full spatial SQL is genuinely needed)db.loadExtension('/path/to/mod_spatialite'); // path must resolve per-OS in packaged appdb.exec("SELECT InitSpatialMetaData(1);");
Related skills
offline-and-mobile-gis— broader offline-first architecture this often feeds intospatial-indexing-strategy— R-tree fundamentals shared with server-side databases