Skip to content

Commit

Permalink
Add bbox filtering to search
Browse files Browse the repository at this point in the history
Add possibility to filter search results by bounding box. This is for
when only results in given area are wanted.
  • Loading branch information
japauliina committed Aug 26, 2024
1 parent cc25376 commit 38c2af7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
19 changes: 19 additions & 0 deletions services/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import re
from itertools import chain

from django.contrib.gis.gdal import SpatialReference
from django.db import connection, reset_queries
from django.db.models import Count
from drf_spectacular.utils import extend_schema, OpenApiParameter
from munigeo import api as munigeo_api
from munigeo.models import Address, AdministrativeDivision
from munigeo.utils import get_default_srid
from rest_framework import serializers, status
from rest_framework.exceptions import ParseError
from rest_framework.generics import GenericAPIView
Expand Down Expand Up @@ -344,6 +346,13 @@ def to_representation(self, obj):
required=False,
type=str,
),
OpenApiParameter(
name="bbox",
location=OpenApiParameter.QUERY,
description="Bounding box in the format 'left,bottom,right,top'.",
required=False,
type=str,
),
],
description="Search for units, services, service nodes, addresses and administrative divisions.",
)
Expand Down Expand Up @@ -587,6 +596,16 @@ def get(self, request):
if services[0]:
units_qs = units_qs.filter(services__in=services)

if "bbox" in self.request.query_params:
bbox = self.request.query_params["bbox"]
if "bbox_srid" in self.request.query_params:
bbox_srid = self.request.query_params["bbox_srid"]
else:
bbox_srid = get_default_srid()
ref = SpatialReference(bbox_srid)
bbox_filter = munigeo_api.build_bbox_filter(ref, bbox, "location")
units_qs = units_qs.filter(**bbox_filter)

if units_order_list:
units_qs = units_qs.annotate(num_services=Count("services")).order_by(
*units_order_list
Expand Down
1 change: 1 addition & 0 deletions services/search/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def units(
last_modified_time=now(),
municipality=municipality,
department=department,
location=Point(24.941387, 60.17103, srid=4326), # Helsinki center
)
# Add service Halli
unit.services.add(4)
Expand Down
20 changes: 20 additions & 0 deletions services/search/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,23 @@ def test_search_with_vertical_bar_in_query(api_client, units):
url = reverse("search") + "?q=|terveysasema||''||'"
response = api_client.get(url)
assert response.status_code == 200


@pytest.mark.django_db
def test_search_with_bbox_parameter(api_client, units):
"""
When bbox parameter is given, only units within the bounding box should be returned.
"""
url = reverse("search") + "?q=halli&type=unit"
response = api_client.get(url)
results = response.json()["results"]
assert len(results) == 3

url = (
reverse("search")
+ "?q=halli&type=unit&bbox=24.93545,60.16952,24.95190,60.17800&bbox_srid=4326"
)
response = api_client.get(url)
results = response.json()["results"]
assert len(results) == 1
assert results[0]["name"]["fi"] == "Jäähalli"

0 comments on commit 38c2af7

Please sign in to comment.