Using the below code, I want to extract a new raster from an original raster using value field and I want to automate the process for 70 more rasters. However, for testing purpose, I’m using only 3 rasters.
Out of 3 rasters inside the working directory, this code works only for one raster which has both ” ESRI grid” and “info” folder inside the working directory. I was not able to keep the “info” folders of other two raster inside the working directory as the “info” folder of 1st raster is already in the working directory.
Could you please let me know how can I keep the “info” folders of other two rasters inside the working directory, so that code will work and I can automate the process?
Code:
import arcpy, os
from arcpy import env
from arcpy.sa import *
#To overwrite output
arcpy.env.overwriteOutput = True
#Set environment settings
env.workspace = "C:/Subhasis/Test/Neshanic_Python"
outws="C:/Subhasis/Test/Neshanic_Python/extract"
#checkout ArcGIS spatial analyst extension license
arcpy.CheckOutExtension("Spatial")
inraster = arcpy.ListRasters("*", "GRID")
for i in inraster:
flds = ("VALUE", "COUNT")
dct = {row[0]:row[1] for row in arcpy.da.SearchCursor(i, flds)}
sumcnt = sum(dct.values())
dct1 = {k:v for (k,v) in dct.items() if k >= 15}
sumcnt1 = sum(dct1.values())
percentage=(float(sumcnt1)/float(sumcnt))
print percentage
newraster = ExtractByAttributes(str(i), "VALUE>=15")
outname=os.path.join(outws,str(i))
newraster.save(outname)