error in saving the TIF files in a separate directory

4178
10
04-15-2015 06:48 AM
Highlighted
New Contributor

Hi Guys,

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>

    Ascii_raster.save(outname)

AttributeError: 'Result' object has no attribute 'save'"

Thanks lot for your time and help!

Code:

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"

outWorkspace="C:/Subhasis/Project-Biological_monitoring/R_SAGA/Raster_data/Final"

dataType="INTEGER"

# Check out the ArcGIS Spatial Analyst extension license

arcpy.CheckOutExtension("Spatial")

for file in arcpy.ListFiles("*.asc"):

    outputraster = file.strip(".asc")+ ".tif"

    Ascii_raster=arcpy.ASCIIToRaster_conversion(file,outputraster,dataType)

    outname=os.path.join(outWorkspace,outputraster)

    Ascii_raster.save(outname)

Tags (2)
Reply
0 Kudos
10 Replies
Highlighted
Occasional Contributor III

Hi Inception,

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.

Highlighted
New Contributor

Thank you!

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

This line is wrong

Ascii_raster=arcpy.ASCIIToRaster_conversion(file,outputraster,dataType)

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

Highlighted
New Contributor

Thank you!

Reply
0 Kudos
Highlighted
Frequent Contributor

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 

  • 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" 
  • outWorkspace="C:/Subhasis/Project-Biological_monitoring/R_SAGA/Raster_data/Final" 
  • dataType="INTEGER" 
  • # Check out the ArcGIS Spatial Analyst extension license 
  • arcpy.CheckOutExtension("Spatial") 
  • for file in arcpy.ListFiles("*.asc"😞 
  •     outputraster = file.strip(".asc")+ ".tif" 
  •     outname=os.path.join(outWorkspace,outputraster)
  •     arcpy.ASCIIToRaster_conversion(file,outname,dataType) 

Highlighted
New Contributor

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>

    arcpy.ASCIIToRaster_conversion(file,outname,dataType)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\conversion.py", line 2217, in ASCIIToRaster

    raise e

ExecuteError: ERROR 010158: Unable to open file AN0008~1.ASC.

ERROR 010067: Error in executing grid expression.

Failed to execute (ASCIIToRaster).

Reply
0 Kudos
Highlighted
Occasional Contributor III

Hi Inception,

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.

Highlighted
Frequent Contributor

From a quick google search of that error code, it generally seems to be an ESRI problem, and most people have fixed by restarting their computers and trying the script again, or fixing their install of ArcGIS.

Combine Tool

raster combine error

Reply
0 Kudos
Highlighted
New Contributor

Thank you!

Reply
0 Kudos