1
1
import click
2
2
from flask .cli import with_appcontext
3
3
from sqlalchemy import func , select
4
+ import sqlalchemy as sa
4
5
5
6
from ref_geo .env import db
6
7
from ref_geo .models import BibAreasTypes , LAreas
@@ -23,3 +24,95 @@ def info():
23
24
)
24
25
for area_type , count in db .session .scalars (q ).unique ().all ():
25
26
click .echo ("\t {}: {}" .format (area_type .type_name , count ))
27
+
28
+
29
+ def change_area_activation_status (area_code , area_name , area_type , in_polygon , enable ):
30
+ """
31
+ Change the activation status of areas in the geographical referential.
32
+
33
+ Parameters
34
+ ----------
35
+ area_code : list of str
36
+ List of area codes to activate or deactivate.
37
+ area_name : list of str
38
+ List of area names to activate or deactivate.
39
+ area_type : list of str
40
+ List of area types to activate or deactivate. The type codes are
41
+ checked in the `bib_areas_types` table.
42
+ in_polygon : str
43
+ WKT polygon defined in WGS84 coordinate reference system. The
44
+ areas inside the polygon will be activated or deactivated.
45
+ enable : bool
46
+ If True, the areas will be activated, otherwise they will be
47
+ deactivated.
48
+ """
49
+ str_ = "activated" if enable else "deactivated"
50
+ if area_code :
51
+ click .echo ("The following area codes will be {}: {}" .format (str_ , ", " .join (area_code )))
52
+ q = sa .update (LAreas ).where (LAreas .area_code .in_ (area_code )).values (enable = enable )
53
+ db .session .execute (q )
54
+ if area_name :
55
+ click .echo ("The following area names will be {}: {}" .format (str_ , ", " .join (area_name )))
56
+ q = sa .update (LAreas ).where (LAreas .area_name .in_ (area_name )).values (enable = enable )
57
+ db .session .execute (q )
58
+ if area_type :
59
+ click .echo ("The following area types will be {}: {}" .format (str_ , ", " .join (area_type )))
60
+ area_type_ids = db .session .scalars (
61
+ select (BibAreasTypes .id_type ).where (BibAreasTypes .type_code .in_ (area_type ))
62
+ ).all ()
63
+ q = sa .update (LAreas ).where (LAreas .id_type .in_ (area_type_ids )).values (enable = enable )
64
+ db .session .execute (q )
65
+ if in_polygon :
66
+ click .echo (
67
+ "The following areas will be {} in the following polygon: {}" .format (str_ , in_polygon )
68
+ )
69
+ in_polygon_cte = select (
70
+ LAreas .id_area , func .ST_Intersects (LAreas .geom_4326 , func .ST_GeomFromText (in_polygon ))
71
+ ).cte ("in_polygon" )
72
+ q = (
73
+ sa .update (LAreas )
74
+ .where (in_polygon_cte .c .id_area == LAreas .id_area )
75
+ .values (enable = enable )
76
+ )
77
+ db .session .execute (q )
78
+ db .session .commit ()
79
+
80
+
81
+ @ref_geo .command ()
82
+ @click .option ("--area-code" , "-a" , multiple = True , help = "Areas' code to deactivate" )
83
+ @click .option ("--area-name" , "-n" , multiple = True , help = "Areas' name to deactivate" )
84
+ @click .option (
85
+ "--area-type" ,
86
+ "-t" ,
87
+ multiple = True ,
88
+ help = "Area type to deactivate (check `type_code` in `bib_areas_types` table)" ,
89
+ )
90
+ @click .option (
91
+ "--in-polygon" ,
92
+ "-p" ,
93
+ help = "Indicate a polygon in which areas will be deactivated. Must be in WKT format (SRID 4326)" ,
94
+ )
95
+ @with_appcontext
96
+ def deactivate (area_code , area_name , area_type , in_polygon ):
97
+ click .echo ("RefGeo : deactivating areas..." )
98
+ change_area_activation_status (area_code , area_name , area_type , in_polygon , False )
99
+
100
+
101
+ @ref_geo .command ()
102
+ @click .option ("--area-code" , "-a" , multiple = True , help = "Areas' code to activate" )
103
+ @click .option ("--area-name" , "-n" , multiple = True , help = "Areas' name to activate" )
104
+ @click .option (
105
+ "--area-type" ,
106
+ "-t" ,
107
+ multiple = True ,
108
+ help = "Area type to activate (check `type_code` in `bib_areas_types` table)" ,
109
+ )
110
+ @click .option (
111
+ "--in-polygon" ,
112
+ "-p" ,
113
+ help = "Indicate a polygon in which areas will be activated. Must be in WKT format (SRID 4326)" ,
114
+ )
115
+ @with_appcontext
116
+ def activate (area_code , area_name , area_type , in_polygon ):
117
+ click .echo ("RefGeo : activating areas..." )
118
+ change_area_activation_status (area_code , area_name , area_type , in_polygon , True )
0 commit comments