diff --git a/CHANGELOG.md b/CHANGELOG.md index 161b033..fe21405 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/), and this +## 3.3.5 - 2024-09-25 + +* Time interval script (fix #145 from PR #147) is now fixed and usable as expected + +## 3.3.4 - 2024-09-24 + +* Plugin menu is now correctly removed from bar when plugin is uninstalled or reloaded + ## 3.3.3 - 2024-08-26 + * update documentation ## 3.3.2 - 2024-08-26 diff --git a/plugin_qgis_lpo/commons/helpers.py b/plugin_qgis_lpo/commons/helpers.py index 216a684..68a4040 100644 --- a/plugin_qgis_lpo/commons/helpers.py +++ b/plugin_qgis_lpo/commons/helpers.py @@ -193,9 +193,8 @@ def sql_datetime_filter_builder( def sql_timeinterval_cols_builder( # noqa C901 self: QgsProcessingAlgorithm, - time_interval_param, - start_year_param: int, - end_year_param: int, + period_type_filter: str, + time_interval_param: str, aggregation_type_param: str, parameters: Dict, context: QgsProcessingContext, @@ -210,41 +209,41 @@ def sql_timeinterval_cols_builder( # noqa C901 "*" if aggregation_type_param == "Nombre de données" else "DISTINCT t.cd_ref" ) if time_interval_param == "Par année": - add_five_years = self.parameterAsEnums( - parameters, self.ADD_FIVE_YEARS, context # type: ignore - ) - if len(add_five_years) > 0: - if (end_year_param - start_year_param + 1) % 5 != 0: + timestamp = datetime.now() + if period_type_filter in ("5 dernières années", "10 dernières années", "Pas de filtre temporel"): + end_year = int(timestamp.strftime("%Y")) + period = period_type_filter.split()[0] + start_year = end_year - int(period if period.isdigit() else 10) + years = [str(year) for year in range(start_year,end_year)] + + elif period_type_filter == "Cette année": + end_year = int(timestamp.strftime("%Y")) + start_year = end_year + years = [timestamp.strftime("%Y"),] + + elif period_type_filter == "Date de début - Date de fin (à définir ci-dessous)": + # Retrieve the start and end dates + start_date = datetime.fromisoformat(self.parameterAsString(parameters, self.START_DATE, context)) + end_date = datetime.fromisoformat(self.parameterAsString(parameters, self.END_DATE, context)) + + if end_date < start_date: raise QgsProcessingException( - "Veuillez renseigner une période en année qui soit " - "divisible par 5 ! Exemple : 2011 - 2020." + "Veuillez renseigner une date de fin postérieure ou égale à la date de début !" ) else: - counter = start_year_param - step_limit = start_year_param - while counter <= end_year_param: - select_data.append( - f"""COUNT({count_param}) filter (WHERE date_an={counter}) AS \"{counter}\" """ - ) - x_var.append(str(counter)) - if counter == step_limit + 4: - select_data.append( - f"""COUNT({count_param}) filter (WHERE date_an>={counter-4} and date_an<={counter}) AS \"{counter-4} - {counter}\" """ - ) - step_limit += 5 - counter += 1 - else: - for year in range(start_year_param, end_year_param + 1): + end_year = int(end_date.strftime("%Y")) + start_year = int(start_date.strftime("%Y")) + years = [str(year) for year in range(start_year,end_year)] + + for year in years: select_data.append( f"""COUNT({count_param}) filter (WHERE date_an={year}) AS \"{year}\"""" ) x_var.append(str(year)) - select_data.append( - f"""COUNT({count_param}) filter (WHERE date_an>={start_year_param} and date_an<={end_year_param}) AS \"TOTAL\"""" - ) + else: - start_month = self.parameterAsEnum(parameters, self.START_MONTH, context) - end_month = self.parameterAsEnum(parameters, self.END_MONTH, context) + monthes = self.parameterAsEnums(parameters, self.MONTHES, context) + self.log(message=f'MONTHES {monthes}') months_numbers_variables = [ "01", "02", @@ -259,60 +258,17 @@ def sql_timeinterval_cols_builder( # noqa C901 "11", "12", ] - if start_year_param == end_year_param: - if end_month < start_month: - raise QgsProcessingException( - "Veuillez renseigner un mois de fin postérieur ou égal au mois de début !" - ) - else: - for month in range(start_month, end_month + 1): - select_data.append( - f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{start_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {start_year_param}\"""" - ) - x_var.append( - self._months_names_variables[month] # type: ignore - + " " - + str(start_year_param) - ) - elif end_year_param == start_year_param + 1: - for month in range(start_month, 12): - select_data.append( - f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{start_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {start_year_param}\"""" - ) - x_var.append( - self._months_names_variables[month] + " " + str(start_year_param) # type: ignore + for month in monthes: + select_data.append( + f"""COUNT({count_param}) filter (WHERE extract(month from date)={month+1}) AS \"{self._months_names_variables[month]}\"""" ) - for month in range(0, end_month + 1): - select_data.append( - f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{end_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {end_year_param}\"""" - ) - x_var.append( - self._months_names_variables[month] + " " + str(end_year_param) - ) - else: - for month in range(start_month, 12): - select_data.append( - f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{start_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {start_year_param}\"""" - ) - x_var.append( - self._months_names_variables[month] + " " + str(start_year_param) - ) - for year in range(start_year_param + 1, end_year_param): - for month in range(0, 12): - select_data.append( - f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{year}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {year}\"""" - ) - x_var.append(self._months_names_variables[month] + " " + str(year)) - for month in range(0, end_month + 1): - select_data.append( - f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{end_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {end_year_param}\"""" - ) - x_var.append( - self._months_names_variables[month] + " " + str(end_year_param) - ) - select_data.append( - f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')>='{start_year_param}-{months_numbers_variables[start_month]}' and to_char(date, 'YYYY-MM')<='{end_year_param}-{months_numbers_variables[end_month]}') AS \"TOTAL\"""" - ) + x_var.append( + self._months_names_variables[month] # type: ignore + ) + # Adding total count + select_data.append( + f"""COUNT({count_param}) AS \"TOTAL\"""" + ) final_select_data = ", ".join(select_data) feedback.pushDebugInfo(final_select_data) return final_select_data, x_var diff --git a/plugin_qgis_lpo/metadata.txt b/plugin_qgis_lpo/metadata.txt index b5ac69f..6f9d810 100644 --- a/plugin_qgis_lpo/metadata.txt +++ b/plugin_qgis_lpo/metadata.txt @@ -21,5 +21,5 @@ qgisMinimumVersion=3.16 qgisMaximumVersion=3.99 # versioning -version=3.3.3 +version=3.3.5 changelog=https://github.com/lpoaura/PluginQGis-LPOData/blob/master/CHANGELOG.md diff --git a/plugin_qgis_lpo/processing/processing_algorithm.py b/plugin_qgis_lpo/processing/processing_algorithm.py index 79ad2cf..4f9bfc3 100644 --- a/plugin_qgis_lpo/processing/processing_algorithm.py +++ b/plugin_qgis_lpo/processing/processing_algorithm.py @@ -77,10 +77,7 @@ class BaseProcessingAlgorithm(QgsProcessingAlgorithm): TIME_INTERVAL = "TIME_INTERVAL" ADD_FIVE_YEARS = "ADD_FIVE_YEARS" TEST = "TEST" - START_MONTH = "START_MONTH" - START_YEAR = "START_YEAR" - END_MONTH = "END_MONTH" - END_YEAR = "END_YEAR" + MONTHES = "MONTHES" EXTRA_WHERE = "EXTRA_WHERE" OUTPUT = "OUTPUT" OUTPUT_NAME = "OUTPUT_NAME" @@ -194,8 +191,6 @@ def __init__(self) -> None: self._lr_columns_db: List[str] = ["lr_r"] self._lr_columns_with_alias: List[str] = ['lr_r as "LR Régionale"'] self._time_interval: str - self._start_year: int - self._end_year: int def tr(self, string: str) -> str: """QgsProcessingAlgorithm translatable string with the self.tr() function.""" @@ -263,30 +258,28 @@ def initAlgorithm(self, _config: None) -> None: # noqa N802 optional_text = "(facultatif)" # self._ts = datetime.now() # self._db_variables = QgsSettings() - self.addParameter( - QgsProcessingParameterProviderConnection( - self.DATABASE, - self.tr( - f"""BASE DE DONNÉES {required_text} : + database = QgsProcessingParameterProviderConnection( + self.DATABASE, + self.tr( + f"""BASE DE DONNÉES {required_text} : sélectionnez votre connexion à la base de données LPO""" - ), - "postgres", - defaultValue="geonature_lpo", - optional=False, - ) + ), + "postgres", + defaultValue="geonature_lpo", + optional=False, ) + self.addParameter(database) # Input vector layer = study area - self.addParameter( - QgsProcessingParameterFeatureSource( - self.STUDY_AREA, - self.tr( - f"""ZONE D'ÉTUDE {required_text} : + study_area = QgsProcessingParameterFeatureSource( + self.STUDY_AREA, + self.tr( + f"""ZONE D'ÉTUDE {required_text} : sélectionnez votre zone d'étude, à partir de laquelle seront extraits les résultats""" - ), - [QgsProcessing.TypeVectorPolygon], - ) + ), + [QgsProcessing.TypeVectorPolygon], ) + self.addParameter(study_area) if self._has_taxonomic_rank_form and self._taxonomic_ranks: self._taxonomic_ranks_labels = [ @@ -317,6 +310,48 @@ def initAlgorithm(self, _config: None) -> None: # noqa N802 ) self.addParameter(taxonomic_rank) + period_type = QgsProcessingParameterEnum( + self.PERIOD, + self.tr( + f"""PÉRIODE {required_text} : + sélectionnez une période pour filtrer vos données + d'observations""" + ), + self._period_variables, + allowMultiple=False, + optional=True, + ) + period_type.setMetadata( + { + "widget_wrapper": { + "useCheckBoxes": True, + "columns": len(self._period_variables) / 2, + } + } + ) + # period_type.setFlags(period_type.flags() | QgsProcessingParameterDefinition.FlagHidden) + self.addParameter(period_type) + + start_date = QgsProcessingParameterString( + self.START_DATE, + f"Date de début {optional_text}", + defaultValue="", + optional=True, + ) + start_date.setMetadata({"widget_wrapper": {"class": DateTimeWidget}}) + # start_date.setFlags(start_date.flags() | QgsProcessingParameterDefinition.FlagHidden) + self.addParameter(start_date) + + end_date = QgsProcessingParameterString( + self.END_DATE, + f"Date de fin {optional_text}", + defaultValue="", + optional=True, + ) + end_date.setMetadata({"widget_wrapper": {"class": DateTimeWidget}}) + # end_date.setFlags(end_date.flags() | QgsProcessingParameterDefinition.FlagHidden) + self.addParameter(end_date) + if self._has_time_interval_form: # ## Time interval and period ### time_interval = QgsProcessingParameterEnum( @@ -337,86 +372,16 @@ def initAlgorithm(self, _config: None) -> None: # noqa N802 ) self.addParameter(time_interval) - self.addParameter( - QgsProcessingParameterEnum( - self.START_MONTH, - self.tr("Mois de début"), - self._months_names_variables, - allowMultiple=False, - optional=True, - ) - ) - - self.addParameter( - QgsProcessingParameterNumber( - self.START_YEAR, - self.tr("Année de début"), - QgsProcessingParameterNumber.Integer, - defaultValue=2010, - minValue=1800, - maxValue=int(self._ts.strftime("%Y")), - ) - ) - - self.addParameter( - QgsProcessingParameterEnum( - self.END_MONTH, - self.tr("Mois de fin"), - self._months_names_variables, - allowMultiple=False, - optional=True, - ) - ) - - self.addParameter( - QgsProcessingParameterNumber( - self.END_YEAR, - self.tr("Année de fin"), - QgsProcessingParameterNumber.Integer, - defaultValue=self._ts.strftime("%Y"), - minValue=1800, - maxValue=int(self._ts.strftime("%Y")), - ) - ) - else: - period_type = QgsProcessingParameterEnum( - self.PERIOD, - self.tr( - f"""PÉRIODE {required_text} : - sélectionnez une période pour filtrer vos données - d'observations""" - ), - self._period_variables, - allowMultiple=False, - optional=False, - ) - period_type.setMetadata( - { - "widget_wrapper": { - "useCheckBoxes": True, - "columns": len(self._period_variables) / 2, - } - } - ) - self.addParameter(period_type) - - start_date = QgsProcessingParameterString( - self.START_DATE, - f"Date de début {optional_text}", - defaultValue="", - optional=True, - ) - start_date.setMetadata({"widget_wrapper": {"class": DateTimeWidget}}) - self.addParameter(start_date) - - end_date = QgsProcessingParameterString( - self.END_DATE, - f"Date de fin {optional_text}", - defaultValue="", + monthes = QgsProcessingParameterEnum( + self.MONTHES, + self.tr("Mois de début"), + self._months_names_variables, + allowMultiple=True, optional=True, + defaultValue=[v for v in range(12)] ) - end_date.setMetadata({"widget_wrapper": {"class": DateTimeWidget}}) - self.addParameter(end_date) + self.addParameter(monthes) + if self._return_geo_agg: areas_types = QgsProcessingParameterEnum( @@ -435,18 +400,17 @@ def initAlgorithm(self, _config: None) -> None: # noqa N802 self.addParameter(areas_types) # ## Taxons filters ## - self.addParameter( - QgsProcessingParameterEnum( - self.GROUPE_TAXO, - self.tr( - f"""TAXONS {optional_text} : + taxon_filter = QgsProcessingParameterEnum( + self.GROUPE_TAXO, + self.tr( + f"""TAXONS {optional_text} : filtrer les données par groupes taxonomiques""" - ), - self._db_variables.value("groupe_taxo"), - allowMultiple=True, - optional=True, - ) + ), + self._db_variables.value("groupe_taxo"), + allowMultiple=True, + optional=True, ) + self.addParameter(taxon_filter) if self._has_histogram: histogram_options = QgsProcessingParameterEnum( @@ -479,28 +443,26 @@ def initAlgorithm(self, _config: None) -> None: # noqa N802 ) self.addParameter(output_histogram) - self.addParameter( - QgsProcessingParameterString( - self.OUTPUT_NAME, - self.tr( - f"""PARAMÉTRAGE DES RESULTATS EN SORTIE + output_name = QgsProcessingParameterString( + self.OUTPUT_NAME, + self.tr( + f"""PARAMÉTRAGE DES RESULTATS EN SORTIE {optional_text} : personnalisez le nom de votre couche en base de données""" - ), - self.tr(self._output_name), - ) + ), + self.tr(self._output_name), ) + self.addParameter(output_name) # Boolean : True = add the summary table in the DB ; False = don't - self.addParameter( - QgsProcessingParameterBoolean( - self.ADD_TABLE, - self.tr( - "Enregistrer les résultats en sortie dans une nouvelle table en base de données" - ), - False, - ) + add_table = QgsProcessingParameterBoolean( + self.ADD_TABLE, + self.tr( + "Enregistrer les résultats en sortie dans une nouvelle table en base de données" + ), + False, ) + self.addParameter(add_table) if self._has_source_data_filter: source_data_where = QgsProcessingParameterEnum( @@ -546,6 +508,9 @@ def initAlgorithm(self, _config: None) -> None: # noqa N802 extra_where.flags() | QgsProcessingParameterDefinition.FlagAdvanced ) self.addParameter(extra_where) + self.log( + message=f"initAlgorithm {self._name} SD{self.START_DATE} ED{self.END_DATE} end" + ) def processAlgorithm( # noqa N802 self, @@ -703,14 +668,7 @@ def processAlgorithm( # noqa N802 self.parameterAsEnum(parameters, self.TIME_INTERVAL, context) ] self.log(message=f"time_interval {self._time_interval}") - self._start_year = self.parameterAsInt(parameters, self.START_YEAR, context) - self.log(message=f"start_year {self._start_year}") - self._end_year = self.parameterAsInt(parameters, self.END_YEAR, context) - self.log(message=f"end_year {self._end_year}") - if self._end_year < self._start_year: - raise QgsProcessingException( - "Veuillez renseigner une année de fin postérieure à l'année de début !" - ) + self._monthes = self.parameterAsEnum(parameters, self.MONTHES, context) taxonomic_rank = self._taxonomic_ranks_labels[ self.parameterAsEnum(parameters, self.TAXONOMIC_RANK, context) ] @@ -726,9 +684,8 @@ def processAlgorithm( # noqa N802 self._x_var, ) = sql_timeinterval_cols_builder( self, + self._period_type, self._time_interval, - self._start_year, - self._end_year, aggregation_type, parameters, context, diff --git a/plugin_qgis_lpo/processing/refresh_data.py b/plugin_qgis_lpo/processing/refresh_data.py index ec4ee98..af0ea84 100644 --- a/plugin_qgis_lpo/processing/refresh_data.py +++ b/plugin_qgis_lpo/processing/refresh_data.py @@ -59,18 +59,17 @@ def initAlgorithm(self, _config: None) -> None: # noqa N802 # self._ts = datetime.now() # self._db_variables = QgsSettings() - self.addParameter( - QgsProcessingParameterProviderConnection( - self.DATABASE, - self.tr( - f"""BASE DE DONNÉES {required_text} : + database = QgsProcessingParameterProviderConnection( + self.DATABASE, + self.tr( + f"""BASE DE DONNÉES {required_text} : sélectionnez votre connexion à la base de données LPO""" - ), - "postgres", - defaultValue="geonature_lpo", - optional=False, - ) + ), + "postgres", + defaultValue="geonature_lpo", + optional=False, ) + self.addParameter(database) def processAlgorithm( self,