0005-opengis-add-bbox-filter-for-custom-queries-20535.patch
passerelle/apps/opengis/models.py | ||
---|---|---|
376 | 376 |
description=_('Query'), |
377 | 377 |
pattern=r'^(?P<query_slug>[\w:_-]+)/$', |
378 | 378 |
perm='can_access', |
379 |
parameters={ |
|
380 |
'bbox': { |
|
381 |
'description': _('Only include results inside bounding box'), |
|
382 |
'example_value': '-0.489,51.28,0.236,51.686' |
|
383 |
} |
|
384 |
}, |
|
379 | 385 |
show=False) |
380 |
def query(self, request, query_slug): |
|
386 |
def query(self, request, query_slug, bbox=None):
|
|
381 | 387 |
query = get_object_or_404(Query, resource=self, slug=query_slug) |
382 |
return query.q(request) |
|
388 |
return query.q(request, bbox)
|
|
383 | 389 | |
384 | 390 |
def export_json(self): |
385 | 391 |
d = super(OpenGIS, self).export_json() |
... | ... | |
448 | 454 |
endpoint.parameters = copy.deepcopy(resource_endpoint.parameters) |
449 | 455 |
return endpoint |
450 | 456 | |
451 |
def q(self, request): |
|
457 |
def q(self, request, bbox):
|
|
452 | 458 |
features = self.features.all() |
453 | 459 |
if not features.exists(): |
454 | 460 |
raise APIError('Data is not synchronized yet. Retry in a few minutes.') |
461 |
if bbox: |
|
462 |
try: |
|
463 |
lonmin, latmin, lonmax, latmax = (float(x) for x in bbox.split(',')) |
|
464 |
except (ValueError, AttributeError): |
|
465 |
raise APIError('Invalid bbox parameter, it must be a comma separated list of ' |
|
466 |
'floating point numbers of the form lonmin,latmin,lonmax,latmax') |
|
467 |
features = features.filter(lon__gte=lonmin, lon__lte=lonmax, lat__gte=latmin, |
|
468 |
lat__lte=latmax) |
|
455 | 469 |
data = { |
456 | 470 |
'type': 'FeatureCollection', |
457 | 471 |
'name': self.typename |
tests/test_opengis.py | ||
---|---|---|
552 | 552 |
if feature['geometry']['coordinates'][0] == 1914059.51) |
553 | 553 |
assert feature_data == feature.data |
554 | 554 | |
555 |
resp = app.get(endpoint + '?bbox=1914041,4224660,1914060,4224670') |
|
556 |
assert len(resp.json['features']) == 1 |
|
557 | ||
558 |
resp = app.get(endpoint + '?bbox=wrong') |
|
559 |
assert resp.json['err'] == 1 |
|
560 | ||
555 | 561 | |
556 | 562 |
@mock.patch('passerelle.utils.Request.get') |
557 | 563 |
def test_opengis_query_cache_update_change(mocked_get, app, connector, query): |
... | ... | |
635 | 641 |
resp = app.get(connector.get_absolute_url()) |
636 | 642 |
assert query.name in resp.text |
637 | 643 |
assert query.description in resp.text |
638 |
assert '/opengis/test/q/test_query/' in resp.text
|
|
644 |
assert '/opengis/test/query/test_query/?bbox=' in resp.text
|
|
639 | 645 | |
640 | 646 | |
641 | 647 |
def test_opengis_export_import(query): |
642 |
- |