Select to view content in your preferred language

clipping rasters by different shapefiles in batch using python

852
3
04-06-2010 11:05 AM
MichelleGuck
Emerging Contributor
Hi all,

I am trying to clip different rasters by different shape files using the gp.clip_managagement tool for clipping rasters. When using a shapefile/feature class with this tool in the python environment how can you set it up to populate the extent (xmin, ymin, xmax, ymax) that it automatically updates when using the tool in the ArcMap/Catalogue interface.

What I am trying to do is clip rasters by a shapefile that contains tiles (in a grid format), when I set environment settings like gp.extent or gp.mask two edge cells are removed versus when I manually use the Clip tool in the management tool box, these cells are preserved.

If anyone can offer assistence to this problem or alternative ideas I'd be happy to try them.
0 Kudos
3 Replies
JieZhang
Esri Contributor
Hi Michelle,

In Python, you do not need to worry about the extent if you already have a clipping feature.

The Clip tool syntax looks like this:

Usage: Clip_management in_raster rectangle out_raster {in_template_dataset} {nodata_value} {NONE | ClippingGeometry}

e.g. gp.Clip_management("image.tif", "#", "clip.tif","feature.shp", "0", "ClippingGeometry")

You only need to give the path to your clipping feature for the "in_template_dataset" parameter. The tool will automatically pick up the extent of your clipping feature.

Jie
0 Kudos
MichelleGuck
Emerging Contributor
This is the error I get when trying the '#' :

Failed to execute. Parameters are not valid.
ERROR 000735: Rectangle: Value is required
Failed to execute (Clip).

Instead of having one .shp {in_template_dataset} I am using a file path send different rasters to be clipped by different .shp files.
0 Kudos
JieZhang
Esri Contributor
Hi Michelle,

Sorry I should have read your other thread more carefully. If you are on 9.3.1, then leaving the extent parameter empty is not acceptable.
But you can still get the extent from the shape file by using the describe function. For example,

desc = gp.describe("c:/workspace/feature.shp")
gp.Clip_management("image.tif", desc.Extent, "clip.tif","feature.shp", "0", "ClippingGeometry")

Let me know if that works for you.
Jie
0 Kudos