2

I am building an arcpy script, where I would like to create a for loop, that for each raster, it clips it from a shapefile, and creates a new output, with name based on the file itself. The code:

import arcpy
arcpy.env.workspace= r"C:\Angela\LC"

#import the raster files for land cover
#Local variables:
n1992_USA = "C:\\Angela\\OK\\nlcd_1992_30meter_whole (1)\\nlcd_1992_30meter_whole.img"
n2001_USA = "C:\\Angela\\OK\\nlcd_2001_landcover_2011_edition_2014_10_10\\nlcd_2001_landcover_2011_edition_2014_10_10\\nlcd_2001_landcover_2011_edition_2014_10_10.img"
n2006_USA = "C:\\Angela\\OK\\nlcd_2006_landcover_2011_edition_2014_10_10\\nlcd_2006_landcover_2011_edition_2014_10_10\\nlcd_2006_landcover_2011_edition_2014_10_10.img"
n2011_USA = "C:\\Angela\\OK\\nlcd_2011_landcover_2011_edition_2014_10_10\\nlcd_2011_landcover_2011_edition_2014_10_10\\nlcd_2011_landcover_2011_edition_2014_10_10.img"

#import rasters
arcpy.MakeRasterLayer_management(n1992_USA, "1992t", "", "-2361915 267885 2266005 3177735", "")
arcpy.MakeRasterLayer_management(n2001_USA, "2001t", "", "-2361915 267885 2266005 3177735", "")
arcpy.MakeRasterLayer_management(n2006_USA, "2006t", "", "-2361915 267885 2266005 3177735", "")
arcpy.MakeRasterLayer_management(n2011_USA, "2011t", "", "-2361915 267885 2266005 3177735", "")

#import admin boundaries
admin_boundaries_usa = "C:\\Angela\\tmp\\cb_2013_us_county_5m.shp"
arcpy.MakeFeatureLayer_management(admin_boundaries_usa, "admin_boundaries_usa", "",
                                  "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;STATEFP STATEFP VISIBLE NONE;COUNTYFP COUNTYFP VISIBLE NONE;COUNTYNS COUNTYNS VISIBLE NONE;AFFGEOID AFFGEOID VISIBLE NONE;GEOID GEOID VISIBLE NONE;NAME NAME VISIBLE NONE;LSAD LSAD VISIBLE NONE;ALAND ALAND VISIBLE NONE;AWATER AWATER VISIBLE NONE")

#crop the latter to texas
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management("admin_boundaries_usa", "NEW_SELECTION", "\"STATEFP\" = '48'")

# Process: Copy Features
arcpy.CopyFeatures_management("admin_boundaries_usa", "tx_shp", "", "0", "0", "0")
arcpy.SelectLayerByAttribute_management("admin_boundaries_usa", "CLEAR_SELECTION")

#dissolve to have the whole state
arcpy.Dissolve_management("tx_shp", "tx_shp_Dissolve", "STATEFP", "", "MULTI_PART", "DISSOLVE_LINES")


#make loop for all rasters to be clipped w/ shapefile
rasterlist=[n1992_USA, n2001_USA, n2006_USA, n2011_USA]
OUTFOLDER = "C:\\Angela\\LC"

Everything works fine, until when I input the loop in ArcGIS:

 for inraster in rasterlist:
    print inraster
    tx_name = os.path.join(OUTFOLDER, str(inraster))
    arcpy.Clip_management(inraster, "-1075276.88510708 309884.007197296 251355.2459483 1547680.1003418", tx_name, "tx_shp_Dissolve", "255", "NONE", "NO_MAINTAIN_EXTENT")

and it retrieves the following:

Runtime error  Traceback (most recent call last):   File "<string>", line 4, in <module>   File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 13771, in Clip     raise e ExecuteError: ERROR 000670: output Output Raster Dataset is same as input Input Raster 

I know I must have a rookie mistake on the loop part, but I can't see what else I could do to make it work.

4
  • Please provide the complete error message, that includes the line number, for any code snippet that you present here.
    – PolyGeo
    Commented Feb 7, 2017 at 23:02
  • That still only looks like part of the error message. Are you saying that when you ran precisely the code that you have presented that is all you got? If so, then how did you run your code?
    – PolyGeo
    Commented Feb 7, 2017 at 23:09
  • Well, There is this button in ArcMap called "Python".......... Yes, that's all I got! Commented Feb 7, 2017 at 23:24
  • 1
    Perhaps you are saying that you first copy/pasted the top block of code into the Python window of ArcMap, and then you copy/pasted the second block of code into the Python window of ArcMap. Perhaps that could account for the line 4 in your error message. The advice on providing code snippets may be worth reviewing sometime, if you have not already.
    – PolyGeo
    Commented Feb 7, 2017 at 23:29

1 Answer 1

2

Your rasterlist items include full paths, so when you try to create an output name tx_name you are creating that with the full path.

