Create endpoint to retrieve region containment info for chch and auck

This commit is contained in:
Dana Lambert 2021-12-01 09:47:14 +13:00 committed by Alistair McIntyre
parent 8f63a917e0
commit a22b1dfb38
3 changed files with 25 additions and 1 deletions

View file

@ -3,7 +3,7 @@ import json
from django.http import Http404
from django.db.models import Q
from .models import Plant, EcologicalRegion, EcologicalDistrictLayer, SoilOrder, SoilVariant
from .models import Plant, EcologicalRegion, EcologicalDistrictLayer, ChristchurchZone, SoilOrder, SoilVariant
from .wms_utils import get_point_from_coordinates
@ -59,6 +59,16 @@ def ecological_district_coordinate_filter(coordinates):
raise Http404(
f"Ecological district layer cannot be found for point {pnt}")
def is_in_auckland(coordinates):
pnt = get_point_from_coordinates(coordinates)
eco_district = EcologicalDistrictLayer.objects.filter(geom__intersects=pnt).first()
print(eco_district.ecologic_2)
return eco_district is not None and eco_district.ecologic_2.name == 'Auckland'
def is_in_christchurch(coordinates):
pnt = get_point_from_coordinates(coordinates)
in_chch = ChristchurchZone.objects.filter(geom__intersects=pnt).first()
return in_chch is not None;
def get_filtered_plants(request):
filtered_plants = Plant.objects.all()

View file

@ -68,6 +68,19 @@ class LINZPropertyViewSet(viewsets.ViewSet):
else:
return HttpResponseBadRequest("No coordinate given.")
class AuckCHCHRegionInformation(viewsets.ViewSet):
""" Filtered viewset defining if coordinate falls inside auckland and chch regions.
"""
def list(self, request):
coordinates = self.request.query_params.get('coordinates')
if coordinates is not None:
in_chch = is_in_christchurch(coordinates)
in_auckland = is_in_auckland(coordinates)
region_details = {"in_chch": in_chch, "in_auckland": in_auckland}
return Response(region_details)
else:
return HttpResponseBadRequest("No coordinate given.")
class HabitatViewSet(viewsets.ModelViewSet):
""" Viewset for all habitats.

View file

@ -24,6 +24,7 @@ router.register(r'plants', views.PlantViewSet)
router.register(r'soil', views.SoilOrderViewSet, basename='soil')
router.register(r'ecologicaldistrict', views.EcologicalDistrictViewSet, basename='ecologicaldistrict')
router.register(r'address', views.LINZPropertyViewSet, basename='address')
router.register(r'region', views.AuckCHCHRegionInformation, basename='region')
router.register(r'habitats', views.HabitatViewSet, basename='habitats')
router.register(r'habitatimage', views.HabitatImageViewSet, basename='habitatimage')