0

I am clipping a raster file with a shapefile grid and I want to save each raster subset individually with the polygon spatial extent.

To select the extent of each polygon, first I select the polygon with arcpy.SelectLayerByAttribute_management and then I get the polygon extent and I use this extent in arcpy.Clip_management function.

But when I run the code each raster subset don't have the extent that I want.

The code is:

def execute(self, parameters, messages): """The source code of the tool."""

a= parameters[0].valueAsText
b= parameters[1].valueAsText`
#-----------------------------

with arcpy.da.SearchCursor(a, ["FID_malla"]) as cursor:

    for row in cursor:

        #select a polygon                           
        arcpy.SelectLayerByAttribute_management(a, "NEW_SELECTION", '"FID_malla" ='+str(row[0]))

        #extent
        shp_ex = arcpy.Describe(a)
        exshp = shp_ex.extent

        xmin=exshp.XMin
        ymin=exshp.YMin
        xmax=exshp.XMax
        ymax=exshp.YMax


        ExtStr = "{} {} {} {}".format(xmin, ymin, xmax, ymax)


        #Execute clip
        arcpy.Clip_management(b, ExtStr, "C:\Users\mvinuales\Desktop\Image_Chips\imagen\\" + str(row[0]) +".tif", a, "256", "ClippingGeometry","NO_MAINTAIN_EXTENT")

    arcpy.SelectLayerByAttribute_management(a,"CLEAR_SELECTION")
  • 1
    What extent do you get? – BERA Jul 31 '18 at 19:20
  • You could use the SHAPE@ token to pull back polygon geometries, look at their extent property, and bypass Select Layer By Attribute. What do test values of a and b look like? – PolyGeo Jul 31 '18 at 20:07
  • @BERA, I get the extent of the raster image (b). @PolyGeo, I use "SHAPE@" and I calculate the extent as row[1].extent, but its continues being raster's extent. a= parameters[0].valueAsText is a shapefile with some polygons and b= parameters[1].valueAsText is a raster image. – MikV89 Aug 3 '18 at 6:58
1

You should only need three parameters in Clip to clip by a rectangle: in_raster, rectangle and out_raster. Try leaving the others as default.

Your selection might not be working. Use three double quotes and AddFieldDelimiters to get it right:

...
import os

with arcpy.da.SearchCursor(a, 'FID_malla') as cursor:
    for row in cursor:
        sql = """{0}={1}""".format(arcpy.AddFieldDelimiters(a,'FID_malla'), row[0])
        arcpy.SelectLayerByAttribute_management(a, "NEW_SELECTION", sql)
        shp_ex = arcpy.Describe(a)
        exshp = shp_ex.extent
        ExtStr = "{} {} {} {}".format(exshp.XMin,exshp.YMin,exshp.XMax,exshp.YMax)
        arcpy.Clip_management(in_raster=b, rectangle=ExtStr, out_raster=os.path.join(r"C:\Users\mvinuales\Desktop\Image_Chips\imagen", str(row[0]) +".tif"))
        arcpy.SelectLayerByAttribute_management(a,"CLEAR_SELECTION")

And as mentioned by @PolyGeo in the comments you can also use the SHAPE@ token. There is no need for selections:

with arcpy.da.SearchCursor(a, ['FID_malla','SHAPE@']) as cursor:
    for row in cursor:
        e = row[1].extent
        ExtStr = "{} {} {} {}".format(e.XMin,e.YMin,e.XMax,e.YMax)
        arcpy.Clip_management(in_raster=b, rectangle=ExtStr, out_raster=os.path.join(r"C:\Users\mvinuales\Desktop\Image_Chips\imagen", str(row[0]) +".tif"))
0

@BERA, thanks for your sugestions. I think that the selection is correct because I get different raster subsects with their particular names. But I have seen that in arcpy.Clip_management the second argument ("EXTENT") need enter the values with (,) not with (.), so I replace it.

But when I run the code the extent of the raster subset is the whole extent of the Input raster. Maybe with ExtStr = "{} {} {} {}".format(xmi,ymi,xma,yma) I don't pass correct the argument.

The code is:

a= parameters[0].valueAsText
b= parameters[1].valueAsText
#-----------------------------`


with arcpy.da.SearchCursor(a, ["FID_malla","SHAPE@"]) as cursor:

    for row in cursor:

        #select a polygon                           
        arcpy.SelectLayerByAttribute_management(a, "NEW_SELECTION", '"FID_malla" ='+str(row[0]))

        #extent
        extent = row[1].extent


        xmin=extent.XMin
        ymin=extent.YMin
        xmax=extent.XMax
        ymax=extent.YMax

        xi=str(xmin)
        yi=str(ymin)
        xa=str(xmax)
        ya=str(ymax)

        xmi=xi.replace(".",",")
        ymi=yi.replace(".",",")
        xma=xa.replace(".",",")
        yma=ya.replace(".",",")


        ExtStr = "{} {} {} {}".format(xmi,ymi,xma,yma)


        arcpy.Clip_management(b, ExtStr, "C:\Users\mvinuales\Desktop\Image_Chips\imagen\\" + str(row[0]) +".tif", a, "256", "ClippingGeometry","NO_MAINTAIN_EXTENT")`


    arcpy.SelectLayerByAttribute_management(a,"CLEAR_SELECTION")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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