Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping a raster file using GDAL w/ Python

EDIT: PARTIAL SOLUTION

Using gdal_translate in the command line seems to do the trick, even though the Python binding doesn't work.

This worked to crop a GeoTiff removing 300 pixel padding from top and left and keeping the next 2000x2000 pixels. gdal_translate -srcwin 300 300 2000 2000 input.tif output.tif

ORIGINAL QUESTION

I've spent an embarrassingly long time trying to figure this out.

I have a series of satellite images in GEOTiff format. Each image has a 300 pixel buffer where it overlaps with the image next to it.

Goal: I am trying to crop off the 300 pixel buffer off of each image and then use as a raster with GDAL.

Constraints: 1) I need to keep all of the metadata and coordinate system information associated with the files 2) I want to do this entirely in Python (no command line)

What I've tried unsuccessfully:

1) using gdal.translate's srcWin function:

raster_data = gdal.Open('image.tif')
x_size = raster_data.RasterXSize
y_size = raster_data.RasterYSize
raster_data_unpadded = gdal.Translate('temp.tif', raster_data,
                                       srcWin = [padding,padding, 
                                       x_size - padding*2, 
                                       y_size-padding*2])

The problem: This produces a black image with no data

2) Cropping image using PIL and then saving back as TIF

 from PIL import Image
 img = Image.open(image.tif)
 x_size, y_size = img.size
 box = (padding, padding, x_size-padding, y_size - padding)
 img_unpadded = img.crop(box)
 img_unpadded.save('unpadded_image.tif')

The problem: PIL fails to save the file. It just hangs. Trying to save as a ".tiff" file produces the error "encoder error -9"

3) Using Rasterio

with rasterio.open("image.tif") as src:

    out_image, out_transform = mask(src, geoms, crop=True)

out_meta = src.meta.copy()

The problem: Rasterio doesn't seem to accept masks in Pixels format (e.g. 300 pixels). Only takes geometries, like a polygon, in the coordinate system of the file. I don't know how to translate between Pixels and coordinates.

like image 649
PAL Avatar asked Jan 30 '18 21:01

PAL


People also ask

How do you crop a raster in Python?

Open the raster dataset that you wish to crop using xarray or rioxarray. Open your shapefile as a geopandas object. Crop the data using the . clip() function.

What is a Gdal raster?

GDAL is a translator library for raster and vector geospatial data formats that is released under an MIT style Open Source License by the Open Source Geospatial Foundation.

What does Gdal warp do?

The gdalwarp utility is an image mosaicing, reprojection and warping utility. The program can reproject to any supported projection, and can also apply GCPs stored with the image if the image is “raw” with control information.


1 Answers

The following should work:

import rasterio
from rasterio.windows import Window

with rasterio.open('input.tif') as src:
    window = Window(padding, padding, src.width - 2 * padding, src.height - 2 * padding)

    kwargs = src.meta.copy()
    kwargs.update({
        'height': window.height,
        'width': window.width,
        'transform': rasterio.windows.transform(window, src.transform)})

    with rasterio.open('cropped.tif', 'w', **kwargs) as dst:
        dst.write(src.read(window=window))
like image 53
Sam Avatar answered Sep 19 '22 17:09

Sam