From 6889a04ed0d31699671c4092be82892c93a4bbf3 Mon Sep 17 00:00:00 2001 From: Joaquim d'Souza Date: Mon, 31 Mar 2025 14:54:04 +0200 Subject: [PATCH] fix: correct sample_point used for postgis geocoding --- hub/data_imports/geocoding_config.py | 4 +++- hub/tests/test_statistics.py | 2 +- utils/postgis_geocoder.py | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/hub/data_imports/geocoding_config.py b/hub/data_imports/geocoding_config.py index be296e881..2e7cf2a1b 100644 --- a/hub/data_imports/geocoding_config.py +++ b/hub/data_imports/geocoding_config.py @@ -8,6 +8,7 @@ from django.conf import settings from django.contrib.gis.geos import Point +from django.contrib.gis.geos.collections import MultiPolygon from django.db.models import Q from asgiref.sync import sync_to_async @@ -534,7 +535,8 @@ async def import_area_data( async def get_postcode_data_for_area(area: "Area", loaders: "Loaders", steps: list): - sample_point = area.polygon.centroid + polygon: MultiPolygon = area.polygon + sample_point = polygon.point_on_surface or polygon.centroid # get postcodeIO result for area.coordinates postcode_data = None diff --git a/hub/tests/test_statistics.py b/hub/tests/test_statistics.py index 133686458..a1202d83f 100644 --- a/hub/tests/test_statistics.py +++ b/hub/tests/test_statistics.py @@ -46,7 +46,7 @@ def setUp(self) -> None: # Some of that dataset uses out of date councils, # which have been merged together in the geocoding # (see `duplicate_councils`) - self.geocodable_council_count = 315 + self.geocodable_council_count = 318 self.count_regions = models.Area.objects.filter(area_type__code="EER").count() def test_count_by_area(self): diff --git a/utils/postgis_geocoder.py b/utils/postgis_geocoder.py index fa86c5961..a9da1feab 100644 --- a/utils/postgis_geocoder.py +++ b/utils/postgis_geocoder.py @@ -86,7 +86,9 @@ def _get_bulk_postcode_geo_from_coords( if mapit_gen: area_filter += f" AND mapit_generation_high = {mapit_gen}" - gis_query = f"SELECT id FROM hub_area WHERE {area_filter} ORDER BY temp.point <-> polygon LIMIT 1" + gis_query = f""" + SELECT id FROM hub_area WHERE {area_filter} AND ST_Contains(polygon, temp.point) LIMIT 1 + """ join = f"LEFT JOIN hub_area AS {alias} ON {alias}.id = ({gis_query})" area_joins.append(join)