3

I'm trying to develop code that is capable of cutting out a mosaic from the polygons of a shapefile using PyQGIS.

The script is run but does not produce any results.


from qgis.core import *
from qgis.gui import *
import processing   
from processing.core.Processing import Processing
import gdal

VECTOR = QgsVectorLayer('C:/Users/phpor/Desktop/','quadricula.shp','ogr')
RASTER = QgsRasterLayer('C:/Users/phpor/Documents/SPU/OS_01/Georreferenciamento_Mosaico_OS_01/Raster/02_Processo/','Aerofoto_50000_Joinville_Mosaico_Recorte.tif')

RASTER.isValid()

row_info = VECTOR.getFeatures()
for row in row_info:
    row_name = row['indice']
    output_file = "C:/Users/phpor/Desktop/teste/" + "cliped_" + str(row['indice']) + ".tif"
    VECTOR.setSubsetString("indice=" + str(row['indice']))

    parameters = {'INPUT': RASTER,'MASK': VECTOR,'NODATA': 255.0,'ALPHA_BAND': False,'CROP_TO_CUTLINE': True,'KEEP_RESOLUTION': True,'OPTIONS': None,'DATA_TYPE': 0,'OUTPUT': output_file}

    clip = processing.run('gdal:cliprasterbymasklayer', parameters)

    VECTOR.setSubsetString('')

print("done")

Now, with some adjusts, I can create a all rasters with the corrects names, but the iteration mask (polygon selected in each iteration) are not respected. In others words, the output of the mask tool are the same for all files (the original draw of the mask).

from qgis.core import *
from qgis.gui import *
import processing
from processing.core.Processing import Processing
import gdal

VECTOR = QgsVectorLayer('C:/Users/phpor/Desktop/quadricula.shp','quadricula.shp','ogr')
RASTER = QgsRasterLayer('C:/Users/phpor/Desktop/aerofoto.tif','aerofoto.tif')
if RASTER.isValid():
    print("ok")
else:
    print("not ok")

Processing.initialize()

row_info = VECTOR.getFeatures()
for row in row_info:
    row_name = row['indice']
    output_file = "C:/Users/phpor/Desktop/teste/" + "cliped_" + str(row['indice']) + ".tif"
    VECTOR.setSubsetString("indice=" + str(row['indice']))

    parameters = {'INPUT': RASTER,
            'MASK': VECTOR,
            'NODATA': 250.0,
            'ALPHA_BAND': False,
            'CROP_TO_CUTLINE': True,
            'KEEP_RESOLUTION': True,
            'OPTIONS': None,
            'DATA_TYPE': 0,
            'OUTPUT': output_file}

    print(row_name)

    clip = processing.run('gdal:cliprasterbymasklayer', parameters)

    VECTOR.setSubsetString('')

 print("done")
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
phporath
  • 31
  • 3
  • Is your vector and raster in the same coordinate system? What does 'not produce any results' mean? Are output files created but full of nodata? Or 0? or no output produced? Are there any error messages? – Michael Stimson Feb 13 '20 at 05:51
  • Yes, my vector and raster are in the same coordinate system. When a I run the script the result are just this:

    "exec(open('C:/Users/phpor/AppData/Local/Temp/tmp_2k__v3b.py'.encode('utf-8')).read())

    done"

    So, It doesn't show any messages error, but in the output address does not create any file.

    – phporath Feb 13 '20 at 12:26

0 Answers0