Socket
Socket
Sign inDemoInstall

geo-tree

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

geo-tree - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

lib/geo-tree-0.1.1.js

2

package.json
{
"name": "geo-tree",
"version": "0.1.0",
"version": "0.1.1",
"description": "High performance library for map-related operations",

@@ -5,0 +5,0 @@ "export-symbol": "GeoTree",

@@ -181,5 +181,10 @@ # Geo-tree library

Finally, you can pass one `lat`/`lng` object and a float number, these two
making up a circle (with provided center and (angle-)radius) in which you search
for the items.
Finally, you can pass one `lat`/`lng` object, a float number, and optionally a
string. These parameters are making up a circle (with provided center and
radius) in which you search for the items. If you don't provide the third string
argument, the units for the provided radius value are native to latitude and
longitude (i.e. angle degrees). If you want to specify different units for
radius, select one of the following: `m` for meters, `km` for kilometers, `yd`
for yards, and `mi` for miles. In case you pass any other string, it is ignored
and the value is not converted.

@@ -189,2 +194,5 @@ set.find({lat: 51, lng: 14}, 2.0);

set.find({lat: 51, lng: 17}, 200.0, 'mi');
// --> ['Prague, Czech Republic', 'Berlin, Germany']
### Iteration over all items

@@ -224,1 +232,2 @@

* 0.1.0 (2014-09-04): initial version (`insert`, `find` and `forEach` operations)
* 0.1.0 (2014-09-16): support for m/km/yd/mi radius value for circle-search operation

@@ -16,2 +16,22 @@ // geo-tree implementation (using red-black tree and z-curve)

// --- helper functions ---
// WARNING: the conversion will work well only for small distances
function convertToAngle(lat, val, units) {
var conversionTable = [
{ units: 'm', ratio: 1.0 },
{ units: 'km', ratio: 1000.0 },
{ units: 'yd', ratio: 0.9144 },
{ units: 'mi', ratio: 1609.34 }
];
for (var i = 0; i < conversionTable.length; i++) {
if (conversionTable[i].units === units) { break; }
}
if (conversionTable.length === i) { return val; }
var angle = (val * conversionTable[i].ratio) / (6378137.0 * Math.cos(Math.PI * lat / 180.0));
return angle * 180.0 / Math.PI;
}
// --- end of helper functions ---
function GeoTree() {

@@ -54,7 +74,8 @@ this.tree = new RBTree();

// { lat: ..., lng: ... }, radius (in angles) - circle
GeoTree.prototype.find = function(arg1, arg2) {
// { lat: ..., lng: ... }, radius, units (m, km, yd, mi) - circle
GeoTree.prototype.find = function(arg1, arg2, arg3) {
var all, radius;
all = (0 === arguments.length);
if (undefined === arg2) { arg2 = arg1; }
if ('number' === typeof(arg2)) { radius = arg2; }
if ('number' === typeof(arg2)) { radius = convertToAngle(arg1.lat, arg2, arg3); }
var minLat, maxLat, minLng, maxLng, minIdx = -Infinity, maxIdx = Infinity;

@@ -61,0 +82,0 @@ if (!all) {

@@ -21,2 +21,11 @@ var assert = require('assert');

function createDistTestSet() {
gt.insert([
{lat: 50.0755, lng: 14.4378, data: 'Praha'}, // dist: Brno: 184.538km, Ostrava: 275.401km, Plzen: 85.023km
{lat: 49.1951, lng: 16.6068, data: 'Brno'}, // dist: Ostrava: 138.475km, Plzen: 241.577km
{lat: 49.8209, lng: 18.2625, data: 'Ostrava'},// dist: Plzen: 351.482km
{lat: 49.7384, lng: 13.3736, data: 'Plzen'} //
]);
}
function addLog(element) {

@@ -125,2 +134,42 @@ log.push(element);

it('find (circle search: units: m)', function() {
createDistTestSet();
var res = gt.find({lat: 50.0755, lng: 14.4378}, 200000.0, 'm').sort();
var expect = ['Brno', 'Plzen', 'Praha'];
assert.equal(res.length, expect.length);
expect.forEach(function(val, idx) { assert.equal(res[idx], val); });
});
it('find (circle search: units: km)', function() {
createDistTestSet();
var res = gt.find({lat: 49.1951, lng: 16.6068}, 200.0, 'km').sort();
var expect = ['Brno', 'Ostrava', 'Praha'];
assert.equal(res.length, expect.length);
expect.forEach(function(val, idx) { assert.equal(res[idx], val); });
});
it('find (circle search: units: yd)', function() {
createDistTestSet();
var res = gt.find({lat: 49.8209, lng: 18.2625}, 153106.0, 'yd').sort();
var expect = ['Brno', 'Ostrava'];
assert.equal(res.length, expect.length);
expect.forEach(function(val, idx) { assert.equal(res[idx], val); });
});
it('find (circle search: units: mi)', function() {
createDistTestSet();
var res = gt.find({lat: 49.7384, lng: 13.3736}, 56.0, 'mi').sort();
var expect = ['Plzen', 'Praha'];
assert.equal(res.length, expect.length);
expect.forEach(function(val, idx) { assert.equal(res[idx], val); });
});
it('find (circle search: units: unknown)', function() {
createDistTestSet();
var res = gt.find({lat: 50.0755, lng: 14.4378}, 2.8, 'hello world').sort();
var expect = ['Brno', 'Plzen', 'Praha'];
assert.equal(res.length, expect.length);
expect.forEach(function(val, idx) { assert.equal(res[idx], val); });
});
//

@@ -127,0 +176,0 @@ // forEach

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc