Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
logocomune committed Jan 29, 2022
0 parents commit 0488aa0
Show file tree
Hide file tree
Showing 12 changed files with 776 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
*.iml
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file contains all available configuration options
# with their default values (in comments).

# Options for analysis running.
run:
tests: false


11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: go

go:
- 1.16.x
- 1.17.x

script:
- go test -tags what -race -coverprofile=coverage.txt -covermode=atomic ./...

after_success:
- bash <(curl -s https://codecov.io/bash)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015-2018 Mark Beech

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Maidenhead Locator for golang
[![Build Status](https://app.travis-ci.com/logocomune/maidenhead.svg?branch=master)](https://app.travis-ci.com/logocomune/maidenhead)
[![Go Report Card](https://goreportcard.com/badge/github.com/logocomune/maidenhead)](https://goreportcard.com/report/github.com/logocomune/maidenhead)
[![codecov](https://codecov.io/gh/logocomune/maidenhead/branch/master/graph/badge.svg)](https://codecov.io/gh/logocomune/maidenhead)

The Maidenhead Locator System (a.k.a. QTH Locator and IARU Locator) is a geocode system used by amateur radio operators to succinctly describe their geographic coordinates.

This golang library compress and decompress latitude and longitude coordinates into Maidenhead locator

## Installation

`go get -u github.com/logocomune/maidenhead`

## Usage
```golang
package main

import (
"fmt"
"github.com/logocomune/maidenhead"
)

func main() {
latitude := 43.723073
longitude := 10.396637

locator, _ := maidenhead.Locator(latitude, longitude, maidenhead.FIELD_PRECISION)
fmt.Println("Locator with field precision:", locator)
locator, _ = maidenhead.Locator(latitude, longitude, maidenhead.SQUARE_PRECSION)
fmt.Println("Locator with square precision:", locator)
locator, err := maidenhead.Locator(latitude, longitude, maidenhead.SUB_SQUARE_PRECISION)
fmt.Println("Locator with sub square precision:", locator, err)
locator, _ = maidenhead.Locator(latitude, longitude, maidenhead.EXTENDED_SQUARE_PRECSION)
fmt.Println("Locator with extended square precision:", locator)
locator, _ = maidenhead.Locator(latitude, longitude, maidenhead.SUB_EXTENDED_SQUARE_PRECISION)
fmt.Println("Locator with sub extended square precision:", locator)

lat, lng, _ := maidenhead.GridCenter("JN53er73OM")
fmt.Printf("Grid center of %s is lat: %f and lng: %f\n", "JN53er73OM", lat, lng)

square, _ := maidenhead.Square("JN53er73OM")
fmt.Printf("Square coordinates of %s are %+v\n", "JN53er73OM", square)

}
```
29 changes: 29 additions & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"github.com/logocomune/maidenhead"
)

func main() {
latitude := 43.723073
longitude := 10.396637

locator, _ := maidenhead.Locator(latitude, longitude, maidenhead.FieldPrecision)
fmt.Println("Locator with field precision:", locator)
locator, _ = maidenhead.Locator(latitude, longitude, maidenhead.SquarePrecision)
fmt.Println("Locator with square precision:", locator)
locator, err := maidenhead.Locator(latitude, longitude, maidenhead.SubSquarePrecision)
fmt.Println("Locator with sub square precision:", locator, err)
locator, _ = maidenhead.Locator(latitude, longitude, maidenhead.ExtendedSquarePrecision)
fmt.Println("Locator with extended square precision:", locator)
locator, _ = maidenhead.Locator(latitude, longitude, maidenhead.SubExtendedSquarePrecision)
fmt.Println("Locator with sub extended square precision:", locator)

lat, lng, _ := maidenhead.GridCenter("JN53er73OM")
fmt.Printf("Grid center of %s is lat: %f and lng: %f\n", "JN53er73OM", lat, lng)

square, _ := maidenhead.Square("JN53er73OM")
fmt.Printf("Square coordinates of %s are %+v\n", "JN53er73OM", square)

}
66 changes: 66 additions & 0 deletions compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package maidenhead

//http://www.w8bh.net/grid_squares.pdf
import (
"errors"
"math"
"strconv"
"strings"
)

//Locator returns the Maidenhead locator for the given latitude and longitude and requested precision.
func Locator(lat, lng float64, precision int) (string, error) {
if math.Abs(lat) >= 90 {
return "", errors.New("invalid latitude allowed values are between -90 and 90")
}

if math.Abs(lng) > 180 {
return "", errors.New("invalid longitude allowed values are between -180 and 180")
}

if precision%2 != 0 {
return "", errors.New("grid size must be even ")
}

if precision/2 > maxSteps {
return "", errors.New("grid size must be less than 5")
}

lat += latSouthPole
lng += lngEastwardGreenwich

step := 0

var locator, tmpLoc string
for step < maxSteps {
tmpLoc, lat, lng = subLocator(lat, lng, step)
if step == 2 {
tmpLoc = strings.ToLower(tmpLoc)
}

locator += tmpLoc

if step == (precision/2 - 1) {
break
}
step++
}

return locator, nil
}

func subLocator(lat, lng float64, step int) (string, float64, float64) {
latTmp := math.Trunc(lat / latDivider[step])
lngTmp := math.Trunc(lng / lngDivider[step])

locator := strconv.Itoa(int(lngTmp)) + strconv.Itoa(int(latTmp))

if (step % 2) == 0 {
locator = alphabet[int(lngTmp)] + alphabet[int(latTmp)]
}

lat -= latTmp * latDivider[step]
lng -= lngTmp * lngDivider[step]

return locator, lat, lng
}
156 changes: 156 additions & 0 deletions compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package maidenhead

import "testing"

func TestLocator(t *testing.T) {

testLat := 43.443534
testLng := 10.254315
testLocator := "JN53dk06MK"

type args struct {
lat float64
lng float64
gridSize int
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Bad latitude 01",
args: args{
lat: 90,
lng: 0,
gridSize: SquarePrecision,
},
want: "",
wantErr: true,
},
{
name: "Bad latitude 02",
args: args{
lat: 91,
lng: 0,
gridSize: SquarePrecision,
},
want: "",
wantErr: true,
},
{
name: "Bad latitude 03",
args: args{
lat: -91,
lng: 0,
gridSize: SquarePrecision,
},
want: "",
wantErr: true,
},
{
name: "Bad longitude 01",
args: args{
lat: 0,
lng: 180.1,
gridSize: SquarePrecision,
},
want: "",
wantErr: true,
},
{
name: "Bad longitude 02",
args: args{
lat: 0,
lng: -180.1,
gridSize: SquarePrecision,
},
want: "",
wantErr: true,
},
{
name: "Grid size not even",
args: args{
lat: 0,
lng: 0,
gridSize: 5,
},
want: "",
wantErr: true,
},
{
name: "Grid size bigger than 10",
args: args{
lat: 0,
lng: 0,
gridSize: 12,
},
want: "",
wantErr: true,
},
{
name: "Grid size 2",
args: args{
lat: testLat,
lng: testLng,
gridSize: FieldPrecision,
},
want: testLocator[0:FieldPrecision],
wantErr: false,
},

{
name: "Grid size 4",
args: args{
lat: testLat,
lng: testLng,
gridSize: SquarePrecision,
},
want: testLocator[0:SquarePrecision],
wantErr: false,
},
{
name: "Grid size 6",
args: args{
lat: testLat,
lng: testLng,
gridSize: SubSquarePrecision,
},
want: testLocator[0:SubSquarePrecision],
wantErr: false,
},
{
name: "Grid size 8",
args: args{
lat: testLat,
lng: testLng,
gridSize: ExtendedSquarePrecision,
},
want: testLocator[0:ExtendedSquarePrecision],
wantErr: false,
},
{
name: "Grid size 10",
args: args{
lat: testLat,
lng: testLng,
gridSize: SubExtendedSquarePrecision,
},
want: testLocator[0:SubExtendedSquarePrecision],
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Locator(tt.args.lat, tt.args.lng, tt.args.gridSize)
if (err != nil) != tt.wantErr {
t.Errorf("Locator() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Locator() got = %v, want %v", got, tt.want)
}
})
}
}
42 changes: 42 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package maidenhead

const (
latSouthPole = 90.0
lngEastwardGreenwich = 180.0

lngFieldWidth = 20.0
lngSquareWidth = 2.0
lngSubSquareWidth = 1 / 12.0
lngExtendedSquareWidth = 1 / 120.0
lngSubExtendedSquareWidth = 1 / 2880.0

latFieldWidth = lngFieldWidth / 2
latSquareWidth = lngSquareWidth / 2
latSubSquareWidth = lngSubSquareWidth / 2
latExtendedSquareWidth = lngExtendedSquareWidth / 2
latSubExtendedSquareWidth = lngSubExtendedSquareWidth / 2

maxSteps = 5

FieldPrecision = 2
SquarePrecision = 4
SubSquarePrecision = 6
ExtendedSquarePrecision = 8
SubExtendedSquarePrecision = 10
)

var latDivider = [5]float64{latFieldWidth, latSquareWidth, latSubSquareWidth, latExtendedSquareWidth, latSubExtendedSquareWidth}

var lngDivider = [5]float64{lngFieldWidth, lngSquareWidth, lngSubSquareWidth, lngExtendedSquareWidth, lngSubExtendedSquareWidth}

var alphabet = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X"}

var alphaMap map[string]int

func init() {
alphaMap = make(map[string]int, len(alphaMap))
for i, v := range alphabet {
alphaMap[v] = i
}
}
Loading

0 comments on commit 0488aa0

Please sign in to comment.