forked from jda/srtm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.go
204 lines (190 loc) · 5.25 KB
/
tile.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
package srtm
import (
"encoding/binary"
"fmt"
"github.com/rs/zerolog/log"
"math"
"os"
"path"
"sort"
"strings"
"sync/atomic"
"time"
)
func tileKey(ll LatLng) string {
return fmt.Sprintf("%s%02d%s%03d",
func() string {
if ll.Latitude < 0 {
return "S"
}
return "N"
}(),
int(math.Abs(math.Floor(ll.Latitude))),
func() string {
if ll.Longitude < 0 {
return "W"
}
return "E"
}(),
int(math.Abs(math.Floor(ll.Longitude))),
)
}
var suffixes = []string{
"",
".hgt",
".gz",
}
func tilePath(tileDir string, ll LatLng) (string, os.FileInfo, error) {
key := tileKey(ll)
tilePath := path.Join(tileDir, key)
for _, s := range suffixes {
tilePath = tilePath + s
info, err := os.Stat(tilePath)
if err == nil || os.IsExist(err) {
return tilePath, info, nil
}
}
return download(tileDir, ll)
}
func (d *SRTM) loadTile(ll LatLng) (*Tile, error) {
key := tileKey(ll)
d.mtx.Lock()
defer d.mtx.Unlock()
if sort.SearchStrings(d.bads, key) < len(d.bads) {
return nil, fmt.Errorf("tile for key '%s' marked as bad", key)
}
t, ok := d.cache.Get(key)
if ok {
return t.(*Tile), nil
}
tPath, info, err := tilePath(d.tileDirectory, ll)
if err != nil {
d.bads = append(d.bads, key)
sort.Strings(d.bads)
return nil, err
}
if strings.HasSuffix(tPath, ".gz") {
sw, size, elevations, err := ReadFile(tPath)
if err != nil {
return nil, err
}
t = &Tile{
f: nil,
sw: sw,
size: size,
elevations: elevations,
}
if evicted := d.cache.Add(key, t); evicted {
log.Debug().Caller().Err(err).Msgf("add tile '%s' to cache with evict oldest", key)
}
log.Debug().Caller().Str("tile path", tPath).Msg("load tile to memory")
return t.(*Tile), nil
}
sw, size, err := Meta(tPath, info.Size())
if err != nil {
return nil, err
}
file, err := os.Open(tPath)
if err != nil {
return nil, err
}
t = &Tile{
f: file,
sw: sw,
size: size,
elevations: nil,
}
if evicted := d.cache.Add(key, t); evicted {
log.Debug().Caller().Err(err).Msgf("add tile '%s' to cache with evict oldest", key)
}
log.Debug().Caller().Str("tile path", tPath).Msg("lazy load tile")
return t.(*Tile), nil
}
// Tile struct contains hgt-tile meta-data and raw elevations slice
type Tile struct {
f *os.File
sw *LatLng
size int
elevations []int16
internalLRU int64
}
func (t *Tile) setLRU(lru time.Time) {
atomic.StoreInt64(&t.internalLRU, lru.UnixNano())
}
func (t *Tile) LRU() time.Time{
u := atomic.LoadInt64(&t.internalLRU)
return time.Unix(u/1e9, u%1e9)
}
// GetElevation returns elevation for lat/lng
func (t *Tile) GetElevation(ll LatLng) (float64, error) {
size := float64(t.size - 1)
row := (ll.Latitude - t.sw.Latitude) * size
col := (ll.Longitude - t.sw.Longitude) * size
if row < 0 || col < 0 || row > size || col > size {
return 0, fmt.Errorf("lat/lng is outside tile bounds (row=%f, col=%f, size=%f)", row, col, size)
}
return t.interpolate(row, col), nil
}
func avg(v1, v2, f float64) float64 {
return v1 + (v2-v1)*f
}
func (t *Tile) normalize(v, max int, description string) int {
if v < 0 {
log.Error().Caller().Msgf("normalize: error value %d of %s", v, description)
return 0
}
if v > max {
log.Error().Caller().Msgf("normalize: error value %d of %s", v, description)
return max
}
return v
}
func (t *Tile) elevation(idx int) (int16, error) {
b := make([]byte, 2)
n, err := t.f.ReadAt(b, int64(idx)*2)
if err != nil {
return 0, err
}
if n != 2 {
return 0, fmt.Errorf("error on read file %s at index %d", t.f.Name(), idx)
}
return int16(binary.BigEndian.Uint16(b)), nil
}
func (t *Tile) quadRowCol(row1, col1, row2, col2, row3, col3, row4, col4 int) (int16, int16, int16, int16) {
idx1 := (t.size-t.normalize(row1, (t.size-1), "row idx1")-1)*t.size + t.normalize(col1, t.size, "col idx1")
idx2 := (t.size-t.normalize(row2, (t.size-1), "row idx2")-1)*t.size + t.normalize(col2, t.size, "col idx2")
idx3 := (t.size-t.normalize(row3, (t.size-1), "row idx3")-1)*t.size + t.normalize(col3, t.size, "col idx3")
idx4 := (t.size-t.normalize(row4, (t.size-1), "row idx4")-1)*t.size + t.normalize(col4, t.size, "col idx4")
if t.elevations != nil {
return t.elevations[idx1], t.elevations[idx2], t.elevations[idx3], t.elevations[idx4]
}
e1, err := t.elevation(idx1)
if err != nil {
log.Error().Caller().Err(err).Int("row", row1).Int("col", col1).Int("idx", idx1).Msg("")
}
e2, err := t.elevation(idx2)
if err != nil {
log.Error().Caller().Err(err).Int("row", row2).Int("col", col2).Int("idx", idx2).Msg("")
}
e3, err := t.elevation(idx3)
if err != nil {
log.Error().Caller().Err(err).Int("row", row3).Int("col", col3).Int("idx", idx3).Msg("")
}
e4, err := t.elevation(idx4)
if err != nil {
log.Error().Caller().Err(err).Int("row", row4).Int("col", col4).Int("idx", idx4).Msg("")
}
return e1, e2, e3, e4
}
func (t *Tile) interpolate(row, col float64) float64 {
rowLow := int(math.Floor(row))
rowHi := rowLow + 1
rowFrac := row - float64(rowLow)
colLow := int(math.Floor(col))
colHi := colLow + 1
colFrac := col - float64(colLow)
v00, v10, v11, v01 := t.quadRowCol(rowLow, colLow, rowLow, colHi, rowHi, colHi, rowHi, colLow)
v1 := avg(float64(v00), float64(v10), colFrac)
v2 := avg(float64(v01), float64(v11), colFrac)
return avg(v1, v2, rowFrac)
}