Skip to content

Commit

Permalink
Merge pull request #64 from ar-siddiqui/feature/aoi_info
Browse files Browse the repository at this point in the history
Version 2.2.2
  • Loading branch information
ar-siddiqui authored Aug 5, 2024
2 parents 54de32c + 02d35ed commit fbe7dda
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 16 deletions.
6 changes: 3 additions & 3 deletions curve_number_generator/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name=Curve Number Generator
qgisMinimumVersion=3.18
description=Curve Number Generator plugin has multiple algorithms to generate the curve number layer for any area of interest using different datasets.
version=2.2.1
version=2.2.2
author=Abdul Raheem Siddiqui
email=ar-siddiqui@outlook.com

Expand Down Expand Up @@ -43,13 +43,13 @@ changelog=Version 2.2.1 - 2024-07-21


# Tags are comma separated with spaces allowed
tags=curve number, CN, NLCD Land Cover, SSURGO, Soil, Hydrologic Soil Group, HSG, Hydrology, SCS, Impervious Surface
tags=curve number, CN, NLCD Land Cover, SSURGO, Soil, Hydrologic Soil Group, HSG, Hydrology, SCS, Impervious Surface, ESA, Oak Ridge National Laboratory, ORNL, ESA, European Space Agency, WorldCover

homepage=https://github.com/ar-siddiqui/curve_number_generator
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
getAndUpdateMessage,
getExtent,
getExtentArea,
getExtentWKTIn3857,
reprojectLayer,
)

Expand Down Expand Up @@ -155,8 +156,8 @@ def processAlgorithm(self, parameters, context, model_feedback):
if not parameters.get("CnLookup", None):
parameters["CnLookup"] = createDefaultLookup(cmd_folder)

area_layer = self.parameterAsVectorLayer(parameters, "aoi", context)
orig_epsg_code = area_layer.crs().authid() # preserve orignal epsg_code to project back to it
aoi_layer = self.parameterAsVectorLayer(parameters, "aoi", context)
orig_epsg_code = aoi_layer.crs().authid() # preserve orignal epsg_code to project back to it

# Reproject layer to EPSG:5070
outputs["ReprojectLayer5070"] = reprojectLayer(
Expand All @@ -165,19 +166,20 @@ def processAlgorithm(self, parameters, context, model_feedback):
context=context,
feedback=feedback,
)
area_layer = context.takeResultLayer(outputs["ReprojectLayer5070"])
aoi_layer = context.takeResultLayer(outputs["ReprojectLayer5070"])
self.aoi_wkt_3857 = getExtentWKTIn3857(aoi_layer)

epsg_code = area_layer.crs().authid()
epsg_code = aoi_layer.crs().authid()

step = 1
feedback.setCurrentStep(step)
if feedback.isCanceled():
return {}

area_acres = getExtentArea(area_layer, QgsUnitTypes.AreaAcres)
area_acres = getExtentArea(aoi_layer, QgsUnitTypes.AreaAcres)

checkAreaLimits(area_acres, 100000, 500000, feedback=feedback)
extent = getExtent(area_layer)
extent = getExtent(aoi_layer)
# add a buffer cell on each side, refer to #49 for reasoning
extent = (extent[0] - 30, extent[1] - 30, extent[2] + 30, extent[3] + 30)
bbox_dim = createRequestBBOXDim(extent, 30)
Expand Down
4 changes: 4 additions & 0 deletions curve_number_generator/processing/algorithms/custom/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
fixGeometries,
gdalPolygonize,
getAndUpdateMessage,
getExtentWKTIn3857,
)

cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
Expand Down Expand Up @@ -116,6 +117,9 @@ def processAlgorithm(self, parameters, context, model_feedback):
results = {}
outputs = {}

aoi_layer = self.parameterAsVectorLayer(parameters, "aoi", context)
self.aoi_wkt_3857 = getExtentWKTIn3857(aoi_layer)

# Prepare Land Cover for Curve Number Calculation
# Polygonize (raster to vector)
outputs["LandCoverPolygonize"] = gdalPolygonize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
generate_cn_exprs,
getAndUpdateMessage,
getExtentInEPSG4326,
getExtentWKTIn3857,
perform_raster_math,
)

Expand Down Expand Up @@ -177,6 +178,7 @@ def processAlgorithm(self, parameters, context, model_feedback):
)

aoi_layer = self.parameterAsVectorLayer(parameters, "aoi", context)
self.aoi_wkt_3857 = getExtentWKTIn3857(aoi_layer)

extent = getExtentInEPSG4326(aoi_layer)
# add a buffer cell on each side, refer to #49 for reasoning
Expand Down
11 changes: 10 additions & 1 deletion curve_number_generator/processing/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Constants
PLUGIN_VERSION = "2.2.1"
PLUGIN_VERSION = "2.2.2"

