Feature request #8602

Clipper for multiple rasters

Added by Luca Congedo about 7 years ago. Updated about 7 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:-
Pull Request or Patch supplied:No Resolution:
Easy fix?:No Copied to github as #:17344

Description

Hello everybody,

I have written a Python script for clippling multiple rasters at once using gdal_translate (see http://fromgistors.blogspot.com/2013/09/how-to-clip-multiple-rasters-at-once.html).

# input variables
# where to save clipped rasters
outputDir = "/home/user/Desktop/clip" 
# upper left point
UX, UY =  298545 , 4628145
# lower right point
LX,  LY = 308715 , 4622745

# actual script
import os
lddLrs = qgis.utils.iface.legendInterface().layers()
for lyr in lddLrs:
 if (lyr.type()==QgsMapLayer.RasterLayer):
  os.system("gdal_translate -projwin " + str(UX) + " " + str(UY) + " " + str(LX) + " " + str(LY) + " -of GTiff " + str(lyr.source()) + " " + outputDir + "/" + str(lyr.name()) + ".tif")
  qgis.utils.iface.addRasterLayer(str(outputDir + "/" + lyr.name() + ".tif"), str(lyr.name() + "_clip.tif"))

It would be nice if it can be implemented in the main Clipper tool in the Raster menu, or maybe ported to C++.

Cheers!

Clip_Multiple_Rasters.py Magnifier - Script (978 Bytes) Luca Congedo, 2013-09-13 01:28 AM

Clip_Multiple_Rasters.py.help - Help file (1.34 KB) Luca Congedo, 2013-09-13 01:28 AM

Associated revisions

Revision 9ce316db
Added by Alexander Bruy about 7 years ago

[processing] add Clip raster by extent algorithm (fix #8602)

History

#1 Updated by Paolo Cavallini about 7 years ago

I suggest to add it to Processing instead: it should be much easier and more flexible.

#2 Updated by Luca Congedo about 7 years ago

It sounds good to me.
I have written the script for the Processing framework in attachment. I have also added the option for No data values.

##Raster=group
##Input=multiple raster
# upper left point
##UX=number 
##UY=number 
# lower right point
##LX=number 
##LY=number
##No_data=boolean
##No_data_value=number 0
##Output_directory=folder

import os
import processing

lddLrs = Input.split(';')

if No_data == 0:
    for lyr in lddLrs:
        os.system("gdal_translate -projwin " + str(UX) + " " + str(UY) + " " + str(LX) + " " + str(LY) + " -of GTiff " + str(lyr) + " " + Output_directory + "/clip_" + str(os.path.basename(str(lyr))))
        #loading fails
        #processing.load(str(Output_directory + "/clip_" + str(os.path.basename(str(lyr)))))
else:
    for lyr in lddLrs:
        os.system("gdal_translate -a_nodata " + str(No_data_value) + " -projwin " + str(UX) + " " + str(UY) + " " + str(LX) + " " + str(LY) + " -of GTiff " + str(lyr) + " " + Output_directory + "/clip_" + str(os.path.basename(str(lyr))))
        #loading fails
        #processing.load(str(Output_directory + "/clip_" + str(os.path.basename(str(lyr)))))

I have tested it in QGIS 1.8 (renaming to SEXTANTE) and QGIS 2.
Clipped rasters are loaded in QGIS 1.8, while it fails to load the clipped rasters in QGIS 2. Maybe I am missing something. However the clipping works.

Cheers!

#3 Updated by Alexander Bruy about 7 years ago

  • Status changed from Open to Closed

#4 Updated by Luca Congedo about 7 years ago

Perfect.
Thank you!

Also available in: Atom PDF