File: position.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (77 lines) | stat: -rw-r--r-- 1,720 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package forms

import (
	"fmt"
	"math"

	"github.com/la5nta/pat/internal/gpsd"
	"github.com/pd0mz/go-maidenhead"
)

type gpsStyle int

const (
	// documentation: https://www.winlink.org/sites/default/files/RMSE_FORMS/insertion_tags.zip
	signedDecimal gpsStyle = iota // 41.1234 -73.4567
	decimal                       // 46.3795N 121.5835W
	degreeMinute                  // 46-22.77N 121-35.01W
	gridSquare                    // JO29PJ
)

func positionFmt(style gpsStyle, pos gpsd.Position) string {
	const notAvailable = "(Not available)"

	var (
		northing   string
		easting    string
		latDegrees int
		latMinutes float64
		lonDegrees int
		lonMinutes float64
	)

	noPos := gpsd.Position{}
	if pos == noPos {
		return notAvailable
	}
	switch style {
	case gridSquare:
		str, err := maidenhead.NewPoint(pos.Lat, pos.Lon).GridSquare()
		if err != nil {
			return notAvailable
		}
		return str
	case degreeMinute:
		{
			latDegrees = int(math.Trunc(math.Abs(pos.Lat)))
			latMinutes = (math.Abs(pos.Lat) - float64(latDegrees)) * 60
			lonDegrees = int(math.Trunc(math.Abs(pos.Lon)))
			lonMinutes = (math.Abs(pos.Lon) - float64(lonDegrees)) * 60
		}
		fallthrough
	case decimal:
		{
			if pos.Lat >= 0 {
				northing = "N"
			} else {
				northing = "S"
			}
			if pos.Lon >= 0 {
				easting = "E"
			} else {
				easting = "W"
			}
		}
	}

	switch style {
	case signedDecimal:
		return fmt.Sprintf("%.4f %.4f", pos.Lat, pos.Lon)
	case decimal:
		return fmt.Sprintf("%.4f%s %.4f%s", math.Abs(pos.Lat), northing, math.Abs(pos.Lon), easting)
	case degreeMinute:
		return fmt.Sprintf("%02d-%05.2f%s %03d-%05.2f%s", latDegrees, latMinutes, northing, lonDegrees, lonMinutes, easting)
	default:
		panic("invalid style")
	}
}