diff --git a/README.md b/README.md index bb931d2..c9e10d6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Plugin to perform Area Weighted Average analysis in QGIS. ## Algorithm Description -This algorithm performs spatial area weighted average analysis on an input polygon layer given an attribute in the overlay polygon layer. Each feature in the input layer will be assigned a spatial area weighted average value of the overlay field. A report of the analysis is generated as a GIS Layer and as HTML. +This algorithm calculates attribute value by performing spatial area weighted average analysis on an input polygon layer given an attribute in the overlay polygon layer. Each feature in the input layer will be assigned a spatial area weighted average value of the overlay field. A report of the analysis is generated as a GIS Layer and as HTML. ## Input Parameters @@ -12,7 +12,7 @@ This algorithm performs spatial area weighted average analysis on an input polyg Polygon layer for which area weighted average will be calculated. - Overlay Layer: - Polygon layer overlapping the Input Layer. + Polygon layer with source data. Must overlap the Input Layer. - Field to Average: Single numeric field in the Overlay Layer. @@ -20,7 +20,7 @@ This algorithm performs spatial area weighted average analysis on an input polyg - Identifier Field for Report [optional]: Name or ID field in the Input Layer. This field will be used to identify features in the report. -- Additional Fields to Keep for Report: +- Additional Fields to Keep for Report [optional]: Fields in the Overlay Layer that will be included in the reports. ## Outputs @@ -38,7 +38,7 @@ This algorithm performs spatial area weighted average analysis on an input polyg Algorithm author: Abdul Raheem Siddiqui Help author: Abdul Raheem Siddiqui -Algorithm version: 0.1 +Algorithm version: 0.2 Contact email: ars.work.ce@gmail.com ## Donate diff --git a/__init__.py b/__init__.py index b1c1518..170250f 100644 --- a/__init__.py +++ b/__init__.py @@ -23,7 +23,7 @@ """ __author__ = "Abdul Raheem Siddiqui" -__date__ = "2021-02-21" +__date__ = "2021-06-25" __copyright__ = "(C) 2021 by Abdul Raheem Siddiqui" diff --git a/area_weighted_average.py b/area_weighted_average.py index 81e0704..8cf0391 100644 --- a/area_weighted_average.py +++ b/area_weighted_average.py @@ -23,7 +23,7 @@ """ __author__ = "Abdul Raheem Siddiqui" -__date__ = "2021-02-21" +__date__ = "2021-06-25" __copyright__ = "(C) 2021 by Abdul Raheem Siddiqui" # This will get replaced with a git SHA1 when you do a git archive diff --git a/area_weighted_average_algorithm.py b/area_weighted_average_algorithm.py index 14ce2a8..36468e8 100644 --- a/area_weighted_average_algorithm.py +++ b/area_weighted_average_algorithm.py @@ -23,7 +23,7 @@ """ __author__ = "Abdul Raheem Siddiqui" -__date__ = "2021-02-21" +__date__ = "2021-06-25" __copyright__ = "(C) 2021 by Abdul Raheem Siddiqui" # This will get replaced with a git SHA1 when you do a git archive @@ -38,6 +38,7 @@ import codecs from tempfile import NamedTemporaryFile + from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtCore import QCoreApplication from qgis.core import ( @@ -54,11 +55,19 @@ QgsProcessingParameterFileDestination, QgsVectorFileWriter, QgsProcessingOutputHtml, + QgsCoordinateReferenceSystem, ) cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0] sys.path.append(cmd_folder) +from cust_functions_awa import ( + check_avail_plugin_version, + upgradeMessage, +) + +curr_version = "0.2" + class AreaWeightedAverageAlgorithm(QgsProcessingAlgorithm): """ @@ -93,7 +102,7 @@ def initAlgorithm(self, config=None): self.addParameter( QgsProcessingParameterVectorLayer( "overlaylayer", - "Overlay Layer", + "Overlay Layer (Data Source)", types=[QgsProcessing.TypeVectorPolygon], defaultValue=None, ) @@ -170,6 +179,15 @@ def initAlgorithm(self, config=None): if (counter + 1) % 25 == 0: self.addOutput(QgsProcessingOutputHtml("Message", "Area Weighted Average")) + # check if new version is available of the plugin + if (counter + 1) % 4 == 0: + try: # try except because this is not a critical part + avail_version = check_avail_plugin_version("Area Weighted Average") + if avail_version != curr_version and avail_version: + upgradeMessage() + except: + pass + def processAlgorithm(self, parameters, context, model_feedback): # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the # overall progress through the model @@ -177,6 +195,32 @@ def processAlgorithm(self, parameters, context, model_feedback): results = {} outputs = {} + input_layer = self.parameterAsVectorLayer(parameters, "inputlayer", context) + overlay_layer = self.parameterAsVectorLayer(parameters, "overlaylayer", context) + + input_epsg_code = input_layer.crs().authid() + overlay_epsg_code = overlay_layer.crs().authid() + + crs_input = QgsCoordinateReferenceSystem(input_epsg_code) + crs_overlay = QgsCoordinateReferenceSystem(overlay_epsg_code) + + if crs_input.isGeographic(): + feedback.reportError( + "CRS of the Input Layer is Geographic. Results accuracy may get impacted. For most accurate results, both input and overlay layers should be in the same Projected CRS\n" + ) + + if crs_overlay.isGeographic(): + feedback.reportError( + "CRS of the Input Layer is Geographic. Results accuracy may get impacted. For most accurate results, both input and overlay layers should be in the same Projected CRS\n" + ) + + if input_epsg_code == overlay_epsg_code: + pass + else: + feedback.reportError( + "Input and Overlay Layers are in different CRS. For most accurate results, both input and overlay layers should be in the same Projected CRS\n" + ) + # add_ID_field to input layer alg_params = { "FIELD_NAME": "input_feat_id", @@ -536,7 +580,7 @@ def processAlgorithm(self, parameters, context, model_feedback): df.sort_values(by="area_prcnt", ascending=False, inplace=True) pd.set_option("display.float_format", "{:.5f}".format) - for i in range(1, total_FIDs): + for i in range(1, total_FIDs + 1): df_sub = df.loc[df["input_feat_id"] == i] df_sub.reset_index(inplace=True, drop=True) avg_value = df_sub.at[0, weighted_field] @@ -623,29 +667,30 @@ def icon(self): return icon def shortHelpString(self): - return """

