0006-opengis-add-bbox-filter-for-custom-queries-20535.patch
passerelle/apps/opengis/models.py | ||
---|---|---|
361 | 361 |
description=_('Query'), |
362 | 362 |
pattern=r'^(?P<query_slug>[\w:_-]+)/$', |
363 | 363 |
perm='can_access', |
364 |
parameters={ |
|
365 |
'bbox': { |
|
366 |
'description': _('Only include results inside bounding box'), |
|
367 |
'example_value': '-0.489,51.28,0.236,51.686' |
|
368 |
} |
|
369 |
}, |
|
364 | 370 |
show=False) |
365 |
def q(self, request, query_slug): |
|
371 |
def q(self, request, query_slug, bbox=None):
|
|
366 | 372 |
query = get_object_or_404(Query, resource=self, slug=query_slug) |
367 |
return query.q(request) |
|
373 |
return query.q(request, bbox)
|
|
368 | 374 | |
369 | 375 |
def export_json(self): |
370 | 376 |
d = super(OpenGIS, self).export_json() |
... | ... | |
434 | 440 |
endpoint.exclude_params = list(self.endpoint.endpoint_info.parameters) |
435 | 441 |
return endpoint |
436 | 442 | |
437 |
def q(self, request): |
|
443 |
def q(self, request, bbox):
|
|
438 | 444 |
features = self.features.all() |
439 | 445 |
if not features.exists(): |
440 | 446 |
raise APIError('Data is not synchronized yet. Retry in a few minutes.') |
447 |
if bbox: |
|
448 |
try: |
|
449 |
lonmin, latmin, lonmax, latmax = (float(x) for x in bbox.split(',')) |
|
450 |
except (ValueError, AttributeError): |
|
451 |
raise APIError('Invalid bbox parameter, it must be a comma separated list of ' |
|
452 |
'floating point numbers of the form lonmin,latmin,lonmax,latmax') |
|
453 |
features = features.filter(lon__gte=lonmin, lon__lte=lonmax, lat__gte=latmin, |
|
454 |
lat__lte=latmax) |
|
441 | 455 |
data = { |
442 | 456 |
'type': 'FeatureCollection', |
443 | 457 |
'name': self.typename |
tests/test_opengis.py | ||
---|---|---|
527 | 527 |
if feature['geometry']['coordinates'][0] == 1914059.51) |
528 | 528 |
assert feature_data == feature.data |
529 | 529 | |
530 |
resp = app.get(endpoint + '?bbox=1914041,4224660,1914060,4224670') |
|
531 |
assert len(resp.json['features']) == 1 |
|
532 | ||
533 |
resp = app.get(endpoint + '?bbox=wrong') |
|
534 |
assert resp.json['err'] == 1 |
|
535 | ||
530 | 536 | |
531 | 537 |
@mock.patch('passerelle.utils.Request.get') |
532 | 538 |
def test_opengis_query_cache_update_change(mocked_get, app, connector, query): |
... | ... | |
610 | 616 |
resp = app.get(connector.get_absolute_url()) |
611 | 617 |
assert query.name in resp.text |
612 | 618 |
assert query.description in resp.text |
613 |
assert '/opengis/test/q/test_query/' in resp.text |
|
619 |
assert '/opengis/test/q/test_query/?bbox=' in resp.text
|
|
614 | 620 | |
615 | 621 | |
616 | 622 |
def test_opengis_export_import(query): |
617 |
- |