Created
August 17, 2022 12:43
-
-
Save lymperis-e/b3e30a0d0649fcdf886ee4d1df52c5d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Model exported as python. | |
Name : Hillshade & Contours | |
Group : DEM Visualisation | |
With QGIS : 31803 | |
""" | |
#REQUIRES THE TERRAIN SHADING PLUGIN | |
from qgis.core import QgsProcessing | |
from qgis.core import QgsProcessingAlgorithm | |
from qgis.core import QgsProcessingMultiStepFeedback | |
from qgis.core import QgsProcessingParameterRasterLayer | |
from qgis.core import QgsProcessingParameterNumber | |
from qgis.core import QgsProcessingParameterVectorDestination | |
from qgis.core import QgsProcessingParameterRasterDestination | |
import processing | |
class HillshadeContours(QgsProcessingAlgorithm): | |
def initAlgorithm(self, config=None): | |
self.addParameter(QgsProcessingParameterRasterLayer('DEM', 'DEM', defaultValue=None)) | |
self.addParameter(QgsProcessingParameterNumber('LateralExaggeration', 'Lateral Exaggeration', type=QgsProcessingParameterNumber.Double, minValue=0.1, defaultValue=5)) | |
self.addParameter(QgsProcessingParameterVectorDestination('Contours', 'Contours', type=QgsProcessing.TypeVectorLine, createByDefault=True, defaultValue=None)) | |
self.addParameter(QgsProcessingParameterRasterDestination('HillshadedDem', 'Hillshaded DEM', createByDefault=True, defaultValue=None)) | |
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(4, model_feedback) | |
results = {} | |
outputs = {} | |
# 1st Gaussian Filter | |
alg_params = { | |
'INPUT': parameters['DEM'], | |
'MODE': 1, | |
'RADIUS': 3, | |
'SIGMA': 2, | |
'RESULT': QgsProcessing.TEMPORARY_OUTPUT | |
} | |
outputs['StGaussianFilter'] = processing.run('saga:gaussianfilter', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
feedback.setCurrentStep(1) | |
if feedback.isCanceled(): | |
return {} | |
# Contour Lines Extraction | |
alg_params = { | |
'BAND': 1, | |
'CREATE_3D': False, | |
'EXTRA': '', | |
'FIELD_NAME': 'ELEV', | |
'IGNORE_NODATA': False, | |
'INPUT': outputs['StGaussianFilter']['RESULT'], | |
'INTERVAL': 20, | |
'NODATA': None, | |
'OFFSET': 0, | |
'OUTPUT': parameters['Contours'] | |
} | |
outputs['ContourLinesExtraction'] = processing.run('gdal:contour', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
results['Contours'] = outputs['ContourLinesExtraction']['OUTPUT'] | |
feedback.setCurrentStep(2) | |
if feedback.isCanceled(): | |
return {} | |
# Terrain Shading Plugin Hillshade | |
alg_params = { | |
'ANGLE': 70, | |
'BIDIRECTIONAL': True, | |
'BYTE_FORMAT': False, | |
'DENOISE': True, | |
'DIRECTION': 315, | |
'INPUT': outputs['StGaussianFilter']['RESULT'], | |
'LAT_EX': parameters['LateralExaggeration'], | |
'LONG_EX': 1, | |
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT | |
} | |
outputs['TerrainShadingPluginHillshade'] = processing.run('terrain_shading:Hillshade (terrain shading)', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
feedback.setCurrentStep(3) | |
if feedback.isCanceled(): | |
return {} | |
# 2nd Gaussian Filter | |
alg_params = { | |
'INPUT': outputs['TerrainShadingPluginHillshade']['OUTPUT'], | |
'MODE': 1, | |
'RADIUS': 3, | |
'SIGMA': 2, | |
'RESULT': parameters['HillshadedDem'] | |
} | |
outputs['NdGaussianFilter'] = processing.run('saga:gaussianfilter', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
results['HillshadedDem'] = outputs['NdGaussianFilter']['RESULT'] | |
return results | |
def name(self): | |
return 'Hillshade & Contours' | |
def displayName(self): | |
return 'Hillshade & Contours' | |
def group(self): | |
return 'DEM Visualisation' | |
def groupId(self): | |
return '' | |
def createInstance(self): | |
return HillshadeContours() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment