TransWikia.com

I am running a processing PyQGIS script with grass i.pca but I am not getting results in my output folder

Geographic Information Systems Asked by Manuel Guerra on December 9, 2020

It runs without problem but I don’t get the expected pca bands, my output folder is empty, I don’t know what to test.

path ='C/Users/Manuel/Documents/INPUT'
output='C/Users/Manuel/Documents/OUTPUT'

bandList = [band for band in 
            os.listdir(path) if 
            band[-4:]=='.TIF']
input = [os.path.join(path,band)for band in bandList]

def PCA_GRASS(input,output):
    processing.runAndLoadResults("grass7:i.pca",
    {'-f': False,'n':False,
    'GRASS_REGION_CELLSIZE_PARAMETER' : 0,
    'GRASS_REGION_PARAMETER' : None,
    'input': input,
    'output': output,
    'percent': 99,
    'rescale':[0,400]})
  outList=[band for band in os.listdir(output) if 
                      band[-4]=='.tif']
  new_output = [ os.path.join(output,band) for band in 
                               outList ]
  for band in new_output:
           iface.addRasterLayer(band,band)

PCA_GRASS(input,output)

When I run the script I open log message and an ogr error appears (can’t open _ address of input files_) I don’t understand why
(https://i.stack.imgur.com/oAv4s.jpg)

I am working with Landsat 8 band and I have tried other images to see if the code works. Using processing whit grass I have performed other tasks satisfactorily but this one is causing me many problems. The images are not the problem when I perform the operation (i.pca) from the processing toolbox, it is done without problem.

I have also changed the syntax with which I enter the input data but nothing.

One Answer

Your code have several issues. I fixed them in following code but with only two raster layers (for time processing reasons):

import processing
import os

path =  '/home/zeito/Desktop/INPUT'
output = '/home/zeito/Desktop/OUTPUT'

bandList = [band for band in 
            os.listdir(path) if 
            band[-4:]=='.tif'] #I changed for 'tif'

input = [ os.path.join(path, band) for band in bandList ]

def PCA_GRASS(input, output):
    processing.run("grass7:i.pca",
    {'-f': False,
     'n':False,
     'GRASS_REGION_CELLSIZE_PARAMETER' : 0,
     'GRASS_REGION_PARAMETER' : None,
     'input': input, 
     'output': output,
     'percent': 99,
     'rescale':[0,400]})
    outList = [band for band in 
               os.listdir(output) if 
               band[-4:]=='.tif'] #I changed for 'tif'
    new_output = [ os.path.join(output, band) for band in outList ]
    for band in new_output:
        iface.addRasterLayer(band, band)

PCA_GRASS(input, output)

After running above script, it loads two raster layers rescaled between [0,400] as expected. It can be observed in following image:

enter image description here

Answered by xunilk on December 9, 2020

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