├── .github ├── dependabot.yml └── workflows │ ├── go.yml │ └── lint.yml ├── .gitignore ├── .golangci.yml ├── LICENSE ├── Makefile ├── README.md ├── geo ├── coordinates.go ├── coordinates_test.go ├── degree.go ├── degree_test.go ├── geo.go ├── radian.go ├── radian_test.go ├── vec3d.go └── vec3d_test.go └── go.mod /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: gomod 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | - package-ecosystem: github-actions 8 | directory: "/" 9 | schedule: 10 | interval: daily 11 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | jobs: 11 | test: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Set up Go 1.24 16 | uses: actions/setup-go@v5 17 | with: 18 | go-version: '1.24' 19 | - name: Check out code 20 | uses: actions/checkout@v4 21 | - name: Run coverage 22 | run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic 23 | - uses: codecov/codecov-action@v5 24 | with: 25 | token: ${{ secrets.CODECOV_TOKEN }} 26 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | permissions: 11 | contents: read 12 | checks: write 13 | 14 | jobs: 15 | golangci-lint: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v4 19 | - uses: actions/setup-go@v5 20 | with: 21 | go-version: '1.24' 22 | - name: golangci-lint 23 | uses: golangci/golangci-lint-action@v6 24 | with: 25 | version: v1.64.5 26 | args: --timeout 5m 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX leaves these everywhere on SMB shares 2 | ._* 3 | 4 | # OSX trash 5 | .DS_Store 6 | 7 | # Eclipse files 8 | .classpath 9 | .project 10 | .settings/** 11 | 12 | # Files generated by JetBrains IDEs, e.g. IntelliJ IDEA 13 | .idea/ 14 | *.iml 15 | 16 | # Vscode files 17 | .vscode 18 | 19 | # This is where the result of the go build goes 20 | /output*/ 21 | /_output*/ 22 | /_output 23 | 24 | # Emacs save files 25 | *~ 26 | \#*\# 27 | .\#* 28 | 29 | # Vim-related files 30 | [._]*.s[a-w][a-z] 31 | [._]s[a-w][a-z] 32 | *.un~ 33 | Session.vim 34 | .netrwhist 35 | 36 | # Go test binaries 37 | *.test 38 | /hack/.test-cmd-auth 39 | 40 | # JUnit test output from ginkgo e2e tests 41 | /junit*.xml 42 | 43 | # Mercurial files 44 | **/.hg 45 | **/.hg* 46 | 47 | # Vagrant 48 | .vagrant 49 | network_closure.sh 50 | 51 | # Local cluster env variables 52 | /cluster/env.sh 53 | 54 | # Compiled binaries in third_party 55 | /third_party/pkg 56 | 57 | .tags* 58 | 59 | # Karma output 60 | /www/test_out 61 | 62 | # precommit temporary directories created by ./hack/verify-generated-docs.sh and ./hack/lib/util.sh 63 | /_tmp/ 64 | /doc_tmp/ 65 | 66 | # Test artifacts produced by Jenkins jobs 67 | /_artifacts/ 68 | 69 | # Go dependencies installed on Jenkins 70 | /_gopath/ 71 | 72 | # Config directories created by gcloud and gsutil on Jenkins 73 | /.config/gcloud*/ 74 | /.gsutil/ 75 | 76 | # direnv .envrc files 77 | .envrc 78 | 79 | # make-related metadata 80 | /.make/ 81 | 82 | # This file used by some vendor repos (e.g. github.com/go-openapi/...) to store secret variables and should not be ignored 83 | !\.drone\.sec 84 | 85 | /bazel-* 86 | *.pyc 87 | 88 | /.*-dockerfile 89 | /.*-container 90 | /.*-push 91 | /.go 92 | /bin -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | run: 2 | timeout: 5m 3 | modules-download-mode: mod 4 | 5 | linters: 6 | enable: 7 | - goimports 8 | - revive 9 | - govet 10 | - staticcheck 11 | - errcheck 12 | 13 | issues: 14 | exclude-use-default: false 15 | max-issues-per-linter: 0 16 | max-same-issues: 0 17 | 18 | linters-settings: 19 | goimports: 20 | local-prefixes: github.com/EpicStep/go-simple-geo -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Stepan Rabotkin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | GOLANGCI_LINT_VERSION = 1.64.5 2 | ACTIONLINT_VERSION = 1.7.7 3 | 4 | test: 5 | go test --timeout 10m -race ./... 6 | 7 | coverage: 8 | go test -race -v -coverpkg=./... -coverprofile=profile.out ./... 9 | go tool cover -func profile.out 10 | 11 | lint: 12 | go run github.com/golangci/golangci-lint/cmd/golangci-lint@v$(GOLANGCI_LINT_VERSION) run 13 | 14 | actionlint: 15 | go run github.com/rhysd/actionlint/cmd/actionlint@v$(ACTIONLINT_VERSION) 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go-simple-geo is a library for simple geo calculations. 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/EpicStep/go-simple-geo.svg)](https://pkg.go.dev/github.com/EpicStep/go-simple-geo/v2) 4 | [![Lint](https://github.com/EpicStep/go-simple-geo/actions/workflows/lint.yml/badge.svg)](https://github.com/EpicStep/go-simple-geo/actions/workflows/lint.yml) 5 | [![Tests](https://github.com/EpicStep/go-simple-geo/actions/workflows/go.yml/badge.svg)](https://github.com/EpicStep/go-simple-geo/actions/workflows/go.yml) 6 | [![codecov](https://codecov.io/gh/EpicStep/go-simple-geo/branch/master/graph/badge.svg?token=UE3A8O81TA)](https://codecov.io/gh/EpicStep/go-simple-geo) 7 | [![Go Report Card](https://goreportcard.com/badge/github.com/EpicStep/go-simple-geo)](https://goreportcard.com/report/github.com/EpicStep/go-simple-geo) 8 | 9 | ---- 10 | 11 | ## Installation 12 | ```bash 13 | go get github.com/EpicStep/go-simple-geo/v2 14 | ``` 15 | 16 | ## Example 17 | 18 | ```go 19 | package main 20 | 21 | import ( 22 | "fmt" 23 | 24 | "github.com/EpicStep/go-simple-geo/v2/geo" 25 | ) 26 | 27 | func main() { 28 | c1 := geo.NewCoordinatesFromDegrees(55.7522, 37.6156) 29 | c2 := geo.NewCoordinatesFromDegrees(59.9386, 30.3141) 30 | 31 | fmt.Println(c1.Distance(c2)) 32 | } 33 | ``` -------------------------------------------------------------------------------- /geo/coordinates.go: -------------------------------------------------------------------------------- 1 | package geo 2 | 3 | import "math" 4 | 5 | // Coordinates is a Lat Long struct. 6 | type Coordinates struct { 7 | Latitude, Longitude float64 8 | } 9 | 10 | // NewCoordinatesFromDegrees return Coordinates from lat and long in Degree. 11 | func NewCoordinatesFromDegrees(latDeg, longDeg float64) *Coordinates { 12 | return &Coordinates{ 13 | Latitude: DegreeToRadians(latDeg), 14 | Longitude: DegreeToRadians(longDeg), 15 | } 16 | } 17 | 18 | // NewCoordinatesFromRadians return Coordinates from lat and long in Radian. 19 | func NewCoordinatesFromRadians(latRads, longRads float64) *Coordinates { 20 | return &Coordinates{ 21 | Latitude: latRads, 22 | Longitude: longRads, 23 | } 24 | } 25 | 26 | // EarthRadiusKilometers ... 27 | const EarthRadiusKilometers = 6371 28 | 29 | // Distance between two points in kilometers. 30 | func (c *Coordinates) Distance(c2 *Coordinates) float64 { 31 | sinLat := math.Sin((c2.Latitude - c.Latitude) / 2) 32 | sinLng := math.Sin((c2.Longitude - c.Longitude) / 2) 33 | 34 | a := sinLat*sinLat + math.Cos(c.Latitude)*math.Cos(c2.Latitude)*sinLng*sinLng 35 | 36 | return 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a)) * EarthRadiusKilometers 37 | } 38 | -------------------------------------------------------------------------------- /geo/coordinates_test.go: -------------------------------------------------------------------------------- 1 | package geo_test 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | "github.com/EpicStep/go-simple-geo/v2/geo" 8 | ) 9 | 10 | func TestCoordinates_Distance(t *testing.T) { 11 | type test struct { 12 | v *geo.Coordinates 13 | v2 *geo.Coordinates 14 | want string 15 | } 16 | 17 | tests := []test{ 18 | { 19 | v: geo.NewCoordinatesFromDegrees(55.9606, 38.0456), 20 | v2: geo.NewCoordinatesFromDegrees(59.9386, 30.3141), 21 | want: "634.70", 22 | }, 23 | { 24 | v: geo.NewCoordinatesFromDegrees(55.7522, 37.6156), 25 | v2: geo.NewCoordinatesFromDegrees(54.7818, 32.0401), 26 | want: "369.22", 27 | }, 28 | { 29 | v: geo.NewCoordinatesFromDegrees(40.7143, -74.006), 30 | v2: geo.NewCoordinatesFromDegrees(37.7749, -122.419), 31 | want: "4129.02", 32 | }, 33 | } 34 | 35 | for _, tt := range tests { 36 | if x := fmt.Sprintf("%.2f", tt.v.Distance(tt.v2)); x != tt.want { 37 | t.Fatalf("incorrect answer for (%.2f %.2f) and (%.2f %.2f). Want: %s. Got: %s", tt.v.Latitude, tt.v.Longitude, tt.v2.Latitude, tt.v.Longitude, tt.want, x) 38 | } 39 | } 40 | } 41 | 42 | func TestNewCoordinatesFromRadians(t *testing.T) { 43 | c := geo.NewCoordinatesFromRadians(0.1, 0.1) 44 | 45 | if c.Latitude != 0.1 || c.Longitude != 0.1 { 46 | t.Fail() 47 | } 48 | } 49 | 50 | func BenchmarkCoordinates_Distance(b *testing.B) { 51 | d := geo.NewCoordinatesFromDegrees(55.9606, 38.0456) 52 | d2 := geo.NewCoordinatesFromDegrees(59.9386, 30.3141) 53 | 54 | b.ResetTimer() 55 | 56 | for i := 0; i < b.N; i++ { 57 | if dist := d.Distance(d2); dist != 634.6971900186131 { 58 | b.Fail() 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /geo/degree.go: -------------------------------------------------------------------------------- 1 | package geo 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | // DegreeToRadians convert degrees to radians 8 | func DegreeToRadians(deg float64) float64 { 9 | return deg * (math.Pi / unfoldedAngle) 10 | } 11 | -------------------------------------------------------------------------------- /geo/degree_test.go: -------------------------------------------------------------------------------- 1 | package geo_test 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | "github.com/EpicStep/go-simple-geo/v2/geo" 8 | ) 9 | 10 | func TestDegree_ToRadians(t *testing.T) { 11 | type test struct { 12 | v float64 13 | want string 14 | } 15 | 16 | tests := []test{ 17 | { 18 | v: 100, 19 | want: "1.75", 20 | }, 21 | { 22 | v: 57.3, 23 | want: "1.00", 24 | }, 25 | { 26 | v: 1, 27 | want: "0.02", 28 | }, 29 | } 30 | 31 | for _, tt := range tests { 32 | if x := fmt.Sprintf("%.2f", geo.DegreeToRadians(tt.v)); x != tt.want { 33 | t.Fatalf("incorrect answer for %.2f. Want: %s. Got: %s", tt.v, tt.want, x) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /geo/geo.go: -------------------------------------------------------------------------------- 1 | // Package geo is a package for simple geo calculations. 2 | package geo 3 | 4 | const unfoldedAngle = 180.0 5 | -------------------------------------------------------------------------------- /geo/radian.go: -------------------------------------------------------------------------------- 1 | package geo 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | // RadiansToDegrees convert radians to degrees. 8 | func RadiansToDegrees(rads float64) float64 { 9 | return rads * (unfoldedAngle / math.Pi) 10 | } 11 | -------------------------------------------------------------------------------- /geo/radian_test.go: -------------------------------------------------------------------------------- 1 | package geo_test 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | "github.com/EpicStep/go-simple-geo/v2/geo" 8 | ) 9 | 10 | func TestRadian_ToDegrees(t *testing.T) { 11 | type test struct { 12 | v float64 13 | want string 14 | } 15 | 16 | tests := []test{ 17 | { 18 | v: 1, 19 | want: "57.30", 20 | }, 21 | { 22 | v: 0.5, 23 | want: "28.65", 24 | }, 25 | { 26 | v: 4, 27 | want: "229.18", 28 | }, 29 | } 30 | 31 | for _, tt := range tests { 32 | if x := fmt.Sprintf("%.2f", geo.RadiansToDegrees(tt.v)); x != tt.want { 33 | t.Fatalf("incorrect answer for %.2f. Want: %s. Got: %s", tt.v, tt.want, x) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /geo/vec3d.go: -------------------------------------------------------------------------------- 1 | package geo 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | // Vec3d is a 3D floating point structure. 8 | type Vec3d struct { 9 | X, Y, Z float64 10 | } 11 | 12 | // SquareDistance return a square of the distance between two vectors. 13 | func (vec *Vec3d) SquareDistance(v *Vec3d) float64 { 14 | return math.Pow(vec.X-v.X, 2) + math.Pow(vec.Y-v.Y, 2) + math.Pow(vec.Z-v.Z, 2) 15 | } 16 | 17 | // ToVec3d return Vec3d from Coordinates. 18 | func (c *Coordinates) ToVec3d() *Vec3d { 19 | r := math.Cos(c.Latitude) 20 | 21 | return &Vec3d{ 22 | X: math.Cos(c.Longitude) * r, 23 | Y: math.Sin(c.Longitude) * r, 24 | Z: math.Sin(c.Latitude), 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /geo/vec3d_test.go: -------------------------------------------------------------------------------- 1 | package geo_test 2 | 3 | import ( 4 | "math" 5 | "testing" 6 | 7 | "github.com/EpicStep/go-simple-geo/v2/geo" 8 | ) 9 | 10 | func TestDistance(t *testing.T) { 11 | type test struct { 12 | v *geo.Vec3d 13 | want float64 14 | } 15 | 16 | tests := []test{ 17 | { 18 | v: &geo.Vec3d{ 19 | X: 0, 20 | Y: 0, 21 | Z: 0, 22 | }, 23 | want: 0, 24 | }, 25 | { 26 | v: &geo.Vec3d{ 27 | X: 1, 28 | Y: 0, 29 | Z: 0, 30 | }, 31 | want: 1, 32 | }, 33 | { 34 | v: &geo.Vec3d{ 35 | X: 0, 36 | Y: 1, 37 | Z: 1, 38 | }, 39 | want: 2, 40 | }, 41 | { 42 | v: &geo.Vec3d{ 43 | X: 1, 44 | Y: 1, 45 | Z: 1, 46 | }, 47 | want: 3, 48 | }, 49 | { 50 | v: &geo.Vec3d{ 51 | X: 1, 52 | Y: 1, 53 | Z: 2, 54 | }, 55 | want: 6, 56 | }, 57 | } 58 | 59 | for _, tt := range tests { 60 | distance := tests[0].v.SquareDistance(tt.v) 61 | 62 | if distance != tt.want { 63 | t.Fatalf("incorrect answer for x:%2f, y:%2f, z:%2f. Want: %2f", tt.v.X, tt.v.Y, tt.v.Z, tt.want) 64 | } 65 | } 66 | } 67 | 68 | func TestGeo_ToVec3d(t *testing.T) { 69 | origin := geo.Vec3d{ 70 | X: 0, 71 | Y: 0, 72 | Z: 0, 73 | } 74 | 75 | c1 := geo.Coordinates{ 76 | Latitude: 0, 77 | Longitude: 0, 78 | } 79 | 80 | c2 := geo.Coordinates{ 81 | Latitude: math.Pi / 2, 82 | Longitude: 0, 83 | } 84 | 85 | c3 := geo.Coordinates{ 86 | Latitude: math.Pi, 87 | Longitude: 0, 88 | } 89 | 90 | p1Vec := c1.ToVec3d() 91 | 92 | if p1 := origin.SquareDistance(p1Vec); p1 != 1 { 93 | t.Fatalf("answer is incorrect. Want: %d, got: %2f", 1, p1) 94 | } 95 | 96 | p2Dist := p1Vec.SquareDistance(c2.ToVec3d()) 97 | 98 | if p2Dist >= 2 && p2Dist <= 1.9 { 99 | t.Fatalf("answer is incorrect. Want: %d, got: %2f", 1, p2Dist) 100 | } 101 | 102 | if p3 := p1Vec.SquareDistance(c3.ToVec3d()); p3 != 4 { 103 | t.Fatalf("answer is incorrect. Want: %d, got: %2f", 1, p3) 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/EpicStep/go-simple-geo/v2 2 | 3 | go 1.24 4 | --------------------------------------------------------------------------------