Using Python and ArcMap 10.0 SP5, I'm trying to reproject and clip a large input raster to exactly the same grid as a smaller target raster (same extent, pixel sizes and projection). The problem is that the script produces an output raster that has 1 more row than the target raster (e.g. 1324 rows instead of 1323 -- columns match perfectly, pixels appear to be on the same grid). There is an extra row of pixels that overhangs the target image - everything else appears to be okay.
The following is my code for projecting and clipping to a target raster. What's needed to ensure that the output extent is exactly the same as in target raster? I'm new to ArcMap so it could be something obvious.
import arcpy
import tempfile
import os
import shutil
sourceImage = "C:/data/WorldClim_alt/WorldClim_Alt.tif"
inputImage = "E:/subset/nCom_2000/nCom2000101to107.tif"
outputFilename = "E:/subset/test/nComTest_clipped.tif"
# Snap to source image and source image extent
arcpy.env.extent = sourceImage
arcpy.env.snapRaster = sourceImage
# Get cell size
cellx = arcpy.GetRasterProperties_management(sourceImage, "CELLSIZEX")
# Get temporary directory and filename
tempDir = tempfile.mkdtemp()
tempFilename = 'E:/subset/test/' + basename(outputFilename) +'_proj.tif'
# Reproject
arcpy.ProjectRaster_management(inputImage,tempFilename,sourceImage, "#", cellx)
#
# Clip to source file
#
# Get envelope for clipping
top = arcpy.GetRasterProperties_management(sourceImage, "TOP")
left = arcpy.GetRasterProperties_management(sourceImage, "LEFT")
bottom = arcpy.GetRasterProperties_management(sourceImage, "BOTTOM")
right = arcpy.GetRasterProperties_management(sourceImage, "RIGHT")
envelope = str(left) + ' ' + str(bottom) + ' '+ str(right) + ' ' + str(top)
# Clip
arcpy.Clip_management(tempFilename, envelope, outputFilename, sourceImage,"#","NONE")
Edit in Response to Paul's comment on Clip help:
From the help on
Clip
, it mentionsIf the clip extent specified is not aligned with the input raster dataset, this tool makes sure that the proper alignment is used. This may cause the output to have a slightly different extent than specified in the tool.
I think that the input to clip (the output from ProjectRaster
) may be aligned with the in_template_dataset
. The snapRaster
environment variable was set to the in_template_dataset
before ProjectRaster
was called. Also, the output cell size of ProjectRaster
was set to the in_template_dataset
cell size. Visual inspection of the grids seems to indicate that they are aligned (except for an extra row and column in the ProjectRaster
output).
The only effect of Clip
seems to be the removal of a single column.
arcpy.sa.ExtractByMask(tempFilename, sourceImage)
. This works --no extra rows or columns!