A simple Go program to calculate circular distances between geographical points using different formulas.
go-distances
calculates the distances between a set of geographical points arranged in a circular path. It prompts the user for the number of points, their latitudes and longitudes, the Earth's radius, and the formula to use for distance calculation. Currently, it supports the Haversine and Vincenty formulas.
- Calculates circular distances for any number of points.
- Supports Haversine and Vicinity distance formulas.
- User-friendly command-line interface.
- Go (version 1.16 or higher)
Either, clone this repo, or download the binary.
git clone https://github.com/dickeyy/go-distances.git
cd go-distances
go install github.com/dickeyy/go-distances@latest
-
Run the program:
If you cloned the repo, run
go run main.go
. If you downloaded the binary, rungo-distances
. -
Follow the prompts:
- Do you want to import points from a file? (y/n): y
- Enter the path to the file.
OR
- Do you want to import points from a file? (y/n): n
- Enter the number of points.
- Enter the latitude and longitude for each point.
- Enter the Earth's radius (in your desired unit, e.g., 6371 for kilometers).
- Enter the formula to use (
haversine
orvincenty
).
-
View the results:
The program will output the calculated distances between each pair of consecutive points in the circular path.
If you wish to import data from a file, the file MUST be in JSON format, and follow the example format below:
{
"places": [
{
"name": "some name",
"latitude": "some latitude", // must be a string in degrees
"longitude": "some longitude"
},
{
"name": "some other name",
"latitude": "some other latitude",
"longitude": "some other longitude"
}
...
],
"earthRadius": 12345, // whatever unit you want
"formula": "haversine" // optional, defaults to "vincenty"
}
Note the comments, the coordinates must be in degrees and represented as strings. The earthRadius
is the radius of the Earth in whatever unit you want, and the formula
is optional and defaults to vincenty
.
This script will run the program on all the test data files in the test-data
directory, if you have a bunch of test data files you want to run quickly. Note: for this to work, you need to build the Go program first. To build the program, run go build -o go-distances main.go
.
More indepth information about the formulas can be found Here.
haversine
: Uses the Haversine formula to calculate the distance between two points on a sphere.vincenty
: Uses the Vincenty formula to calculate the distance between two points on a sphere.sloc
: Uses the Spherical Law of Cosines formula to calculate the distance between two points on a sphere.
MIT License, see LISENCE file.