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.
Best Answer
In my experience with clip management, these types of issues are often due to rounding errors. Basically it seems like actual image coordinates are often saved with greater precision than what is returned from, for instance, GetRasterProperties_management. In some tools, the output will be resampled based on the exact coordinates, but here, I believe it's just selecting input cells that are within the extent (the goodness or badness of this depends on what you're trying to do). For example, if the actual image coordinate of 'top' is
And your clip extent specified is
an additional row will be selected, because the extent extends ever so slightly beyond that top row. To remedy this, I often select an extent that is a fraction of a cell inside of the actual clipping extent (for top and right, that's coordinates slightly smaller, while for bottom and left it's coordinates slightly larger). In your example (assuming the cells are greater than 1 unit resolution) I would do something like
Good luck, ArcGIS loves to play games with these types of things.