Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bbox filtering to search #256

Merged
merged 2 commits into from
Aug 28, 2024
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
1 change: 1 addition & 0 deletions services/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,7 @@ def render(self, data, media_type=None, renderer_context=None):
LEVEL_PARAMETER,
UNIT_GEOMETRY_PARAMETER,
UNIT_GEOMETRY_3D_PARAMETER,
BBOX_PARAMETER,
]
)
class UnitViewSet(
Expand Down
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"
Loading