table of contents

Raster Cropping & Clipping

About

Often times working with raster data you will only want to work with a subset of the data. The subset of the data might be specified as a bounding box or by the boundaries of a shapefile. In this modules we'll learn how to crop and clip raster data.

Data

Process

Clip raster by bounding box

You can clip a raster by a specified bounding box. The coordinates will be based on the projection of the raster data, so if you need to, take a peek using the gdalinfo command. If you know the coordinates you'd like to clip to, you can input them as arguments of gdalwarp:

gdalwarp -te input.tif clipped_output.tif

where:

Let's clip our sf_4269.tif file with a set of coordinates:

gdalwarp -te -122.4267 37.7492 -122.4029 37.769 sf_4269.tif sf_4269-clippedByCoords.tif

You may notice a black line - this indicates no data values since our bounding box was beyond the image bounds.

Clip raster to SHP / NoData for pixels beyond polygon boundary

We can use a shapefile to clip out a subset of the raster data.

gdalwarp -dstnodata -cutline input_polygon.shp input.tif clipped_output.tif

where:

Let's clip the sf_4269.tif to the boundary of downtown.shp:

gdalwarp -dstnodata -9999 -cutline downtown.shp sf_4269.tif sf_4269-downtown.tif

Crop raster dimensions to vector bounding box

We can also use the bounding box of a vector file to crop our raster data.

gdalwarp -cutline input_polygon.shp -crop_to_cutline input.tif cropped_output.tif

where:

gdalwarp -cutline downtown.shp -crop_to_cutline sf_4269.tif sf_4269-downtownBounds.tif