Skip to content

Commit

Permalink
Merge pull request #51 from ar-siddiqui/feature/misc-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ar-siddiqui authored Oct 25, 2023
2 parents 2ae771f + cc705b7 commit 95f4527
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
6 changes: 4 additions & 2 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.1.2
version=2.1.3
author=Abdul Raheem Siddiqui
[email protected]

Expand All @@ -21,7 +21,9 @@ repository=https://github.com/ar-siddiqui/curve_number_generator

hasProcessingProvider=yes
# Uncomment the following line and add your changelog:
changelog= Version 2.1.2 - 2023-05-24
changelog= Version 2.1.3 - 2023-10-17
- Change NLCD Land Cover and Impervious layer to 2021 dataset
Version 2.1.2 - 2023-05-24
- conus_nlcd_ssurgo: Fix missing data on corners of a rectangle AOI
Version 2.1.1 - 2023-03-19
- Fix custom algorithm formula
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
getExtent,
getExtentArea,
reprojectLayer,
getAndUpdateMessage,
)
from qgis.core import (
QgsCoordinateReferenceSystem,
Expand Down Expand Up @@ -181,13 +182,13 @@ def processAlgorithm(self, parameters, context, model_feedback):
# NLCD Impervious Raster
if parameters.get("NLCDImpervious", None):
outputs["DownloadNlcdImp"] = downloadFile(
CONUS_NLCD_SSURGO["NLCD_IMP_2019"].format(
CONUS_NLCD_SSURGO["NLCD_IMP_2021"].format(
epsg_code,
bbox_dim[0],
bbox_dim[1],
",".join([str(item) for item in extent]),
),
"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Impervious_L48/ows",
"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2021_Impervious_L48/ows",
"Error requesting land use data from 'www.mrlc.gov'. Most probably because either their server is down or there is a certification issue.\nThis should be temporary. Try again later.\n",
context=context,
feedback=feedback,
Expand Down Expand Up @@ -225,13 +226,13 @@ def processAlgorithm(self, parameters, context, model_feedback):
# NLCD Land Cover Data
if any([parameters.get("NLCDLandCover", None), parameters.get("CurveNumber", None)]):
outputs["DownloadNlcdLC"] = downloadFile(
CONUS_NLCD_SSURGO["NLCD_LC_2019"].format(
CONUS_NLCD_SSURGO["NLCD_LC_2021"].format(
epsg_code,
bbox_dim[0],
bbox_dim[1],
",".join([str(item) for item in extent]),
),
"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/ows",
"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2021_Land_Cover_L48/ows",
"Error requesting land use data from 'www.mrlc.gov'. Most probably because either their server is down or there is a certification issue.\nThis should be temporary. Try again later.\n",
context=context,
feedback=feedback,
Expand Down Expand Up @@ -452,7 +453,13 @@ def icon(self):
return icon

def shortHelpString(self):
return f"""<html><body><a "href"="https://github.com/ar-siddiqui/curve_number_generator/wiki/Tutorials#curve-number-generator-conus-nlcd--ssurgo">Video Tutorial</a></h3>
msg = ""
try:
msg = getAndUpdateMessage()
except Exception as e:
print(e)

return msg + f"""<html><body><a "href"="https://github.com/ar-siddiqui/curve_number_generator/wiki/Tutorials#curve-number-generator-conus-nlcd--ssurgo">Video Tutorial</a></h3>
<h2>Algorithm description</h2>
<p>This algorithm generates Curve Number layer for the given Area of Interest within the contiguous United States. It can also download Soil, Land Cover, and Impervious Surface datasets for the same area.</p>
<h2>Input parameters</h2>
Expand All @@ -468,9 +475,9 @@ def shortHelpString(self):
If checked the algorithm will assume HSG A/B/C for each dual category soil.</p>
<h2>Outputs</h2>
<h3>NLCD Land Cover</h3>
<p>NLCD 2019 Land Cover Raster</p>
<p>NLCD 2021 Land Cover Raster</p>
<h3>NLCD Impervious Surface</h3>
<p>NLCD 2019 Impervious Surface Raster</p>
<p>NLCD 2021 Impervious Surface Raster</p>
<h3>Soils</h3>
<p>SSURGO Extended Soil Dataset </p>
<h3>Curve Number</h3>
Expand Down
9 changes: 7 additions & 2 deletions curve_number_generator/processing/algorithms/custom/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from curve_number_generator.processing.config import PLUGIN_VERSION
from curve_number_generator.processing.curve_number_generator_algorithm import CurveNumberGeneratorAlgorithm
from curve_number_generator.processing.tools.curve_numper import CurveNumber
from curve_number_generator.processing.tools.utils import clip, fixGeometries, gdalPolygonize
from curve_number_generator.processing.tools.utils import clip, fixGeometries, gdalPolygonize, getAndUpdateMessage
from qgis.core import (
QgsProcessing,
QgsProcessingMultiStepFeedback,
Expand Down Expand Up @@ -195,7 +195,12 @@ def icon(self):
return icon

def shortHelpString(self):
return f"""<html><body><a "href"="https://github.com/ar-siddiqui/curve_number_generator/wiki/Tutorials#curve-number-generator-custom">Video Tutorial</a></h3>
try:
msg = getAndUpdateMessage()
except Exception as e:
print(e)

return msg + f"""<html><body><a "href"="https://github.com/ar-siddiqui/curve_number_generator/wiki/Tutorials#curve-number-generator-custom">Video Tutorial</a></h3>
<h2>Algorithm description</h2>
<p>This algorithm generates Curve Number layer for the given Area of Interest given a Land Cover Raster, Soil Layer, and a Lookup Table.</p>
<h2>Input parameters</h2>
Expand Down
8 changes: 5 additions & 3 deletions curve_number_generator/processing/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Constants
PLUGIN_VERSION = "2.1.2"
PLUGIN_VERSION = "2.1.3"

REGISTRATION_FORM_LINK = (
"https://docs.google.com/forms/d/e/1FAIpQLSe-X-OR6tFvULP6oiiPK3M49-v07sjtxTu8md9XAuBOwxk7Xg/formResponse"
Expand All @@ -16,11 +16,13 @@

CONUS_NLCD_SSURGO = {
# urls
"NLCD_IMP_2019": "https://www.mrlc.gov/geoserver/ows?version=1.1.0&SERVICE=WCS&VERSION=1.0.0&REQUEST=GetCoverage&FORMAT=GeoTIFF&COVERAGE=mrlc_download:NLCD_2019_Impervious_L48&CRS={}&WIDTH={}&HEIGHT={}&BBOX={}&",
"NLCD_LC_2019": "https://www.mrlc.gov/geoserver/ows?version=1.1.0&SERVICE=WCS&VERSION=1.0.0&REQUEST=GetCoverage&FORMAT=GeoTIFF&COVERAGE=mrlc_download:NLCD_2019_Land_Cover_L48&CRS={}&WIDTH={}&HEIGHT={}&BBOX={}&",
"NLCD_IMP_2021": "https://www.mrlc.gov/geoserver/ows?version=1.1.0&SERVICE=WCS&VERSION=1.0.0&REQUEST=GetCoverage&FORMAT=GeoTIFF&COVERAGE=mrlc_download:NLCD_2021_Impervious_L48&CRS={}&WIDTH={}&HEIGHT={}&BBOX={}&",
"NLCD_LC_2021": "https://www.mrlc.gov/geoserver/ows?version=1.1.0&SERVICE=WCS&VERSION=1.0.0&REQUEST=GetCoverage&FORMAT=GeoTIFF&COVERAGE=mrlc_download:NLCD_2021_Land_Cover_L48&CRS={}&WIDTH={}&HEIGHT={}&BBOX={}&",
"SSURGO_Soil": "https://sdmdataaccess.sc.egov.usda.gov/Spatial/SDMWGS84GEOGRAPHIC.wfs?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=mapunitpolyextended&SRSNAME=EPSG:4326&BBOX={}",
}

MESSAGE_URL = "https://gist.githubusercontent.com/ar-siddiqui/2260461cfd0107150840ab6fb4f83516/raw"

LIST_OF_COUNTRIES = [
"",
"Afghanistan",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
sys.path.append(cmd_folder)
qgis_settings_path = QgsApplication.qgisSettingsDirPath().replace("\\", "/")
cn_log_path = os.path.join(qgis_settings_path, "curve_number_generator.log")

__author__ = "Abdul Raheem Siddiqui"
__date__ = "2022-06-29"
Expand Down
40 changes: 38 additions & 2 deletions curve_number_generator/processing/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
import pickle
import requests
import time
import xml.etree.ElementTree as ET

import processing
import requests
from curve_number_generator.processing.config import PLUGIN_VERSION, PROFILE_DICT
from curve_number_generator.processing.config import PLUGIN_VERSION, PROFILE_DICT, MESSAGE_URL
from qgis.core import (
Qgis,
QgsApplication,
Expand All @@ -21,7 +23,41 @@
qgis_settings_path = QgsApplication.qgisSettingsDirPath().replace("\\", "/")
cn_log_path = os.path.join(qgis_settings_path, "curve_number_generator.log")
cn_pickle_path = os.path.join(qgis_settings_path, "curve_number_generator.p")

cn_msg_path = os.path.join(qgis_settings_path, 'curve_number_generator_msg.html')
cn_msg_cache_duration = 24 * 60 * 60 # 24 hours in seconds

def fetchMessage(url, timeout=2) -> str:
response = requests.get(url, timeout=timeout)
response.raise_for_status()
return response.text


def saveToCache(message):
with open(cn_msg_path, 'w') as file:
file.write(message)

def isCacheValid():
if os.path.exists(cn_msg_path):
file_timestamp = os.path.getmtime(cn_msg_path)
if time.time() - file_timestamp < cn_msg_cache_duration:
return True
return False

def loadMessageFromCache():
if isCacheValid():
with open(cn_msg_path, 'r') as file:
text = file.read()
return text
return ""

def getAndUpdateMessage():
cached_message = loadMessageFromCache()
if cached_message:
return cached_message

fetched_message = fetchMessage(MESSAGE_URL)
saveToCache(fetched_message)
return fetched_message

def incrementUsageCounter() -> int:
# log usage
Expand Down

0 comments on commit 95f4527

Please sign in to comment.