Thursday 16 June 2016

arcgis desktop - Clipping raster by multiple datasets or polygons?



I would like to clip a DEM using a grid of polygons. It's probably easier to use multiple polygons in one shape file, but I haven't managed this so I'm trying to use a for loop so I can loop through each dataset in a gdb (each contains only one polygon).


Here's my code (doing it in the python window).


#creating a workspace and a list of feature classes
arcpy.env.workspace = "C:/data/lidar/lidar.gdb"
fcs = arcpy.ListFeatureClasses()

#looping through each feature class and creating a raster based on the extent of
#feature class

for fc in fcs:

arcpy.Clip_management("perth", "#", "C:/data/lidar", fc, "", "ClippingGeometry")

My code doesn't execute however, it just sits there, waiting for something else... but what? I can get it to work for one clip, but not with the loop.


I'm sure I should be doing something else for the output, to name each new raster by feature class or something... but again, don't know how. Please let me know if I should add any more info.



Answer



One thing I notice is that your third parameter is a hard coded output (C:/data/lidar). The way it's written now will loop through each of your features and overwrite the output every time, but since you may not have allowed the automatic overwriting of files, this could potentially be the hang-up. Try creating a unique output name for each iteration:


#creating a workspace and a list of feature classes
arcpy.env.workspace = "C:/data/lidar/lidar.gdb"
fcs = arcpy.ListFeatureClasses()


#looping through each feature class and creating a raster based on the extent of
#feature class

i=0
for fc in fcs:
outputPath = "C:/data/lidar" + str(i)
i+=1
arcpy.Clip_management("perth", "#", outputPath, fc, "", "ClippingGeometry")

Also, I'm not certain that you intended to place the outputs in the C:/data folder named lidar... note that the third parameter in clip is the full path of your output raster, not a folder that it will be placed in. If you don't specify an extension in your output path name and place them in a standard folder, it will be a grid, so right now your program is attempting to create a new grid dataset named 'lidar' in the C:/data folder.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...