forked from GeoNet/fits
/
site.go
206 lines (176 loc) · 5.94 KB
/
site.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
package main
import (
"database/sql"
"github.com/GeoNet/web"
"github.com/GeoNet/web/api/apidoc"
"html/template"
"net/http"
"strings"
)
const (
siteGeoJSON = `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
siteid AS "siteID",
height,
ground_relationship AS "groundRelationship",
name,
networkID as "networkID"
) as l
)) as properties FROM (fits.site join fits.network using (networkpk)) as s `
fc = ` ) As f ) as fc`
)
var siteDoc = apidoc.Endpoint{Title: "Site",
Description: `Look up site information.`,
Queries: []*apidoc.Query{
siteTypeD,
siteD,
},
}
var siteD = &apidoc.Query{
Accept: web.V1GeoJSON,
Title: "Site",
Description: "Find information for individual sites.",
Example: "/site?siteID=HOLD&networkID=CG",
ExampleHost: exHost,
URI: "/site?siteID=(siteID)&networkID=(networkID)",
Required: map[string]template.HTML{
"siteID": siteIDDoc,
"networkID": networkIDDoc,
},
Props: siteProps,
}
func site(w http.ResponseWriter, r *http.Request) {
if err := siteD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
networkID := r.URL.Query().Get("networkID")
siteID := r.URL.Query().Get("siteID")
if !validSite(w, r, networkID, siteID) {
return
}
w.Header().Set("Content-Type", web.V1GeoJSON)
b, err := geoJSONSite(networkID, siteID)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
web.Ok(w, r, &b)
}
var siteTypeD = &apidoc.Query{
Accept: web.V1GeoJSON,
Title: "Sites",
Description: "Filter sites by observation type, method, and location.",
Example: "/site?typeID=e",
ExampleHost: exHost,
URI: "/site?[typeID=(typeID)]&[methodID=(methodID)]&[within=POLYGON((...))]",
Optional: map[string]template.HTML{
"typeID": typeIDDoc,
"methodID": methodIDDoc + ` typeID must be specified as well.`,
"within": withinDoc,
},
Props: siteProps,
}
func siteType(w http.ResponseWriter, r *http.Request) {
if err := siteTypeD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
v := r.URL.Query()
if v.Get("methodID") != "" && v.Get("typeID") == "" {
web.BadRequest(w, r, "typeID must be specified when methodID is specified.")
return
}
var typeID, methodID, within string
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
}
}
w.Header().Set("Content-Type", web.V1GeoJSON)
b, err := geoJSONSites(typeID, methodID, within)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
web.Ok(w, r, &b)
}
// validSite checks that the siteID and networkID combination exists in the DB.
func validSite(w http.ResponseWriter, r *http.Request, networkID, siteID string) bool {
var d string
err := db.QueryRow("select siteID FROM fits.site join fits.network using (networkpk) where siteid = $2 and networkid = $1", networkID, siteID).Scan(&d)
if err == sql.ErrNoRows {
web.NotFound(w, r, "invalid siteID and networkID combination: "+siteID+" "+networkID)
return false
}
if err != nil {
web.ServiceUnavailable(w, r, err)
return false
}
return true
}
func geoJSONSite(networkID, siteID string) ([]byte, error) {
var d string
err := db.QueryRow(
siteGeoJSON+` WHERE siteid = $1 and networkid = $2`+fc, siteID, networkID).Scan(&d)
return []byte(d), err
}
func geoJSONSites(typeID, methodID, within string) ([]byte, error) {
var d string
var err error
switch {
case typeID == "" && methodID == "" && within == "":
err = db.QueryRow(
siteGeoJSON + fc).Scan(&d)
case typeID == "" && methodID == "" && within != "":
err = db.QueryRow(
siteGeoJSON+
`where ST_Within(location::geometry, ST_GeomFromText($1, 4326))`+
fc, within).Scan(&d)
case typeID != "" && methodID == "" && within == "":
err = db.QueryRow(
siteGeoJSON+
` where sitepk IN
(select distinct on (sitepk) sitepk from fits.observation where observation.typepk = (select typepk from fits.type where typeid = $1))`+fc, typeID).Scan(&d)
case typeID != "" && methodID == "" && within != "":
err = db.QueryRow(
siteGeoJSON+
` where sitepk IN
(select distinct on (sitepk) sitepk from fits.observation where observation.typepk = (select typepk from fits.type where typeid = $1))
AND ST_Within(ST_Shift_Longitude(location::geometry), ST_Shift_Longitude(ST_GeomFromText($2, 4326)))`+fc, typeID, within).Scan(&d)
case typeID != "" && methodID != "" && within == "":
err = db.QueryRow(
siteGeoJSON+
` where sitepk IN
(select distinct on (sitepk) sitepk from fits.observation where
observation.typepk = (select typepk from fits.type where typeid = $1)
AND observation.methodpk = (select methodpk from fits.method where methodid = $2))`+fc, typeID, methodID).Scan(&d)
case typeID != "" && methodID != "" && within != "":
err = db.QueryRow(
siteGeoJSON+
` where sitepk IN
(select distinct on (sitepk) sitepk from fits.observation where
observation.typepk = (select typepk from fits.type where typeid = $1)
AND observation.methodpk = (select methodpk from fits.method where methodid = $2))
AND ST_Within(ST_Shift_Longitude(location::geometry), ST_Shift_Longitude(ST_GeomFromText($3, 4326)))`+fc, typeID, methodID, within).Scan(&d)
}
return []byte(d), err
}