Skip to content

Commit

Permalink
fixed merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
hepplerj committed Feb 5, 2025
2 parents 79f4df1 + fb9e52a commit 91d9418
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
98 changes: 98 additions & 0 deletions bom-shapefiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package apiary

import (
"context"
"fmt"
"log"
"net/http"
"strconv"
)

// ParishShpHandler returns a GeoJSON FeatureCollection containing parish
// polygons and can receive year, city_cnty, and subunit parameters.
func (s *Server) ParishShpHandler() http.HandlerFunc {
baseQuery := `
SELECT json_build_object(
'type', 'FeatureCollection',
'features', COALESCE(json_agg(parishes.feature), '[]'::json)
)
FROM (
SELECT json_build_object(
'type', 'Feature',
'id', id,
'properties', json_build_object(
'par', par,
'civ_par', civ_par,
'dbn_par', dbn_par,
'omeka_par', omeka_par,
'subunit', subunit,
'city_cnty', city_cnty,
'start_yr', start_yr,
'sp_total', sp_total,
'sp_per', sp_per
),
'geometry', ST_AsGeoJSON(
ST_Transform(
ST_SetSRID(geom_01, 27700),
4326
),
6
)::json
) AS feature
FROM bom.parishes_shp
WHERE 1=1
`

return func(w http.ResponseWriter, r *http.Request) {
// Get query parameters
year := r.URL.Query().Get("year")
subunit := r.URL.Query().Get("subunit")
cityCounty := r.URL.Query().Get("city_cnty")

// Build the query with optional filters
query := baseQuery
var params []interface{}
paramCount := 1

if year != "" {
if yearInt, err := strconv.Atoi(year); err == nil {
query += fmt.Sprintf(" AND start_yr = $%d", paramCount)
params = append(params, yearInt)
paramCount++
}
}

if subunit != "" {
query += fmt.Sprintf(" AND subunit = $%d", paramCount)
params = append(params, subunit)
paramCount++
}

if cityCounty != "" {
query += fmt.Sprintf(" AND city_cnty = $%d", paramCount)
params = append(params, cityCounty)
paramCount++
}

// Close the subquery and main query
query += ") AS parishes;"

var result string
var err error

if len(params) == 0 {
err = s.DB.QueryRow(context.Background(), query).Scan(&result)
} else {
err = s.DB.QueryRow(context.Background(), query, params...).Scan(&result)
}

if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, result)
}
}
5 changes: 5 additions & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ func (s *Server) EndpointsHandler() http.HandlerFunc {
},
},
},
{
"BOM: Parish polygons",
baseurl + "/bom/geometries",
nil,
},
{
"BOM: Bills of Mortality",
baseurl + "/bom/bills?start-year=1636&end-year=1754",
Expand Down
1 change: 1 addition & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s *Server) Routes() {
s.Router.HandleFunc("/apb/verse-quotations", s.APBVerseQuotationsHandler()).Methods("GET", "HEAD")
s.Router.HandleFunc("/apb/verse-trend", s.APBVerseTrendHandler()).Methods("GET", "HEAD")
s.Router.HandleFunc("/bom/parishes", s.ParishesHandler()).Methods("GET", "HEAD")
s.Router.HandleFunc("/bom/geometries", s.ParishShpHandler()).Methods("GET", "HEAD")
s.Router.HandleFunc("/bom/totalbills", s.TotalBillsHandler()).Methods("GET", "HEAD")
s.Router.HandleFunc("/bom/statistics", s.StatisticsHandler()).Methods("GET", "HEAD")
s.Router.HandleFunc("/bom/bills", s.BillsHandler()).Methods("GET", "HEAD")
Expand Down

0 comments on commit 91d9418

Please sign in to comment.