-
Notifications
You must be signed in to change notification settings - Fork 1
/
usgs.go
115 lines (103 loc) · 3.02 KB
/
usgs.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
package source
import (
"bytes"
"encoding/json"
"fmt"
"io"
"time"
)
const UsgsID ID = "USGS"
type Usgs struct {
source
}
func NewUsgs() Usgs {
return Usgs{source{
Name: "USGS", Url: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson",
Method: REST, SourceID: UsgsID,
}}
}
func (s Usgs) Transform(r io.Reader) ([]EarthquakeData, error) {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r)
if err != nil {
return nil, fmt.Errorf("cannot read from buffer: %v", err)
}
if buf.Len() <= 0 {
return []EarthquakeData{}, nil
}
var eventsRes UsgsGeoJsonResponse
err = json.Unmarshal(buf.Bytes(), &eventsRes)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal event data: %v", err)
}
maxFeatures := 10
if featuresLen := len(eventsRes.Features); featuresLen < maxFeatures {
maxFeatures = featuresLen
}
features := eventsRes.Features[:maxFeatures]
events := make([]EarthquakeData, 0, len(features))
for _, feature := range features {
data := EarthquakeData{
Mag: feature.Properties.Mag,
MagType: feature.Properties.Magtype,
Depth: feature.Geometry.Coordinates[2],
Time: time.Unix(feature.Properties.Time/1000, 0).UTC(),
Lat: feature.Geometry.Coordinates[1],
Lon: feature.Geometry.Coordinates[0],
Location: feature.Properties.Place,
DetailsURL: feature.Properties.URL,
SourceID: s.SourceID,
EventID: feature.ID,
}
events = append(events, data)
}
return events, nil
}
type UsgsGeoJsonResponse struct {
Type string `json:"type"`
Metadata struct {
Generated int64 `json:"generated"`
URL string `json:"url"`
Title string `json:"title"`
Status int `json:"status"`
API string `json:"api"`
Count int `json:"count"`
} `json:"metadata"`
Features []struct {
Type string `json:"type"`
Properties struct {
Mag float64 `json:"mag"`
Place string `json:"place"`
Time int64 `json:"time"`
Updated int64 `json:"updated"`
Tz interface{} `json:"tz"`
URL string `json:"url"`
Detail string `json:"detail"`
Felt interface{} `json:"felt"`
Cdi interface{} `json:"cdi"`
Mmi interface{} `json:"mmi"`
Alert interface{} `json:"alert"`
Status string `json:"status"`
Tsunami int `json:"tsunami"`
Sig int `json:"sig"`
Net string `json:"net"`
Code string `json:"code"`
Ids string `json:"ids"`
Sources string `json:"sources"`
Types string `json:"types"`
Nst int `json:"nst"`
Dmin float64 `json:"dmin"`
Rms float64 `json:"rms"`
Gap float64 `json:"gap"`
Magtype string `json:"magType"`
Type string `json:"type"`
Title string `json:"title"`
} `json:"properties"`
Geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
ID string `json:"id"`
} `json:"features"`
Bbox []float64 `json:"bbox"`
}