Last active
July 8, 2020 15:47
-
-
Save fgianoli/46a45c5fe042a952580947a282feedc1 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
from qgis.core import QgsProcessing | |
from qgis.core import QgsProcessingAlgorithm | |
from qgis.core import QgsProcessingMultiStepFeedback | |
from qgis.core import QgsProcessingParameterRasterLayer | |
from qgis.core import QgsProcessingParameterRasterDestination | |
from qgis.core import QgsCoordinateReferenceSystem | |
import processing | |
class Copernicus(QgsProcessingAlgorithm): | |
def initAlgorithm(self, config=None): | |
self.addParameter(QgsProcessingParameterRasterLayer('raster', 'raster', defaultValue=None)) | |
self.addParameter(QgsProcessingParameterRasterDestination('Final_resampled', 'final_resampled', 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(5, model_feedback) | |
results = {} | |
outputs = {} | |
input_raster = self.parameterAsRasterLayer(parameters, 'raster', context) | |
Xmin = -180 + ((1 / 112) / 2) | |
Xmax = 180 - ((1 / 112) / 2) | |
Ymax = 80 - ((1 / 112) / 2) | |
Ymin = -60 + ((1 / 112) / 2) | |
pixelX = 1. / 112. | |
pixelY = 1. / 112. | |
stats = input_raster.dataProvider().bandStatistics(1) | |
src_min = stats.minimumValue | |
src_max = stats.maximumValue | |
dst_min = stats.minimumValue | |
dst_max = stats.maximumValue | |
print(src_min, src_max, dst_min, dst_max) | |
print("Checkpoint Charlie: ", Xmin, Xmax, Ymax, Ymin, pixelX) | |
# Riclassifica con tabella | |
alg_params = { | |
'DATA_TYPE': 5, | |
'INPUT_RASTER': parameters['raster'], | |
'NODATA_FOR_MISSING': False, | |
'NO_DATA': -9999, | |
'RANGE_BOUNDARIES': 0, | |
'RASTER_BAND': 1, | |
'TABLE': [-1,1,1,1,255,0], | |
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT | |
} | |
outputs['RiclassificaConTabella'] = processing.run('native:reclassifybytable', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
feedback.setCurrentStep(1) | |
if feedback.isCanceled(): | |
return {} | |
# translate Average | |
tra_extra = "-of Gtiff -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 " | |
tra_extra += " -projwin " + str(Xmin) + " " + str(Ymax) + " " + str(Xmax) + " " + str(Ymin) | |
tra_extra += " -r average -tr " + str(pixelX) + " " + str(pixelY) | |
tra_extra += " -scale " + str(src_min) + " " + str(src_max) + " " + str(dst_min) + " " + str(dst_max) | |
alg_params = { | |
'COPY_SUBDATASETS': False, | |
'DATA_TYPE': 0, | |
'EXTRA': tra_extra, | |
'INPUT': parameters['raster'], | |
'NODATA': None, | |
'OPTIONS': '', | |
'TARGET_CRS': None, | |
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT | |
} | |
outputs['TranslateAverage'] = processing.run('gdal:translate', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
feedback.setCurrentStep(2) | |
if feedback.isCanceled(): | |
return {} | |
# Raster calculator | |
alg_params = { | |
'CELLSIZE': 0, | |
'CRS': QgsCoordinateReferenceSystem('EPSG:4326'), | |
'EXPRESSION': '\"\'Convertito\' from algorithm \'translate MODE\'@1\" * \"\'Convertito\' from algorithm \'translate Average\'@1\"', | |
'EXTENT': None, | |
'LAYERS': outputs['TranslateAverage']['OUTPUT'], | |
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT | |
} | |
outputs['RasterCalculator'] = processing.run('qgis:rastercalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
feedback.setCurrentStep(3) | |
if feedback.isCanceled(): | |
return {} | |
# translate MODE | |
tra_extra_mode = "-of Gtiff -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 " | |
tra_extra_mode += " -projwin " + str(Xmin) + " " + str(Ymax) + " " + str(Xmax) + " " + str(Ymin) | |
tra_extra_mode += " -r mode -tr " + str(pixelX) + " " + str(pixelY) | |
alg_params = { | |
'COPY_SUBDATASETS': False, | |
'DATA_TYPE': 0, | |
'EXTRA': tra_extra_mode, | |
'INPUT': outputs['RiclassificaConTabella']['OUTPUT'], | |
'NODATA': None, | |
'OPTIONS': '', | |
'TARGET_CRS': None, | |
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT | |
} | |
outputs['TranslateMode'] = processing.run('gdal:translate', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
feedback.setCurrentStep(4) | |
if feedback.isCanceled(): | |
return {} | |
# Riclassifica con tabella | |
alg_params = { | |
'DATA_TYPE': 5, | |
'INPUT_RASTER': outputs['RasterCalculator']['OUTPUT'], | |
'NODATA_FOR_MISSING': False, | |
'NO_DATA': -9999, | |
'RANGE_BOUNDARIES': 0, | |
'RASTER_BAND': 1, | |
'TABLE': [-9999,-1,-9999,1,9999,-9999], | |
'OUTPUT': parameters['Final_resampled'] | |
} | |
outputs['RiclassificaConTabella'] = processing.run('native:reclassifybytable', alg_params, context=context, feedback=feedback, is_child_algorithm=True) | |
results['Final_resampled'] = outputs['RiclassificaConTabella']['OUTPUT'] | |
return results | |
def name(self): | |
return 'Copernicus' | |
def displayName(self): | |
return 'Copernicus' | |
def group(self): | |
return 'Copernicus' | |
def groupId(self): | |
return 'Copernicus' | |
def createInstance(self): | |
return Copernicus() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment