Sep 06, 2020 01:27 PM
// updates the Deal table
let tDeals = base.getTable(‘Deals’);
let qDeals = await tDeals.selectRecordsAsync();
for (let rDeals of qDeals.records){
let d= getDistanceFromLatLonInKm(rDeals.getCellValue(‘Latitide’),rDeals.getCellValue(‘Longitude’),rDeals.getCellValue(‘BLa’),rDeals.getCellValue(‘BLong’));
await tDeals.updateRecordAsync(rDeals.id, {‘Distance’ : d})
}
// distance between 2 given points (in miles)
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 3958.75586579; // Radius of the earth in miles
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in miles
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
Dec 07, 2020 04:59 PM
Hi!
Thanks for the script. I didn’t get it to work however.
Are you interesting in helping me solve it? Of course I can pay for it.
Dec 07, 2020 05:19 PM
Hi Tobias,
Yes, I’d like to lend you a hand…
Dec 08, 2020 05:15 AM
The code @Benito_Abraham shared is functional; it definitely works. I use a variant of that approach; feel free to test this one.
My hunch is you are running into changes that are necessary to use this code in the context of your own data/table.
output.markdown('# Distance From Point A to Point B');
let pointA = [37.09652780, -113.5684164]; // St George
let pointB = [36.1251954,-115.3154252]; // Las Vegas
output.markdown("Point A: " + pointA.toString());
output.markdown("Point B: " + pointB.toString());
let distanceInMiles = distance(pointA[0], pointA[1], pointB[0], pointB[1]);
output.markdown("Distance in Miles: " + distanceInMiles);
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::: :::
//::: This routine calculates the distance between two points (given the :::
//::: latitude/longitude of those points). It is being used to calculate :::
//::: the distance between two locations using GeoDataSource (TM) prodducts :::
//::: :::
//::: Definitions: :::
//::: South latitudes are negative, east longitudes are positive :::
//::: :::
//::: Passed to function: :::
//::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
//::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
//::: unit = the unit you desire for results :::
//::: where: 'M' is statute miles (default) :::
//::: 'K' is kilometers :::
//::: 'N' is nautical miles :::
//::: :::
//::: Worldwide cities and other features databases with latitude longitude :::
//::: are available at https://www.geodatasource.com :::
//::: :::
//::: For enquiries, please contact sales@geodatasource.com :::
//::: :::
//::: Official Web site: https://www.geodatasource.com :::
//::: :::
//::: GeoDataSource.com (C) All Rights Reserved 2018 :::
//::: :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function distance(lat1, lon1, lat2, lon2, unit) {
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
}
else {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist;
}
}