maps: add distance filter to geojson (#24558)

This commit is contained in:
Frédéric Péters 2018-07-01 21:40:33 +02:00
parent bfc98e907a
commit 86a5b3aea8
5 changed files with 43 additions and 2 deletions

View File

@ -24,6 +24,8 @@ from django.core.urlresolvers import reverse_lazy
from django import forms
from django.conf import settings
import pyproj
from combo.data.models import CellBase
from combo.data.library import register_cell_class
from combo.utils import requests
@ -158,6 +160,24 @@ class MapLayer(models.Model):
else:
features = data
if request and request.GET.get('distance'):
distance = float(request.GET['distance'])
center_lat = float(request.GET['lat'])
center_lng = float(request.GET['lng'])
geod = pyproj.Geod(ellps='WGS84')
south_lat = geod.fwd(center_lng, center_lat, 180, distance)[1]
north_lat = geod.fwd(center_lng, center_lat, 0, distance)[1]
east_lng = geod.fwd(center_lng, center_lat, 90, distance)[0]
west_lng = geod.fwd(center_lng, center_lat, -90, distance)[0]
def match(feature):
if feature['geometry']['type'] != 'Point':
return True
lng, lat = feature['geometry']['coordinates']
return bool(west_lng < lng < east_lng and south_lat < lat < north_lat)
features = [x for x in features if match(x)]
if request and request.GET.get('q'):
query = slugify(request.GET['q'])

1
debian/control vendored
View File

@ -12,6 +12,7 @@ Depends: ${misc:Depends}, ${python:Depends},
python-django (>= 1.8),
python-djangorestframework (>= 3.3),
python-gadjo (>= 0.53),
pytgon-pyproj,
python-requests,
python-feedparser,
python-xstatic-josefinsans,

View File

@ -14,3 +14,4 @@ djangorestframework>=3.3, <3.4
django-haystack
whoosh
sorl-thumbnail
pyproj

View File

@ -152,6 +152,7 @@ setup(
'whoosh',
'sorl-thumbnail',
'Pillow',
'pyproj',
],
zip_safe=False,
cmdclass={

View File

@ -40,8 +40,8 @@ SAMPLE_GEOJSON_CONTENT = '''{
"geometry": {
"type": "Point",
"coordinates": [
2.548828125,
48.83579746243093
2.558828125,
48.84579746243093
]
}
}
@ -225,3 +225,21 @@ def test_get_geojson(layer, user):
assert len(json.loads(resp.content)['features']) == 1
assert 'orig=combo' in requests_get.call_args[0][1]
assert not 'email=admin%40localhost&' in requests_get.call_args[0][1]
# check distance query on geojson
layer.geojson_url = 'http://example.org/geojson?t6'
layer.include_user_identifier = False
layer.save()
with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
requests_get.return_value = mock.Mock(
content=SAMPLE_GEOJSON_CONTENT,
json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT),
status_code=200)
resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?lng=2.54&lat=48.84&distance=2000')
assert len(json.loads(resp.content)['features']) == 2
resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?lng=2.54&lat=48.84&distance=1000')
assert len(json.loads(resp.content)['features']) == 1
resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?lng=2.54&lat=48.84&distance=100')
assert len(json.loads(resp.content)['features']) == 0