2

I have two folders, one which is the shapefiles which define the polygons for my extract by mask (there are 33 of them) and one which has my rasters (10 of them). For each of the 33 shapefiles I want to do an extract by mask with all 10 of the rasters. So my output will be 330 new rasters.

I have tried two different ways to do this each with the same result. The first is to use a zipped list:

arcpy.CheckOutExtension('Spatial')
arcpy.env.workspace=r'G:\raster_pathway'
rasters=arcpy.ListRasters('*.tif')
arcpy.env.workspace=r'G:\shapefile_pathway'
shapefiles=arcpy.ListFeatureClasses('*.shp')
out=r'G:\output_pathway'
for ras1, shape1 in zip(rasters, shapefiles):
 outpath=os.path.join(out,shape1[:-4] + '_' + ras2[:-4] + '.tif')
 arcpy.gp.ExtractByMask_sa(ras1,shape1, outpath)

the second way is to use a nested loop:

 arcpy.CheckOutExtension('Spatial')
 arcpy.env.workspace=r'G:\raster_pathway'
 rasters=arcpy.ListRasters('*.tif')
 arcpy.env.workspace=r'G:\shapefile_pathway'
 shapefiles=arcpy.ListFeatureClasses('*.shp')
 out=r'G:\output_pathway'
 for shapefile in shapefiles:
 for raster in rasters:
 outpath=os.path.join(out, shapefile[:-4] + '_' + raster[:-4] +'.tif')
 arcpy.gp.ExtractByMask_sa(raster,shapefile, outpath)

in both cases the following error is returned:

Traceback (most recent call last):
 File "<ipython-input-11-5034b4c5c647>", line 1, in <module>
 runfile('G:/Stefano/python codes/ecoregions_extract_mask.py', wdir='G:/Stefano/python codes')
 File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
 execfile(filename, namespace)
 File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
 exec(compile(scripttext, filename, 'exec'), glob, loc)
 File "G:/Stefano/python codes/ecoregions_extract_mask.py", line 22, in <module>
 arcpy.gp.ExtractByMask_sa(raster,shapefile, outpath)
 File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda>
 return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster: CDL_2006_Group_lookup_new_prj30m.tif does not exist.
Failed to execute (ExtractByMask).

CDL_2006_Group_lookup_new_prj30m.tif is the first raster within rasters

It definitely exists though.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 13, 2016 at 15:46

1 Answer 1

2

When you use the ListRasters and ListFeatureClasses functions, you are within different workspaces. The paths are relative to the workspace, so no, CDL_2006_Group_lookup_new_prj30m.tif does not exist, because you're telling it to look for r'G:\shapefile_pathway\CDL_2006_Group_lookup_new_prj30m.tif'

Rewrite the first few lines to be:

import os
arcpy.CheckOutExtension('Spatial')
arcpy.env.workspace=r'G:\raster_pathway'
rasters = [os.path.join(arcpy.env.workspace, i) for i in arcpy.ListRasters('*.tif')]
arcpy.env.workspace=r'G:\shapefile_pathway'
shapefiles = [os.path.join(arcpy.env.workspace, i) for i in arcpy.ListFeatureClasses('*.shp')]

This will store the full paths to the datasets in the lists.

answered Jun 13, 2016 at 16:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.