I have a geocoded raster that I'd like to pad with NaNs to 64 x 64 pixels. I've tested rioxarray.DataArray.pad_box(), which does work, but it's a bit complex to calculate new bounding box corners that will give said shape.

Is there a way to simply pad the raster to my desired output shape? Like xarray's DataArray.pad(), but that computes the new spatial coordinates as well, which xarray doesn't do.

Example of xarray's pad:

import xarray as xa
import rioxarray as rioxa

im_xa = xa.DataArray(np.random.randint(0, 255, (37, 33), dtype="uint8"), 
          dims=('y', 'x'), coords={'x': np.linspace(447560, 447960, 33), 
            'y': np.linspace(6760930, 6761380, 37)})
im_xa = im_xa.rio.write_crs(3006)
xleft = np.ceil((64 - im_xa.shape[1]) / 2).astype('int')
xright = np.floor((64 - im_xa.shape[1]) / 2).astype('int')
yleft = np.ceil((64 - im_xa.shape[0]) / 2).astype('int')
yright = np.floor((64 - im_xa.shape[0]) / 2).astype('int')
im_xa_pad = im_xa.pad({'x': (xleft, xright), 'y': (yleft, yright)})

Windows 10, Python 3.8.2, xarray 0.15.1, rioxarray 0.1.1

  • Please clarify what software you are using.
    – mikeLdub
    Mar 1 at 13:24
  • Unfortunately, there isn't currently a function like that in rioxarray.
    – snowman2
    Mar 5 at 2:17


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.