forked from holizz/go-tile-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
77 lines (63 loc) · 1.37 KB
/
http.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
package tiles
import (
"image/png"
"log"
"net/http"
"strconv"
"strings"
)
type TileHandler struct {
prefix string
data *OsmData
}
// prefix should be of the form "/tiles" (without the trailing slash)
func NewTileHandler(prefix, pbfPath string) *TileHandler {
// Read PBF
log.Println("Parsing PBF file...")
osmData, err := ParsePbf(pbfPath)
if err != nil {
panic(err)
}
log.Println("Parsing PBF file... [DONE]")
return &TileHandler{
prefix: prefix,
data: osmData,
}
}
func (th *TileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[len(th.prefix):]
debug := false
debug_ := r.URL.Query()["debug"]
if len(debug_) == 1 && debug_[0] == "1" {
debug = true
}
if !(strings.HasPrefix(path, "/") && strings.HasSuffix(path, ".png")) {
w.Write([]byte("404"))
return
}
xyz := strings.Split(path[1:len(path)-4], "/")
if len(xyz) != 3 {
w.Write([]byte("404"))
return
}
xyz_ := []int64{}
for _, value := range xyz {
intVal, err := strconv.Atoi(value)
if err != nil {
w.Write([]byte("404"))
return
}
xyz_ = append(xyz_, int64(intVal))
}
zoom := xyz_[0]
x := xyz_[1]
y := xyz_[2]
nwPt := getLonLatFromTileName(x, y, zoom)
sePt := getLonLatFromTileName(x+1, y+1, zoom)
img, err := DrawTile(nwPt, sePt, zoom, th.data, debug)
if err != nil {
panic(err)
}
// Ignore broken pipe errors
png.Encode(w, img)
}