Skip to content

Commit

Permalink
feat(commands, activate/deactivate areas
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Jul 11, 2024
1 parent d9f3fea commit cc37772
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/ref_geo/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
from flask.cli import with_appcontext
from sqlalchemy import func, select
import sqlalchemy as sa

from ref_geo.env import db
from ref_geo.models import BibAreasTypes, LAreas
Expand All @@ -23,3 +24,95 @@ def info():
)
for area_type, count in db.session.scalars(q).unique().all():
click.echo("\t{}: {}".format(area_type.type_name, count))


def change_area_activation_status(area_code, area_name, area_type, in_polygon, enable):
"""
Change the activation status of areas in the geographical referential.
Parameters
----------
area_code : list of str
List of area codes to activate or deactivate.
area_name : list of str
List of area names to activate or deactivate.
area_type : list of str
List of area types to activate or deactivate. The type codes are
checked in the `bib_areas_types` table.
in_polygon : str
WKT polygon defined in WGS84 coordinate reference system. The
areas inside the polygon will be activated or deactivated.
enable : bool
If True, the areas will be activated, otherwise they will be
deactivated.
"""
str_ = "activated" if enable else "deactivated"
if area_code:
click.echo("The following area codes will be {}: {}".format(str_, ", ".join(area_code)))
q = sa.update(LAreas).where(LAreas.area_code.in_(area_code)).values(enable=enable)
db.session.execute(q)
if area_name:
click.echo("The following area names will be {}: {}".format(str_, ", ".join(area_name)))
q = sa.update(LAreas).where(LAreas.area_name.in_(area_name)).values(enable=enable)
db.session.execute(q)
if area_type:
click.echo("The following area types will be {}: {}".format(str_, ", ".join(area_type)))
area_type_ids = db.session.scalars(
select(BibAreasTypes.id_type).where(BibAreasTypes.type_code.in_(area_type))
).all()
q = sa.update(LAreas).where(LAreas.id_type.in_(area_type_ids)).values(enable=enable)
db.session.execute(q)
if in_polygon:
click.echo(
"The following areas will be {} in the following polygon: {}".format(str_, in_polygon)
)
in_polygon_cte = select(
LAreas.id_area, func.ST_Intersects(LAreas.geom_4326, func.ST_GeomFromText(in_polygon))
).cte("in_polygon")
q = (
sa.update(LAreas)
.where(in_polygon_cte.c.id_area == LAreas.id_area)
.values(enable=enable)
)
db.session.execute(q)
db.session.commit()


@ref_geo.command()
@click.option("--area-code", "-a", multiple=True, help="Areas' code to deactivate")
@click.option("--area-name", "-n", multiple=True, help="Areas' name to deactivate")
@click.option(
"--area-type",
"-t",
multiple=True,
help="Area type to deactivate (check `type_code` in `bib_areas_types` table)",
)
@click.option(
"--in-polygon",
"-p",
help="Indicate a polygon in which areas will be deactivated. Must be in WKT format (SRID 4326)",
)
@with_appcontext
def deactivate(area_code, area_name, area_type, in_polygon):
click.echo("RefGeo : deactivating areas...")
change_area_activation_status(area_code, area_name, area_type, in_polygon, False)


@ref_geo.command()
@click.option("--area-code", "-a", multiple=True, help="Areas' code to activate")
@click.option("--area-name", "-n", multiple=True, help="Areas' name to activate")
@click.option(
"--area-type",
"-t",
multiple=True,
help="Area type to activate (check `type_code` in `bib_areas_types` table)",
)
@click.option(
"--in-polygon",
"-p",
help="Indicate a polygon in which areas will be activated. Must be in WKT format (SRID 4326)",
)
@with_appcontext
def activate(area_code, area_name, area_type, in_polygon):
click.echo("RefGeo : activating areas...")
change_area_activation_status(area_code, area_name, area_type, in_polygon, True)

0 comments on commit cc37772

Please sign in to comment.