Skip to content

Commit

Permalink
sk42, coords
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Sep 6, 2024
1 parent 03154b9 commit 3b6f21a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
48 changes: 47 additions & 1 deletion pkg/coord/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
)

var (
r1 = regexp.MustCompile(`[xX](?P<x>\d{5,}),?\s+[yY](?P<y>\d{5,})`)
r1 = regexp.MustCompile(`[xX]=?(?P<x>\d{5,}),?\s+[yY]=?(?P<y>\d{5,})`)
r2 = regexp.MustCompile(`(?P<x>-?\d+\.\d+),?\s+(?P<y>-?\d+\.\d+)`)
r3 = regexp.MustCompile(`(?P<x>\d+\.\d+)([nNsS]),?\s+(?P<y>\d+\.\d+)([eEwW])`)
)

func StringToLatLon(s string) (float64, float64, error) {
Expand All @@ -33,5 +35,49 @@ func StringToLatLon(s string) (float64, float64, error) {
return lat, lon, nil
}

if r2.MatchString(s) {
res := r2.FindStringSubmatch(s)

lat, err := strconv.ParseFloat(res[1], 64)

if err != nil {
return 0, 0, err
}

lon, err := strconv.ParseFloat(res[2], 64)

if err != nil {
return 0, 0, err
}

return lat, lon, nil
}

if r3.MatchString(s) {
res := r3.FindStringSubmatch(s)

lat, err := strconv.ParseFloat(res[1], 64)

if err != nil {
return 0, 0, err
}

if res[2] == "S" || res[2] == "s" {
lat = -lat
}

lon, err := strconv.ParseFloat(res[3], 64)

if err != nil {
return 0, 0, err
}

if res[4] == "W" || res[4] == "w" {
lon = -lon
}

return lat, lon, nil
}

return 0, 0, nil
}
24 changes: 19 additions & 5 deletions pkg/coord/strings_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package coord

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSK42(t *testing.T) {
lat, lon, err := StringToLatLon("x5709130 y6648746")
type testData struct {
s string
x, y float64
}

assert.NoError(t, err)
func TestSK42(t *testing.T) {
data := []testData{
{"x5709130 y6648746", 51.49220977324127, 35.14007432073565},
{"X=5709130, y6648746", 51.49220977324127, 35.14007432073565},
{"51.49 35.14", 51.49, 35.14},
{"51.49, -35.14", 51.49, -35.14},
{"51.49N 35.14E", 51.49, 35.14},
{"51.49N, 35.14w", 51.49, -35.14},
}

fmt.Println(lat, lon)
for _, d := range data {
lat, lon, err := StringToLatLon(d.s)
assert.NoError(t, err)
assert.Equal(t, d.x, lat)
assert.Equal(t, d.y, lon)
}
}

0 comments on commit 3b6f21a

Please sign in to comment.