2

I am an Economists and I am new to GIS. I am using ArcGIS for desktop 10.6. I am trying to clip a raster with several polygon features - saving output rasters according to a field name in the polygon layer.

I used the code by Midavalo. The code is running perfectly but the problem is that it gives me an output of light raster for one province out of 200 I have in my field, it looks like it does not loop through i. It has been 2 months working on this but I get no help.

import arcpy, os

arcpy.env.overwriteOutput = True

arcpy.env.workspace = "C:\Users\Somaqaqa\Desktop\jupyter_python-tutorial1\installPycharm_pythonscript+\creating a file database\GiSPython_China\just_rastes"

clipshapefile = r"C:\Users\Somaqaqa\Desktop\jupyter_python-tutorial1\installPycharm_pythonscript+\creating a file database\GiSPython_China\Data\chn_admbnda_adm1_ocha.shp"

rasterlist = arcpy.ListRasters()

clipshapes = set() with arcpy.da.SearchCursor(clipshapefile, ['ADM1_EN']) as cursor:

for row in cursor:
     clipshapes.add(row[0])
Loop through input rasters, and clip by each shape in the input shapefile
for raster in rasterlist:

rastername, rasterext = os.path.splitext(raster)
for i in clipshapes:

    newRaster = "{}_FID{}.tif".format(rastername, i)
    newRasterPath = os.path.join(outputWorkspace, newRaster)
    Cliplayer = "Ras {}".format(raster)

 if arcpy.Exists('ClipLayer'):

arcpy.Delete_management('ClipLayer')
arcpy.MakeFeatureLayer_management(clipshapefile, Cliplayer, " ADM1_EN = '{}'".format(i))

arcpy.Clip_management(raster, "#", newRasterPath, Cliplayer, "0", "ClippingGeometry", 
"MAINTAIN_EXTENT")
print raster

print i

1 Answer 1

1

The problem is in the nested loop (all the clipping should be in the internal loop). I'm not sure how your file is idented because when I copy your code and paste in my IDE it shows many identation problems. Anyway, after fixing these problems and adding the output workspace (It is missing in the sample given), I tested it with 2 rasters and 3 shapefiles and had a output of 6 rasters:

import arcpy, os

arcpy.env.overwriteOutput = True

arcpy.env.workspace = 'C:\\Teste\\raster\\Desmatamento_Maryland'

clipshapefile = 'C:\\Teste\\shp\\Test_Clip_Maryland.shp'

outputWorkspace = 'C:\\Teste\\raster\\output'

rasterlist = arcpy.ListRasters()

clipshapes = set()

with arcpy.da.SearchCursor(clipshapefile, ['ADM1_EN']) as cursor:
    for row in cursor:
        clipshapes.add(row[0])

#Loop through input rasters, and clip by each shape in the input shapefile
for raster in rasterlist:
    rastername, rasterext = os.path.splitext(raster)

    for i in clipshapes:
        newRaster = "{}_FID{}.tif".format(rastername, i)
        newRasterPath = os.path.join(outputWorkspace, newRaster)
        Cliplayer = "Ras {}".format(raster)

        if arcpy.Exists('ClipLayer'):
            arcpy.Delete_management('ClipLayer')

        arcpy.MakeFeatureLayer_management(clipshapefile, Cliplayer, " ADM1_EN = '{}'".format(i))

        arcpy.Clip_management(raster, "#", newRasterPath, Cliplayer, "0", "ClippingGeometry", 
        "MAINTAIN_EXTENT")
        print raster

        print i

print 'End of script'

Your Answer

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

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