I am trying to convert more than 100 ascii files to raster using python code. All the lines (except Last two) in my code presented below is successfully converting Ascii to tif format in the working directory. However, I want to save the new TIF files in a separate directory. Therefore, the last two lines in my code, I am trying to save in a new directory. But, I am getting the below error:
"Traceback (most recent call last):
File "C:\Subhasis\Project-Biological_monitoring\R_SAGA\Raster_data\ASCII_Raster.py", line 22, in <module>
AttributeError: 'Result' object has no attribute 'save'"
Thanks lot for your time and help!
import arcpy, os
from arcpy import env
from arcpy.sa import *
#To overwrite output
arcpy.env.overwriteOutput = True
#set work environment
env.workspace = "C:/Subhasis/Project-Biological_monitoring/R_SAGA/Raster_data"
# Check out the ArcGIS Spatial Analyst extension license
for file in arcpy.ListFiles("*.asc"):
outputraster = file.strip(".asc")+ ".tif"
My python knowledge is fairly limited but I think the issue may be that you are attempting to use a raster object method (.save) on a variable that is not a raster object (Ascii_raster). According to the following resource, raster objects are created as output from a map algebra expression or by casting a raster dataset as a raster object: The interaction of the Raster object—ArcGIS Help | ArcGIS for Desktop.
Reviewing your script, it looks like the tiffs are already being saved in the current workspace during the conversion process. In this case, you might consider adding a CopyRaster_management process to copy "outputraster" to the separate directory. Alternatively, if you don't need the tiffs in the current workspace, you could swap lines 20 and 21 and use "outname" as the output dataset for the ascii to raster conversion.
There may be more efficient approaches (or I could be completely wrong) but just thought I would toss out some initial suggestions.
This line is wrong
syntax from the help files on ASCIIToRaster
# Set local variables
inDEM = "c:/data/fixed.dem"
outRaster = "c:/output/fixeddem.tif"
rasterType = "FLOAT"
zFactor = 0.30488
# Execute DEMToRaster
arcpy.DEMToRaster_conversion(inDEM, outRaster, rasterType, zFactor)
And you should print/arcpy.AddMessage your filename to ensure it is correct
I'm not sure why you are trying to create an object for your raster, instead of just having it create the tif in the directory you want. I believe if you just switch up some lines in your code this should fix the problem. Right now, Ascii_Raster is a result object, not a raster object, so it cannot be saved using that method since it doesn't have that method.
You should be able to use your full path to have it save the raster in the path you set
import arcpy, os
Thank s for your reply! I switched the last two line like you suggested. I got the following error message:
Traceback (most recent call last):
File "C:\Subhasis\Project-Biological_monitoring\R_SAGA\Raster_data\ASCII_Raster.py", line 21, in <module>
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\conversion.py", line 2217, in ASCIIToRaster
ExecuteError: ERROR 010158: Unable to open file AN0008~1.ASC.
ERROR 010067: Error in executing grid expression.
Failed to execute (ASCIIToRaster).
Can you please re-post your newly edited python snippet. Also, I would suggest trying to run the file in question (AN0008~1.ASC) through the Ascii to Raster geoprocessing tool manually (ArcToolbox > Conversion Tools > To Raster) to verify there is not a file specific issue.