Geospatial
Redlite implements Redis geospatial commands using SQLite’s R*Tree spatial index. Requires --features geo when compiling.
Installation
Section titled “Installation”cargo add redlite --features geo# orcargo install redlite --features geoCommands
Section titled “Commands”| Command | Syntax | Description |
|---|---|---|
| GEOADD | GEOADD key [NX|XX] longitude latitude member [longitude latitude member ...] | Add geospatial items |
| GEOPOS | GEOPOS key member [member ...] | Get coordinates |
| GEODIST | GEODIST key member1 member2 [M|KM|MI|FT] | Calculate distance |
| GEOHASH | GEOHASH key member [member ...] | Get geohash string |
| GEOSEARCH | GEOSEARCH key (FROMMEMBER member | FROMLONLAT longitude latitude) (BYRADIUS radius [M|KM|MI|FT] | BYBOX width height [M|KM|MI|FT]) [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] | Search by radius or box |
| GEOSEARCHSTORE | GEOSEARCHSTORE destination source ... | Search and store results |
Examples
Section titled “Examples”Add Locations
Section titled “Add Locations”# Add cities with coordinates127.0.0.1:6379> GEOADD locations -122.4194 37.7749 "San Francisco"(integer) 1
127.0.0.1:6379> GEOADD locations -73.9857 40.7484 "New York"(integer) 1
127.0.0.1:6379> GEOADD locations -0.1276 51.5074 "London"(integer) 1Get Coordinates
Section titled “Get Coordinates”127.0.0.1:6379> GEOPOS locations "San Francisco" "New York"1) 1) "-122.41940000000001" 2) "37.77489999999999"2) 1) "-73.98569999999999" 2) "40.748399999999997"Calculate Distance
Section titled “Calculate Distance”# Distance in kilometers127.0.0.1:6379> GEODIST locations "San Francisco" "New York" KM"4138.3798"
# Distance in miles127.0.0.1:6379> GEODIST locations "San Francisco" "New York" MI"2571.6719"Search by Radius
Section titled “Search by Radius”# Find locations within 5000km of San Francisco127.0.0.1:6379> GEOSEARCH locations FROMMEMBER "San Francisco" BYRADIUS 5000 KM1) "San Francisco"2) "New York"
# With coordinates and distance127.0.0.1:6379> GEOSEARCH locations FROMMEMBER "San Francisco" BYRADIUS 5000 KM WITHCOORD WITHDIST1) 1) "San Francisco" 2) "0.0000" 3) 1) "-122.41940000000001" 2) "37.77489999999999"2) 1) "New York" 2) "4138.3798" 3) 1) "-73.98569999999999" 2) "40.748399999999997"Search from Coordinates
Section titled “Search from Coordinates”# Find locations within 1000km of coordinates127.0.0.1:6379> GEOSEARCH locations FROMLONLAT -122.0 38.0 BYRADIUS 1000 KM1) "San Francisco"Search by Box
Section titled “Search by Box”# Find locations within bounding box127.0.0.1:6379> GEOSEARCH locations FROMMEMBER "San Francisco" BYBOX 6000 6000 KM1) "San Francisco"2) "New York"Geohash
Section titled “Geohash”127.0.0.1:6379> GEOHASH locations "San Francisco" "New York"1) "9q8yyk8y"2) "dr5regw2"Distance units supported:
| Unit | Description |
|---|---|
M | Meters |
KM | Kilometers |
MI | Miles |
FT | Feet |
Library Mode (Rust)
Section titled “Library Mode (Rust)”use redlite::Db;
let db = Db::open("mydata.db")?;
// Add locationsdb.geoadd("locations", &[ (-122.4194, 37.7749, "San Francisco"), (-73.9857, 40.7484, "New York"),])?;
// Get positionslet positions = db.geopos("locations", &["San Francisco", "New York"])?;
// Calculate distance (in meters by default)let distance = db.geodist("locations", "San Francisco", "New York", None)?;
// Search by radiuslet results = db.geosearch_radius( "locations", -122.4194, 37.7749, 5000.0, "KM", None,)?;Use Cases
Section titled “Use Cases”Store Locator
Section titled “Store Locator”# Add store locationsGEOADD stores -122.4 37.8 "Store A"GEOADD stores -122.5 37.7 "Store B"
# Find nearest stores within 10kmGEOSEARCH stores FROMLONLAT -122.45 37.75 BYRADIUS 10 KM COUNT 5 WITHDISTDelivery Zones
Section titled “Delivery Zones”# Add delivery addressesGEOADD deliveries -122.41 37.77 "order:1"GEOADD deliveries -122.42 37.78 "order:2"
# Find orders within driver's rangeGEOSEARCH deliveries FROMMEMBER "driver:1" BYRADIUS 5 KMLocation-Based Services
Section titled “Location-Based Services”# Add points of interestGEOADD poi -122.419 37.775 "Museum"GEOADD poi -122.420 37.776 "Park"
# Find nearby attractionsGEOSEARCH poi FROMLONLAT -122.42 37.78 BYRADIUS 1 KM WITHCOORDImplementation
Section titled “Implementation”- Backend: SQLite R*Tree extension for spatial indexing
- Storage: Coordinates stored as (longitude, latitude) pairs
- Precision: IEEE 754 double precision
- Index: Automatic R*Tree index for efficient radius queries
- Distance: Haversine formula for great-circle distances