n1992_USA = "C:\\Angela\\OK\\nlcd_1992_30meter_whole (1)\\nlcd_1992_30meter_whole.img"
n2001_USA = "C:\\Angela\\OK\\nlcd_2001_landcover_2011_edition_2014_10_10\\nlcd_2001_landcover_2011_edition_2014_10_10\\nlcd_2001_landcover_2011_edition_2014_10_10.img"
n2006_USA = "C:\\Angela\\OK\\nlcd_2006_landcover_2011_edition_2014_10_10\\nlcd_2006_landcover_2011_edition_2014_10_10\\nlcd_2006_landcover_2011_edition_2014_10_10.img"
n2011_USA = "C:\\Angela\\OK\\nlcd_2011_landcover_2011_edition_2014_10_10\\nlcd_2011_landcover_2011_edition_2014_10_10\\nlcd_2011_landcover_2011_edition_2014_10_10.img"

rasterlist=[n1992_USA, n2001_USA, n2006_USA, n2011_USA]

for inraster in rasterlist:
    print inraster
    tx_name = os.path.join(OUTFOLDER, str(inraster))
    arcpy.Clip_management(inraster, "-1075276.88510708 309884.007197296 251355.2459483 1547680.1003418", tx_name, "tx_shp_Dissolve", "255", "NONE", "NO_MAINTAIN_EXTENT")

You need to remove the file paths from your rasterlist so it just stores filenames that can then be used as input (os.join.path() to your input folder) and as output (os.join.path() to your output folder).

inputfolder = "C:\\Angela\\OK\\nlcd_2001_landcover_2011_edition_2014_10_10\\nlcd_2001_landcover_2011_edition_2014_10_10"
outputfolder = "C:\\Angela\\LC"

n1992_USA = "nlcd_1992_30meter_whole.img"
n2001_USA = "nlcd_2001_landcover_2011_edition_2014_10_10.img"
n2006_USA = "nlcd_2006_landcover_2011_edition_2014_10_10.img"
n2011_USA = "nlcd_2011_landcover_2011_edition_2014_10_10.img"

rasterlist=[n1992_USA, n2001_USA, n2006_USA, n2011_USA]

for inraster in rasterlist:
    print inraster
    inpath = os.path.join(inputfolder, inraster)
    tx_name = os.path.join(outputfolder, inraster)
    arcpy.Clip_management(inpath, "-1075276.88510708 309884.007197296 251355.2459483 1547680.1003418", tx_name, "tx_shp_Dissolve", "255", "NONE", "NO_MAINTAIN_EXTENT")

After thinking about how to handle the varied folder paths, I think just splitting your input rasters into separate lists containing a path item and a filename item may work. Then in your for loop you can make use of the two separately, or join them as required.

n1992_USA = ["C:\\Angela\\OK\\nlcd_1992_30meter_whole (1)", "nlcd_1992_30meter_whole.img"]
n2001_USA = ["C:\\Angela\\OK\\nlcd_2001_landcover_2011_edition_2014_10_10\\nlcd_2001_landcover_2011_edition_2014_10_10", "nlcd_2001_landcover_2011_edition_2014_10_10.img"]
n2006_USA = ["C:\\Angela\\OK\\nlcd_2006_landcover_2011_edition_2014_10_10\\nlcd_2006_landcover_2011_edition_2014_10_10", "nlcd_2006_landcover_2011_edition_2014_10_10.img"]
n2011_USA = ["C:\\Angela\\OK\\nlcd_2011_landcover_2011_edition_2014_10_10\\nlcd_2011_landcover_2011_edition_2014_10_10", "nlcd_2011_landcover_2011_edition_2014_10_10.img"]

rasterlist=[n1992_USA, n2001_USA, n2006_USA, n2011_USA]
OUTFOLDER = "C:\\Angela\\LC"

for inraster in rasterlist:
    inpath = inraster[0] # Get the path
    rasterfilename = inraster[1] # Get the raster filename
    inputfilepath = os.path.join(inpath, rasterfilename)
    tx_name = os.path.join(OUTFOLDER, rasterfilename)
    arcpy.Clip_management(inputfilepath , "-1075276.88510708 309884.007197296 251355.2459483 1547680.1003418", tx_name, "tx_shp_Dissolve", "255", "NONE", "NO_MAINTAIN_EXTENT")
13
  • Thanks for the suggestions. Indeed it's a great suggestion, but the in folder is not the same for each one of them. But I think I can work around from now on. Thank you so much! Commented Feb 7, 2017 at 23:16
  • @A_R then you'll either need to loop through the folders, add the folder info later, or strip the paths. This is the cause of your error
    – Midavalo
    Commented Feb 7, 2017 at 23:18
  • Yes, I think that add the folder info later is the way to go! Thank you once again Commented Feb 7, 2017 at 23:23
  • @A_R alternatively you could loop through your input rasters earlier and copy them to a temp folder so they're all in the same location, and then use your second loop to use those as input into your output folder
    – Midavalo
    Commented Feb 7, 2017 at 23:24
  • 1
    @A_R I have added another option
    – Midavalo
    Commented Feb 8, 2017 at 17:08

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.