Algorithm Description

-

This algorithm performs spatial area weighted average analysis on an input polygon layer given an attribute in the overlay polygon layer. Each feature in the input layer will be assigned a spatial area weighted average value of the overlay field. A report of the analysis is generated as a GIS Layer and as HTML.

+ return """

Video Tutorials

+

Algorithm Description

+

This algorithm calculates attribute value by performing spatial area weighted average analysis on an input polygon layer given an attribute in the overlay polygon layer. Each feature in the input layer will be assigned a spatial area weighted average value of the overlay field. A report of the analysis is generated as a GIS Layer and as HTML.

Input Parameters

Input Layer

Polygon layer for which area weighted average will be calculated.

Overlay Layer

-

Polygon layer overlapping the Input Layer.

+

Polygon layer with source data. Must overlap the Input Layer.

Field to Average

Single numeric field in the Overlay Layer.

Identifier Field for Report [optional]

Name or ID field in the Input Layer. This field will be used to identify features in the report.

-

Additional Fields to Keep for Report

+

Additional Fields to Keep for Report [optional]

Fields in the Overlay Layer that will be included in the reports.

Outputs

Result

-

Input layer but with the additional attribute of weighted value.

+

Input layer but with the additional attribute of field to average.

Report as Layer

-

Report as a GIS layer.

+

Report of the analysis as a GIS layer.

Report as HTML [optional]

-

Report containing feature-wise breakdown of the analysis.

+

Report of the analysis as text tables.

Algorithm author: Abdul Raheem Siddiqui

Help author: Abdul Raheem Siddiqui

-

Algorithm version: 0.1

+

Algorithm version: 0.2

Contact email: ars.work.ce@gmail.com

** If the python library pandas is not installed on the QGIS installation of python; this algorithm will try to install pandas library to the QGIS installation of python.

""" diff --git a/area_weighted_average_provider.py b/area_weighted_average_provider.py index 2b79706..0a5a7e1 100644 --- a/area_weighted_average_provider.py +++ b/area_weighted_average_provider.py @@ -23,7 +23,7 @@ """ __author__ = "Abdul Raheem Siddiqui" -__date__ = "2021-02-21" +__date__ = "2021-06-25" __copyright__ = "(C) 2021 by Abdul Raheem Siddiqui" # This will get replaced with a git SHA1 when you do a git archive diff --git a/cust_functions_awa.py b/cust_functions_awa.py new file mode 100644 index 0000000..93b2a42 --- /dev/null +++ b/cust_functions_awa.py @@ -0,0 +1,44 @@ +def check_avail_plugin_version(plugin_name: str) -> str: + import requests + import xml.etree.ElementTree as ET + from qgis.core import Qgis + + qgis_version = Qgis.QGIS_VERSION.replace("-", ".").split(".") + qgis_version = qgis_version[0] + "." + qgis_version[1] + + r = requests.get( + f"https://plugins.qgis.org/plugins/plugins.xml?qgis={qgis_version}" + ) + + xml_str = r.text + root = ET.fromstring(xml_str) + + for plugin in root.findall("pyqgis_plugin"): + name = plugin.get("name") + if name == plugin_name: + experimental = plugin.find("experimental").text + if experimental == "False": + return plugin.find("version").text + return None + + +def installPlugin(): + import pyplugin_installer + + pyplugin_installer.instance().fetchAvailablePlugins(False) + pyplugin_installer.instance().installPlugin("area_weighted_average") + + +def upgradeMessage(): + from qgis.utils import iface + from qgis.PyQt.QtWidgets import QPushButton + from qgis.core import Qgis + + widget = iface.messageBar().createMessage( + "Area Weighted Average Plugin", "Newer version of the plugin is available." + ) + button = QPushButton(widget) + button.setText("Upgrade") + button.pressed.connect(installPlugin) + widget.layout().addWidget(button) + iface.messageBar().pushWidget(widget, duration=10) diff --git a/help/source/conf.py b/help/source/conf.py index 0279fc7..cd918f6 100644 --- a/help/source/conf.py +++ b/help/source/conf.py @@ -41,16 +41,16 @@ # General information about the project. project = u"AreaWeightedAverage" -copyright = u"2013, Abdul Raheem Siddiqui" +copyright = u"2021, Abdul Raheem Siddiqui" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = "0.1" +version = "0.2" # The full version, including alpha/beta/rc tags. -release = "0.1" +release = "0.2" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/metadata.txt b/metadata.txt index f2ccb2a..06d35e6 100644 --- a/metadata.txt +++ b/metadata.txt @@ -6,7 +6,7 @@ name=Area Weighted Average qgisMinimumVersion=3.6 description=Area Weighted Average. -version=0.1 +version=0.2 author=Abdul Raheem Siddiqui email=ars.work.ce@gmail.com @@ -20,16 +20,19 @@ repository=https://github.com/ar-siddiqui/area_weighted_average hasProcessingProvider=yes # Uncomment the following line and add your changelog: -# changelog= +changelog= Version 0.2 - 2021-06-25 + - Fix bug that was causing last feature to drop in HTML report + - Warn users about different CRS + - Warn users about geographic CRS # Tags are comma separated with spaces allowed -tags=weighted, spatial, average, mean, analysis, report, overlap, spatial join, area, mean +tags=weighted, spatial, average, mean, analysis, report, overlap, spatial join, area, mean, zonal statistics homepage=https://github.com/ar-siddiqui/area_weighted_average category=Analysis icon=icon.png # experimental flag -experimental=True +experimental=False # deprecated flag (applies to the whole plugin, not just a single version) deprecated=False