Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@victusfate
Last active May 1, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victusfate/eabc105c85d72d85465e19386e51f5f5 to your computer and use it in GitHub Desktop.
Save victusfate/eabc105c85d72d85465e19386e51f5f5 to your computer and use it in GitHub Desktop.
perturb lat,lon by fixed radius, added example for php nerds
const geolib = require('geolib')
// https://gis.stackexchange.com/questions/25877/generating-random-locations-nearby
const perturbedLatLon = (lat,lon,deltaM) => {
const r = deltaM/111300 // = 100 meters
const y0 = lat
const x0 = lon
const u = Math.random()
const v = Math.random()
const w = r * Math.sqrt(u)
const t = 2 * Math.PI * v
const x = w * Math.cos(t)
const y1 = w * Math.sin(t)
const x1 = x / Math.cos(y0)
newY = y0 + y1
newX = x0 + x1
// console.log({ r:r, y0:y0, x0:x0, u:u, v:v, w:w, t:t, x:x, y1:y1, x1: x1, newY:newY, newX:newX })
return [newY,newX]
}
if (process.argv.length < 4) {
console.log('usage node perturbLatLon lat lon (deltaM opt)');
process.exit(0);
}
let deltaM = 1000; // meters
const origLat = parseFloat(process.argv[2]);
const origLon = parseFloat(process.argv[3]);
if (process.argv.length >= 5) {
deltaM = parseFloat(process.argv[4]);
}
for (let i=0;i < 1000;i++) {
const point = perturbedLatLon(origLat,origLon,deltaM)
const distance = geolib.getDistance({ latitude: origLat, longitude: origLon}, { latitude: point[0], longitude: point[1] });
console.log({ origin: [origLat,origLon], newPoint: point, distanceMeters: distance })
}
<?php
function perturbedLatLon($lat,$lon,$deltaM) {
$r = $deltaM/111300;
$y0 = $lat;
$x0 = $lon;
$u = mt_rand() / mt_getrandmax();
$v = mt_rand() / mt_getrandmax();
$w = $r * sqrt($u);
$t = 2 * pi() * $v;
$x = $w * cos($t);
$y1 = $w * sin($t);
$x1 = $x / cos($y0);
$newY = $y0 + $y1;
$newX = $x0 + $x1;
// dbg('perturbedLatLon',[ 'r' => $r, 'y0' => $y0, 'x0' => $x0, 'u' => $u, 'v' => $v, 'w' => $w, 't' => $t, 'x' => $x, 'y1' => $y1, 'x1' => $x1, 'newLat' => $newY, 'newLon' => $newX ]);
return [$newY,$newX];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment