Geospatial indexing for leveldb
Experimental - subject to change.
$ npm install level-geospatial
The module takes a leveldb database (or a sub-level):
var db = require'level''path_to_your_database';var geo = require'level-geospatial'db;
You can then start adding key/values, along with latitude/longitude values.
// lat, lon, key, valuegeoputlat:52.081959 lon:1.415904 'Location1' 'My value'if err console.logerr;;
You can retrieve a value back like this:
// this is the fast way of getting the valuegeogetlat:52.081959 lon:1.415904 'Location1'console.logdata;;// this is the slower/convenient way of getting the valuegeogetByKey'Location1'console.logdata;;// the data returned looks like this:quadKey: '1222222212112112222210'position:lat: 52.081959lon: 1.415904id: 'Location1'value: 'My value'
You can search within a radius (in meters) of a given point:
// lat, lon, radius in metersgeosearchlat:52.081959 lon:1.415904 15000on'data'console.logdata;// the data returned looks like this:quadKey: '1222222212112112222210'position:lat: 52.081959lon: 1.415904id: 'Location1'value: 'My value'distance: 1232.232323 // this is the distance in meters from your search
Please note, the results are not returned in any meaningful order.
You can update/delete like this:
// to update the value/location:geoputlat:53.1 lon:2.2 "Location1" "NEW VALUE"if err console.logerr;;// to deletegeodel"Location1"if err console.logerr;;
The data is indexed using a quadtree. When you index a point, it's quadkey is calculated to a depth of 22.
The quadkey is a string which stores where the point lives in the quadtree. The quadkey notation used is the same as Bing Maps. The quadkeys can be inserted into this URL to retrieve a map tile for a given location:
When you search the database, the quadkey is calculated for search location. The radius is then used to calculate an appropriate depth in the quadtree to search to.
Potential matches within those quads are then tested using a simple distance calculation to work out if they are close enough to be included in the results.
Currently searches that span the international date line will not return all results. Batch operations are not supported.