-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
235 lines (193 loc) · 5.07 KB
/
Copy pathmain.go
File metadata and controls
235 lines (193 loc) · 5.07 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
package main
import (
"fmt"
"io"
"log"
"math"
"os"
"path"
"regexp"
"strconv"
"io/ioutil"
"net/http"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
)
var LINZ_API_KEY = getEnv("LINZ_API_KEY", "")
var BASE_PATH = getEnv("LINZ_BASE_PATH", "/mapcache")
var NZ_FILE = getEnv("LINZ_BOUND_FILE", "nz.geojson")
var b, _ = ioutil.ReadFile(NZ_FILE)
var nzBounds, _ = geojson.UnmarshalFeatureCollection(b)
var req, hit, miss int
func tileHandler(w http.ResponseWriter, r *http.Request) {
req++
// extract x, y and z
re := regexp.MustCompile(`(aerial|topo)/(\d+)/(\d+)/(\d+)\.png$`)
submatch := re.FindStringSubmatch(r.URL.Path)
if !(len(submatch) == 5) {
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, "MISSINGPARAM", r.RequestURI)
http.NotFound(w, r)
return
}
z, zerr := strconv.Atoi(submatch[2])
x, xerr := strconv.Atoi(submatch[3])
y, yerr := strconv.Atoi(submatch[4])
layer := submatch[1]
layerid := "set=2"
if layer == "topo" {
layerid = "layer=2343"
}
if xerr != nil || yerr != nil || zerr != nil {
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, "BADNUMBER", r.RequestURI)
http.NotFound(w, r)
return
}
if !isXYZInsidePolygon(x, y, z) {
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, "OUTOFBOUND", r.RequestURI)
http.NotFound(w, r)
return
}
// build the file path
imgbase := path.Join(BASE_PATH, layer, i2s(z), i2s(x))
imgpath := path.Join(imgbase, fmt.Sprintf("%d.png", y))
// if the tile exists, serve it
if FileExists(imgpath) {
// serve file
hit++
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, "OK/HIT", r.RequestURI)
w.Header().Set("Content-Type", "image/png")
http.ServeFile(w, r, imgpath)
return
}
// have we logged a miss before?
if FileExists(imgpath + ".404") {
hit++
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, "MISSING/HIT", r.RequestURI)
http.NotFound(w, r)
return
}
os.MkdirAll(imgbase, os.ModePerm)
tileurl := fmt.Sprintf("https://tiles-a.data-cdn.linz.govt.nz/services;key=%s/tiles/v4/%s/EPSG:3857/%d/%d/%d.png",
LINZ_API_KEY, layerid, z, x, y)
// download and serve the tile
if DownloadTile(tileurl, imgpath) {
miss++
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, "OK/MISS", r.RequestURI)
w.Header().Set("Content-Type", "image/png")
http.ServeFile(w, r, imgpath)
return
}
log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, "UNKNOWN", r.RequestURI)
http.NotFound(w, r)
return
}
func DownloadTile(url, path string) bool {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalln(err)
return false
}
req.Header.Set("User-Agent", "LINZ MapCache (for OpenStreetMap); cameron@dewitte.id.au")
resp, err := client.Do(req)
if err != nil {
emptyFile, err := os.Create(path + ".404")
if err != nil {
log.Fatalln(err)
} else {
emptyFile.Close()
}
return false
}
if resp.StatusCode != http.StatusOK {
return false
}
defer resp.Body.Close()
out, err := os.Create(path)
if err != nil {
log.Fatalln(err)
return false
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return true
}
func FileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else if os.IsNotExist(err) {
return false
}
return false
}
func isXYZInsidePolygon(x, y, z int) bool {
corners := [4]orb.Point{
XYZ2LL(x, y, z),
XYZ2LL(x+1, y, z),
XYZ2LL(x, y+1, z),
XYZ2LL(x+1, y+1, z),
}
for c := 0; c < 4; c++ {
if isPointInsidePolygon(corners[c]) {
return true
}
}
return false
}
func isPointInsidePolygon(point orb.Point) bool {
for _, feature := range nzBounds.Features {
// Try on a MultiPolygon to begin
multiPoly, isMulti := feature.Geometry.(orb.MultiPolygon)
if isMulti {
if planar.MultiPolygonContains(multiPoly, point) {
return true
}
} else {
// Fallback to Polygon
polygon, isPoly := feature.Geometry.(orb.Polygon)
if isPoly {
if planar.PolygonContains(polygon, point) {
return true
}
}
}
}
return false
}
func XYZ2LL(x, y, z int) orb.Point {
xf := float64(x)
yf := float64(y)
zf := float64(z)
long := xf/math.Exp2(zf)*360.0 - 180.0
n := math.Pi - 2*math.Pi*yf/math.Exp2(zf)
lat := 180 / math.Pi * math.Atan(0.5*(math.Exp(n)-math.Exp(-n)))
return orb.Point{long, lat}
}
func i2s(i int) string {
return strconv.Itoa(i)
}
func statsHandler(w http.ResponseWriter, r *http.Request) {
hostname, err := os.Hostname()
if err != nil {
fmt.Fprintf(w, "{\"hostname\":\"unknown\", \"requests\": %d, \"hit\":%d, \"miss\": %d}", req, hit, miss)
} else {
fmt.Fprintf(w, "{\"hostname\":\"%s\", \"requests\": %d, \"hit\":%d, \"miss\": %d}", hostname, req, hit, miss)
}
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func main() {
if LINZ_API_KEY == "" {
log.Fatal("Provide an API key through the LINZ_API_KEY environment variable")
return
}
http.HandleFunc("/linz_aerial/", tileHandler)
http.HandleFunc("/linz_topo/", tileHandler)
http.HandleFunc("/linz_stats/", statsHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}