Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion hub/data_imports/geocoding_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hub/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion utils/postgis_geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down