-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.go
156 lines (114 loc) · 3.02 KB
/
svg.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
package api
import (
"fmt"
"log/slog"
"net/http"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-sanitize"
"github.com/whosonfirst/go-whosonfirst-spelunker"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
"github.com/whosonfirst/go-whosonfirst-svg"
)
type SVGSize struct {
Label string
MaxHeight int
MaxWidth int
}
type SVGHandlerOptions struct {
Sizes map[string]SVGSize
Spelunker spelunker.Spelunker
}
func DefaultSVGSizes() map[string]SVGSize {
sm := SVGSize{
Label: "sm",
MaxHeight: 300,
MaxWidth: 300,
}
med := SVGSize{
Label: "med",
MaxHeight: 640,
MaxWidth: 640,
}
lg := SVGSize{
Label: "lg",
MaxHeight: 1024,
MaxWidth: 1024,
}
sz := map[string]SVGSize{
"sm": sm,
"med": med,
"lg": lg,
}
return sz
}
func SVGHandler(opts *SVGHandlerOptions) (http.Handler, error) {
logger := slog.Default()
fn := func(rsp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logger = logger.With("request", req.URL)
logger = logger.With("address", req.RemoteAddr)
req_uri, err, status := httpd.ParseURIFromRequest(req, nil)
if err != nil {
logger.Error("Failed to parse URI from request", "error", err)
http.Error(rsp, err.Error(), status)
return
}
logger = logger.With("wofid", req_uri.Id)
f, err := httpd.FeatureFromRequestURI(ctx, opts.Spelunker, req_uri)
if err != nil {
slog.Error("Failed to get by ID", "id", req_uri.Id, "error", err)
http.Error(rsp, spelunker.ErrNotFound.Error(), http.StatusNotFound)
return
}
sn_opts := sanitize.DefaultOptions()
sz := "lg"
query := req.URL.Query()
query_sz := query.Get("size")
req_sz, err := sanitize.SanitizeString(query_sz, sn_opts)
if err != nil {
http.Error(rsp, err.Error(), status)
return
}
if req_sz != "" {
sz = req_sz
}
sz_info, ok := opts.Sizes[sz]
if !ok {
http.Error(rsp, "Invalid output size", http.StatusBadRequest)
return
}
rsp.Header().Set("Content-Type", "application/json")
rsp.Header().Set("Content-Type", "image/svg+xml")
opts := svg.NewDefaultOptions()
opts.Height = float64(sz_info.MaxHeight)
opts.Width = float64(sz_info.MaxWidth)
opts.Writer = rsp
// to do: support for custom styles:
// https://github.com/whosonfirst/go-whosonfirst-browser/v6issues/19
opts.StyleFunction = func(f []byte) (map[string]string, error) {
attrs := make(map[string]string)
type_rsp := gjson.GetBytes(f, "geometry.type")
if !type_rsp.Exists() {
return nil, fmt.Errorf("Missing geometry.type")
}
geom_type := type_rsp.String()
// log.Println(geom_type)
switch geom_type {
case "LineString":
attrs["fill-opacity"] = "0.0"
attrs["stroke-width"] = "1.0"
attrs["stroke-opacity"] = "2.0"
attrs["stroke"] = "#000"
case "Point", "MultiPoint":
// something something something
// https://github.com/whosonfirst/go-whosonfirst-browser/v6issues/18
default:
// pass
}
return attrs, nil
}
svg.FeatureToSVG(f, opts)
}
h := http.HandlerFunc(fn)
return h, nil
}