3

I am working in QGIS 3.24.0. I am working on a project using the NLCD raster for the contiguous USA. I need to extract 208 circular buffers (10km radius) and have them each saved as a separate raster layer (208 separate raster layers). I have created the buffers and run "Clip raster by mask layer" to extract the raster data, but this results in one raster layer whereas I need 208 separate layers.

For help visualizing, below is a snippet of my raster layer. Each of the three circular buffers is part of the same layer (208 buffers total), but I need them to be separate layers.

enter image description here

Is there a different way to run the extraction or perhaps a separate tool I should be using?

3 Answers 3

3

Split your vector layer into 208 separate layers using split vector layer (yeah, tool names in QGIS are rather straightforward), then run the clip raster by mask layer tool in batch mode (bottom left). If done in a vanilla project, filling the vector layers is easier, since you don't have to manually select each of them.

2

In the Clip raster by mask layer click the green arrows beside the buffer polygon layer:

enter image description here

It is an option to iterate over the buffer polygon layer to use every polygon inside the polygon layer as a separate clip file.

The output will be separate raster layers for every polygon. The only drawback of using this option is that you need to name them manually.

Here is the output:

enter image description here

1

With pyqgis you can create a temp clip layer from each polygon and clip the raster using this:

vectorlayer = QgsProject.instance().mapLayersByName('Buffered')[0] #Change Buffered to the name of your vector layer
rasterlayer = QgsProject.instance().mapLayersByName('nh_69_5')[0] #Change to raster name
raster_output_folder = r'/home/bera/Desktop/GIStest/raster_outputs/' #The folder where the output raster will be saved

for num, polygon in enumerate(vectorlayer.getFeatures(), 1): #For each polygon
    print(f"processing polygon {num}")
    polyid = polygon.id() #Fetch its id
    raster_output = os.path.join(raster_output_folder, f"raster_{num}.tif") #Create a output raster filename
    cliplayer = processing.run("native:extractbyexpression", 
        {'INPUT':vectorlayer,'EXPRESSION':f'$id={polyid}','OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT'] #Extract the polygon by its id
    
    #And clip the raster with the temp cliplayer
    processing.run("gdal:cliprasterbymasklayer", 
        {'INPUT':rasterlayer,'MASK':cliplayer,'SOURCE_CRS':None,
        'TARGET_CRS':None,'TARGET_EXTENT':None,'NODATA':None,
        'ALPHA_BAND':False,'CROP_TO_CUTLINE':True,'KEEP_RESOLUTION':False,
        'SET_RESOLUTION':False,'X_RESOLUTION':None,'Y_RESOLUTION':None,
        'MULTITHREADING':False,'OPTIONS':'','DATA_TYPE':0,'EXTRA':'',
        'OUTPUT':raster_output})

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.