I'm resampling a raster, and I would like it to line up and match the dimensions of another template raster. For whatever reason, the output of the Resample_management function has an added row and column.

import arcpy
from arcpy import env  

# Set file paths
in_raster = "inputRas.tif"
templateRaster = "templateRas.tif"
out_raster = "outRas.tif"

# Set 'Snap raster' and 'Extent' environment variables
arcpy.env.extent = arcpy.sa.Raster(templateRaster).extent
arcpy.env.snapRaster = templateRaster

# Get cell size from template raster (which is 1 m)
cellsizeX = arcpy.Describe(templateRaster).children[0].meanCellWidth
cellsizeY = arcpy.Describe(templateRaster).children[0].meanCellHeight
cellsize = str(cellsizeX) + " " + str(cellsizeY)

# Resample input raster
arcpy.Resample_management(in_raster, out_raster, cellsize)

# Print environment extent
print arcpy.env.extent

# Print extent of the output raster
print arcpy.sa.Raster(out_raster).extent

The resulting extents:

582568 5816053 583282 5816790 NaN NaN NaN NaN
582567 5816053 583282 5816791 NaN NaN NaN NaN

The function has essentially added one row of cells to the top of the output and one column of cells to the left. Why?

  • What was original cell size? – FelixIP Oct 19 '17 at 19:20
  • It was 0.09282 m – Andrew Plowright Oct 19 '17 at 19:42
  • If so, 5 columns and rows were added. Something doesn't match here. Did it reduce number of columns by factor of 2. – FelixIP Oct 19 '17 at 19:59
  • Sorry, the cell size for templateRaster was 1 m, and the cell size for in_raster (the raster being resampled) was 0.09282. I would expect that out_raster would have the same dimensions row/col numbers as templateRaster but that's where the extra row and column have appeared. – Andrew Plowright Oct 19 '17 at 20:07
  • 1
    Pragmatic approach: convert extent required to polygon and use it to clip the one you don't like. Also result often depends on how you run the script. Results can be different when you ran it from current mxd, catalog or standalone script. Most naughty in my opinion mxd. – FelixIP Oct 19 '17 at 20:45

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.