Cropping raster (rectangle) with shapefile (mask) in QGIS

extract-by-maskqgisrastershapefile

My study area is in black, and in grey the shape of Portugal

My study area is in black, and in grey the shape of Portugal.

I am creating my study area. It corresponds to the coastal area of Portugal. I have a shapefile for Portugal. I want to crop a raster that corresponds to my study area ( a rectangle), with the shape of Portugal, but what I need is the ocean, so the part that is outside of the mask, the complementary part.

How can I do that?

Best Answer

(1) First, create a rectangle which encompasses your raster.

(1a) Start Extract layer extent tool (in Layer tools group).

enter image description here

(1b) Select your raster as the Input Layer. Run the tool.

enter image description here

(2) You will get a Extent layer.

(2a) Now start Difference tool (in Vector overlay group).

enter image description here

(2b) Select newly created Extent as the Inpur Layer. Also select your coastal polygon layer as the Overlay Layer. Then run the tool.

enter image description here

(3) You would see Difference layer (i.e. your ocean area).

(3a) Start Clip raster by mask layer tool (in GDAL > Raster extraction).

enter image description here

(3b) Select your raster as the Input Layer, and Difference polygon as the Mask Layer. Click on the checkbox for Create an output alpha band. Run the tool.

enter image description here

(4) Hope your final output is something like below.

enter image description here