REGISTRATION_FORM_LINK = (
"https://docs.google.com/forms/d/e/1FAIpQLSe-X-OR6tFvULP6oiiPK3M49-v07sjtxTu8md9XAuBOwxk7Xg/formResponse"
Expand All @@ -12,6 +12,15 @@
"country": "entry.1731088696",
}

AOI_WKTS_FORM_LINK = (
"https://docs.google.com/forms/d/e/1FAIpQLSfDin5hEO_NfZWNlX1sClns5joAIJJZPFWffdsT-W70_1ynZg/formResponse"
)

AOI_WKTS_FORM_ENRIES = {
"algorithm": "entry.1692109239",
"aoi_wkt": "entry.725672119",
}

PROFILE_DICT = {"usage_counter": 1, "registered": False}

CONUS_NLCD_SSURGO = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import os
import sys

import requests
from qgis.core import QgsApplication, QgsProcessingAlgorithm
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtGui import QIcon

from curve_number_generator.processing.config import (
AOI_WKTS_FORM_ENRIES,
AOI_WKTS_FORM_LINK,
REGISTRATION_FORM_ENRIES,
REGISTRATION_FORM_LINK,
)
Expand Down Expand Up @@ -70,6 +73,7 @@ def __init__(self):
super().__init__()
# necessary to store LayerPostProcessor instances in class variable because of scoping issue
self.styler_dict = {}
self.aoi_wkt_3857 = ""

def postProcessAlgorithm(self, context, feedback):
try: # try-except because trivial features
Expand All @@ -88,7 +92,10 @@ def postProcessAlgorithm(self, context, feedback):
form = RegisterForm("Register Curve Number Plugin", REGISTRATION_FORM_LINK, REGISTRATION_FORM_ENRIES)
form.show()

self.postWKTInfo()

except Exception as e:
# pass
feedback.reportError(
f"Algorithm finished successfully but post processing failed. {e}",
False,
Expand Down Expand Up @@ -128,3 +135,13 @@ def tr(self, string):

def helpUrl(self):
return "mailto:ar-siddiqui@outlook.com"

def postWKTInfo(self):

data = {
AOI_WKTS_FORM_ENRIES["algorithm"]: self.name(),
AOI_WKTS_FORM_ENRIES["aoi_wkt"]: self.aoi_wkt_3857,
}

r = requests.post(AOI_WKTS_FORM_LINK, data=data, timeout=5)
r.raise_for_status()
16 changes: 10 additions & 6 deletions curve_number_generator/processing/tools/registration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import requests
from curve_number_generator.processing.config import LIST_OF_COUNTRIES
from curve_number_generator.processing.tools.utils import getMessageWidget, setRegistrationTrue, displayMessageWidget
from qgis import processing
from qgis.core import (
QgsFeatureSink,
Expand All @@ -11,12 +9,18 @@
QgsProcessingParameterFeatureSource,
)
from qgis.gui import QgsMessageBar
from qgis.PyQt.QtCore import QCoreApplication, QRegExp, pyqtSlot, Qt
from qgis.PyQt.QtWidgets import *
from qgis.PyQt.QtCore import QCoreApplication, QRegExp, Qt, pyqtSlot
from qgis.PyQt.QtGui import QFont

from qgis.PyQt.QtWidgets import *
from qgis.utils import iface

from curve_number_generator.processing.config import LIST_OF_COUNTRIES
from curve_number_generator.processing.tools.utils import (
displayMessageWidget,
getMessageWidget,
setRegistrationTrue,
)


class RegisterForm(QDialog):
def __init__(self, window_title: str, formResponseLink: str, formEntries: dict):
Expand Down Expand Up @@ -89,7 +93,7 @@ def postInfo(self):
self.formEntries["country"]: self.countryComboBox.currentText(),
}

r = requests.post(self.formResponseLink, data=data)
r = requests.post(self.formResponseLink, data=data, timeout=5)
if r.status_code == 200:
setRegistrationTrue()
widget = getMessageWidget(
Expand Down
11 changes: 11 additions & 0 deletions curve_number_generator/processing/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ def getExtentInEPSG4326(layer) -> tuple:
return xmin, ymin, xmax, ymax


def getExtentWKTIn3857(layer) -> str:
source_crs = layer.crs()
target_crs = QgsCoordinateReferenceSystem("EPSG:3857")
transform = QgsCoordinateTransform(source_crs, target_crs, QgsProject.instance())

extent = layer.extent()
extent_3857 = transform.transform(extent)

return extent_3857.asWktPolygon()


def getExtent(layer) -> tuple:
# Get extent of the area boundary layer
extent = layer.extent()
Expand Down

0 comments on commit fbe7dda

Please sign in to comment.