I have managed to do this manually at Geoprocessing>Environments>Processing Extent and setting the shapefile in the drop down bar.

I was wonder if this could be automated with a python script?

My goal is to create a loop to change the processing extent prior to using spatial analysis tools on a various rasters that represent the areas of the shapefiles. This will keep their extent size minimal.

Edit (Current Code): (Not working)

for i in range (31):
... arcpy.env.extent = (r"C:\Folder\Shapefile"+str(i)+".shp")

Receiving a runtime error stating "Error in accessing environment "

  • so I don't define what str (i) is? – JC11 Feb 7 '14 at 20:34
  • You already defined i in your for loop – user2856 Feb 7 '14 at 22:31

Yes. As per the the extent help, set arcpy.env.extent = 'path to shapefile' (assuming ArcGIS 10+ as you didn't specify). Don't forget to set the Snap Raster environment so your output raster isn't shifted.

  • I set my extent in a loop and received the runtimeerror: error in accessing environment <extent> Here is my code: for i in range (31): ... arcpy.env.extent = (r"E:\data\Shapefile"+str(i)) Is something stated wrong here? – JC11 Feb 7 '14 at 19:50
  • you are missing the + ".shp" after str(i) – radouxju Feb 7 '14 at 19:56
  • @radouxju: I've added the +".shp" after str (i). Still giving the same error. I'll post the code as an edit in the question. – JC11 Feb 7 '14 at 20:09
  • 1
    Took a break from attempting this. Tried again with the same code, worked fine. No idea what was wrong before. – JC11 Feb 14 '14 at 19:58

here is another approach, with some test in the loop to help you identify if something goes wrong :

import arcpy

fcs = arcpy.ListFeatureClasses("C:\\folder\\Shapefile_root*") #shapefile_root is the start of the name of your shapefile

for fc in fcs:
    print fc #just checking that you are looping on the good shp
    arcpy.env.extent = fc
    print arcpy.env.extent # checking that the extent is updated
  • I haven't managed to get this code to work. When i type fc, it trys to autocomplete it to fcs. Nothing prints – JC11 Feb 7 '14 at 20:39
  • what do you have when you type "fcs" (it should be an object) ? Is your indentation in the "for" loop correct ? – radouxju Feb 7 '14 at 20:43
  • you should try copying this script and launching it as a stand alone. Make sure that your path is correct (once it is E, once it is C in your question) – radouxju Feb 7 '14 at 20:47

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.