2

I'm trying to write a script in arcpy which reclassified a List of Rasters. This rasters are made by another tool before so there is already a loop. Actually I get the error 000582 and before this the script could reclassify but overwrote the rasters. In the end I like to count the pixel with the value 1 so that I create one raster with the sum of the value 1 as pixelvalue. How can I do this and what is wrong with the following script? Do I need the tool “plus” at the end?

import arcpy
import os
from arcpy import env
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")
env.overwriteOutput = True
env.workspace = r"D:\Julia_T\projekt\aufgabe3\testdatenA"
Dir = env.workspace
#folder to list
ascFileList = arcpy.ListFiles("*.asc")
print ascFileList


for ascFile in ascFileList:
#geoprocessing steps
ascFileName = os.path.splitext (ascFile)[0]
print ascFileName
#works
rastFile = ascFileName + "_output.img"
print rastFile
#works
#run the tool
arcpy.ASCIIToRaster_conversion (env.workspace + "\\" + ascFile, env.workspace + "\\" +     rastFile, "INTEGER")
# Process: Projektion definieren
arcpy.DefineProjection_management (env.workspace + "\\" + rastFile,     "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRI    MEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
#list raster
rasters = arcpy.ListRasters("*", "IMG")
for ras in rasters:
    print "working..." + ras #works
    # new name
    name = "class_" + ras
    # Recalssify
    reclassField = "Value"
    remap = RemapValue ([["8",1],["2",0],["5",0]])
    outReclassify = Reclassify(ras, reclassField, remap, "NODATA")
    #save
    outReclassify.save (Dir + "\\" + name)

# Execute Plus
#outPlus = Plus(inRaster1, inRaster2)

I'm working with ArcMap 10.2.1.

2 Answers 2

2

To sum many rasters (> 2) together, I'd suggest using Cell Statistics. The example from ArcMap help:

# Execute CellStatistics
outCellStatistics = CellStatistics([inRaster01, inRaster02, inRaster03], "SUM", "NODATA")
# Save the output 
outCellStatistics.save("C:/sapyexamples/output/cellstats")

It is slightly trickier in your case since you have an arbitrary number of rasters in your list; I think, though, that you may be able to use a variable (e.g. populate a new list of reclassified rasters -- rcRasterList -- and put that into the CellStatistics function).

3
  • I think it is working with creating two layer using the tool 'plus': One for the rasterlist and the other for a single reclassified raster. But I can not trust the value at the moment. So I will try your way – thanks
    – Nora
    Aug 6, 2014 at 13:25
  • Plus is designed to work with only two rasters, and if you feed it a list of rasters instead, I wouldn't expect it to work right. (Do let me know if my suggestion works or not, because I've never actually tried it with a list variable.)
    – Erica
    Aug 6, 2014 at 13:32
  • I had a pretty hart time getting this cellstatistic tool working: I need to put in the hole new ListRaster for the toolinput at the second loop. ' outCellStat = CellStatistics (arcpy.ListRasters("class_2*"),"SUM", "NODATA")'
    – Nora
    Aug 7, 2014 at 8:36
0

This is the result, it was kind of tricky for me!

import arcpy
import os
from arcpy import env
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")
env.overwriteOutput = True
env.workspace = r"D:\aufgabe_3\karadarya\2003\Step8"

#folder to list
ascFileList = arcpy.ListFiles("*.asc")
print ascFileList

for ascFile in ascFileList:
#geoprocessing steps
ascFileName = os.path.splitext (ascFile)[0]
print ascFileName
#works
rastFile = ascFileName + "_output.img"
print rastFile
#works
#run the tool
arcpy.ASCIIToRaster_conversion (env.workspace + "\\" + ascFile, env.workspace + "\\" +  rastFile, "INTEGER")
# Process: Projektion definieren
arcpy.DefineProjection_management (env.workspace + "\\" + rastFile,   "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRI  MEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
#list raster
# Execute Reclassify
reclassField = "Value"
remap = RemapValue ([["8",1],["2",0],["5",0]])
outReclassify = Reclassify(env.workspace + "\\" + rastFile, reclassField,remap,    "NODATA")
outReclassify.save (ascFileName+"_reclassified.img") #works

print "Ende Schleife"
recl_rasterlist = arcpy.ListRasters("*_reclassified*")

outCellStat = CellStatistics (recl_rasterlist,"SUM", "NODATA")
outCellStat.save(r"D:\aufgabe_3\result\cellStat2003.img")`
1
  • Could you please comment on this script (e.g. why this is useful, what is different, etc.)?
    – Aaron
    Aug 7, 2014 at 17:06

Your Answer

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

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