I am using python and QGIS 2.0. I am trying to clip rasters in a folder by one polygon feature. It's the first time for me using (let's say) "PyQGIS", I was used to arcpy before. Anyway, I don't get my simple script to work, any suggestion would be very appreciated!
import qgis.core, qgis,utils QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True) QgsApplication.initQgis() CLIP= "C:/Users/unim/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/study_area_foscagno.shp" INPUT_FOLDER="C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/LE71930282000259EDC00" OUTPUT= "C:/Users/unim/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/foscagno_pyqgis/" for RASTER in INPUT_FOLDER.tif do echo "Processing $RASTER" gdalwarp -q -cutline CLIP -crop_to_cutline -of GTiff RASTER OUTPUT+ "clip_"+ RASTER done QgsApplication.exitQgis() Below are the improvements I've done since now, not getting the script to work though, but I think I might be getting closer...
import qgis.core, qgis.utils, os, fnmatch from osgeo import gdal CLIP= "C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/study_area_foscagno.shp" INPUT_FOLDER= "C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/LE71930282000259EDC00/DNs2Reflectance_LE71930282000259EDC00" OUTPUT= "C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/Cloud_mask_AltaValtellina/clip_2_foscagno" def findRasters (path, filter): for root, dirs, files in os.walk(path): for file in fnmatch.filter(files, filter): yield os.path.join (root, file) for raster in findRasters (INPUT_FOLDER, '*.tif'): print (raster) outRaster = OUTPUT + '/clip_' + raster cmd = 'gdalwarp -dstnodata 0 -q -cutline CLIP -crop_to_cutline %s %s' % (raster, outRaster) os.system (cmd) I think there might be something wrong in the "gdal" command, as the "print" function does its job properely, but no file is written to the output, neither I receive any error. By the way, it's been hard to fond an easy documentation on gdal coding...
أكثر...
import qgis.core, qgis,utils QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True) QgsApplication.initQgis() CLIP= "C:/Users/unim/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/study_area_foscagno.shp" INPUT_FOLDER="C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/LE71930282000259EDC00" OUTPUT= "C:/Users/unim/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/foscagno_pyqgis/" for RASTER in INPUT_FOLDER.tif do echo "Processing $RASTER" gdalwarp -q -cutline CLIP -crop_to_cutline -of GTiff RASTER OUTPUT+ "clip_"+ RASTER done QgsApplication.exitQgis() Below are the improvements I've done since now, not getting the script to work though, but I think I might be getting closer...
import qgis.core, qgis.utils, os, fnmatch from osgeo import gdal CLIP= "C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/study_area_foscagno.shp" INPUT_FOLDER= "C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/LE71930282000259EDC00/DNs2Reflectance_LE71930282000259EDC00" OUTPUT= "C:/Users/unimi/Documents/Umberto/Universita/PhD/Guglielmin/Permafrost/Alta_Valtellina/Landsat_ita/Cloud_mask_AltaValtellina/clip_2_foscagno" def findRasters (path, filter): for root, dirs, files in os.walk(path): for file in fnmatch.filter(files, filter): yield os.path.join (root, file) for raster in findRasters (INPUT_FOLDER, '*.tif'): print (raster) outRaster = OUTPUT + '/clip_' + raster cmd = 'gdalwarp -dstnodata 0 -q -cutline CLIP -crop_to_cutline %s %s' % (raster, outRaster) os.system (cmd) I think there might be something wrong in the "gdal" command, as the "print" function does its job properely, but no file is written to the output, neither I receive any error. By the way, it's been hard to fond an easy documentation on gdal coding...
أكثر...