Skip to content

Commit

Permalink
Merge pull request #12 from ar-siddiqui/feature/v0.2
Browse files Browse the repository at this point in the history
Feature/v0.2
  • Loading branch information
ar-siddiqui authored Jun 25, 2021
2 parents 2140b96 + d64ca2f commit 8283d2d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
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

- 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
Expand All @@ -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: [email protected]

## Donate
Expand Down
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""

__author__ = "Abdul Raheem Siddiqui"
__date__ = "2021-02-21"
__date__ = "2021-06-25"
__copyright__ = "(C) 2021 by Abdul Raheem Siddiqui"


Expand Down
2 changes: 1 addition & 1 deletion area_weighted_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 56 additions & 11 deletions area_weighted_average_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand All @@ -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):
"""
Expand Down Expand Up @@ -93,7 +102,7 @@ def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterVectorLayer(
"overlaylayer",
"Overlay Layer",
"Overlay Layer (Data Source)",
types=[QgsProcessing.TypeVectorPolygon],
defaultValue=None,
)
Expand Down Expand Up @@ -170,13 +179,48 @@ 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
feedback = QgsProcessingMultiStepFeedback(13, 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",
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -623,29 +667,30 @@ def icon(self):
return icon

def shortHelpString(self):
return """<html><body><h2>Algorithm Description</h2>
<p>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.</p>
return """<html><body><h3><a "href"="https://github.com/ar-siddiqui/area_weighted_average/wiki/Tutorials">Video Tutorials</a></h3>
<h2>Algorithm Description</h2>
<p>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.</p>
<h2>Input Parameters</h2>
<h3>Input Layer</h3>
<p>Polygon layer for which area weighted average will be calculated.</p>
<h3>Overlay Layer</h3>
<p>Polygon layer overlapping the Input Layer.</p>
<p>Polygon layer with source data. Must overlap the Input Layer.</p>
<h3>Field to Average</h3>
<p>Single numeric field in the Overlay Layer.</p>
<h3>Identifier Field for Report [optional]</h3>
<p>Name or ID field in the Input Layer. This field will be used to identify features in the report.</p>
<h3>Additional Fields to Keep for Report</h3>
<h3>Additional Fields to Keep for Report [optional]</h3>
<p>Fields in the Overlay Layer that will be included in the reports.</p>
<h2>Outputs</h2>
<h3>Result</h3>
<p>Input layer but with the additional attribute of weighted value.</p>
<p>Input layer but with the additional attribute of field to average.</p>
<h3>Report as Layer</h3>
<p>Report as a GIS layer.</p>
<p>Report of the analysis as a GIS layer.</p>
<h3>Report as HTML [optional]</h3>
<p>Report containing feature-wise breakdown of the analysis.</p>
<p>Report of the analysis as text tables.</p>
<p align="right">Algorithm author: Abdul Raheem Siddiqui</p>
<p align="right">Help author: Abdul Raheem Siddiqui</p>
<p align="right">Algorithm version: 0.1</p>
<p align="right">Algorithm version: 0.2</p>
<p align="right">Contact email: [email protected]</p>
<p>** 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.</p>
</body></html>"""
Expand Down
2 changes: 1 addition & 1 deletion area_weighted_average_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions cust_functions_awa.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions help/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 7 additions & 4 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]

Expand All @@ -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
Expand Down

0 comments on commit 8283d2d

Please sign in to comment.