pygeobox 1.0.1__py3-none-any.whl → 1.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pygeobox/__init__.py +63 -63
- pygeobox/api.py +2517 -2517
- pygeobox/apikey.py +232 -232
- pygeobox/attachment.py +297 -297
- pygeobox/base.py +364 -364
- pygeobox/basemap.py +162 -162
- pygeobox/dashboard.py +316 -316
- pygeobox/enums.py +354 -354
- pygeobox/exception.py +47 -47
- pygeobox/field.py +309 -309
- pygeobox/map.py +858 -858
- pygeobox/model3d.py +334 -334
- pygeobox/mosaic.py +647 -647
- pygeobox/plan.py +260 -260
- pygeobox/query.py +668 -668
- pygeobox/raster.py +756 -756
- pygeobox/route.py +63 -63
- pygeobox/scene.py +317 -317
- pygeobox/settings.py +165 -165
- pygeobox/task.py +354 -354
- pygeobox/tile3d.py +294 -294
- pygeobox/tileset.py +585 -585
- pygeobox/user.py +423 -423
- pygeobox/utils.py +43 -43
- pygeobox/vectorlayer.py +1149 -1149
- pygeobox/version.py +248 -248
- pygeobox/view.py +859 -859
- pygeobox/workflow.py +315 -315
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/METADATA +18 -33
- pygeobox-1.0.3.dist-info/RECORD +37 -0
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/licenses/LICENSE +21 -21
- pygeobox-1.0.1.dist-info/RECORD +0 -37
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/WHEEL +0 -0
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/top_level.txt +0 -0
pygeobox/query.py
CHANGED
@@ -1,668 +1,668 @@
|
|
1
|
-
from urllib.parse import urljoin
|
2
|
-
from typing import Dict, List, TYPE_CHECKING, Union
|
3
|
-
|
4
|
-
from .utils import clean_data
|
5
|
-
from .base import Base
|
6
|
-
from .task import Task
|
7
|
-
from .enums import QueryResultType, QueryGeometryType, QueryParamType
|
8
|
-
|
9
|
-
if TYPE_CHECKING:
|
10
|
-
from . import GeoboxClient
|
11
|
-
from .user import User
|
12
|
-
|
13
|
-
class Query(Base):
|
14
|
-
"""
|
15
|
-
A class to interact with queries in GeoBox.
|
16
|
-
"""
|
17
|
-
BASE_ENDPOINT: str = 'queries/'
|
18
|
-
|
19
|
-
def __init__(self,
|
20
|
-
api: 'GeoboxClient',
|
21
|
-
uuid: str = None,
|
22
|
-
data: Dict = {}):
|
23
|
-
"""
|
24
|
-
Constructs all the necessary attributes for the Query object.
|
25
|
-
|
26
|
-
Args:
|
27
|
-
api (Api): The API instance.
|
28
|
-
uuid (str): The UUID of the query.
|
29
|
-
data (dict, optional): The data of the query.
|
30
|
-
"""
|
31
|
-
self.result = {}
|
32
|
-
self._system_query = False
|
33
|
-
super().__init__(api, uuid=uuid, data=data)
|
34
|
-
|
35
|
-
|
36
|
-
def _check_access(self) -> None:
|
37
|
-
"""
|
38
|
-
Check if the query is a system query.
|
39
|
-
|
40
|
-
Returns:
|
41
|
-
None
|
42
|
-
|
43
|
-
Raises:
|
44
|
-
PermissionError: If the query is a read-only system query.
|
45
|
-
"""
|
46
|
-
if self._system_query:
|
47
|
-
raise PermissionError("Cannot modify system queries - they are read-only")
|
48
|
-
|
49
|
-
|
50
|
-
@property
|
51
|
-
def sql(self) -> str:
|
52
|
-
"""
|
53
|
-
Get the SQL of the query.
|
54
|
-
|
55
|
-
Returns:
|
56
|
-
str: The SQL of the query.
|
57
|
-
|
58
|
-
Example:
|
59
|
-
>>> from geobox import GeoboxClient
|
60
|
-
>>> from geobox.query import Query
|
61
|
-
>>> client = GeoboxClient()
|
62
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
63
|
-
>>> query.sql
|
64
|
-
'SELECT * FROM some_layer'
|
65
|
-
"""
|
66
|
-
return self.data['sql']
|
67
|
-
|
68
|
-
|
69
|
-
@sql.setter
|
70
|
-
def sql(self, value: str) -> None:
|
71
|
-
"""
|
72
|
-
Set the SQL of the query.
|
73
|
-
|
74
|
-
Args:
|
75
|
-
value (str): The SQL of the query.
|
76
|
-
|
77
|
-
Returns:
|
78
|
-
None
|
79
|
-
|
80
|
-
Example:
|
81
|
-
>>> from geobox import GeoboxClient
|
82
|
-
>>> from geobox.query import Query
|
83
|
-
>>> client = GeoboxClient()
|
84
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
85
|
-
>>> query.sql = 'SELECT * FROM some_layer'
|
86
|
-
"""
|
87
|
-
self.data['sql'] = value
|
88
|
-
|
89
|
-
|
90
|
-
@property
|
91
|
-
def params(self) -> List[Dict]:
|
92
|
-
"""
|
93
|
-
Get the parameters of the query.
|
94
|
-
|
95
|
-
Returns:
|
96
|
-
List[Dict]: The parameters of the query.
|
97
|
-
|
98
|
-
Example:
|
99
|
-
>>> from geobox import GeoboxClient
|
100
|
-
>>> from geobox.query import Query
|
101
|
-
>>> client = GeoboxClient()
|
102
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
103
|
-
>>> query.params
|
104
|
-
[{'name': 'layer', 'value': '12345678-1234-5678-1234-567812345678', 'type': 'Layer'}]
|
105
|
-
"""
|
106
|
-
if not isinstance(self.data.get('params'), list):
|
107
|
-
self.data['params'] = []
|
108
|
-
|
109
|
-
return self.data['params']
|
110
|
-
|
111
|
-
|
112
|
-
@params.setter
|
113
|
-
def params(self, value: Dict) -> None:
|
114
|
-
"""
|
115
|
-
Set the parameters of the query.
|
116
|
-
|
117
|
-
Args:
|
118
|
-
value (Dict): The parameters of the query.
|
119
|
-
|
120
|
-
Returns:
|
121
|
-
None
|
122
|
-
|
123
|
-
Example:
|
124
|
-
>>> from geobox import GeoboxClient
|
125
|
-
>>> from geobox.query import Query
|
126
|
-
>>> client = GeoboxClient()
|
127
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
128
|
-
>>> query.params = [{'name': 'layer', 'value': '12345678-1234-5678-1234-567812345678', 'type': 'Layer'}]
|
129
|
-
"""
|
130
|
-
if not isinstance(self.data.get('params'), list):
|
131
|
-
self.data['params'] = []
|
132
|
-
|
133
|
-
self.data['params'] = value
|
134
|
-
|
135
|
-
|
136
|
-
@classmethod
|
137
|
-
def get_queries(cls, api: 'GeoboxClient', **kwargs) -> Union[List['Query'], int]:
|
138
|
-
"""
|
139
|
-
Get Queries
|
140
|
-
|
141
|
-
Args:
|
142
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
143
|
-
|
144
|
-
Keyword Args:
|
145
|
-
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
|
146
|
-
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
147
|
-
search_fields (str): comma separated list of fields for searching
|
148
|
-
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
149
|
-
return_count (bool): Whether to return total count. default is False.
|
150
|
-
skip (int): Number of queries to skip. default is 0.
|
151
|
-
limit(int): Maximum number of queries to return. default is 10.
|
152
|
-
user_id (int): Specific user. privileges required.
|
153
|
-
shared (bool): Whether to return shared queries. default is False.
|
154
|
-
|
155
|
-
Returns:
|
156
|
-
List[Query] | int: list of queries or the number of queries.
|
157
|
-
|
158
|
-
Example:
|
159
|
-
>>> from geobox import GeoboxClient
|
160
|
-
>>> from geobox.query import Query
|
161
|
-
>>> client = GeoboxClient()
|
162
|
-
>>> queries = Query.get_queries(client)
|
163
|
-
or
|
164
|
-
>>> queries = client.get_queries()
|
165
|
-
"""
|
166
|
-
params = {
|
167
|
-
'f': 'json',
|
168
|
-
'q': kwargs.get('q'),
|
169
|
-
'search': kwargs.get('search'),
|
170
|
-
'search_field': kwargs.get('search_field'),
|
171
|
-
'order_by': kwargs.get('order_by'),
|
172
|
-
'return_count': kwargs.get('return_count', False),
|
173
|
-
'skip': kwargs.get('skip', 0),
|
174
|
-
'limit': kwargs.get('limit', 10),
|
175
|
-
'user_id': kwargs.get('user_id'),
|
176
|
-
'shared': kwargs.get('shared', False)
|
177
|
-
}
|
178
|
-
return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: Query(api, item['uuid'], item))
|
179
|
-
|
180
|
-
|
181
|
-
@classmethod
|
182
|
-
def create_query(cls, api: 'GeoboxClient', name: str, display_name: str = None, sql: str = None, params: List = None) -> 'Query':
|
183
|
-
"""
|
184
|
-
Creates a new query.
|
185
|
-
|
186
|
-
Args:
|
187
|
-
api (Api): The GeoboxClient instance for making requests.
|
188
|
-
name (str): The name of the query.
|
189
|
-
display_name (str, optional): The display name of the query.
|
190
|
-
sql (str, optional): The SQL statement for the query.
|
191
|
-
params (list, optional): The parameters for the SQL statement.
|
192
|
-
|
193
|
-
Returns:
|
194
|
-
Query: The created query instance.
|
195
|
-
|
196
|
-
Example:
|
197
|
-
>>> from geobox import GeoboxClient
|
198
|
-
>>> from geobox.query import Query
|
199
|
-
>>> client = GeoboxClient()
|
200
|
-
>>> query = Query.create_query(client, name='query_name', display_name='Query Name', sql='SELECT * FROM some_layer')
|
201
|
-
or
|
202
|
-
>>> query = client.create_query(name='query_name', display_name='Query Name', sql='SELECT * FROM some_layer')
|
203
|
-
"""
|
204
|
-
data = {
|
205
|
-
"name": name,
|
206
|
-
"display_name": display_name,
|
207
|
-
"sql": sql,
|
208
|
-
"params": params
|
209
|
-
}
|
210
|
-
return super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: Query(api, item['uuid'], item))
|
211
|
-
|
212
|
-
|
213
|
-
@classmethod
|
214
|
-
def get_query(cls, api: 'GeoboxClient', uuid: str, user_id: int = None) -> 'Query':
|
215
|
-
"""
|
216
|
-
Retrieves a query by its UUID.
|
217
|
-
|
218
|
-
Args:
|
219
|
-
api (Api): The GeoboxClient instance for making requests.
|
220
|
-
uuid (str): The UUID of the query.
|
221
|
-
user_id (int, optional): specific user ID. privileges required.
|
222
|
-
|
223
|
-
Returns:
|
224
|
-
Query: The retrieved query instance.
|
225
|
-
|
226
|
-
Example:
|
227
|
-
>>> from geobox import GeoboxClient
|
228
|
-
>>> from geobox.query import Query
|
229
|
-
>>> client = GeoboxClient()
|
230
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
231
|
-
or
|
232
|
-
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
233
|
-
"""
|
234
|
-
params = {
|
235
|
-
'f': 'json',
|
236
|
-
'user_id': user_id
|
237
|
-
}
|
238
|
-
return super()._get_detail(api, cls.BASE_ENDPOINT, uuid, params, factory_func=lambda api, item: Query(api, item['uuid'], item))
|
239
|
-
|
240
|
-
|
241
|
-
@classmethod
|
242
|
-
def get_query_by_name(cls, api: 'GeoboxClient', name: str, user_id: int = None) -> Union['Query', None]:
|
243
|
-
"""
|
244
|
-
Get a query by name
|
245
|
-
|
246
|
-
Args:
|
247
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
248
|
-
name (str): the name of the query to get
|
249
|
-
user_id (int, optional): specific user. privileges required.
|
250
|
-
|
251
|
-
Returns:
|
252
|
-
Query | None: returns the query if a query matches the given name, else None
|
253
|
-
|
254
|
-
Example:
|
255
|
-
>>> from geobox import GeoboxClient
|
256
|
-
>>> from geobox.query import Query
|
257
|
-
>>> client = GeoboxClient()
|
258
|
-
>>> query = Query.get_query_by_name(client, name='test')
|
259
|
-
or
|
260
|
-
>>> query = client.get_query_by_name(name='test')
|
261
|
-
"""
|
262
|
-
queries = cls.get_queries(api, q=f"name = '{name}'", user_id=user_id)
|
263
|
-
if queries and queries[0].name == name:
|
264
|
-
return queries[0]
|
265
|
-
else:
|
266
|
-
return None
|
267
|
-
|
268
|
-
|
269
|
-
@classmethod
|
270
|
-
def get_system_queries(cls, api: 'GeoboxClient', **kwargs) -> List['Query']:
|
271
|
-
"""
|
272
|
-
Returns the system queries as a list of Query objects.
|
273
|
-
|
274
|
-
Args:
|
275
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
276
|
-
|
277
|
-
Keyword Args:
|
278
|
-
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'".
|
279
|
-
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
280
|
-
search_fields (str): comma separated list of fields for searching.
|
281
|
-
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
282
|
-
return_count (bool): whether to return the total count of queries. default is False.
|
283
|
-
skip (int): number of queries to skip. minimum is 0. default is 0.
|
284
|
-
limit (int): number of queries to return. minimum is 1. default is 100.
|
285
|
-
user_id (int): specific user. privileges required.
|
286
|
-
shared (bool): whether to return shared queries. default is False.
|
287
|
-
|
288
|
-
Returns:
|
289
|
-
List[Query]: list of system queries.
|
290
|
-
|
291
|
-
Example:
|
292
|
-
>>> from geobox import GeoboxClient
|
293
|
-
>>> from geobox.query import Query
|
294
|
-
>>> client = GeoboxClient()
|
295
|
-
>>> queries = Query.get_system_queries(client)
|
296
|
-
or
|
297
|
-
>>> queries = client.get_system_queries()
|
298
|
-
"""
|
299
|
-
params = {
|
300
|
-
'f': 'json',
|
301
|
-
'q': kwargs.get('q'),
|
302
|
-
'search': kwargs.get('search'),
|
303
|
-
'search_fields': kwargs.get('search_fields'),
|
304
|
-
'order_by': kwargs.get('order_by'),
|
305
|
-
'return_count': kwargs.get('return_count', False),
|
306
|
-
'skip': kwargs.get('skip', 0),
|
307
|
-
'limit': kwargs.get('limit', 100),
|
308
|
-
'user_id': kwargs.get('user_id'),
|
309
|
-
'shared': kwargs.get('shared', False)
|
310
|
-
}
|
311
|
-
endpoint = urljoin(cls.BASE_ENDPOINT, 'systemQueries/')
|
312
|
-
def factory_func(api, item):
|
313
|
-
query = Query(api, item['uuid'], item)
|
314
|
-
query._system_query = True
|
315
|
-
return query
|
316
|
-
|
317
|
-
return super()._get_list(api, endpoint, params, factory_func=factory_func)
|
318
|
-
|
319
|
-
|
320
|
-
def add_param(self, name: str, value: str, type: 'QueryParamType', default_value: str = None, Domain: Dict = None) -> None:
|
321
|
-
"""
|
322
|
-
Add a parameter to the query parameters.
|
323
|
-
|
324
|
-
Args:
|
325
|
-
name (str): The name of the parameter.
|
326
|
-
value (str): The value of the parameter.
|
327
|
-
type (str): The type of the parameter (default: 'Layer').
|
328
|
-
default_value (str, optional): The default value for the parameter.
|
329
|
-
Domain (Dict, optional): Domain information for the parameter.
|
330
|
-
|
331
|
-
Returns:
|
332
|
-
None
|
333
|
-
|
334
|
-
Raises:
|
335
|
-
PermissionError: If the query is a read-only system query.
|
336
|
-
|
337
|
-
Example:
|
338
|
-
>>> from geobox import GeoboxClient
|
339
|
-
>>> from geobox.query import Query
|
340
|
-
>>> client = GeoboxClient()
|
341
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
342
|
-
or
|
343
|
-
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
344
|
-
>>> query.add_param(name='param_name', value='param_value', type=QueryParamType.LAYER)
|
345
|
-
"""
|
346
|
-
self._check_access()
|
347
|
-
|
348
|
-
self.params.append({
|
349
|
-
'name': name,
|
350
|
-
'value': value,
|
351
|
-
'type': type.value,
|
352
|
-
'default_value': default_value,
|
353
|
-
'Domain': Domain
|
354
|
-
})
|
355
|
-
|
356
|
-
|
357
|
-
def remove_param(self, name: str) -> None:
|
358
|
-
"""
|
359
|
-
Remove a parameter from the query parameters by name.
|
360
|
-
|
361
|
-
Args:
|
362
|
-
name (str): The name of the parameter to remove.
|
363
|
-
|
364
|
-
Returns:
|
365
|
-
None
|
366
|
-
|
367
|
-
Raises:
|
368
|
-
ValueError: If the parameter is not found in query parameters.
|
369
|
-
PermissionError: If the query is a read-only system query.
|
370
|
-
|
371
|
-
Example:
|
372
|
-
>>> from geobox import GeoboxClient
|
373
|
-
>>> from geobox.query import Query
|
374
|
-
>>> client = GeoboxClient()
|
375
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
376
|
-
or
|
377
|
-
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
378
|
-
>>> query.remove_param(name='param_name')
|
379
|
-
"""
|
380
|
-
self._check_access()
|
381
|
-
|
382
|
-
for i, param in enumerate(self.params):
|
383
|
-
if param.get('name') == name:
|
384
|
-
self.params.pop(i)
|
385
|
-
return
|
386
|
-
|
387
|
-
raise ValueError(f"Parameter with name '{name}' not found in query parameters")
|
388
|
-
|
389
|
-
|
390
|
-
def execute(self,
|
391
|
-
f: str = "json",
|
392
|
-
result_type: QueryResultType = QueryResultType.both,
|
393
|
-
return_count: bool = None,
|
394
|
-
out_srid: int = None,
|
395
|
-
quant_factor: int = 1000000,
|
396
|
-
bbox_srid: int = None,
|
397
|
-
skip: int = None,
|
398
|
-
limit: int = None,
|
399
|
-
skip_geometry: bool = False) -> Union[Dict, int]:
|
400
|
-
"""
|
401
|
-
Executes a query with the given SQL statement and parameters.
|
402
|
-
|
403
|
-
Args:
|
404
|
-
f (str): the output format of the executed query. options are: json, topojson. default is json.
|
405
|
-
result_type (QueryResultType, optional): The type of result to return (default is "both").
|
406
|
-
return_count (bool, optional): Whether to return the count of results.
|
407
|
-
out_srid (int, optional): The output spatial reference ID.
|
408
|
-
quant_factor (int, optional): The quantization factor (default is 1000000).
|
409
|
-
bbox_srid (int, optional): The bounding box spatial reference ID.
|
410
|
-
skip (int, optional): The number of results to skip.
|
411
|
-
limit (int, optional): The maximum number of results to return.
|
412
|
-
skip_geometry (bool): Whether to skip the geometry part of the features or not. default is False.
|
413
|
-
|
414
|
-
Returns:
|
415
|
-
Dict | int: The result of the query execution or the count number of the result
|
416
|
-
|
417
|
-
Example:
|
418
|
-
>>> from geobox import GeoboxClient
|
419
|
-
>>> from geobox.query import Query
|
420
|
-
>>> client = GeoboxClient()
|
421
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
422
|
-
or
|
423
|
-
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
424
|
-
>>> query.execute(f='json')
|
425
|
-
"""
|
426
|
-
self._check_access()
|
427
|
-
|
428
|
-
if not self.sql:
|
429
|
-
raise ValueError('"sql" parameter is required for this action!')
|
430
|
-
if not self.params:
|
431
|
-
raise ValueError('"params" parameter is required for this action!')
|
432
|
-
|
433
|
-
data = clean_data({
|
434
|
-
"f": f if f in ['json', 'topojson'] else None,
|
435
|
-
"sql": self.sql,
|
436
|
-
"params": self.params,
|
437
|
-
"result_type": result_type.value,
|
438
|
-
"return_count": return_count,
|
439
|
-
"out_srid": out_srid,
|
440
|
-
"quant_factor": quant_factor,
|
441
|
-
"bbox_srid": bbox_srid,
|
442
|
-
"skip": skip,
|
443
|
-
"limit": limit,
|
444
|
-
"skip_geometry": skip_geometry
|
445
|
-
})
|
446
|
-
|
447
|
-
endpoint = urljoin(self.BASE_ENDPOINT, 'exec/')
|
448
|
-
self.result = self.api.post(endpoint, data)
|
449
|
-
return self.result
|
450
|
-
|
451
|
-
|
452
|
-
def update(self, **kwargs) -> Dict:
|
453
|
-
"""
|
454
|
-
Updates the query with new data.
|
455
|
-
|
456
|
-
Keyword Args:
|
457
|
-
name (str): The new name of the query.
|
458
|
-
display_name (str): The new display name of the query.
|
459
|
-
sql (str): The new SQL statement for the query.
|
460
|
-
params (list): The new parameters for the SQL statement.
|
461
|
-
|
462
|
-
Returns:
|
463
|
-
Dict: The updated query data.
|
464
|
-
|
465
|
-
Raises:
|
466
|
-
PermissionError: If the query is a read-only system query.
|
467
|
-
|
468
|
-
Example:
|
469
|
-
>>> from geobox import GeoboxClient
|
470
|
-
>>> from geobox.query import Query
|
471
|
-
>>> client = GeoboxClient()
|
472
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
473
|
-
or
|
474
|
-
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
475
|
-
>>> query.update(name='new_name')
|
476
|
-
"""
|
477
|
-
self._check_access()
|
478
|
-
|
479
|
-
data = {
|
480
|
-
"name": kwargs.get('name'),
|
481
|
-
"display_name": kwargs.get('display_name'),
|
482
|
-
"sql": kwargs.get('sql'),
|
483
|
-
"params": kwargs.get('params')
|
484
|
-
}
|
485
|
-
super()._update(self.endpoint, data)
|
486
|
-
|
487
|
-
|
488
|
-
def save(self) -> None:
|
489
|
-
"""
|
490
|
-
Save the query. Creates a new query if query uuid is None, updates existing query otherwise.
|
491
|
-
|
492
|
-
Returns:
|
493
|
-
None
|
494
|
-
|
495
|
-
Example:
|
496
|
-
>>> from geobox import GeoboxClient
|
497
|
-
>>> from geobox.query import Query
|
498
|
-
>>> client = GeoboxClient()
|
499
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
500
|
-
>>> query.sql = "SELECT * FROM some_layer"
|
501
|
-
>>> query.save()
|
502
|
-
"""
|
503
|
-
self.params = [item for item in self.params if item.get('value')]
|
504
|
-
|
505
|
-
try:
|
506
|
-
if self.__getattr__('uuid'):
|
507
|
-
self.update(name=self.data['name'], display_name=self.data['display_name'], sql=self.sql, params=self.params)
|
508
|
-
except AttributeError:
|
509
|
-
response = self.api.post(self.BASE_ENDPOINT, self.data)
|
510
|
-
self.endpoint = urljoin(self.BASE_ENDPOINT, f'{response["uuid"]}/')
|
511
|
-
self.data.update(response)
|
512
|
-
|
513
|
-
|
514
|
-
def delete(self) -> str:
|
515
|
-
"""
|
516
|
-
Deletes a query.
|
517
|
-
|
518
|
-
Returns:
|
519
|
-
str: The response from the API.
|
520
|
-
|
521
|
-
Raises:
|
522
|
-
PermissionError: If the query is a read-only system query
|
523
|
-
|
524
|
-
Example:
|
525
|
-
>>> from geobox import GeoboxClient
|
526
|
-
>>> from geobox.query import Query
|
527
|
-
>>> client = GeoboxClient()
|
528
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
529
|
-
>>> query.delete()
|
530
|
-
"""
|
531
|
-
self._check_access()
|
532
|
-
super().delete(self.endpoint)
|
533
|
-
|
534
|
-
|
535
|
-
def share(self, users: List['User']) -> None:
|
536
|
-
"""
|
537
|
-
Shares the query with specified users.
|
538
|
-
|
539
|
-
Args:
|
540
|
-
users (List[User]): The list of user objects to share the query with.
|
541
|
-
|
542
|
-
Returns:
|
543
|
-
None
|
544
|
-
|
545
|
-
Raises:
|
546
|
-
PermissionError: If the query is a read-only system query.
|
547
|
-
|
548
|
-
Example:
|
549
|
-
>>> from geobox import GeoboxClient
|
550
|
-
>>> from geobox.query import Query
|
551
|
-
>>> client = GeoboxClient()
|
552
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
553
|
-
>>> users = client.search_users(search="John")
|
554
|
-
>>> query.share(users=users)
|
555
|
-
"""
|
556
|
-
self._check_access()
|
557
|
-
super()._share(self.endpoint, users)
|
558
|
-
|
559
|
-
|
560
|
-
def unshare(self, users: List['User']) -> None:
|
561
|
-
"""
|
562
|
-
Unshares the query with specified users.
|
563
|
-
|
564
|
-
Args:
|
565
|
-
users (List[User]): The list of user objects to unshare the query with.
|
566
|
-
|
567
|
-
Returns:
|
568
|
-
None
|
569
|
-
|
570
|
-
Raises:
|
571
|
-
PermissionError: If the query is a read-only system query.
|
572
|
-
|
573
|
-
Example:
|
574
|
-
>>> from geobox import GeoboxClient
|
575
|
-
>>> from geobox.query import Query
|
576
|
-
>>> client = GeoboxClient()
|
577
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
578
|
-
>>> users = client.search_users(search="John")
|
579
|
-
>>> query.unshare(users=users)
|
580
|
-
"""
|
581
|
-
self._check_access()
|
582
|
-
super()._unshare(self.endpoint, users)
|
583
|
-
|
584
|
-
|
585
|
-
def get_shared_users(self, search: str, skip: int = 0, limit: int = 10) -> List['User']:
|
586
|
-
"""
|
587
|
-
Retrieves the list of users the query is shared with.
|
588
|
-
|
589
|
-
Args:
|
590
|
-
search (str, optional): the search query.
|
591
|
-
skip (int, optional): The number of users to skip.
|
592
|
-
limit (int, optional): The maximum number of users to retrieve.
|
593
|
-
|
594
|
-
Returns:
|
595
|
-
List[User]: The list of shared users.
|
596
|
-
|
597
|
-
Example:
|
598
|
-
>>> from geobox import GeoboxClient
|
599
|
-
>>> from geobox.query import Query
|
600
|
-
>>> client = GeoboxClient()
|
601
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
602
|
-
>>> users = client.search_users(search="John")
|
603
|
-
>>> query.get_shared_users(search='John', skip=0, limit=10)
|
604
|
-
"""
|
605
|
-
self._check_access()
|
606
|
-
params = {
|
607
|
-
'search': search,
|
608
|
-
'skip': skip,
|
609
|
-
'limit': limit
|
610
|
-
}
|
611
|
-
return super()._get_shared_users(self.endpoint, params)
|
612
|
-
|
613
|
-
|
614
|
-
@property
|
615
|
-
def thumbnail(self) -> str:
|
616
|
-
"""
|
617
|
-
Retrieves the thumbnail URL for the query.
|
618
|
-
|
619
|
-
Returns:
|
620
|
-
str: The thumbnail URL.
|
621
|
-
"""
|
622
|
-
self._check_access()
|
623
|
-
endpoint = f'{self.api.base_url}{self.endpoint}thumbnail.png'
|
624
|
-
return endpoint
|
625
|
-
|
626
|
-
|
627
|
-
def save_as_layer(self, layer_name: str, layer_type: 'QueryGeometryType') -> Dict:
|
628
|
-
"""
|
629
|
-
Saves the query as a new layer.
|
630
|
-
|
631
|
-
Args:
|
632
|
-
sql (str): The SQL statement for the query.
|
633
|
-
params (list): The parameters for the SQL statement.
|
634
|
-
layer_name (str): The name of the new layer.
|
635
|
-
layer_type (QueryGeometryType): The type of the new layer.
|
636
|
-
|
637
|
-
Returns:
|
638
|
-
Dict: The response from the API.
|
639
|
-
|
640
|
-
Raises:
|
641
|
-
PermissionError: If the query is a read-only system query.
|
642
|
-
|
643
|
-
Example:
|
644
|
-
>>> from geobox import GeoboxClient
|
645
|
-
>>> from geobox.query import Query
|
646
|
-
>>> client = GeoboxClient()
|
647
|
-
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
648
|
-
>>> query.save_as_layer(layer_name='test', layer_type=QueryGeometryType.POLYGON)
|
649
|
-
"""
|
650
|
-
self._check_access()
|
651
|
-
|
652
|
-
params = [{
|
653
|
-
"name": item.get('name'),
|
654
|
-
"type": item.get('type'),
|
655
|
-
"value": item.get('default_value') if not item.get('value') else item.get('value')
|
656
|
-
} for item in self.params]
|
657
|
-
|
658
|
-
data = clean_data({
|
659
|
-
"sql": self.sql,
|
660
|
-
"params": params,
|
661
|
-
"layer_name": layer_name,
|
662
|
-
"layer_type": layer_type.value
|
663
|
-
})
|
664
|
-
|
665
|
-
endpoint = urljoin(self.BASE_ENDPOINT, 'saveAsLayer/')
|
666
|
-
response = self.api.post(endpoint, data)
|
667
|
-
task = Task.get_task(self.api, response.get('task_id'))
|
668
|
-
return task
|
1
|
+
from urllib.parse import urljoin
|
2
|
+
from typing import Dict, List, TYPE_CHECKING, Union
|
3
|
+
|
4
|
+
from .utils import clean_data
|
5
|
+
from .base import Base
|
6
|
+
from .task import Task
|
7
|
+
from .enums import QueryResultType, QueryGeometryType, QueryParamType
|
8
|
+
|
9
|
+
if TYPE_CHECKING:
|
10
|
+
from . import GeoboxClient
|
11
|
+
from .user import User
|
12
|
+
|
13
|
+
class Query(Base):
|
14
|
+
"""
|
15
|
+
A class to interact with queries in GeoBox.
|
16
|
+
"""
|
17
|
+
BASE_ENDPOINT: str = 'queries/'
|
18
|
+
|
19
|
+
def __init__(self,
|
20
|
+
api: 'GeoboxClient',
|
21
|
+
uuid: str = None,
|
22
|
+
data: Dict = {}):
|
23
|
+
"""
|
24
|
+
Constructs all the necessary attributes for the Query object.
|
25
|
+
|
26
|
+
Args:
|
27
|
+
api (Api): The API instance.
|
28
|
+
uuid (str): The UUID of the query.
|
29
|
+
data (dict, optional): The data of the query.
|
30
|
+
"""
|
31
|
+
self.result = {}
|
32
|
+
self._system_query = False
|
33
|
+
super().__init__(api, uuid=uuid, data=data)
|
34
|
+
|
35
|
+
|
36
|
+
def _check_access(self) -> None:
|
37
|
+
"""
|
38
|
+
Check if the query is a system query.
|
39
|
+
|
40
|
+
Returns:
|
41
|
+
None
|
42
|
+
|
43
|
+
Raises:
|
44
|
+
PermissionError: If the query is a read-only system query.
|
45
|
+
"""
|
46
|
+
if self._system_query:
|
47
|
+
raise PermissionError("Cannot modify system queries - they are read-only")
|
48
|
+
|
49
|
+
|
50
|
+
@property
|
51
|
+
def sql(self) -> str:
|
52
|
+
"""
|
53
|
+
Get the SQL of the query.
|
54
|
+
|
55
|
+
Returns:
|
56
|
+
str: The SQL of the query.
|
57
|
+
|
58
|
+
Example:
|
59
|
+
>>> from geobox import GeoboxClient
|
60
|
+
>>> from geobox.query import Query
|
61
|
+
>>> client = GeoboxClient()
|
62
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
63
|
+
>>> query.sql
|
64
|
+
'SELECT * FROM some_layer'
|
65
|
+
"""
|
66
|
+
return self.data['sql']
|
67
|
+
|
68
|
+
|
69
|
+
@sql.setter
|
70
|
+
def sql(self, value: str) -> None:
|
71
|
+
"""
|
72
|
+
Set the SQL of the query.
|
73
|
+
|
74
|
+
Args:
|
75
|
+
value (str): The SQL of the query.
|
76
|
+
|
77
|
+
Returns:
|
78
|
+
None
|
79
|
+
|
80
|
+
Example:
|
81
|
+
>>> from geobox import GeoboxClient
|
82
|
+
>>> from geobox.query import Query
|
83
|
+
>>> client = GeoboxClient()
|
84
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
85
|
+
>>> query.sql = 'SELECT * FROM some_layer'
|
86
|
+
"""
|
87
|
+
self.data['sql'] = value
|
88
|
+
|
89
|
+
|
90
|
+
@property
|
91
|
+
def params(self) -> List[Dict]:
|
92
|
+
"""
|
93
|
+
Get the parameters of the query.
|
94
|
+
|
95
|
+
Returns:
|
96
|
+
List[Dict]: The parameters of the query.
|
97
|
+
|
98
|
+
Example:
|
99
|
+
>>> from geobox import GeoboxClient
|
100
|
+
>>> from geobox.query import Query
|
101
|
+
>>> client = GeoboxClient()
|
102
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
103
|
+
>>> query.params
|
104
|
+
[{'name': 'layer', 'value': '12345678-1234-5678-1234-567812345678', 'type': 'Layer'}]
|
105
|
+
"""
|
106
|
+
if not isinstance(self.data.get('params'), list):
|
107
|
+
self.data['params'] = []
|
108
|
+
|
109
|
+
return self.data['params']
|
110
|
+
|
111
|
+
|
112
|
+
@params.setter
|
113
|
+
def params(self, value: Dict) -> None:
|
114
|
+
"""
|
115
|
+
Set the parameters of the query.
|
116
|
+
|
117
|
+
Args:
|
118
|
+
value (Dict): The parameters of the query.
|
119
|
+
|
120
|
+
Returns:
|
121
|
+
None
|
122
|
+
|
123
|
+
Example:
|
124
|
+
>>> from geobox import GeoboxClient
|
125
|
+
>>> from geobox.query import Query
|
126
|
+
>>> client = GeoboxClient()
|
127
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
128
|
+
>>> query.params = [{'name': 'layer', 'value': '12345678-1234-5678-1234-567812345678', 'type': 'Layer'}]
|
129
|
+
"""
|
130
|
+
if not isinstance(self.data.get('params'), list):
|
131
|
+
self.data['params'] = []
|
132
|
+
|
133
|
+
self.data['params'] = value
|
134
|
+
|
135
|
+
|
136
|
+
@classmethod
|
137
|
+
def get_queries(cls, api: 'GeoboxClient', **kwargs) -> Union[List['Query'], int]:
|
138
|
+
"""
|
139
|
+
Get Queries
|
140
|
+
|
141
|
+
Args:
|
142
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
143
|
+
|
144
|
+
Keyword Args:
|
145
|
+
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
|
146
|
+
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
147
|
+
search_fields (str): comma separated list of fields for searching
|
148
|
+
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
149
|
+
return_count (bool): Whether to return total count. default is False.
|
150
|
+
skip (int): Number of queries to skip. default is 0.
|
151
|
+
limit(int): Maximum number of queries to return. default is 10.
|
152
|
+
user_id (int): Specific user. privileges required.
|
153
|
+
shared (bool): Whether to return shared queries. default is False.
|
154
|
+
|
155
|
+
Returns:
|
156
|
+
List[Query] | int: list of queries or the number of queries.
|
157
|
+
|
158
|
+
Example:
|
159
|
+
>>> from geobox import GeoboxClient
|
160
|
+
>>> from geobox.query import Query
|
161
|
+
>>> client = GeoboxClient()
|
162
|
+
>>> queries = Query.get_queries(client)
|
163
|
+
or
|
164
|
+
>>> queries = client.get_queries()
|
165
|
+
"""
|
166
|
+
params = {
|
167
|
+
'f': 'json',
|
168
|
+
'q': kwargs.get('q'),
|
169
|
+
'search': kwargs.get('search'),
|
170
|
+
'search_field': kwargs.get('search_field'),
|
171
|
+
'order_by': kwargs.get('order_by'),
|
172
|
+
'return_count': kwargs.get('return_count', False),
|
173
|
+
'skip': kwargs.get('skip', 0),
|
174
|
+
'limit': kwargs.get('limit', 10),
|
175
|
+
'user_id': kwargs.get('user_id'),
|
176
|
+
'shared': kwargs.get('shared', False)
|
177
|
+
}
|
178
|
+
return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: Query(api, item['uuid'], item))
|
179
|
+
|
180
|
+
|
181
|
+
@classmethod
|
182
|
+
def create_query(cls, api: 'GeoboxClient', name: str, display_name: str = None, sql: str = None, params: List = None) -> 'Query':
|
183
|
+
"""
|
184
|
+
Creates a new query.
|
185
|
+
|
186
|
+
Args:
|
187
|
+
api (Api): The GeoboxClient instance for making requests.
|
188
|
+
name (str): The name of the query.
|
189
|
+
display_name (str, optional): The display name of the query.
|
190
|
+
sql (str, optional): The SQL statement for the query.
|
191
|
+
params (list, optional): The parameters for the SQL statement.
|
192
|
+
|
193
|
+
Returns:
|
194
|
+
Query: The created query instance.
|
195
|
+
|
196
|
+
Example:
|
197
|
+
>>> from geobox import GeoboxClient
|
198
|
+
>>> from geobox.query import Query
|
199
|
+
>>> client = GeoboxClient()
|
200
|
+
>>> query = Query.create_query(client, name='query_name', display_name='Query Name', sql='SELECT * FROM some_layer')
|
201
|
+
or
|
202
|
+
>>> query = client.create_query(name='query_name', display_name='Query Name', sql='SELECT * FROM some_layer')
|
203
|
+
"""
|
204
|
+
data = {
|
205
|
+
"name": name,
|
206
|
+
"display_name": display_name,
|
207
|
+
"sql": sql,
|
208
|
+
"params": params
|
209
|
+
}
|
210
|
+
return super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: Query(api, item['uuid'], item))
|
211
|
+
|
212
|
+
|
213
|
+
@classmethod
|
214
|
+
def get_query(cls, api: 'GeoboxClient', uuid: str, user_id: int = None) -> 'Query':
|
215
|
+
"""
|
216
|
+
Retrieves a query by its UUID.
|
217
|
+
|
218
|
+
Args:
|
219
|
+
api (Api): The GeoboxClient instance for making requests.
|
220
|
+
uuid (str): The UUID of the query.
|
221
|
+
user_id (int, optional): specific user ID. privileges required.
|
222
|
+
|
223
|
+
Returns:
|
224
|
+
Query: The retrieved query instance.
|
225
|
+
|
226
|
+
Example:
|
227
|
+
>>> from geobox import GeoboxClient
|
228
|
+
>>> from geobox.query import Query
|
229
|
+
>>> client = GeoboxClient()
|
230
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
231
|
+
or
|
232
|
+
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
233
|
+
"""
|
234
|
+
params = {
|
235
|
+
'f': 'json',
|
236
|
+
'user_id': user_id
|
237
|
+
}
|
238
|
+
return super()._get_detail(api, cls.BASE_ENDPOINT, uuid, params, factory_func=lambda api, item: Query(api, item['uuid'], item))
|
239
|
+
|
240
|
+
|
241
|
+
@classmethod
|
242
|
+
def get_query_by_name(cls, api: 'GeoboxClient', name: str, user_id: int = None) -> Union['Query', None]:
|
243
|
+
"""
|
244
|
+
Get a query by name
|
245
|
+
|
246
|
+
Args:
|
247
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
248
|
+
name (str): the name of the query to get
|
249
|
+
user_id (int, optional): specific user. privileges required.
|
250
|
+
|
251
|
+
Returns:
|
252
|
+
Query | None: returns the query if a query matches the given name, else None
|
253
|
+
|
254
|
+
Example:
|
255
|
+
>>> from geobox import GeoboxClient
|
256
|
+
>>> from geobox.query import Query
|
257
|
+
>>> client = GeoboxClient()
|
258
|
+
>>> query = Query.get_query_by_name(client, name='test')
|
259
|
+
or
|
260
|
+
>>> query = client.get_query_by_name(name='test')
|
261
|
+
"""
|
262
|
+
queries = cls.get_queries(api, q=f"name = '{name}'", user_id=user_id)
|
263
|
+
if queries and queries[0].name == name:
|
264
|
+
return queries[0]
|
265
|
+
else:
|
266
|
+
return None
|
267
|
+
|
268
|
+
|
269
|
+
@classmethod
|
270
|
+
def get_system_queries(cls, api: 'GeoboxClient', **kwargs) -> List['Query']:
|
271
|
+
"""
|
272
|
+
Returns the system queries as a list of Query objects.
|
273
|
+
|
274
|
+
Args:
|
275
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
276
|
+
|
277
|
+
Keyword Args:
|
278
|
+
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'".
|
279
|
+
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
280
|
+
search_fields (str): comma separated list of fields for searching.
|
281
|
+
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
282
|
+
return_count (bool): whether to return the total count of queries. default is False.
|
283
|
+
skip (int): number of queries to skip. minimum is 0. default is 0.
|
284
|
+
limit (int): number of queries to return. minimum is 1. default is 100.
|
285
|
+
user_id (int): specific user. privileges required.
|
286
|
+
shared (bool): whether to return shared queries. default is False.
|
287
|
+
|
288
|
+
Returns:
|
289
|
+
List[Query]: list of system queries.
|
290
|
+
|
291
|
+
Example:
|
292
|
+
>>> from geobox import GeoboxClient
|
293
|
+
>>> from geobox.query import Query
|
294
|
+
>>> client = GeoboxClient()
|
295
|
+
>>> queries = Query.get_system_queries(client)
|
296
|
+
or
|
297
|
+
>>> queries = client.get_system_queries()
|
298
|
+
"""
|
299
|
+
params = {
|
300
|
+
'f': 'json',
|
301
|
+
'q': kwargs.get('q'),
|
302
|
+
'search': kwargs.get('search'),
|
303
|
+
'search_fields': kwargs.get('search_fields'),
|
304
|
+
'order_by': kwargs.get('order_by'),
|
305
|
+
'return_count': kwargs.get('return_count', False),
|
306
|
+
'skip': kwargs.get('skip', 0),
|
307
|
+
'limit': kwargs.get('limit', 100),
|
308
|
+
'user_id': kwargs.get('user_id'),
|
309
|
+
'shared': kwargs.get('shared', False)
|
310
|
+
}
|
311
|
+
endpoint = urljoin(cls.BASE_ENDPOINT, 'systemQueries/')
|
312
|
+
def factory_func(api, item):
|
313
|
+
query = Query(api, item['uuid'], item)
|
314
|
+
query._system_query = True
|
315
|
+
return query
|
316
|
+
|
317
|
+
return super()._get_list(api, endpoint, params, factory_func=factory_func)
|
318
|
+
|
319
|
+
|
320
|
+
def add_param(self, name: str, value: str, type: 'QueryParamType', default_value: str = None, Domain: Dict = None) -> None:
|
321
|
+
"""
|
322
|
+
Add a parameter to the query parameters.
|
323
|
+
|
324
|
+
Args:
|
325
|
+
name (str): The name of the parameter.
|
326
|
+
value (str): The value of the parameter.
|
327
|
+
type (str): The type of the parameter (default: 'Layer').
|
328
|
+
default_value (str, optional): The default value for the parameter.
|
329
|
+
Domain (Dict, optional): Domain information for the parameter.
|
330
|
+
|
331
|
+
Returns:
|
332
|
+
None
|
333
|
+
|
334
|
+
Raises:
|
335
|
+
PermissionError: If the query is a read-only system query.
|
336
|
+
|
337
|
+
Example:
|
338
|
+
>>> from geobox import GeoboxClient
|
339
|
+
>>> from geobox.query import Query
|
340
|
+
>>> client = GeoboxClient()
|
341
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
342
|
+
or
|
343
|
+
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
344
|
+
>>> query.add_param(name='param_name', value='param_value', type=QueryParamType.LAYER)
|
345
|
+
"""
|
346
|
+
self._check_access()
|
347
|
+
|
348
|
+
self.params.append({
|
349
|
+
'name': name,
|
350
|
+
'value': value,
|
351
|
+
'type': type.value,
|
352
|
+
'default_value': default_value,
|
353
|
+
'Domain': Domain
|
354
|
+
})
|
355
|
+
|
356
|
+
|
357
|
+
def remove_param(self, name: str) -> None:
|
358
|
+
"""
|
359
|
+
Remove a parameter from the query parameters by name.
|
360
|
+
|
361
|
+
Args:
|
362
|
+
name (str): The name of the parameter to remove.
|
363
|
+
|
364
|
+
Returns:
|
365
|
+
None
|
366
|
+
|
367
|
+
Raises:
|
368
|
+
ValueError: If the parameter is not found in query parameters.
|
369
|
+
PermissionError: If the query is a read-only system query.
|
370
|
+
|
371
|
+
Example:
|
372
|
+
>>> from geobox import GeoboxClient
|
373
|
+
>>> from geobox.query import Query
|
374
|
+
>>> client = GeoboxClient()
|
375
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
376
|
+
or
|
377
|
+
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
378
|
+
>>> query.remove_param(name='param_name')
|
379
|
+
"""
|
380
|
+
self._check_access()
|
381
|
+
|
382
|
+
for i, param in enumerate(self.params):
|
383
|
+
if param.get('name') == name:
|
384
|
+
self.params.pop(i)
|
385
|
+
return
|
386
|
+
|
387
|
+
raise ValueError(f"Parameter with name '{name}' not found in query parameters")
|
388
|
+
|
389
|
+
|
390
|
+
def execute(self,
|
391
|
+
f: str = "json",
|
392
|
+
result_type: QueryResultType = QueryResultType.both,
|
393
|
+
return_count: bool = None,
|
394
|
+
out_srid: int = None,
|
395
|
+
quant_factor: int = 1000000,
|
396
|
+
bbox_srid: int = None,
|
397
|
+
skip: int = None,
|
398
|
+
limit: int = None,
|
399
|
+
skip_geometry: bool = False) -> Union[Dict, int]:
|
400
|
+
"""
|
401
|
+
Executes a query with the given SQL statement and parameters.
|
402
|
+
|
403
|
+
Args:
|
404
|
+
f (str): the output format of the executed query. options are: json, topojson. default is json.
|
405
|
+
result_type (QueryResultType, optional): The type of result to return (default is "both").
|
406
|
+
return_count (bool, optional): Whether to return the count of results.
|
407
|
+
out_srid (int, optional): The output spatial reference ID.
|
408
|
+
quant_factor (int, optional): The quantization factor (default is 1000000).
|
409
|
+
bbox_srid (int, optional): The bounding box spatial reference ID.
|
410
|
+
skip (int, optional): The number of results to skip.
|
411
|
+
limit (int, optional): The maximum number of results to return.
|
412
|
+
skip_geometry (bool): Whether to skip the geometry part of the features or not. default is False.
|
413
|
+
|
414
|
+
Returns:
|
415
|
+
Dict | int: The result of the query execution or the count number of the result
|
416
|
+
|
417
|
+
Example:
|
418
|
+
>>> from geobox import GeoboxClient
|
419
|
+
>>> from geobox.query import Query
|
420
|
+
>>> client = GeoboxClient()
|
421
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
422
|
+
or
|
423
|
+
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
424
|
+
>>> query.execute(f='json')
|
425
|
+
"""
|
426
|
+
self._check_access()
|
427
|
+
|
428
|
+
if not self.sql:
|
429
|
+
raise ValueError('"sql" parameter is required for this action!')
|
430
|
+
if not self.params:
|
431
|
+
raise ValueError('"params" parameter is required for this action!')
|
432
|
+
|
433
|
+
data = clean_data({
|
434
|
+
"f": f if f in ['json', 'topojson'] else None,
|
435
|
+
"sql": self.sql,
|
436
|
+
"params": self.params,
|
437
|
+
"result_type": result_type.value,
|
438
|
+
"return_count": return_count,
|
439
|
+
"out_srid": out_srid,
|
440
|
+
"quant_factor": quant_factor,
|
441
|
+
"bbox_srid": bbox_srid,
|
442
|
+
"skip": skip,
|
443
|
+
"limit": limit,
|
444
|
+
"skip_geometry": skip_geometry
|
445
|
+
})
|
446
|
+
|
447
|
+
endpoint = urljoin(self.BASE_ENDPOINT, 'exec/')
|
448
|
+
self.result = self.api.post(endpoint, data)
|
449
|
+
return self.result
|
450
|
+
|
451
|
+
|
452
|
+
def update(self, **kwargs) -> Dict:
|
453
|
+
"""
|
454
|
+
Updates the query with new data.
|
455
|
+
|
456
|
+
Keyword Args:
|
457
|
+
name (str): The new name of the query.
|
458
|
+
display_name (str): The new display name of the query.
|
459
|
+
sql (str): The new SQL statement for the query.
|
460
|
+
params (list): The new parameters for the SQL statement.
|
461
|
+
|
462
|
+
Returns:
|
463
|
+
Dict: The updated query data.
|
464
|
+
|
465
|
+
Raises:
|
466
|
+
PermissionError: If the query is a read-only system query.
|
467
|
+
|
468
|
+
Example:
|
469
|
+
>>> from geobox import GeoboxClient
|
470
|
+
>>> from geobox.query import Query
|
471
|
+
>>> client = GeoboxClient()
|
472
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
473
|
+
or
|
474
|
+
>>> query = client.get_query(uuid="12345678-1234-5678-1234-567812345678")
|
475
|
+
>>> query.update(name='new_name')
|
476
|
+
"""
|
477
|
+
self._check_access()
|
478
|
+
|
479
|
+
data = {
|
480
|
+
"name": kwargs.get('name'),
|
481
|
+
"display_name": kwargs.get('display_name'),
|
482
|
+
"sql": kwargs.get('sql'),
|
483
|
+
"params": kwargs.get('params')
|
484
|
+
}
|
485
|
+
super()._update(self.endpoint, data)
|
486
|
+
|
487
|
+
|
488
|
+
def save(self) -> None:
|
489
|
+
"""
|
490
|
+
Save the query. Creates a new query if query uuid is None, updates existing query otherwise.
|
491
|
+
|
492
|
+
Returns:
|
493
|
+
None
|
494
|
+
|
495
|
+
Example:
|
496
|
+
>>> from geobox import GeoboxClient
|
497
|
+
>>> from geobox.query import Query
|
498
|
+
>>> client = GeoboxClient()
|
499
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
500
|
+
>>> query.sql = "SELECT * FROM some_layer"
|
501
|
+
>>> query.save()
|
502
|
+
"""
|
503
|
+
self.params = [item for item in self.params if item.get('value')]
|
504
|
+
|
505
|
+
try:
|
506
|
+
if self.__getattr__('uuid'):
|
507
|
+
self.update(name=self.data['name'], display_name=self.data['display_name'], sql=self.sql, params=self.params)
|
508
|
+
except AttributeError:
|
509
|
+
response = self.api.post(self.BASE_ENDPOINT, self.data)
|
510
|
+
self.endpoint = urljoin(self.BASE_ENDPOINT, f'{response["uuid"]}/')
|
511
|
+
self.data.update(response)
|
512
|
+
|
513
|
+
|
514
|
+
def delete(self) -> str:
|
515
|
+
"""
|
516
|
+
Deletes a query.
|
517
|
+
|
518
|
+
Returns:
|
519
|
+
str: The response from the API.
|
520
|
+
|
521
|
+
Raises:
|
522
|
+
PermissionError: If the query is a read-only system query
|
523
|
+
|
524
|
+
Example:
|
525
|
+
>>> from geobox import GeoboxClient
|
526
|
+
>>> from geobox.query import Query
|
527
|
+
>>> client = GeoboxClient()
|
528
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
529
|
+
>>> query.delete()
|
530
|
+
"""
|
531
|
+
self._check_access()
|
532
|
+
super().delete(self.endpoint)
|
533
|
+
|
534
|
+
|
535
|
+
def share(self, users: List['User']) -> None:
|
536
|
+
"""
|
537
|
+
Shares the query with specified users.
|
538
|
+
|
539
|
+
Args:
|
540
|
+
users (List[User]): The list of user objects to share the query with.
|
541
|
+
|
542
|
+
Returns:
|
543
|
+
None
|
544
|
+
|
545
|
+
Raises:
|
546
|
+
PermissionError: If the query is a read-only system query.
|
547
|
+
|
548
|
+
Example:
|
549
|
+
>>> from geobox import GeoboxClient
|
550
|
+
>>> from geobox.query import Query
|
551
|
+
>>> client = GeoboxClient()
|
552
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
553
|
+
>>> users = client.search_users(search="John")
|
554
|
+
>>> query.share(users=users)
|
555
|
+
"""
|
556
|
+
self._check_access()
|
557
|
+
super()._share(self.endpoint, users)
|
558
|
+
|
559
|
+
|
560
|
+
def unshare(self, users: List['User']) -> None:
|
561
|
+
"""
|
562
|
+
Unshares the query with specified users.
|
563
|
+
|
564
|
+
Args:
|
565
|
+
users (List[User]): The list of user objects to unshare the query with.
|
566
|
+
|
567
|
+
Returns:
|
568
|
+
None
|
569
|
+
|
570
|
+
Raises:
|
571
|
+
PermissionError: If the query is a read-only system query.
|
572
|
+
|
573
|
+
Example:
|
574
|
+
>>> from geobox import GeoboxClient
|
575
|
+
>>> from geobox.query import Query
|
576
|
+
>>> client = GeoboxClient()
|
577
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
578
|
+
>>> users = client.search_users(search="John")
|
579
|
+
>>> query.unshare(users=users)
|
580
|
+
"""
|
581
|
+
self._check_access()
|
582
|
+
super()._unshare(self.endpoint, users)
|
583
|
+
|
584
|
+
|
585
|
+
def get_shared_users(self, search: str, skip: int = 0, limit: int = 10) -> List['User']:
|
586
|
+
"""
|
587
|
+
Retrieves the list of users the query is shared with.
|
588
|
+
|
589
|
+
Args:
|
590
|
+
search (str, optional): the search query.
|
591
|
+
skip (int, optional): The number of users to skip.
|
592
|
+
limit (int, optional): The maximum number of users to retrieve.
|
593
|
+
|
594
|
+
Returns:
|
595
|
+
List[User]: The list of shared users.
|
596
|
+
|
597
|
+
Example:
|
598
|
+
>>> from geobox import GeoboxClient
|
599
|
+
>>> from geobox.query import Query
|
600
|
+
>>> client = GeoboxClient()
|
601
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
602
|
+
>>> users = client.search_users(search="John")
|
603
|
+
>>> query.get_shared_users(search='John', skip=0, limit=10)
|
604
|
+
"""
|
605
|
+
self._check_access()
|
606
|
+
params = {
|
607
|
+
'search': search,
|
608
|
+
'skip': skip,
|
609
|
+
'limit': limit
|
610
|
+
}
|
611
|
+
return super()._get_shared_users(self.endpoint, params)
|
612
|
+
|
613
|
+
|
614
|
+
@property
|
615
|
+
def thumbnail(self) -> str:
|
616
|
+
"""
|
617
|
+
Retrieves the thumbnail URL for the query.
|
618
|
+
|
619
|
+
Returns:
|
620
|
+
str: The thumbnail URL.
|
621
|
+
"""
|
622
|
+
self._check_access()
|
623
|
+
endpoint = f'{self.api.base_url}{self.endpoint}thumbnail.png'
|
624
|
+
return endpoint
|
625
|
+
|
626
|
+
|
627
|
+
def save_as_layer(self, layer_name: str, layer_type: 'QueryGeometryType') -> Dict:
|
628
|
+
"""
|
629
|
+
Saves the query as a new layer.
|
630
|
+
|
631
|
+
Args:
|
632
|
+
sql (str): The SQL statement for the query.
|
633
|
+
params (list): The parameters for the SQL statement.
|
634
|
+
layer_name (str): The name of the new layer.
|
635
|
+
layer_type (QueryGeometryType): The type of the new layer.
|
636
|
+
|
637
|
+
Returns:
|
638
|
+
Dict: The response from the API.
|
639
|
+
|
640
|
+
Raises:
|
641
|
+
PermissionError: If the query is a read-only system query.
|
642
|
+
|
643
|
+
Example:
|
644
|
+
>>> from geobox import GeoboxClient
|
645
|
+
>>> from geobox.query import Query
|
646
|
+
>>> client = GeoboxClient()
|
647
|
+
>>> query = Query.get_query(client, uuid="12345678-1234-5678-1234-567812345678")
|
648
|
+
>>> query.save_as_layer(layer_name='test', layer_type=QueryGeometryType.POLYGON)
|
649
|
+
"""
|
650
|
+
self._check_access()
|
651
|
+
|
652
|
+
params = [{
|
653
|
+
"name": item.get('name'),
|
654
|
+
"type": item.get('type'),
|
655
|
+
"value": item.get('default_value') if not item.get('value') else item.get('value')
|
656
|
+
} for item in self.params]
|
657
|
+
|
658
|
+
data = clean_data({
|
659
|
+
"sql": self.sql,
|
660
|
+
"params": params,
|
661
|
+
"layer_name": layer_name,
|
662
|
+
"layer_type": layer_type.value
|
663
|
+
})
|
664
|
+
|
665
|
+
endpoint = urljoin(self.BASE_ENDPOINT, 'saveAsLayer/')
|
666
|
+
response = self.api.post(endpoint, data)
|
667
|
+
task = Task.get_task(self.api, response.get('task_id'))
|
668
|
+
return task
|