0

I have about 400 rasters that I want to clip each of them based on a polygon object.

The code I tried to use is the following:

import arcpy, os
origpath = ur'd:\test'
jpgs = [f for f in os.listdir(origpath) if f.endswith('.jpg') and os.path.isfile(os.path.join(origpath,f))]
for jpg in jpgs:
    r = arcpy.sa.Raster(os.path.join(origpath,jpg))
    p = arcpy.Polygon(arcpy.Array([arcpy.Point(562879.802,4538631.518),arcpy.Point(583906.505,4538842.192),arcpy.Point(584139.129,4518489.715),arcpy.Point(563054.256,4518279.244)]))
    arcpy.Clip_management(r,p,os.path.join(origpath,'clipped_'+jpg),'#','#','ClippingGeometry')

When it runs it produces an error:

ERROR 000628: Cannot set input into parameter rectangle.

I assume that it has to do with the Polygon Object, I have inserted, for the Extent parameter.

The last solution would be to put this one polygon in a feature class and then add this feature class as the extent parameter.

Is there any direct way to use the Polygon Object to clip my rasters?

1 Answer 1

1

The help for Clip (management) gives this example:

import arcpy
arcpy.Clip_management("c:/data/image.tif","1952602 294196 1953546 296176",
    "c:/data/clip.gdb/clip01", "#", "#", "NONE", "NO_MAINTAIN_EXTENT")

It looks like you want to provide the clip extent as a string of 4 numeric values (ordered X-Minimum, Y-Minimum, X-Maximum, Y-Maximum) instead of creating a polygon object.

To preserve your existing code, make the following change:

import arcpy, os
origpath = ur'd:\test'
jpgs = [f for f in os.listdir(origpath) if f.endswith('.jpg') and 
os.path.isfile(os.path.join(origpath,f))]
for jpg in jpgs:
    r = arcpy.sa.Raster(os.path.join(origpath,jpg))
    p = arcpy.Polygon(arcpy.Array([arcpy.Point(562879.802,4538631.518),arcpy.Point(583906.505,4538842.192),arcpy.Point(584139.129,4518489.715),arcpy.Point(563054.256,4518279.244)]))
    extent_p = p.extent
    arcpy.Clip_management(r, '{} {} {} {}'.format(extent_p.XMin, extent_p.YMin, extent_p.XMax, extent_p.YMax), os.path.join(origpath,'clipped_'+jpg),'#','#','ClippingGeometry')
1
  • It seems that it passes the error with the extent parameter as you correctly suggested but now it produces a new error: The clip feature is outside the raster extent. Failed to execute (Clip). The extents of the polygon and the raster are exactly the same. What am I doing wrong?
    – Panos
    Oct 13, 2018 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.