-
Notifications
You must be signed in to change notification settings - Fork 0
/
quakes.go
99 lines (76 loc) · 2.16 KB
/
quakes.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
package geonet
import (
"fmt"
"time"
)
// DefaultMMI is the mercali modified index score that
// is used by default in API calls where none is specified
const DefaultMMI = 2
// Quake represents an Earthquake event
type Quake struct {
quakeProperties
Coordinates []float64 `json:"coordinates"`
}
type quakeProperties struct {
// the unique public identifier for this quake
PublicID string `json:"publicID"`
// the origin time of the quake
Time time.Time `json:"time"`
// the depth of the quake in km
Depth float64 `json:"depth"`
// the summary magnitude for the quake
Magnitude float64 `json:"magnitude"`
// distance and direction to the nearest locality
Locality string `json:"locality"`
// the calculated MMI shaking at the closest locality in the New Zealand region, -1..8
MMI int `json:"mmi"`
// the quality of this information; best, good, caution, deleted
Quality string `json:"quality"`
}
// Quake will get a specific quake by the ID
func (c *Client) Quake(id string) (Quake, error) {
resp := quakeAPIResponse{}
if err := c.Get(sfmt("/quake/%s", id), &resp); err != nil {
return Quake{}, err
}
if len(resp.Quakes()) == 0 {
return Quake{}, fmt.Errorf("quake could not be parsed")
}
return resp.Quakes()[0], nil
}
// Quakes will return any recent quakes with the given MMI or higher
func (c *Client) Quakes(mmis ...int) ([]Quake, error) {
mmi := DefaultMMI
if len(mmis) > 0 {
mmi = mmis[0]
}
resp := quakeAPIResponse{}
if err := c.Get(sfmt("/quake?MMI=%d", mmi), &resp); err != nil {
return []Quake{}, err
}
return resp.Quakes(), nil
}
type quakeAPIResponse struct {
Type string `json:"type"`
Features []quakeFeature `json:"features"`
}
func (res quakeAPIResponse) Quakes() []Quake {
quakes := []Quake{}
for _, qf := range res.Features {
q := Quake{qf.Properties, qf.Geometry.Coordinates}
quakes = append(quakes, q)
}
return quakes
}
type feature struct {
Type string `json:"type"`
Geometry geometry `json:"geometry"`
}
type quakeFeature struct {
feature
Properties quakeProperties `json:"properties"`
}
type geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
}