TransWikia.com

QGIS Merge tool in Python with glob

Geographic Information Systems Asked by Nathan Fleischman on November 27, 2020

I am testing this code, which I have been fixing:

import os
import sys
from glob import glob
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterRasterDestination
import processing
import json

sys.argv = ['I:nfleischQGIS ToolsMosaicLandsat8.py', 'I:nfleischQGIS ToolsMosaicLandsat8.json']

class mosaic(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        jsonfile=sys.argv[1]
        print(jsonfile)
        with open(jsonfile,"r") as fp:
            objs = json.load(fp)
            print(objs)
        for command in objs:
            specifications = command["specifications"]
            print(specifications)
            # Merge
            bound_area = specifications["bound_area"]
            print(bound_area)
            row_path_list = specifications["row_path_list"]
            print(row_path_list)
            bands = specifications["bands"]
            print(bands)
            years = specifications["years"]
            print(years)
            input_folder = specifications["input_folder"]
            output_folder = specifications["output_folder"]
            for year in years:
                print(year)
                for band in bands:
                    print(band)
                    self.addParameter(QgsProcessingParameterRasterDestination('gdal:merge_1:Mosaic', 'Mosaic', createByDefault=True, defaultValue='{}/{}_Mosaic_{}_{}.TIF'.format(output_folder,bound_area,year,band)))

    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(1, model_feedback)
        results = {}
        outputs = {}
        
        jsonfile=sys.argv[1]
        print(jsonfile)
        with open(jsonfile,"r") as fp:
            objs = json.load(fp)
            print(objs)
        for command in objs:
            specifications = command["specifications"]
            print(specifications)
            # Merge
            bound_area = specifications["bound_area"]
            print(bound_area)
            row_path_list = specifications["row_path_list"]
            print(row_path_list)
            bands = specifications["bands"]
            print(bands)
            years = specifications["years"]
            print(years)
            input_folder = specifications["input_folder"]
            output_folder = specifications["output_folder"]
            for year in years:
                print(year)
                for band in bands:
                    print(band)
                    input_rasters = []
                    for row_path in row_path_list:
                        print(row_path)
                        input_rasters.append(glob("{}/LC08_L1TP_{}_{}*/Preprocessed_{}.TIF".format(input_folder,row_path, year, band)))
                    print(input_rasters)
                    alg_params = {
                        'DATA_TYPE': 2,
                        'EXTRA': '',
                        'INPUT': input_rasters,
                        'NODATA_INPUT': None,
                        'NODATA_OUTPUT': None,
                        'OPTIONS': '',
                        'PCT': False,
                        'SEPARATE': False,
                        'OUTPUT': '{}/{}_Mosaic_{}_{}.TIF'.format(output_folder,bound_area,year,band)
                    }
                    outputs['Merge'] = processing.run('gdal:merge', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
                    results['Mosaic'] = outputs['Merge']['OUTPUT']
        return results

    def name(self):
        return 'model'

    def displayName(self):
        return 'model'

    def group(self):
        return ''

    def groupId(self):
        return ''

    def createInstance(self):
        return mosaic()

The problem I have is that I keep getting this error:

Unable to execute algorithm
Incorrect parameter value for INPUT
Unable to execute algorithm
Incorrect parameter value for INPUT
Execution failed after 0.26 seconds

In similar code that I fixed, the problem was the glob, which was unnecessary. But in this case, I need the glob. What is causing the error?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP