-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtile_circles.go
More file actions
executable file
·255 lines (215 loc) · 7.13 KB
/
tile_circles.go
File metadata and controls
executable file
·255 lines (215 loc) · 7.13 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"fmt"
"github.com/fogleman/gg"
"github.com/gorilla/mux"
"image"
"image/png"
"io"
"log"
"math"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
"ttnmapper-tms/types"
)
func GetCirclesTile(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
networkId := vars["network_id"]
gatewayId, singleGateway := vars["gateway_id"]
// We've chosen to use mux.NewRouter().UseEncodedPath() which will return the path variables in encoded form.
// This is necessary to correctly pass NS_TTS:// (two forward slashes).
// Decode now.
networkId, _ = url.QueryUnescape(networkId)
gatewayId, _ = url.QueryUnescape(gatewayId)
z, err := strconv.Atoi(vars["z"])
if err != nil {
log.Println(w, "Z invalid")
http.Error(w, "z invalid", http.StatusBadRequest)
return
}
x, err := strconv.Atoi(vars["x"])
if err != nil {
log.Println(w, "X invalid")
http.Error(w, "x invalid", http.StatusBadRequest)
return
}
if strings.HasSuffix(vars["y"], ".png") {
vars["y"] = strings.TrimSuffix(vars["y"], ".png")
}
y, err := strconv.Atoi(vars["y"])
if err != nil {
log.Println(w, "Y invalid")
http.Error(w, "y invalid", http.StatusBadRequest)
return
}
log.Printf("Circles tile %s - %s: %d/%d/%d\t", networkId, gatewayId, z, x, y)
tileFileName := ""
if singleGateway {
tileFileName = fmt.Sprintf("%s/gateway/%s/%s/%d/%d/%d.png", myConfiguration.CacheDirCircles, url.QueryEscape(networkId), url.QueryEscape(gatewayId), z, x, y)
} else {
tileFileName = fmt.Sprintf("%s/network/%s/%d/%d/%d.png", myConfiguration.CacheDirCircles, url.QueryEscape(networkId), z, x, y)
}
tileInCacheOutdated := true
tileExistInCache := false
if file, err := os.Stat(tileFileName); err == nil {
tileExistInCache = true
modifiedTime := file.ModTime()
// Check the last modified time of the file to see if the time is still new enough
if modifiedTime.Add(GetCacheDurationForZoom(z) * 2).After(time.Now()) {
tileInCacheOutdated = false
}
}
//log.Println("Cache enabled: ", myConfiguration.CacheEnabled)
//log.Println("Tile in cache: ", tileExistInCache)
//log.Println("Tile outdated: ", tileInCacheOutdated)
//log.Println("Single gateway: ", singleGateway)
// Only cache global tiles, not per gateway tiles
if myConfiguration.CacheEnabled && tileExistInCache && !tileInCacheOutdated && !singleGateway {
log.Printf("serving from cache\n")
//promTmsCirclesCacheCount.Inc()
//Check if file exists and open
tileFile, err := os.Open(tileFileName)
if err != nil {
//File not found, send 404
http.Error(w, "File not found.", 404)
return
}
_, err = io.Copy(w, tileFile) //'Copy' the file to the client
if err != nil {
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tileFile.Close() //Close after function returns
if err != nil {
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
log.Printf("generating tile\n")
//promTmsCirclesCreateCount.Inc()
//tileStart := time.Now()
// Circles can overlap tiles. Generate a list of z19 tiles in the current tile, with a buffer around this tile.
// Z - buffer for one z19 tile
// 19 - 1
// 18 - 0.5
// 17 - 0.25
// 16 - 0.125
// 15 - 0.0625
// 14 - 0.03125
// min circle radius is 6*1.6=9.6
// 9.6 / 256 = 0.0375 = min buffer
buffer := 1 / math.Pow(2, float64(19-z))
if buffer < 0.0375 {
buffer = 0.0375
}
xMin, yMin, xMax, yMax := GetZ19TileRangeBuffer(x, y, z, buffer)
//log.Println("Selecting data")
var samples []types.Sample
if singleGateway {
samples, err = GetGatewaySamplesInRange(networkId, gatewayId, xMin, yMin, xMax, yMax)
} else {
samples, err = GetNetworkSamplesInRange(networkId, xMin, yMin, xMax, yMax)
}
// Database error
if err != nil {
http.Error(w, "database error", http.StatusInternalServerError)
return
}
// Sort by RSSI ascending
sort.Sort(types.ByRssi(samples))
// Do something with tile data
tile := CreateCirclesTile(x, y, z, samples)
if myConfiguration.CacheEnabled && !singleGateway {
StoreTileInFile(tile, tileFileName)
}
// Set cache headers in the response
w.Header().Set("Cache-Control", "public, max-age=86400")
w.Header().Set("Expires", time.Now().Add(24*time.Hour).Format(http.TimeFormat))
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
err = png.Encode(w, tile)
if err != nil {
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Prometheus stats
//gatewayElapsed := time.Since(tileStart)
//promTmsCirclesDuration.Observe(float64(gatewayElapsed.Nanoseconds()) / 1000.0 / 1000.0) //nanoseconds to milliseconds
}
}
func CreateCirclesTile(x int, y int, z int, samples []types.Sample) image.Image {
// x, y, z is for outer tile
// Draw image for x-1, y-1 to x+2, y+2
// Z19 indexes are:
xMin, yMin, _, _ := GetZ19TileRangeBuffer(x-1, y-1, z, 0)
_, _, xMax, yMax := GetZ19TileRangeBuffer(x+1, y+1, z, 0)
xWidth := float64(xMax - xMin)
yWidth := float64(yMax - yMin)
// For a z19 tile we need to draw a circle that fills the whole tile. That is a circle with radius half the diagonal of the tile.
nominalRadius := 181.0 //math.Sqrt(256^2*256^2) / 2
// For every zoom level higher than 19 we can half the radius
zDiff := float64(19 - z)
nominalRadius = nominalRadius / (math.Pow(2, zDiff))
//log.Println("Equivalent radius ", nominalRadius)
nominalRadius = math.Max(nominalRadius, 6.0) // minimum is 3 pixels radius
dc := gg.NewContext(768, 768)
for _, sample := range samples {
// Add 0.5 because the tile index is the NW corner, but we want to draw it in the middle of the z19 tile
pixelY := ((float64(sample.Y-yMin) + 0.5) / yWidth) * 768.0 // pixels from top
pixelX := ((float64(sample.X-xMin) + 0.5) / xWidth) * 768.0 // pixels from left
signal := sample.MaxBucketIndex
if signal == 12 {
// Out of range, do not draw
dc.DrawCircle(pixelX, pixelY, nominalRadius*1.6)
dc.SetRGB(0, 0, 0)
} else if signal >= 5 { //11..5
dc.DrawCircle(pixelX, pixelY, nominalRadius*1.5)
dc.SetRGB(0, 0, 1)
} else if signal == 4 {
dc.DrawCircle(pixelX, pixelY, nominalRadius*1.4)
dc.SetRGB(0, 1, 1)
} else if signal == 3 {
dc.DrawCircle(pixelX, pixelY, nominalRadius*1.3)
dc.SetRGB(0, 1, 0)
} else if signal == 2 {
dc.DrawCircle(pixelX, pixelY, nominalRadius*1.2)
dc.SetRGB(1, 1, 0)
} else if signal == 1 {
dc.DrawCircle(pixelX, pixelY, nominalRadius*1.1)
dc.SetRGB(1, 0.5, 0)
} else {
dc.DrawCircle(pixelX, pixelY, nominalRadius)
dc.SetRGB(1, 0, 0)
}
dc.Fill()
}
srcImage := dc.Image()
// Crop out tile
tile := srcImage.(interface {
SubImage(r image.Rectangle) image.Image
}).SubImage(image.Rect(256, 256, 2*256, 2*256))
return tile
}
func StoreTileInFile(tile image.Image, filename string) {
tileFolderName := filename[:strings.LastIndex(filename, "/")]
CreateDirIfNotExist(tileFolderName)
newImage, err := os.Create(filename)
if err != nil {
log.Println(err.Error())
}
err = png.Encode(newImage, tile)
if err != nil {
log.Println(err.Error())
}
err = newImage.Close()
if err != nil {
log.Println(err.Error())
}
}