Thursday, 15 August 2019

When a raster is clipped to a polygon with Python, why does it still take on the extent of the entire shapefile?


I am clipping a raster image to each polygon row in a shapefile in ArcMap 9.3 using the Clip tool (Data Management) in Python with Clipping Geometry set. While the clipped raster successfully depicts the intersection of the raster and the polygon, the extent of the clipped raster is still that of the original raster. Here is my code:


# Import system modules 
import sys
import string
import os

import arcgisscripting
# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
# Load required toolboxes...
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
# Designate a workspace
gp.workspace = "L:\Clip" # Assign variables...
clipto = "test.shp" # shapefile to clip to
clipit = "vfcm.tif" # features to be clipped


# Cursor time
rows = gp.SearchCursor(clipto)
row = rows.Next()

# clip management
while row:
n = str(row.VAHU6) # assign a variable for the processing message based on a field
clipgeo = row.Shape
print "clipping to: " +n # tells what row was clipped
# Clip_management {in_template_dataset} {nodata_value} {NONE | ClippingGeometry}

outputname = str(row.VAHU6) + "_c"
extent=str(clipgeo.extent)
gp.Clip_management(clipit, extent, outputname, clipgeo, "", ClippingGeometry)
#update str if necessary
print str(row.VAHU6)
row=rows.next()

# reset the array...
del rows
del gp


Do you know why I am having this problem? When I run the tool in Model Builder, it works correctly. This problem is evident both in the file size (which is 10x greater than when correctly clipped) and when I "zoom to layer" in ArcMap. If you know how to adjust the code to work, I'd be very appreciative!


Thank you!


UPDATE: Even if I export one polygon from the shapefile manually, look up the extent in the source tab, and then type those coordinates directly into the extent in my code, it still doesn't clip properly.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...