forked from junghao/geonet-rest
/
intensity.go
234 lines (203 loc) · 7.39 KB
/
intensity.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
package main
import (
"database/sql"
"github.com/GeoNet/web"
"github.com/GeoNet/web/api/apidoc"
"html/template"
"net/http"
"regexp"
"time"
)
var impactDoc = apidoc.Endpoint{Title: "Impact",
Description: `Look up impact information`,
Queries: []*apidoc.Query{
// intensityReportedD,
// intensityReportedLatestD,
intensityMeasuredLatestD,
},
}
var zoomRe = regexp.MustCompile(`^(5|6)$`)
var intensityMeasuredLatestD = &apidoc.Query{
Accept: web.V1GeoJSON,
Title: "Measured Intensity - Latest",
Description: "Retrieve measured intensity information in the last sixty minutes.",
Example: "/intensity?type=measured",
ExampleHost: exHost,
URI: "/intensity?type",
Required: map[string]template.HTML{
"type": `<code>measured</code> is the only allowed value.`,
},
Props: map[string]template.HTML{
"max_mmi": `the maximum <a href="http://info.geonet.org.nz/x/w4IO">Modified Mercalli Intensity (MMI)</a> measured at the point in the last sixty minutes.`,
},
}
func intensityMeasuredLatest(w http.ResponseWriter, r *http.Request) {
if err := intensityMeasuredLatestD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
if r.URL.Query().Get("type") != "measured" {
web.BadRequest(w, r, "type must be measured.")
return
}
var d string
err := db.QueryRow(
`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, COALESCE(array_to_json(array_agg(f)), '[]') as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(s.location)::json as geometry,
row_to_json(( select l from
(
select mmi
) as l ))
as properties from (select location, mmi
FROM impact.intensity_measured) as s
) As f ) as fc`).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}
// latest reported intensity
var intensityReportedLatestD = &apidoc.Query{
Accept: web.V1GeoJSON,
Title: "Reported Intensity - Latest",
Description: "Retrieve reported intensity information in the last sixty minutes.",
Example: "/intensity?type=reported&zoom=5",
ExampleHost: exHost,
URI: "/intensity?type=reported&zoom=(int)",
Required: map[string]template.HTML{
"type": `<code>reported</code> is the only allowed value.`,
"zoom": `The zoom level to aggregate values at. This controls the size of the area that values are aggregated at. The point returned
will be the center of each area. Allowed values are one of <code>5, 6</code>.`,
},
Props: map[string]template.HTML{
"max_mmi": `the maximum <a href="http://info.geonet.org.nz/x/w4IO">Modified Mercalli Intensity (MMI)</a>
in the area around the point in the last sixty minutes.`,
"min_mmi": `the minimum <a href="http://info.geonet.org.nz/x/w4IO">Modified Mercalli Intensity (MMI)</a>
in the area of around the point in the last sixty minutes.`,
"count": `the count of <a href="http://info.geonet.org.nz/x/w4IO">Modified Mercalli Intensity (MMI)</a>
values reported in the area of around the point in the last sixty minutes.`,
},
}
func intensityReportedLatest(w http.ResponseWriter, r *http.Request) {
if err := intensityReportedLatestD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
if r.URL.Query().Get("type") != "reported" {
web.BadRequest(w, r, "type must be reported.")
return
}
zoom := r.URL.Query().Get("zoom")
if !zoomRe.MatchString(r.URL.Query().Get("zoom")) {
web.BadRequest(w, r, "Invalid zoom")
return
}
var d string
err := db.QueryRow(
`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, COALESCE(array_to_json(array_agg(f)), '[]') as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(s.location)::json as geometry,
row_to_json(( select l from
(
select max_mmi,
min_mmi,
count
) as l ))
as properties from (select st_pointfromgeohash(geohash` + zoom + `) as location,
min(mmi) as min_mmi,
max(mmi) as max_mmi,
count(mmi) as count
FROM impact.intensity_reported
WHERE time >= (now() - interval '60 minutes')
group by (geohash` + zoom + `)) as s
) As f ) as fc`).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}
// reported intensity
var intensityReportedD = &apidoc.Query{
Accept: web.V1GeoJSON,
Title: "Reported Intensity",
Description: "Retrieve reported intensity information in a 15 minute time window after an event.",
Example: "/intensity?type=reported&zoom=5&publicID=2013p407387",
ExampleHost: exHost,
URI: "/intensity?type=reported&zoom=(int)&publicID=(publicID)",
Required: map[string]template.HTML{
"type": `<code>reported</code> is the only allowed value.`,
"zoom": `The zoom level to aggregate values at. This controls the size of the area that values are aggregated at. The point returned
will be the center of each area. Allowed values are one of <code>5, 6</code>.`,
"publicID": `a valid quake ID e.g., <code>2014p715167</code>`,
},
Props: map[string]template.HTML{
"max_mmi": `the maximum <a href="http://info.geonet.org.nz/x/w4IO">Modified Mercalli Intensity (MMI)</a>
in the area around the point in the last sixty minutes.`,
"min_mmi": `the minimum <a href="http://info.geonet.org.nz/x/w4IO">Modified Mercalli Intensity (MMI)</a>
in the area of around the point in the last sixty minutes.`,
"count": `the count of <a href="http://info.geonet.org.nz/x/w4IO">Modified Mercalli Intensity (MMI)</a>
values reported in the area of around the point in the last sixty minutes.`,
},
}
func intensityReported(w http.ResponseWriter, r *http.Request) {
if err := intensityReportedD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
if r.URL.Query().Get("type") != "reported" {
web.BadRequest(w, r, "type must be reported.")
return
}
zoom := r.URL.Query().Get("zoom")
if !zoomRe.MatchString(r.URL.Query().Get("zoom")) {
web.BadRequest(w, r, "Invalid zoom")
return
}
// Check that the publicid exists in the DB.
// If it does we keep the origintime - we need it later on.
publicID := r.URL.Query().Get("publicID")
originTime := time.Time{}
err := db.QueryRow("select origintime FROM qrt.quake_materialized where publicid = $1", publicID).Scan(&originTime)
if err == sql.ErrNoRows {
web.NotFound(w, r, "invalid publicID: "+publicID)
return
}
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
query := `SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, COALESCE(array_to_json(array_agg(f)), '[]') as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(s.location)::json as geometry,
row_to_json(( select l from
(
select max_mmi,
min_mmi,
count
) as l ))
as properties from (select st_pointfromgeohash(geohash` + zoom + `) as location,
min(mmi) as min_mmi,
max(mmi) as max_mmi,
count(mmi) as count
FROM impact.intensity_reported
WHERE time >= $1
AND time <= $2
group by (geohash` + zoom + `)) as s
) As f ) as fc`
var d string
err = db.QueryRow(query, originTime.Add(time.Duration(-1*time.Minute)), originTime.Add(time.Duration(15*time.Minute))).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}