From ff197cc1a885ae66393b2b41056c639162128d2c Mon Sep 17 00:00:00 2001 From: Matthew Northcott Date: Wed, 8 Feb 2023 14:16:07 +1300 Subject: [PATCH] [#40] Address search backend --- backend/right_tree/api/views.py | 11 +++++-- backend/right_tree/api/wms_utils.py | 47 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/backend/right_tree/api/views.py b/backend/right_tree/api/views.py index 118b8b8..c457fed 100644 --- a/backend/right_tree/api/views.py +++ b/backend/right_tree/api/views.py @@ -8,7 +8,7 @@ from right_tree.api.models import Habitat, HabitatImage, Plant, EcologicalDistri from right_tree.api.serializers import HabitatImageSerializer, HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer, ZoneSerializer from .filters import * -from .wms_utils import get_address_from_coordinates +from .wms_utils import get_address_from_coordinates, search_address from .resource_generation_utils import create_plant_csv_file, get_plant_resource_filepath, create_planting_guide_pdf, PLANTING_GUIDE_PDF_FILENAME @@ -61,12 +61,17 @@ class LINZPropertyViewSet(viewsets.ViewSet): def list(self, request): coordinates = self.request.query_params.get('coordinates') - if coordinates is not None: + address = self.request.query_params.get('search') + + if address is not None: + results = search_address(address) + return Response(results) + elif coordinates is not None: address_data = get_address_from_coordinates(coordinates) serializer = AddressSerializer(address_data) return Response(serializer.data) else: - return HttpResponseBadRequest("No coordinate given.") + return HttpResponseBadRequest("No parameters given.") class AuckCHCHRegionInformation(viewsets.ViewSet): """ Filtered viewset defining if coordinate falls inside auckland and chch regions. diff --git a/backend/right_tree/api/wms_utils.py b/backend/right_tree/api/wms_utils.py index 65d3eb4..9aac9e6 100644 --- a/backend/right_tree/api/wms_utils.py +++ b/backend/right_tree/api/wms_utils.py @@ -1,7 +1,9 @@ import json import os +import re import requests from urllib.parse import urlencode +from unicodedata import normalize from django.contrib.gis.geos import Point, GEOSGeometry from django.conf import settings @@ -12,6 +14,10 @@ PROPERTY_TILE_LAYER = "layer-50804" PROPERTY_INFO_LAYER = "layer-53353" +search_num = re.compile(r'((?