1

I want to clip a raster image using a shapefile with differents polygons (each polygon have its "FID_malla" value [FID_malla is large data type]) and save the subset rasters individually with "FID_malla" name.

So if I have 50 polygons the result of the clip will be 50 raster images. To do that I use for iterator.

I run this code, but I obtain a error in SelectLayerByAttribute: Parameters are not valid . Also, I don't know if after SelectLayerByAttribute operation the code runs well.

def getParameterInfo(self):
    """Define parameter definitions"""

# First parameter
    param0 = arcpy.Parameter(
        displayName="Shape recorte",
        name="in_features",
        datatype="GPTableView",
        parameterType="Required",
        direction="Input")

# Second parameter
    param1 = arcpy.Parameter(
        displayName="Imagen satelite",
        name="raster",
        datatype="DERasterDataset",
        parameterType="Required",
        direction="Input")

    parameters = [param0, param1]
    return parameters

def isLicensed(self):
    """Set whether tool is licensed to execute."""
    return True
def updateParameters(self, parameters):return
def updateMessages(self, parameters):return

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:
        arcpy.SelectLayerByAttribute_management (a,  "NEW_SELECTION",  "FID_malla={}".format(row[0]))  

        #Execute ExtractByMask
        out = ExtractByMask(b, a)

        out.save("C:\Users\mvinuales\Desktop\Image_Chips\imagen" + "\_" + str(row[0]))
2

Two issues:

  1. Your where clause is incorrect as you had identified. A very simple check is to run the tool in Arcmap, go to geoprocessing > Results and right click on your tool and copy snippet, then look at it! As you will discover because your source data is a shapefile then the field delimiter needs to be double quotes, an example is shown below where bob is a shapefile:
arcpy.SelectLayerByAttribute_management(in_layer_or_view="bob", selection_type="NEW_SELECTION", where_clause='"FID" = 2')
  1. You are not ending your output raster with a file extension such as tif, this will mean your output will be an ESRI raster, if that is what you truly desire?
  • I this particular case the sentence is arcpy.SelectLayerByAttribute_management(a, "NEW_SELECTION", '"FID_malla" ='+str(row[0])) – MikV89 Jul 27 '18 at 8:52

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.