Skip to content

Commit

Permalink
ADD: Optional choice to add a limit to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
neo-garaix committed Nov 7, 2024
1 parent b444c90 commit 08867ce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lizmap_server/expression_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ def virtualFields(params: Dict[str, str], response: QgsServerResponse, project:
FILTER=An expression to filter layer
FIELDS=list of requested field separated by comma
WITH_GEOMETRY=False
LIMIT=number of features to return or nothing to return all
"""
logger = Logger()
layer_name = params.get('LAYER', '')
Expand Down Expand Up @@ -865,6 +866,16 @@ def virtualFields(params: Dict[str, str], response: QgsServerResponse, project:

req = QgsFeatureRequest()

# set limit
req_limit = params.get('LIMIT', '-1')
try:
req.setLimit(int(req_limit))
except ValueError:
raise ExpressionServiceError(
"Bad request error",
f"Invalid LIMIT for 'VirtualFields': \"{req_limit}\"",
400)

# get filter
req_filter = params.get('FILTER', '')
if req_filter:
Expand Down
36 changes: 36 additions & 0 deletions test/test_expression_service_virtualfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,39 @@ def test_request_with_filter_fields_geometry(client):
assert b['features'][0]['properties']['a'] == 1
assert 'b' in b['features'][0]['properties']
assert b['features'][0]['properties']['b'] == 2

def test_request_limit(client):
""" Test Expression VirtualFields request
"""
projectfile = "france_parts.qgs"

# Make a request
qs = "?SERVICE=EXPRESSION&REQUEST=VirtualFields&MAP=france_parts.qgs&LAYER=france_parts"
qs += "&VIRTUALS={\"a\":\"%s\", \"b\":\"%s\"}" % (
quote('1', safe=''), quote('1 + 1', safe=''))
qs += "&LIMIT=2"
rv = client.get(qs, projectfile)
assert rv.status_code == 200
assert rv.headers.get('Content-Type', '').find('application/json') == 0

b = json.loads(rv.content.decode('utf-8'))
assert 'type' in b
assert b['type'] == 'FeatureCollection'

assert 'features' in b
assert len(b['features']) == 2

assert 'type' in b['features'][0]
assert b['features'][0]['type'] == 'Feature'

assert 'geometry' in b['features'][0]
assert b['features'][0]['geometry'] is None

assert 'properties' in b['features'][0]
assert 'NAME_1' in b['features'][0]['properties']
assert 'Region' in b['features'][0]['properties']

assert 'a' in b['features'][0]['properties']
assert b['features'][0]['properties']['a'] == 1
assert 'b' in b['features'][0]['properties']
assert b['features'][0]['properties']['b'] == 2

0 comments on commit 08867ce

Please sign in to comment.