1

enter image description here

I'm trying to clip the above raster dataset to each of the ecoregions (outlined in black) in this state with a Python script. I have a function in my script that does this without a problem when the raster dataset covers the entire state, but in cases like this one, I receive an error because the dataset doesn't overlap with every ecoregion.

This is the function that handles the clip:

def ecoclip_reclass(input_fc, clip_fc, name):
    # Create a list to store the paths of the clipped feature classes
    clipped_feature_classes = []

    # Create a search cursor for the clip feature class
    with arcpy.da.SearchCursor(clip_fc, ["SHAPE@", "OID@"]) as cursor:
        for row in cursor:
            # Extract the geometry and OID of each record
            clip_geometry = row[0]
            clip_oid = row[1]

            # Set the output feature class name based on the clip OID
            output_feature_class = os.path.join(output_workspace, f"{name}_Eco_{clip_oid}")

            # Perform the clip operation
            arcpy.management.Clip(input_fc, "", output_feature_class, clip_geometry, "255",
                                  "ClippingGeometry")

            # Add the path to the clipped feature class to the list
            clipped_feature_classes.append(output_feature_class)

In the next step, I'm going to be reclassifying these datasets, so I need to have values in each ecoregion even if there isn't any data in it. Is there a way to add something to my function that will carry out the clip if there's data in the ecoregion, but assign the area a value of 0 if there is no data in the ecoregion?

0

1 Answer 1

1

You can include a test for overlapping between the raster dataset (input_fc) extent and the clipping geometry (clip_geometry) before attempting to clip.

Raster datasets are always rectangular so using its extent property is sufficient but there is no guarantee that the intersection contains valid data, to test for this you will need a spatial analyst license to test IsNull, convert to a polygon and cursor through the intersection to find if any of the clipped polygons contain value = 0 (not null).

def ecoclip_reclass(input_fc, clip_fc, name):
    # Create a list to store the paths of the clipped feature classes
    clipped_feature_classes = []

    # Create a search cursor for the clip feature class
    with arcpy.da.SearchCursor(clip_fc, ["SHAPE@", "OID@"]) as cursor:
        for row in cursor:
            # Extract the geometry and OID of each record
            clip_geometry = row[0]
            clip_oid = row[1]
            
            # test for overlap before clipping
            if arcpy.Describe(input_fc).extent.overlaps(clip_geometry):
              # Set the output feature class name based on the clip OID
              output_feature_class = os.path.join(output_workspace, f"{name}_Eco_{clip_oid}")
              
              # Perform the clip operation
              arcpy.management.Clip(input_fc, "", output_feature_class, clip_geometry, "255",
                                    "ClippingGeometry")
              
              # Add the path to the clipped feature class to the list
              clipped_feature_classes.append(output_feature_class)

BTW I've noticed that your procedure has no return, I'm assuming that there was more code after this code block that finishes properly.

1
  • Yes, the second half of the function performs the reclass. Thanks for your help.
    – Luis
    Commented Jun 20 at 16:27

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