Skip to content

Commit 91d9418

Browse files
committed
fixed merge conflict
2 parents 79f4df1 + fb9e52a commit 91d9418

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

bom-shapefiles.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package apiary
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"strconv"
9+
)
10+
11+
// ParishShpHandler returns a GeoJSON FeatureCollection containing parish
12+
// polygons and can receive year, city_cnty, and subunit parameters.
13+
func (s *Server) ParishShpHandler() http.HandlerFunc {
14+
baseQuery := `
15+
SELECT json_build_object(
16+
'type', 'FeatureCollection',
17+
'features', COALESCE(json_agg(parishes.feature), '[]'::json)
18+
)
19+
FROM (
20+
SELECT json_build_object(
21+
'type', 'Feature',
22+
'id', id,
23+
'properties', json_build_object(
24+
'par', par,
25+
'civ_par', civ_par,
26+
'dbn_par', dbn_par,
27+
'omeka_par', omeka_par,
28+
'subunit', subunit,
29+
'city_cnty', city_cnty,
30+
'start_yr', start_yr,
31+
'sp_total', sp_total,
32+
'sp_per', sp_per
33+
),
34+
'geometry', ST_AsGeoJSON(
35+
ST_Transform(
36+
ST_SetSRID(geom_01, 27700),
37+
4326
38+
),
39+
6
40+
)::json
41+
) AS feature
42+
FROM bom.parishes_shp
43+
WHERE 1=1
44+
`
45+
46+
return func(w http.ResponseWriter, r *http.Request) {
47+
// Get query parameters
48+
year := r.URL.Query().Get("year")
49+
subunit := r.URL.Query().Get("subunit")
50+
cityCounty := r.URL.Query().Get("city_cnty")
51+
52+
// Build the query with optional filters
53+
query := baseQuery
54+
var params []interface{}
55+
paramCount := 1
56+
57+
if year != "" {
58+
if yearInt, err := strconv.Atoi(year); err == nil {
59+
query += fmt.Sprintf(" AND start_yr = $%d", paramCount)
60+
params = append(params, yearInt)
61+
paramCount++
62+
}
63+
}
64+
65+
if subunit != "" {
66+
query += fmt.Sprintf(" AND subunit = $%d", paramCount)
67+
params = append(params, subunit)
68+
paramCount++
69+
}
70+
71+
if cityCounty != "" {
72+
query += fmt.Sprintf(" AND city_cnty = $%d", paramCount)
73+
params = append(params, cityCounty)
74+
paramCount++
75+
}
76+
77+
// Close the subquery and main query
78+
query += ") AS parishes;"
79+
80+
var result string
81+
var err error
82+
83+
if len(params) == 0 {
84+
err = s.DB.QueryRow(context.Background(), query).Scan(&result)
85+
} else {
86+
err = s.DB.QueryRow(context.Background(), query, params...).Scan(&result)
87+
}
88+
89+
if err != nil {
90+
log.Println(err)
91+
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
92+
return
93+
}
94+
95+
w.Header().Set("Content-Type", "application/json")
96+
fmt.Fprint(w, result)
97+
}
98+
}

endpoints.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func (s *Server) EndpointsHandler() http.HandlerFunc {
154154
},
155155
},
156156
},
157+
{
158+
"BOM: Parish polygons",
159+
baseurl + "/bom/geometries",
160+
nil,
161+
},
157162
{
158163
"BOM: Bills of Mortality",
159164
baseurl + "/bom/bills?start-year=1636&end-year=1754",

routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (s *Server) Routes() {
1919
s.Router.HandleFunc("/apb/verse-quotations", s.APBVerseQuotationsHandler()).Methods("GET", "HEAD")
2020
s.Router.HandleFunc("/apb/verse-trend", s.APBVerseTrendHandler()).Methods("GET", "HEAD")
2121
s.Router.HandleFunc("/bom/parishes", s.ParishesHandler()).Methods("GET", "HEAD")
22+
s.Router.HandleFunc("/bom/geometries", s.ParishShpHandler()).Methods("GET", "HEAD")
2223
s.Router.HandleFunc("/bom/totalbills", s.TotalBillsHandler()).Methods("GET", "HEAD")
2324
s.Router.HandleFunc("/bom/statistics", s.StatisticsHandler()).Methods("GET", "HEAD")
2425
s.Router.HandleFunc("/bom/bills", s.BillsHandler()).Methods("GET", "HEAD")

0 commit comments

Comments
 (0)