diff --git a/backend/right_tree/api/filters.py b/backend/right_tree/api/filters.py index faf0a8a..ffde0af 100644 --- a/backend/right_tree/api/filters.py +++ b/backend/right_tree/api/filters.py @@ -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() diff --git a/backend/right_tree/api/views.py b/backend/right_tree/api/views.py index a239cb9..e48b11e 100644 --- a/backend/right_tree/api/views.py +++ b/backend/right_tree/api/views.py @@ -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. diff --git a/backend/right_tree/urls.py b/backend/right_tree/urls.py index 579d76e..cd13a1e 100644 --- a/backend/right_tree/urls.py +++ b/backend/right_tree/urls.py @@ -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')