Skip to content

Instantly share code, notes, and snippets.

@malte-wessel
Last active August 13, 2017 08:40
Show Gist options
  • Save malte-wessel/e21134a2754f8011e18a744966952e58 to your computer and use it in GitHub Desktop.
Save malte-wessel/e21134a2754f8011e18a744966952e58 to your computer and use it in GitHub Desktop.
// https://github.com/salsita/geo-tree
// Red-black tree for geo coordinates, scalar indices with z-curve
import GeoTree from 'geo-tree';
const gt = new GeoTree();
// Items sorted by relevance, data holds itemId
const items = [
{lat: 48.85886, lng: 2.34706, data: 3849712},
{lat: 52.50754, lng: 13.42614, data: 234910},
{lat: 50.05967, lng: 14.46562, data: 115502},
...
];
// Index items by coordinates
gt.insert(items);
// Items that will be shown on map
const validItems = [];
// Keeps track of seen items
const itemsSeenById = {}
// Minimal distance between items
const minDistance = 10.0;
for (let i = 0, l = items.lenght; i < l; i++) {
const item = items[i];
// This item was marked as seen, ignore
if (itemsSeenById[data]) continue;
validItems.push(item);
// Find items that violate min distance and mark them as seen
const neighbours = gt.find(item.lat, item.lng, minDistance);
for (let j = 0, k = neighbours.length; j < k; j++) {
const neighbour = neighbours[j];
itemsSeenById[neighbour.data] = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment