2

I clipped multiple rasters to the shape of a polygon shapefile. But, the output rasters do not have exactly the same shapes. I am doing correlation analyses per pixel so I need the array shapes to match up exactly.

Half of them are size (3080, 890), and the other rasters are now (3081, 891).

There's also another problem where even rasters that are of the same shapes, do not have the same physical XY coordinates (but maybe this is less of an issue).

All of the steps I performed on the original rasters to get to this point, using: Scale all rasters to same resolution of 15.0 (some were upsampled, some were downsampled), convert to float32 dtype, crop to polygon shape.

I tried this using both rasterio and gdal, and got similar results with both methods.

Here are a few of the final rasters printed with their metadata info

1.tif
{'driver': 'GTiff', 'dtype': 'float32', 'nodata': nan, 'width': 890, 'height': 3080, 'count': 1, 'crs': CRS.from_epsg(32629), 
'transform': Affine(15.0, 0.0, 232058.5,
       0.0, -15.0, 1438428.5), 'tiled': False, 'interleave': 'band'}
XY coords of (0,0) pixel: (232066.0, 1438421.0)

4.tif
{'driver': 'GTiff', 'dtype': 'float32', 'nodata': nan, 'width': 890, 'height': 3080, 'count': 1, 'crs': CRS.from_epsg(32629), 
'transform': Affine(15.0, 0.0, 232060.0,
       0.0, -15.0, 1438430.0), 'tiled': False, 'interleave': 'band'}
XY coords of (0,0) pixel: (232067.5, 1438422.5)

7.tif
{'driver': 'GTiff', 'dtype': 'float32', 'nodata': nan, 'width': 891, 'height': 3081, 'count': 1, 'crs': CRS.from_epsg(32629), 
'transform': Affine(15.0, 0.0, 232051.0,
       0.0, -15.0, 1438441.5), 'tiled': False, 'interleave': 'band'}
XY coords of (0,0) pixel: (232058.5, 1438434.0)

9.tif
{'driver': 'GTiff', 'dtype': 'int16', 'nodata': -1.0, 'width': 891, 'height': 3081, 'count': 1, 'crs': CRS.from_epsg(32629), 
'transform': Affine(15.0, 0.0, 232053.930561042,
       0.0, -15.0, 1438436.08069127), 'tiled': False, 'interleave': 'band'}
XY coords of (0,0) pixel: (232061.430561042, 1438428.58069127)

Is there a way to make them all the same shape? I assume that clipping to the polygon has some small error which is why they are off by 1 pixel col/row.

I was thinking I could just remove the first col and row from the (3081, 891) rasters to make the sizes equal. But then, how do I update the Affine transformation such that the raster is still geographically valid?

Edit: More info My original rasters are all in the same CRS, but they are different sizes and had different resolutions.

The gdal commands I used were

  • gdalwarp -tr 15.0 -15.0 orig/K.tif proc/K_resamp.tif
  • gdalwarp -cutline orig/outline.shp -crop_to_cutline -of GTiff -dstnodata 255 proc/K_resamp.tif proc/K_cropped.tif
  • Are all the original images having the same size and georeferencing? How did you crop the images? You can use gdalwarp as an example of alternatives that you have. See options -tr, -te, -ts, -tap. – user30184 Mar 18 at 16:11
  • @user30184 added more details to the post. They had different res and sizes originally so I scaled and cropped them. But the resulting shapes are different. – rasen58 Mar 18 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.