From 9e199db27324e3b7cc7f2bcf151d7b8a9c9e00b9 Mon Sep 17 00:00:00 2001 From: Richard Hull Date: Sat, 28 Jun 2025 14:49:38 +0100 Subject: [PATCH 1/2] Inflate the bound box to include polygons whos centroids might be just outside the current bounds --- routes/search.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/routes/search.go b/routes/search.go index ae8f4cd..f8cd04b 100644 --- a/routes/search.go +++ b/routes/search.go @@ -62,6 +62,10 @@ func PolygonSearch(spatialIndex *spatialindex.SpatialIndex, cache *memoize.Memoi tooBig := isTooBig(bbox) target := map[bool]string{true: "districts", false: "units"}[tooBig] + if target == "units" { + expandBounds(&bbox, 100) + } + requested := make(map[string]struct{}, 100) districts := make(map[string]struct{}, 20) @@ -110,6 +114,14 @@ func PolygonSearch(spatialIndex *spatialindex.SpatialIndex, cache *memoize.Memoi } } +func expandBounds(bbox *[]uint32, extendBy int) { + b := *bbox + b[0] = uint32(int(b[0]) - extendBy) // min_easting + b[1] = uint32(int(b[1]) - extendBy) // min_northing + b[2] = uint32(int(b[2]) + extendBy) // max_easting + b[3] = uint32(int(b[3]) + extendBy) // max_northing +} + func parseBBox(bboxStr string) ([]uint32, error) { bboxParts := strings.Split(bboxStr, ",") if len(bboxParts) != 4 { From f940d46f4e7d272d2b5def7112dadcf69340643f Mon Sep 17 00:00:00 2001 From: Richard Hull Date: Sat, 28 Jun 2025 15:06:28 +0100 Subject: [PATCH 2/2] Address PR comments --- routes/search.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/routes/search.go b/routes/search.go index f8cd04b..9fabd45 100644 --- a/routes/search.go +++ b/routes/search.go @@ -17,6 +17,8 @@ import ( "github.com/paulmach/orb/geojson" ) +const UNITS_BOUNDS_EXPANSION = 100 + type SearchResponse struct { Results []spatialindex.CodePoint `json:"results"` Attribution []string `json:"attribution"` @@ -63,7 +65,7 @@ func PolygonSearch(spatialIndex *spatialindex.SpatialIndex, cache *memoize.Memoi target := map[bool]string{true: "districts", false: "units"}[tooBig] if target == "units" { - expandBounds(&bbox, 100) + expandBounds(&bbox, UNITS_BOUNDS_EXPANSION) } requested := make(map[string]struct{}, 100) @@ -114,12 +116,12 @@ func PolygonSearch(spatialIndex *spatialindex.SpatialIndex, cache *memoize.Memoi } } -func expandBounds(bbox *[]uint32, extendBy int) { +func expandBounds(bbox *[]uint32, extendBy uint32) { b := *bbox - b[0] = uint32(int(b[0]) - extendBy) // min_easting - b[1] = uint32(int(b[1]) - extendBy) // min_northing - b[2] = uint32(int(b[2]) + extendBy) // max_easting - b[3] = uint32(int(b[3]) + extendBy) // max_northing + b[0] -= extendBy // min_easting + b[1] -= extendBy // min_northing + b[2] += extendBy // max_easting + b[3] += extendBy // max_northing } func parseBBox(bboxStr string) ([]uint32, error) {