Hi Ron,
Thanks for your answer. I initially tried to convert the ruby code into a javascript code, but I don't know anything about coding and it was too hard for me to make it work. Then I asked the ChatGPT bot to write me a code from scratch, here's the prompt and answer :
I want an airtable script code that use the Haversine formula to create new coordinates that are 25km from given coordinates
-> Here is an example of an Airtable script that uses the Haversine formula to generate new coordinates that are 25 km away from a set of given coordinates:
function generateNewCoordinates(lat, lon) {
// Earth's radius in kilometers
var R = 6371;
// Distance to move in kilometers
var distance = 25;
// Bearing (degrees)
var bearing = Math.random() * 360;
// Convert latitude and longitude to radians
var lat1 = lat * (Math.PI / 180);
var lon1 = lon * (Math.PI / 180);
// Calculate new latitude and longitude
var lat2 = Math.asin( Math.sin(lat1)*Math.cos(distance/R) +
Math.cos(lat1)*Math.sin(distance/R)*Math.cos(bearing) );
var lon2 = lon1 + Math.atan2(Math.sin(bearing)*Math.sin(distance/R)*Math.cos(lat1),
Math.cos(distance/R)-Math.sin(lat1)*Math.sin(lat2));
// Convert back to degrees
lat2 = lat2 * (180 / Math.PI);
lon2 = lon2 * (180 / Math.PI);
// Return new coordinates
return [lat2, lon2];
}You can use this function in your script, and pass it the initial coordinates (latitude and longitude) as arguments, to generate a new set of coordinates that are 25 km away from the original coordinates.
---
Then I asked for some improvments :
Can I have the same code but with a distance withnin a radius of 25km maximum and 15km minimum ?Sure! Here's an updated version of the script that generates new coordinates within a radius of 25 km maximum and 15km minimum from a given set of coordinates:
function generateNewCoordinates(lat, lon) {
// Earth's radius in kilometers
var R = 6371;
// Maximum distance to move in kilometers
var maxDistance = 25;
// Minimum distance to move in kilometers
var minDistance = 15;
// Random distance within the range
var distance = Math.random() * (maxDistance - minDistance) + minDistance;
// Bearing (degrees)
var bearing = Math.random() * 360;
// Convert latitude and longitude to radians
var lat1 = lat * (Math.PI / 180);
var lon1 = lon * (Math.PI / 180);
// Calculate new latitude and longitude
var lat2 = Math.asin( Math.sin(lat1)*Math.cos(distance/R) +
Math.cos(lat1)*Math.sin(distance/R)*Math.cos(bearing) );
var lon2 = lon1 + Math.atan2(Math.sin(bearing)*Math.sin(distance/R)*Math.cos(lat1),
Math.cos(distance/R)-Math.sin(lat1)*Math.sin(lat2));
// Convert back to degrees
lat2 = lat2 * (180 / Math.PI);
lon2 = lon2 * (180 / Math.PI);
// Return new coordinates
return [lat2, lon2];
}This script works the same way as the previous one, but now it calculates a random distance between 15 and 25 kilometers, instead of always generating coordinates 25km away from the initial point.
---
That's quite amzing, I managed with that to build the script and launch it within an automation. I though I'd killed Airtable as it stays on "pending" for quite long on the second task on maaaany, but after dinner the job was done !
Some weeks ago I would not have though about coding without knowing anything in JS : for this AI is woow.
For those who would like to do the same, just add this two lines at the end of the above AI generated code :
let inputConfig = input.config();
output.set('RandomCoordinates', generateNewCoordinates(inputConfig.lat, inputConfig.lon));