-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
242 lines (217 loc) · 6.96 KB
/
parse.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package gpsnmea
import (
"fmt"
"strconv"
"strings"
//"log"
"math"
)
type Gprmc struct {
Raw string
Fields []string
TimeStamp string
Validity bool
Lat float64
LatCord string
Long float64
LongCord string
Speed float64
TrueCourse float64
DateStamp string
MagneticVar float64
MagneticVarCord string
Checksum int64
}
type Gpgga struct {
Raw string
Fileds []string
TimeStamp string
Lat float64
LatCord string
Long float64
LongCord string
FixQuality int
NumberSat int
HDop float64
Altitude float64
AltCord string
Geoidal float64
GeoidalUnit string
Dgpsupdate float64
DrefStationId float64
Checksum int64
}
func ParseRMC(s string) *Gprmc {
fields := strings.Split(s, ",")
//log.Printf("fields: %v,\nlen: %d", fields, len(fields))
if len(fields) < 12 {
return nil
}
timeStamp := fields[1]
validity := false
if fields[2] == "A" {
validity = true
}
lat, _ := strconv.ParseFloat(fields[3], 64)
long, _ := strconv.ParseFloat(fields[5], 64)
speed, _ := strconv.ParseFloat(fields[7], 64)
trueCourse, _ := strconv.ParseFloat(fields[8], 64)
dateStamp := fields[9]
magneticVar, _ := strconv.ParseFloat(fields[10], 64)
fields2 := strings.Split(fields[12], "*")
if len(fields2) < 2 {
return nil
}
magneticVarCord := fields2[0]
checksum, _ := strconv.ParseInt(fields2[1], 16, 32)
return &Gprmc{
s,
fields,
timeStamp,
validity,
lat,
fields[4],
long,
fields[6],
speed,
trueCourse,
dateStamp,
magneticVar,
magneticVarCord,
checksum,
}
}
func ParseGGA(s string) *Gpgga {
fields := strings.Split(s, ",")
if len(fields) < 14 {
return nil
}
timeStamp := fields[1]
lat, _ := strconv.ParseFloat(fields[2], 64)
long, _ := strconv.ParseFloat(fields[4], 64)
fixQ, _ := strconv.Atoi(fields[6])
numberSat, _ := strconv.Atoi(fields[7])
dop, _ := strconv.ParseFloat(fields[8], 64)
alt, _ := strconv.ParseFloat(fields[9], 64)
geoid, _ := strconv.ParseFloat(fields[11], 64)
dgps, _ := strconv.ParseFloat(fields[13], 64)
fields2 := strings.Split(fields[14], "*")
if len(fields2) < 2 {
return nil
}
drefStation, _ := strconv.ParseFloat(fields2[0], 64)
checksum, _ := strconv.ParseInt(fields2[1], 16, 32)
return &Gpgga{
s,
fields,
timeStamp,
lat,
fields[3],
long,
fields[5],
fixQ,
numberSat,
dop,
alt,
fields[10],
geoid,
fields[12],
dgps,
drefStation,
checksum,
}
}
func LatLongToDecimalDegree(num float64, cord string) float64 {
dec, fra := math.Modf(num / 100)
if strings.Contains(cord, "W") || strings.Contains(cord, "S") {
return (-1) * (dec + fra*100/60)
}
return dec + fra*100/60
}
func DecimalDegreeToLat(lat float64) string {
latDirection := "N"
if lat <= 0 {
latDirection = "S"
lat = -lat
}
latitude := uint8(lat)
latitudeMinutes := uint8((lat - float64(latitude)) * 60)
latitudeSeconds := (lat - float64(latitude) - float64(latitudeMinutes)/60) * 3600
latitudeSecondsDecimal := latitudeSeconds * 100 / 60
if latitudeSecondsDecimal < 1 {
latitudeSecondsDecimal = latitudeSecondsDecimal * (10000)
} else if latitudeSecondsDecimal < 10 {
latitudeSecondsDecimal = latitudeSecondsDecimal * (100000)
} else {
latitudeSecondsDecimal = latitudeSecondsDecimal * (1000000)
}
return fmt.Sprintf("%02d%02d.%6d,%v", latitude, latitudeMinutes, int(latitudeSecondsDecimal), latDirection)
}
func DecimalDegreeToLon(lon float64) string {
lonDirection := "E"
if lon <= 0 {
lonDirection = "W"
lon = -lon
}
longitude := uint8(lon)
longitudeMinutes := uint8((lon - float64(longitude)) * 60)
longitudeSeconds := (lon - float64(longitude) - float64(longitudeMinutes)/60) * 3600
longitudeSecondsDecimal := longitudeSeconds * 100 / 60
if longitudeSecondsDecimal < 1 {
longitudeSecondsDecimal = longitudeSecondsDecimal * (10000)
} else if longitudeSecondsDecimal < 10 {
longitudeSecondsDecimal = longitudeSecondsDecimal * (100000)
} else {
longitudeSecondsDecimal = longitudeSecondsDecimal * (1000000)
}
return fmt.Sprintf("%03d%02d.%06d,%v", longitude, longitudeMinutes, int(longitudeSecondsDecimal), lonDirection)
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::: :::
//::: This routine calculates the distance between two points (given the :::
//::: latitude/longitude of those points). It is being used to calculate :::
//::: the distance between two locations using GeoDataSource (TM) prodducts :::
//::: :::
//::: Definitions: :::
//::: South latitudes are negative, east longitudes are positive :::
//::: :::
//::: Passed to function: :::
//::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
//::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
//::: unit = the unit you desire for results :::
//::: where: 'M' is statute miles (default) :::
//::: 'K' is kilometers :::
//::: 'N' is nautical miles :::
//::: :::
//::: Worldwide cities and other features databases with latitude longitude :::
//::: are available at https://www.geodatasource.com :::
//::: :::
//::: For enquiries, please contact sales@geodatasource.com :::
//::: :::
//::: Official Web site: https://www.geodatasource.com :::
//::: :::
//::: GeoDataSource.com (C) All Rights Reserved 2018 :::
//::: :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
func Distance(lat1 float64, lng1 float64, lat2 float64, lng2 float64, unit ...string) float64 {
const PI float64 = 3.141592653589793
radlat1 := float64(PI * lat1 / 180)
radlat2 := float64(PI * lat2 / 180)
theta := float64(lng1 - lng2)
radtheta := float64(PI * theta / 180)
dist := math.Sin(radlat1)*math.Sin(radlat2) + math.Cos(radlat1)*math.Cos(radlat2)*math.Cos(radtheta)
if dist > 1 {
dist = 1
}
dist = math.Acos(dist)
dist = dist * 180 / PI
dist = dist * 60 * 1.1515
if len(unit) > 0 {
if unit[0] == "K" {
dist = dist * 1.609344
} else if unit[0] == "N" {
dist = dist * 0.8684
}
}
return dist
}