texspace

mcp
SUMMARY

Planet-scale pure-Rust sub-2ms spatial geocoding engine (mmap SQLite + S2 + native MCP)

README.md

texspace

A Rust geocoder implementing TEXSPACE (Robust Location Search from Text Queries, Sengar et al., ACMGIS '07) over memory-mapped SQLite shards and S2 cell geometry, built from OpenStreetMap, Overture Maps, OpenAddresses, and GeoNames.

It runs as a CLI, HTTP server, embeddable Rust crate, C/Python library, or offline mobile library on iOS and Android without Elasticsearch, PostgreSQL, or libpostal.

Quickstart

Install the CLI from crates.io (or grab a pre-built binary from GitHub Releases):

cargo install texspace

Pull either compact mobile city packs (5–44 MB compressed) or full server shards from shards.texspace.dev:

# Compact offline mobile packs (world base + one city, ~37 MB total)
texspace pull --mobile world_base,sf_city --out data/mobile
texspace query --db data/mobile "4865 Mission St Apt 4B, San Francisco, CA 94112" --compact

# Or pull a regional server shard (e.g. California ~344 MB core + 262 MB buildings)
texspace pull us_ca --with-buildings --out data/us_ca
texspace query --db data/us_ca "4865 Mission St Apt 4B, San Francisco, CA 94112" --compact

# Or pull the full 128-shard planet (~34.3 GB compressed, ~98 GiB uncompressed, 99.984% of world cities >=50k)
texspace pull planet --with-buildings --out data/planet

Example output:

{
  "label": "4865 Mission Street, San Francisco, 94112, California",
  "lat": 37.72047,
  "lon": -122.43765,
  "match_level": "rooftop",
  "housenumber": "4865",
  "unit": "Apt 4B",
  "confidence": 0.96,
  "conflicting_tokens": [],
  "ms": 3.15
}

How it works

Parse-first geocoders (libpostal / Pelias / Nominatim) label tokens into {housenumber, street, city, postcode} before checking geography, so swapped fields or a wrong city can misroute the lookup. texspace intersects spatial footprints first:

query
  -> normalize text, extract sub-premise/PO box, fast-path coords & Plus Codes
  -> router.db: RoaringBitmap shard postings + global admin/landmark trigrams
  -> per-shard subsequence mapping + in-RAM S2 CellUnion DFS intersection
  -> 4-tier address lookup: parcel -> rooftop -> building-snapped -> interpolated
  -> GBDT ranker + Platt confidence calibration
  1. Spatial footprints (crates/spatial): Entities and normalized attributes are stored as S2 CellUnion coverings in SQLite WITHOUT ROWID tables using sign-bit-flipped Hilbert keys for B-tree range scans.
  2. Sharding & routing (crates/index, crates/engine/src/router.rs): The global index is split across 128 read-only mmap SQLite shards (core.db, optional parcels.db and buildings.db sidecars) behind a 1.88 GB router.db, plus optional sibling geoip.db (offline IPv4/IPv6 + ASN), tz.db (offline IANA timezone polygons + embedded TZif v2/v3 DST rules), and live osm_delta.db (atomic hot-reloading OSM diffs with tombstone suppression). Mobile packs skip router.db and auto-mount loose *.db files in multi_pack mode.
  3. Subsequence search, romanization & lazy rescue (crates/text/src/romanize.rs, crates/engine/src/subsequence.rs, resolve.rs, rescue.rs): Contiguous query token spans (u64 bitmasks) are looked up exact-first, intersected via galloping S2 union in RAM, and lazily expanded inside spatial focuses for typos, omitted suffixes/given names, Double Metaphone phonetic variants, and automatic non-Latin romanization (Chinese Pinyin, Japanese Hepburn Kana, Korean Hangul, Cyrillic, Greek, Hebrew, Arabic, Thai).
  4. Address resolution (crates/index/src/reader.rs): Resolves house numbers in order: parcel polygon, exact rooftop point, building-snapped interpolation (using S2-L16 building footprints), then curved polyline interpolation.

Resource usage

Measured on a single Hetzner CPX42 (8 vCPU, 16 GB RAM, NVMe) hosting all 128 planet shards + 59 building sidecars + geoip.db (~100 GiB on disk), and on Apple Silicon for mobile packs:

Scope Packs / Shards Min RAM Disk (raw / zstd) Notes
Mobile (world_base + city) 1–5 packs ~10 MB dirty (~25 MB RSS) 16–190 MB / 5–44 MB per metro iOS Swift & Android Kotlin SDKs (mobile/)
State / Metro (us_ca, gb_lon) 1 shard 256 MB 0.2–0.8 GB Single laptop or small VPS
Continent (us or eu) 30–52 shards 2 GB 25–35 GB 4 vCPU NVMe VPS
Full planet + GeoIP 128 shards + geoip.db 4 GB (16 GB rec.) ~100 GiB / 34.3 GB ~65 MB idle heap; OS page cache handles hot pages

On the 8-vCPU full-planet box, /v1/geoip runs at ~0.02 ms (15–40 µs), /v1/reverse around 1.3–1.9 ms p50, /v1/autocomplete 3.5–4.9 ms p50, /v1/search 21–24 ms p50, and parallel /v1/batch ~111 queries/sec.

Interfaces

CLI & HTTP server

texspace query --db data/global "10 Downing Street, London, SW1A 2AA"
texspace structured --db data/global --housenumber 4865 --street "Mission St" --postalcode 94112
texspace reverse --db data/global --lat -33.25232 --lon 151.48382
texspace autocomplete --db data/global "4865 Miss" --focus-lat 37.72 --focus-lon -122.44
texspace nearby --db data/global "hospitals within 3km of SFO"
texspace geoip --db data/global 8.8.8.8 2606:4700:4700::1111
texspace timezone --db data/global --lat 37.7749 --lon -122.4194
texspace sync-osm --db data/global --source minute --watch
texspace apply-osc --db data/global local_diff.osc.gz
texspace batch --db data/global --input addresses.csv --output geocoded.csv --threads 0
texspace build-overlay --input depots.csv -o depots.db
texspace build-geoip --city-csv dbip-city-lite.csv --asn-tsv ip2asn-combined.tsv -o data/global/geoip.db
texspace build-tz --tsv tz_polygons.tsv -o data/global/tz.db
texspace serve --db data/global --port 4000 --osm-sync minute

texspace serve exposes Pelias-compatible GeoJSON endpoints (/v1/search, /v1/search/structured, /v1/reverse, /v1/autocomplete, /v1/nearby, /v1/batch), offline GeoIP endpoints (/v1/geoip, /v1/geoip/batch, plus focus.ip=<ip|auto> on search/autocomplete), offline IANA Timezone endpoints (/v1/timezone, /v1/timezone/batch, also enriching GeoIP results automatically with timezone), a compact JSON mode (?format=compact), OpenAPI 3.1 (/openapi.json), and a MapLibre demo UI at /. Passing --osm-sync minute|hour|day (or running texspace sync-osm externally) atomically swaps <db>/osm_delta.db and hot-reloads live traffic with zero downtime. A stdio MCP server with 9 tools is also available via texspace mcp --db <path>.

Rust, Python, and C ABI

use texspace_engine::{QueryOptions, TexspaceEngine};

let engine = TexspaceEngine::open("data/global")?;
let resp = engine.query("10 Downing Street, London, SW1A 2AA", &QueryOptions::default())?;
let ip = engine.geoip("8.8.8.8")?;
let tz = engine.timezone(37.7749, -122.4194, None)?;
from texspace import TexspaceEngine

with TexspaceEngine("data/global") as geo:
    print(geo.search("10 Downing Street, London, SW1A 2AA"))
    print(geo.reverse(-33.25232, 151.48382))
    print(geo.geoip("8.8.8.8"))
    print(geo.timezone(37.7749, -122.4194))

Pre-built libtexspace_ffi libraries and include/texspace.h are attached to each release.

Offline mobile packs (iOS & Android)

51 pre-built packs (world_base + 50 metros) are hosted at https://shards.texspace.dev/mobile/latest.json, or you can extract a custom bounding box with texspace pack-mobile. Both mobile/ios/ (Swift Package / XCFramework) and mobile/android/ (Kotlin + JNI) include a pure-Rust zstd unpacker inside libtexspace_ffi and geo-aware pack selection (ensureEngine(forLat:lon:)). See mobile/README.md for setup details.

# Auto-pick the local metro pack from GPS coordinates
texspace pull --mobile --near 37.7749,-122.4194 --out data/mobile

Docker

docker build -t texspace .
docker run -p 4000:4000 -v texspace_data:/data -e TEXSPACE_PULL_PRESET=us_ca texspace

Workspace layout

Crate Role
crates/spatial S2 CellUnion footprints, galloping intersection, Hilbert key encoding, WKB/WKT codecs
crates/text Text normalization, CJK segmentation, German decompounding, sub-premise/PO box parsing, Plus Codes, Double Metaphone, trigrams
crates/ingest Importers for Overture Maps, OSM PBF, WOF, OpenAddresses, GeoNames, parcels, and custom CSV overlays
crates/index Partitioned SQLite builder, reader, router.db, mobile pack extractor, integrity audit
crates/engine Thread-local mmap reader pool, TEXSPACE DFS, focus-scoped rescue, 4-tier address lookup, GBDT ranker, reverse/autocomplete/nearby
crates/cli texspace CLI, Axum HTTP server, embedded MapLibre demo UI, stdio MCP server
crates/ffi C ABI (cdylib + staticlib), ruzstd decompression helper, Python ctypes bindings (python/texspace)

Live updates, romanization & coverage notes

  • Live incremental OSM diff replication: texspace sync-osm (or texspace serve --osm-sync minute|hour|day) streams official OpenStreetMap .osc.gz changefiles in pure Rust and atomically hot-reloads <db>/osm_delta.db (including <delete> tombstones against immutable base shards) with zero server downtime. Offline .osc / .osc.gz files can also be applied via texspace apply-osc.
  • Automatic non-Latin romanization: crates/text/src/romanize.rs deterministically romanizes Chinese Hanzi (Pinyin), Japanese Kana (Hepburn), Korean Hangul, Cyrillic, Greek, Hebrew, Arabic, and Thai at both index build time and focus-scoped query time (so native-only streets like 建国门外大街 in Beijing or Тверская улица in Moscow resolve when queried in Latin script even without upstream name:en). Japanese Kanji readings without Kana/aliases still depend on upstream name:en where available.
  • Global rooftop & open-data coverage: Planet shards now combine Overture Maps with Geofabrik OSM rooftop extracts across the US, Canada, Australia, France (including fr_center), UK, Ireland, Germany (20.2M rooftops), Netherlands, Spain, Italy, Baltics, Balkans, Romania/Bulgaria (729k rooftops in eu_east), Brazil (56.7M rooftops), Taiwan (4.75M rooftops in tw_hk_mo), Japan, South Korea, Singapore, and South Africa (scripts/enrich_osm_addresses.py can ingest additional Geofabrik extracts on demand). Countries without open national address registries or dense OSM addr:* tagging (such as mainland China and parts of rural Africa/South Asia) remain street- or locality-level unless supplemented with custom overlays (texspace build-overlay).

Tests & evaluation

# Offline unit and integration tests
cargo test --offline

# Verify shard integrity
texspace audit --db data/global --check

Evaluation scripts and query sets live under eval/ and scripts/ (scripts/bench_global.py, scripts/bench_geocoders.py, scripts/build_third_party_global_eval.py). On the strictly untouched 440-query, 9-country, 5-continent third-party civic holdout (eval/third_party_global_holdout.json, sourced exclusively from independent municipal/national open-data portals with zero Overture/OSM/OpenAddresses/GeoNames sampling and zero country bias):

  • texspace ($0, offline): 93.4% Top-1, 95.2% Top-3, 13.7 m median error (44.8% ≤10 m), ECE 0.021, 23.7 ms p50.
  • Google Maps ($5,000/1M): 93.6% Top-1 (McNemar exact p = 1.0000), 93.9% Top-3, 20.9 m median error (30.0% ≤10 m), ECE 0.078, 170.7 ms p50.
  • Mapbox v6 ($750–$5,000/1M): 87.5% Top-1 (texspace +5.9 pp, p = 1.56e-4), 90.0% Top-3, 19.6 m median error, ECE 0.262, 206.0 ms p50.
  • Geocode Earth / Pelias ($500–$1,000/1M): 76.6% Top-1 (texspace +16.8 pp, p = 5.93e-15), 77.0% Top-3, 23.4 m median error, 258.2 ms p50.

Full 6-provider results (including ArcGIS and Photon) and reproduction commands live in eval/third_party_global_holdout_report.md.

License & data attribution

  • Source code: licensed under the MIT License.
  • Pre-built shards, GeoIP & Timezone (texspace pull, geoip.db, tz.db): compiled from open geospatial, timezone & IP datasets — Overture Maps Foundation (CDLA-Permissive-2.0 / ODbL-1.0), OpenStreetMap contributors (ODbL-1.0), GeoNames (CC-BY-4.0), US National Address Database (Public Domain), Who's On First (CC0), DB-IP City Lite (CC-BY-4.0), IPtoASN (PDDL-1.0), timezone-boundary-builder (ODbL-1.0), and IANA TZDB (Public Domain). See ATTRIBUTION.md.

Yorumlar (0)

Sonuc bulunamadi