Nota: Este módulo almacena todos los datos en la memoria; recuérdelo.
Uber busca rápidamente ubicaciones cercanas por coordenadas.
Admite Array
, Object
, JSON
y GeoJSON
como datos de entrada.
$ npm install geo-nearby --save
const Geo = require ( 'geo-nearby' ) ;
const dataSet = [
{ i : 'Perth' , g : 3149853951719405 } ,
{ i : 'Adelaide' , g : 3243323516150966 } ,
{ i : 'Melbourne' , g : 3244523307653507 } ,
{ i : 'Canberra' , g : 3251896081369449 } ,
{ i : 'Sydney' , g : 3252342838034651 } ,
{ i : 'Brisbane' , g : 3270013708086451 } ,
{ i : 'Sydney' , g : 3252342838034651 }
] ;
const geo = new Geo ( dataSet ) ;
geo . nearBy ( - 33.87 , 151.2 , 5000 ) ; // 5000 - 5km
En g
geohash almacenado con precisión de 52 bits.
Si desea cambiar el nombre de la propiedad, puede hacerlo con las opciones:
const Geo = require ( 'geo-nearby' ) ;
const dataSet = [
{ id : 1 , name : 'Perth' , geoHash : 3149853951719405 } ,
{ id : 2 , name : 'Adelaide' , geoHash : 3243323516150966 } ,
{ id : 3 , name : 'Melbourne' , geoHash : 3244523307653507 } ,
{ id : 4 , name : 'Canberra' , geoHash : 3251896081369449 } ,
{ id : 5 , name : 'Sydney' , geoHash : 3252342838034651 } ,
{ id : 6 , name : 'Brisbane' , geoHash : 3270013708086451 } ,
{ id : 7 , name : 'Sydney' , geoHash : 3252342838034651 }
] ;
const geo = new Geo ( dataSet , { hash : 'geoHash' } ) ;
geo . nearBy ( - 33.87 , 151.2 , 5000 ) ;
Para obtener el mejor rendimiento, se recomienda utilizar la sintaxis del conjunto de datos predeterminada:
const dataSet = [
...
{ i : < id > , g: < geo hash > } ,
{ i : < id > , g : < geo hash > } ,
...
];
Puede utilizar un método createCompactSet
para crear un conjunto de datos con la sintaxis recomendada de sus datos:
const data = [
[ - 35.30278 , 149.14167 , 'Canberra' ] ,
[ - 33.86944 , 151.20833 , 'Sydney' ] ,
[ - 37.82056 , 144.96139 , 'Melbourne' ] ,
[ - 34.93333 , 138.58333 , 'Adelaide' ] ,
[ - 27.46778 , 153.02778 , 'Brisbane' ] ,
[ - 31.95306 , 115.85889 , 'Perth' ]
] ;
const dataSet = Geo . createCompactSet ( data ) ;
const geo = new Geo ( dataSet , { sorted : true } ) ;
geo . nearBy ( - 33.87 , 151.2 , 5000 ) ;
createCompactSet
admite Array
, JSON
analizado y no analizado, GeoJSON
analizado y no analizado como datos de entrada:
const data = {
type : 'FeatureCollection' ,
features : [
{ type : 'Feature' , geometry : { type : 'Point' , coordinates : [ 44 , 64 ] } , properties : { name : 'Arkhangelskaya Oblast' } } ,
{ type : 'Feature' , geometry : { type : 'Point' , coordinates : [ 40.5433 , 64.5401 ] } , properties : { name : 'Arkhangelsk' } } ,
{ type : 'Feature' , geometry : { type : 'Point' , coordinates : [ 39.8302 , 64.5635 ] } , properties : { name : 'Severodvinsk' } } ,
{ type : 'Feature' , geometry : { type : 'Point' , coordinates : [ 40.8122 , 64.4165 ] } , properties : { name : 'Novodvinsk' } } ,
{ type : 'Feature' , geometry : { type : 'Point' , coordinates : [ 46.64963 , 61.25745 ] } , properties : { name : 'Kotlas' } }
]
} ;
const dataSet = Geo . createCompactSet ( data , { id : 'name' } ) ;
const geo = new Geo ( dataSet , { sorted : true } ) ;
geo . nearBy ( 64.54 , 40.54 , 5000 ) ;
También puede cambiar los valores predeterminados para un método createCompactSet
si sus datos se ven diferentes:
const data = [
{ _id : 1000 , name : 'Arkhangel’skaya Oblast’' , country : 'RU' , coord : { lon : 44 , lat : 64 } , admin1 : 'Arkhangelskaya' } ,
{ _id : 1001 , name : 'Arkhangelsk' , country : 'RU' , coord : { lon : 40.5433 , lat : 64.5401 } , admin1 : 'Arkhangelskaya' } ,
{ _id : 1002 , name : 'Severodvinsk' , country : 'RU' , coord : { lon : 39.8302 , lat : 64.5635 } , admin1 : 'Arkhangelskaya' } ,
{ _id : 1003 , name : 'Novodvinsk' , country : 'RU' , coord : { lon : 40.8122 , lat : 64.4165 } , admin1 : 'Arkhangelskaya' } ,
{ _id : 1004 , name : 'Kotlas' , country : 'RU' , coord : { lon : 46.64963 , lat : 61.25745 } , admin1 : 'Arkhangelskaya' }
] ;
const dataSet = Geo . createCompactSet ( data , { id : '_id' , lat : [ 'coord' , 'lat' ] , lon : [ 'coord' , 'lon' ] } ) ;
const geo = new Geo ( dataSet , { sorted : true } ) ;
geo . nearBy ( 64.54 , 40.54 , 5000 ) ;
Puede especificar la propiedad setOptions
en options
del constructor, creará un conjunto de datos automáticamente, pero puede llevar mucho tiempo para datos grandes:
const data = [
{ lat : - 35.30278 , lon : 149.14167 , name : 'Canberra' } ,
{ lat : - 33.86944 , lon : 151.20833 , name : 'Sydney' } ,
{ lat : - 37.82056 , lon : 144.96139 , name : 'Melbourne' } ,
{ lat : - 34.93333 , lon : 138.58333 , name : 'Adelaide' } ,
{ lat : - 27.46778 , lon : 153.02778 , name : 'Brisbane' } ,
{ lat : - 31.95306 , lon : 115.85889 , name : 'Perth' }
] ;
const geo = new Geo ( data , { setOptions : { id : 'name' , lat : 'lat' , lon : 'lon' } } ) ;
geo . nearBy ( - 33.87 , 151.2 , 5000 ) ;
Si tiene una gran cantidad de datos, puede ser más inteligente guardarlos en un archivo:
const data = require ( './huge.data.set.file.json' ) ;
Geo . createCompactSet ( { id : '_id' , lat : 'lat' , lon : 'lon' , file : './compact.set.json' } ) ;
Y luego cargar en variable:
const dataSet = require ( './compact.set.json' ) ;
const geo = new Geo ( dataSet , { sorted : true } ) ;
geo . nearBy ( 64.54 , 40.54 , 5000 ) ;
Para limitar los resultados, tienes dos formas:
1. Defina límite en las opciones. Eso le permite definir un límite permanente para los resultados.
const geo = new Geo ( dataSet , { limit : 1 } ) ;
geo . nearBy ( 64.54 , 40.54 , 3000 ) ;
new Geo ( dataSet , { limit : 1 } ) . nearBy ( 64.54 , 40.54 , 3000 ) ;
geo . nearBy ( - 33.87 , 151.2 , 5000 ) ;
En todos estos casos, los resultados se limitarán a 1.
2. Defina el límite mediante el método limit()
. Eso le permite definir un límite temporal para los resultados.
const foo = new Geo ( dataSet ) . limit ( 1 ) ;
foo . nearBy ( 64.54 , 40.54 , 5000 ) ; //up to 1
foo . nearBy ( 64.54 , 40.54 , 5000 ) ; //no limits
const bar = new Geo ( dataSet , { limit : 1 } ) . limit ( 10 ) ;
bar . nearBy ( 64.54 , 40.54 , 5000 ) ; //up to 10
bar . nearBy ( 64.54 , 40.54 , 5000 ) ; //up to 1. Options limit - permanent limit.
bar . limit ( 2 ) . nearBy ( 64.54 , 40.54 , 5000 ) ; //up to 2
Para una definición más precisa, puede utilizar un rango de distancias. Es un poco más lento pero más preciso.
const geo = new Geo ( dataSet ) ;
geo . limit ( 2 ) . nearBy ( 64.54 , 40.54 , [ 250 , 30000 ] ) ;
Nota: No utilice una distancia demasiado pequeña como valor inicial. Para los valores, la ejecución de menos de 250 scripts puede llevar demasiado tiempo para un conjunto de datos sin clasificar. Normalmente 250 - 500 es suficiente.
Si creó un conjunto de datos mediante el método createCompactSet
o su propio conjunto de datos está ordenado por propiedad geohash
en orden ascendente, puede activar una búsqueda binaria extremadamente rápida.
Simplemente establezca la propiedad sorted
como true
en options
del constructor.
Una búsqueda binaria es 20 veces más rápida de lo normal.
const geo = new Geo ( dataSet , { sorted : true } ) ;
geo . limit ( 1 ) . nearBy ( 64.54 , 40.54 , [ 250 , 30000 ] ) ;
Si tiene un conjunto de datos (con sintaxis recomendada) que no está ordenado, puede ordenarlo fácilmente, simplemente establezca la propiedad sort
como true
en options
del constructor.
El conjunto de datos se ordenará automáticamente mediante un algoritmo de introclasificación rápido. Pero tenga en cuenta que la clasificación llevará algún tiempo.
En algunos casos, la búsqueda en un conjunto de datos sin ordenar será más rápida que ordenar y buscar.
const geo = new Geo ( dataSet , { sort : true } ) ;
geo . limit ( 1 ) . nearBy ( 64.54 , 40.54 , [ 250 , 30000 ] ) ;
Constructor.
sorted
como true
habilitará la búsqueda binaria (modo súper rápido)hash
de arriba) const geo = new Geo ( dataSet , { hash : 'geo' , limit : 1 , sorted : true } ) ;
Método de búsqueda de lugares cercanos.
const geo = new Geo ( dataSet ) ;
geo . nearBy ( 64.54 , 40.54 , [ 500 , 300000 ] ) ;
Límite temporal de resultados.
const geo = new Geo ( dataSet ) ;
geo . limit ( 1 ) . nearBy ( 64.54 , 40.54 , [ 500 , 300000 ] ) ;
El método crea un conjunto de datos.
Método estático.
const dataSet = Geo . createCompactSet ( data , { id : [ 'names' , 'name' , 'id' ] } ) ;
La licencia MIT (MIT)
Copyright (c) 2015-2016 Alexey Bystrov