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.
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)
-
3The "info" directory contents are part of the grid format. You can copy multiple grids into a single workspace, but you can't merge info directories at the filesystem level without data loss.Vince– Vince2014年07月24日 16:10:54 +00:00Commented Jul 24, 2014 at 16:10
-
1Further to Vince's advice you should always use ArcCatalog to move spatial data otherwise you get yourself into the mess that you are in. If a folder (e.g. c:\temp) was going to hold multiple ESRI grids then you only ever see 1 info folder but this contains important information about every grid dataset. Use ArcCatalog to copy and paste all your ESRI grids into a single folder.Hornbydd– Hornbydd2014年07月24日 16:45:15 +00:00Commented Jul 24, 2014 at 16:45
-
@Vince and Hornbydd, Thanks for your quick response. What I did I converted the grid to TIF format which has no common file between TIF files and that solved my problem. Thanks guys!Inception– Inception2014年07月24日 17:43:28 +00:00Commented Jul 24, 2014 at 17:43
-
@Inception I think you should write your comment up as an answer, perhaps referencing the advice offered by Vince and Hornbydd to make it worthwhile.PolyGeo– PolyGeo ♦2014年08月10日 01:22:58 +00:00Commented Aug 10, 2014 at 1:22
1 Answer 1
As indicated in the Comments the asker of this question was able to achieve a solution by converting the grid to TIF which is a more self contained format that does not require some parts of it to be written to a common folder (i.e. info).
Had the original format been a mandatory requirement then this could also have been achieved by using ArcPy Copy and Delete functions that are aware of how to move all parts of the grid correctly.
Explore related questions
See similar questions with these tags.