22
22
from atlas .modeles .entities .vmTaxref import VmTaxref
23
23
24
24
25
- def area_types (session ):
26
- query = session .query (
27
- VmBibAreasTypes .id_type ,
28
- VmBibAreasTypes .type_code ,
29
- VmBibAreasTypes .type_name ,
30
- VmBibAreasTypes .type_desc ,
31
- )
32
- return [q ._asdict () for q in query .all ()]
33
-
34
-
35
- def get_id_area (session , type_code , area_code ):
36
- try :
37
- query = (
38
- session .query (VmAreas .id_area )
39
- .join (VmBibAreasTypes , VmBibAreasTypes .id_type == VmAreas .id_type )
40
- .filter (
41
- and_ (
42
- VmAreas .area_code .ilike (area_code ),
43
- VmBibAreasTypes .type_code .ilike (type_code ),
44
- )
45
- )
46
- )
47
- current_app .logger .debug ("<get_id_area> query: {}" .format (query ))
48
- result = query .one ()
49
- return result .id_area
50
- except Exception as e :
51
- current_app .logger .error ("<get_id_area> error {}" .format (e ))
52
-
53
-
54
25
def get_area_from_id (session , id_area ):
26
+ """
27
+ Get area info from an id
28
+ """
55
29
query = (
56
30
session .query (
57
31
VmAreas .id_area ,
@@ -73,13 +47,11 @@ def get_area_from_id(session, id_area):
73
47
74
48
75
49
def last_observations_area_maille (session , myLimit , idArea ):
50
+ """
51
+ Gets the last observations for a specific area
52
+ """
76
53
q_last_obs = (
77
54
session .query (
78
- # VmObservations.cd_ref.label("cd_ref"),
79
- # VmObservations.dateobs.label("dateobs"),
80
- # VmTaxons.lb_nom.label("lb_nom"),
81
- # VmTaxons.nom_vern.label("nom_vern"),
82
- # VmObservations.the_geom_point.label("the_geom_point"),
83
55
VmObservations .cd_ref ,
84
56
VmObservations .dateobs ,
85
57
VmTaxons .lb_nom ,
@@ -171,6 +143,15 @@ def get_observers_area(session, id_area):
171
143
172
144
173
145
def search_area_by_type (session , search = None , type_code = None , filter_area_codes = [], limit = 50 ):
146
+ """
147
+ Filter out the areas by the provided params:
148
+
149
+ Args:
150
+ session: the db session
151
+ search (str): to be able to filter against the name and the area_code
152
+ filter_area_codes (list): to exclude area codes
153
+ limit (int): restricts the number of objects returns
154
+ """
174
155
query = (
175
156
session .query (
176
157
VmAreas .id_area ,
@@ -201,6 +182,18 @@ def search_area_by_type(session, search=None, type_code=None, filter_area_codes=
201
182
202
183
203
184
def get_areas_geometries (session , type_code = None , filter_area_codes = [], limit = 50 ):
185
+ """
186
+ Returns a Feature collection of all the areas
187
+
188
+ Args:
189
+ session: the db session
190
+ type_code (str): filter out by a type_code
191
+ filter_area_codes (list): ignores the codes provided in this list
192
+ limit (int): restricts the number of objects returns
193
+
194
+ Returns:
195
+ FeatureCollection: the geometries as geosjon
196
+ """
204
197
query = (
205
198
session .query (
206
199
VmAreas .area_name ,
@@ -236,6 +229,10 @@ def get_areas_geometries(session, type_code=None, filter_area_codes=[], limit=50
236
229
237
230
238
231
def get_areas_observations (session , limit , id_area ):
232
+ """
233
+ For a provided area_code and cd_ref, computes the observations in
234
+ this area
235
+ """
239
236
query = (
240
237
session .query (
241
238
VmObservations .id_observation ,
@@ -265,6 +262,10 @@ def get_areas_observations(session, limit, id_area):
265
262
266
263
267
264
def get_areas_observations_by_cd_ref (session , area_code , cd_ref ):
265
+ """
266
+ For a provided area_code and cd_ref, computes grid observations in
267
+ this area
268
+ """
268
269
req = (
269
270
session .query (
270
271
VmObservations .id_observation ,
@@ -294,6 +295,10 @@ def get_areas_observations_by_cd_ref(session, area_code, cd_ref):
294
295
295
296
296
297
def get_areas_grid_observations_by_cd_ref (session , area_code , cd_ref ):
298
+ """
299
+ For a provided area_code and cd_ref, computes the observations in
300
+ this area
301
+ """
297
302
query = (
298
303
session .query (
299
304
TGrid .id_maille ,
@@ -325,6 +330,13 @@ def get_areas_grid_observations_by_cd_ref(session, area_code, cd_ref):
325
330
326
331
327
332
def get_area_taxa (session , id_area ):
333
+ """
334
+ Returns the list of taxa observed in the area defined by id_area
335
+
336
+ Args:
337
+ session: the db session
338
+ id_area (int): the id of the area
339
+ """
328
340
query = (
329
341
session .query (
330
342
VmTaxons .cd_ref ,
@@ -378,6 +390,13 @@ def get_area_taxa(session, id_area):
378
390
379
391
380
392
def get_surrounding_areas (session , id_area ):
393
+ """
394
+ Returns the areas around the given id_area
395
+
396
+ Args:
397
+ session: the db session
398
+ id_area (int): the id of the area
399
+ """
381
400
subquery = (
382
401
session .query (VmAreas .the_geom ).filter (VmAreas .id_area == id_area ).subquery ()
383
402
)
@@ -398,8 +417,16 @@ def get_surrounding_areas(session, id_area):
398
417
399
418
400
419
def stats (session , type_codes ):
420
+ """
421
+ Return the total number of area for each type_code provided
422
+
423
+ Args:
424
+ session: the db session
425
+ type_codes (list): list of strings of each type code
426
+ """
401
427
sums = []
402
428
for type_code in type_codes :
429
+ # Use a case to be able to select a sum of each type
403
430
sums .append (func .sum (case ((VmBibAreasTypes .type_code == type_code , 1 ), else_ = 0 )).label (type_code ))
404
431
405
432
query = (
0 commit comments