-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecords.go
152 lines (135 loc) · 3.26 KB
/
records.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
package osgb
import (
"bytes"
"encoding/csv"
"io"
"log"
"math"
"strconv"
"github.com/mjjbell/go-osgb/internal/data"
)
type record struct {
recordNo uint32
etrs89Easting uint32
etrs89Northing uint32
ostnEastShift float64
ostnNorthShift float64
ostnGeoidHeight float64
geoidRegion geoidRegion
}
func readRecords(translationVectorFile string) ([]record, error) {
res := []record{}
data, err := data.Asset(translationVectorFile)
if err != nil {
return nil, err
}
r := csv.NewReader(bytes.NewReader(data))
// Read header
if _, err := r.Read(); err != nil {
return nil, err
}
for {
rec, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
recordNo, err := strconv.ParseUint(rec[0], 10, 32)
if err != nil {
return nil, err
}
etrs89Easting, err := strconv.ParseUint(rec[1], 10, 32)
if err != nil {
return nil, err
}
etrs89Northing, err := strconv.ParseUint(rec[2], 10, 32)
if err != nil {
return nil, err
}
ostnEastShift, err := strconv.ParseFloat(rec[3], 64)
if err != nil {
return nil, err
}
ostnNorthShift, err := strconv.ParseFloat(rec[4], 64)
if err != nil {
return nil, err
}
ostnGeoidHeight, err := strconv.ParseFloat(rec[5], 64)
if err != nil {
return nil, err
}
geoidDatum, err := strconv.ParseUint(rec[6], 10, 8)
if err != nil {
return nil, err
}
res = append(res,
record{
recordNo: uint32(recordNo),
etrs89Easting: uint32(etrs89Easting),
etrs89Northing: uint32(etrs89Northing),
ostnEastShift: ostnEastShift,
ostnNorthShift: ostnNorthShift,
ostnGeoidHeight: ostnGeoidHeight,
geoidRegion: geoidDatumToRegion(geoidDatum),
})
}
return res, nil
}
func (tr *transformer) getShiftRecord(eastIndex, northIndex uint32) (*record, error) {
recordIndex := eastIndex + northIndex*nEastIndices
if recordIndex <= 0 || recordIndex >= uint32(len(tr.records)) {
return nil, ErrPointOutsidePolygon
}
rec := &tr.records[recordIndex]
if rec.geoidRegion == Region_OUTSIDE_BOUNDARY {
return nil, ErrPointOutsidePolygon
}
if rec.geoidRegion == Region_OUTSIDE_TRANSFORMATION {
return nil, ErrPointOutsideTransformation
}
return rec, nil
}
type shiftRecords struct {
s2, s3, s0, s1 *record
}
func (tr *transformer) getShiftRecords(etrs89Coord *planeCoord) (*shiftRecords, error) {
eastIndex := eastingIndex(etrs89Coord.easting)
northIndex := northingIndex(etrs89Coord.northing)
bl, err := tr.getShiftRecord(eastIndex, northIndex)
if err != nil {
return nil, err
}
br, err := tr.getShiftRecord(eastIndex+1, northIndex)
if err != nil {
return nil, err
}
rt, err := tr.getShiftRecord(eastIndex+1, northIndex+1)
if err != nil {
return nil, err
}
tl, err := tr.getShiftRecord(eastIndex, northIndex+1)
if err != nil {
return nil, err
}
return &shiftRecords{
s0: bl,
s1: br,
s2: rt,
s3: tl,
}, nil
}
func eastingIndex(easting float64) uint32 {
return uint32(math.Floor(easting / 1000.0))
}
func northingIndex(northing float64) uint32 {
return uint32(math.Floor(northing / 1000.0))
}
func geoidDatumToRegion(id uint64) geoidRegion {
if id < 0 || id > 16 {
// This is unexpected and we dont't know how to recover from this.
log.Fatalf("unexpected geoid datum ID %d", id)
}
return geoidRegion(id)
}