forked from GeoNet/fits
/
maps.go
315 lines (273 loc) · 10.5 KB
/
maps.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"github.com/GeoNet/map180"
"github.com/GeoNet/web"
"github.com/GeoNet/web/api/apidoc"
"html/template"
"net/http"
"strconv"
"strings"
)
var mapDoc = apidoc.Endpoint{Title: "Maps",
Description: `Simple maps of sites.`,
Queries: []*apidoc.Query{
siteMapD,
siteTypeMapD,
},
}
var siteMapD = &apidoc.Query{
Accept: "",
Title: "Site Maps",
Description: "Maps of specific sites",
Discussion: `<p>A minimal query specifies a single site by <code>networkID</code> and <code>siteID</code>. The map bounds
are calculated to suit the selected site and keep New Zealand in the map. The site is marked with a red triangle with the
site at the center. Width defaults to 130 and the height is calculated from the map bounds and width. If the map is included in
a page using an object tag (and viewed using a recent web browser) then when the site marker is moused over a label for
the site is briefly displayed. If the image is included in an img tag the mouse over functionality is not available.</p>
<p>
<object data="/map/site?networkID=LI&siteID=GISB" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?networkID=LI&siteID=GISB" type="image/svg+xml"></object></code><br/><br/>
</p>
<p>
Multiple sites can be specified with the <code>sites</code> query parameter. The map bounds are calculated from
the sites and maps wrap the 180 meridian.<br /><br />
<object data="/map/site?sites=LI.GISB,LI.CHTI,CG.RAUL" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?sites=LI.GISB,LI.CHTI,CG.RAUL" type="image/svg+xml"></object></code><br/><br/>
</p>
<p>
<object data="/map/site?sites=LI.GISB,LI.CHTI,CG.RAUL,GN.FALE" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?sites=LI.GISB,LI.CHTI,GN.FALE" type="image/svg+xml"></object></code><br/><br/>
</p>
<p>
<object data="/map/site?sites=LI.GISB,LI.CHTI,CG.RAUL,GN.FALE,LI.SCTB" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?sites=LI.GISB,LI.CHTI,GN.FALE,LI.SCTB" type="image/svg+xml"></object></code><br/><br/>
</p>
<p>
The size of the map can be changed with the <code>width</code> parameter. The map bounds can be controlled with the
<code>bbox</code> query parameter either by specifiying the lower left and upper right corners or by using one of the named
map bounds. Zoomed in maps have higher resolution map data. When width allows the full site name is included in the label.<br />
<object data="/map/site?sites=CG.RAUL&width=500&bbox=RaoulIsland" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?sites=CG.RAUL&width=500&bbox=RaoulIsland" type="image/svg+xml"></object></code>
</p>
<p><br/>
Map data are assembled from a number of sources:
<ul>
<li>1:10m - <a href="http://www.naturalearthdata.com/">Natural Earth</a></li>
<li>1:50m - <a href="http://www.naturalearthdata.com/">Natural Earth</a></li>
<li>NZTopo 1:500k - <a href="https://data.linz.govt.nz/">LINZ Data Service</a></li>
<li>NZTopo 1:250k - <a href="https://data.linz.govt.nz/">LINZ Data Service</a></li>
<li>NZTopo 1:50k - <a href="https://data.linz.govt.nz/">LINZ Data Service</a></li>
</ul>
NZTopo data is licensed by LINZ for re-use under the <a href="https://creativecommons.org/licenses/by/3.0/nz/">
Creative Commons Attribution 3.0 New Zealand licence</a>.
</p>
`,
URI: `/map/site?(networkID=(string)&siteID=(string)|&sites=(networkID.siteID,...))[&bbox=(float,float,float,float)|string][&width=(int)]`,
Optional: map[string]template.HTML{
"networkID": networkIDDoc + ` Specify <code>networkID</code> and <code>siteID</code> or <code>sites</code>`,
"siteID": siteIDDoc,
"sites": `A comma separated list of sites specified by the <code>networkID</code>
and <code>siteID</code> joined with a <code>.</code> e.g., <code>LI.GISB,LI.TAUP</code>.`,
"width": widthDoc,
"bbox": bboxDoc,
"insetBbox": ` If specified then is used to draw a small inset map in the upper left corner. Useful for
giving context to zoomed in regions. Same specification options as <code>bbox</code>.`,
},
}
type st struct {
networkID, siteID string
}
func siteMap(w http.ResponseWriter, r *http.Request) {
if err := siteMapD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
v := r.URL.Query()
bbox := v.Get("bbox")
var insetBbox string
if v.Get("insetBbox") != "" {
insetBbox = v.Get("insetBbox")
err := map180.ValidBbox(insetBbox)
if err != nil {
web.BadRequest(w, r, err.Error())
return
}
}
if v.Get("sites") == "" && (v.Get("siteID") == "" && v.Get("networkID") == "") {
web.BadRequest(w, r, "please specify sites or networkID and siteID")
return
}
if v.Get("sites") != "" && (v.Get("siteID") != "" || v.Get("networkID") != "") {
web.BadRequest(w, r, "please specify either sites or networkID and siteID")
return
}
if v.Get("sites") == "" && (v.Get("siteID") == "" || v.Get("networkID") == "") {
web.BadRequest(w, r, "please specify networkID and siteID")
return
}
err := map180.ValidBbox(bbox)
if err != nil {
web.BadRequest(w, r, err.Error())
return
}
width := 130
if v.Get("width") != "" {
width, err = strconv.Atoi(v.Get("width"))
if err != nil {
web.BadRequest(w, r, "invalid width.")
return
}
}
var s []st
if v.Get("sites") != "" {
for _, ns := range strings.Split(v.Get("sites"), ",") {
nss := strings.Split(ns, ".")
if len(nss) != 2 {
web.BadRequest(w, r, "invalid sites query.")
return
}
s = append(s, st{networkID: nss[0], siteID: nss[1]})
}
} else {
s = append(s, st{networkID: v.Get("networkID"),
siteID: v.Get("siteID")})
}
markers := make([]map180.Marker, 0)
for _, site := range s {
if !validSite(w, r, site.networkID, site.siteID) {
return
}
g, err := geoJSONSite(site.networkID, site.siteID)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
m, err := geoJSONToMarkers(g)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
markers = append(markers, m...)
}
b, err := wm.SVG(bbox, width, markers, insetBbox)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
w.Header().Set("Content-Type", "image/svg+xml")
web.OkBuf(w, r, &b)
}
var siteTypeMapD = &apidoc.Query{
Accept: "",
Title: "Site Type Maps",
Description: "Maps of sites filtered by observation type, method, and location.",
Discussion: `<p>Maps of site type have the same <code>width</code> and <code>bbox</code> query parameters as
maps for individual sites. The type of site displayed can be filtered by <code>typeID</code>, <code>methodID</code>, and <code>within</code>.</p>
<p>
<object data="/map/site?typeID=u&width=500&bbox=NewZealand" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?typeID=u&width=500&bbox=NewZealand" type="image/svg+xml"></object></code><br/><br/>
<br />
</p>
<p>
<object data="/map/site?typeID=z&width=500&bbox=LakeTaupo&insetBbox=NewZealand" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?typeID=z&width=500&bbox=LakeTaupo&insetBbox=NewZealand" type="image/svg+xml"></object></code><br/><br/>
</p>
<p>
<object data="/map/site?typeID=SO2-flux-a&methodID=mdoas-m" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?typeID=SO2-flux-a&methodID=mdoas-m" type="image/svg+xml"></object></code><br/><br/>
</p>
<p>
<object data="/map/site?typeID=t&width=500&bbox=WhiteIsland&insetBbox=NewZealand" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?typeID=t&width=500&bbox=WhiteIsland&insetBbox=NewZealand" type="image/svg+xml"></object></code><br/><br/>
</p>
<p>
<object data="/map/site?typeID=t&methodID=thermcoup&bbox=177.185,-37.531,177.197,-37.52&width=400&within=POLYGON((177.18+-37.52,177.19+-37.52,177.20+-37.53,177.18+-37.52))" type="image/svg+xml"></object><br/><br/>
<code><object data="http://fits.geonet.org.nz/map/site?typeID=t&methodID=thermcoup&bbox=177.185,-37.531,177.197,-37.52&width=400&within=POLYGON((177.18+-37.52,177.19+-37.52,177.20+-37.53,177.18+-37.52))" type="image/svg+xml"></object></code><br/><br/>
</p>
`,
URI: `/map/site?[typeID=(typeID)]&[methodID=(methodID)]&[within=POLYGON((...))][&bbox=(float,float,float,float)|string][&width=(int)]`,
Optional: map[string]template.HTML{
"typeID": typeIDDoc,
"methodID": methodIDDoc + ` typeID must be specified as well.`,
"within": withinDoc,
"width": widthDoc,
"bbox": bboxDoc,
"insetBbox": ` If specified then is used to draw a small inset map in the upper left corner. Useful for
giving context to zoomed in regions. Same specification options as <code>bbox</code>.`,
},
}
func siteTypeMap(w http.ResponseWriter, r *http.Request) {
if err := siteTypeMapD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
v := r.URL.Query()
bbox := v.Get("bbox")
err := map180.ValidBbox(bbox)
if err != nil {
web.BadRequest(w, r, err.Error())
return
}
var insetBbox, typeID, methodID, within string
width := 130
if v.Get("insetBbox") != "" {
insetBbox = v.Get("insetBbox")
err := map180.ValidBbox(insetBbox)
if err != nil {
web.BadRequest(w, r, err.Error())
return
}
}
if v.Get("width") != "" {
width, err = strconv.Atoi(v.Get("width"))
if err != nil {
web.BadRequest(w, r, "invalid width.")
return
}
}
if v.Get("methodID") != "" && v.Get("typeID") == "" {
web.BadRequest(w, r, "typeID must be specified when methodID is specified.")
return
}
if v.Get("typeID") != "" {
typeID = v.Get("typeID")
if !validType(w, r, typeID) {
return
}
if v.Get("methodID") != "" {
methodID = v.Get("methodID")
if !validTypeMethod(w, r, typeID, methodID) {
return
}
}
}
if v.Get("within") != "" {
within = strings.Replace(v.Get("within"), "+", "", -1)
if !validPoly(w, r, within) {
return
}
} else if bbox != "" {
within, err = map180.BboxToWKTPolygon(bbox)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
}
g, err := geoJSONSites(typeID, methodID, within)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
m, err := geoJSONToMarkers(g)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b, err := wm.SVG(bbox, width, m, insetBbox)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
w.Header().Set("Content-Type", "image/svg+xml")
web.OkBuf(w, r, &b)
}