Skip to content

Instantly share code, notes, and snippets.

@missinglink
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save missinglink/15792b99c1afe56ef0dd to your computer and use it in GitHub Desktop.

Select an option

Save missinglink/15792b99c1afe56ef0dd to your computer and use it in GitHub Desktop.
QuadTree precision calculator
// Requires nodejs and npm
// @see: https://github.com/isaacs/nave for easy installation
// @install nodejs: [sudo] bash nave.sh usemain stable
// Usage:
// $> npm install quadtree geolib
// $> node quadtree.js
var quadtree = require('quadtree');
var geolib = require('geolib');
var coordinate = { lng: -0.058202, lat: 51.53254 };
var precision = 20;
var encoded = quadtree.encode( coordinate, precision );
var decoded = quadtree.decode( encoded );
var distance = geolib.getDistance( coordinate, decoded.origin );
console.log( 'precision:', precision );
console.log( 'input:', coordinate );
console.log( 'encoded:', encoded );
console.log( 'decoded:', decoded.origin );
console.log( 'error margin:', decoded.error );
console.log( 'distance from input:', distance + ' meters' );
@missinglink
Copy link
Copy Markdown
Author

Example output:

$> node quadtree.js 
precision: 20
input: { lng: -0.058202, lat: 51.53254 }
encoded: 01331331313303032112
decoded: { lng: -0.058193206787109375, lat: 51.532487869262695 }
error margin: { lng: 0.000171661376953125, lat: 0.0000858306884765625 }
distance from input: 6 meters
$> node quadtree.js 
precision: 18
input: { lng: -109.367523, lat: -27.093364 }
encoded: 203102302011301003
decoded: { lng: -109.36683654785156, lat: -27.09331512451172 }
error margin: { lng: 0.0006866455078125, lat: 0.00034332275390625 }
distance from input: 68 meters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment