Python Ascii to Raster

4207
7
09-03-2015 04:03 AM
robsimpson
New Contributor

Hi have around 100Asciis i need converting to rasters. I have this little script

# Import system modules 

import arcgisscripting, os, sys

 

# Create the Geoprocessor object 

gp = arcgisscripting.create(9.3) 

 

# Set local variables 

InAsciiFile = None 

inDir = r"E:\Hull Deliverables - Final\Defences Operating\DepthGrids\1d" 

OutRaster = r"E:\Hull Deliverables - Final\Analysis for Duty\allascii\rast"

 

for InAsciiFile in os.listdir(inDir): 

    if InAsciiFile.rsplit(".")[-1] == "asc": 

        print InAsciiFile 

             

# Process: ASCIIToRaster_conversion 

gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFile)), os.path.join((OutRaster,InAsciiFile.rsplit(".")[0][:9]), "FLOAT")

but for some reason i get the following error

😞

  File "H:\Documents\python\asciitorast3.py", line 17, in <module>

    gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFile)), os.path.join((OutRaster,InAsciiFile.rsplit(".")[0][:9]), "FLOAT")

ExecuteError: Failed to execute. Parameters are not valid.

ERROR 000885: Input ASCII raster file: E:\Hull Deliverables - Final\Defences Operating\DepthGrids\1d\info's does not have a file extension.

ERROR 000859: The required parameter Output raster is empty, or is not the type of Raster Dataset.

Failed to execute (ASCIIToRaster).

any help would be appreciated

thanks

Tags (4)
0 Kudos
7 Replies
ChrisSmith7
Frequent Contributor

Rob,

See Ian's answer below!

I'm going to leave the doc link in my answer though!

http://help.arcgis.com/EN/arcgisdesktop/10.0/help/index.html#//00120000002s000000

0 Kudos
IanMurray
Frequent Contributor

I think the problem is you are setting InAsciiFile = None, and never changing its value except in the for-loop which is a local variable, not global.  Since your ASCIItoRaster_conversion is outside the forloop, it uses the None as an input with os.path.join not the file name.  Move the conversion in the forloop so it can use the local variable InAsciiFile, not the global value.

ChrisSmith7
Frequent Contributor

Ah, wow - I must have had a long night, lol.

0 Kudos
robsimpson
New Contributor

Im new to python so sorry for the probably basic question, i think i did as you mentioned above but now get the following error

Traceback (most recent call last):

  File "H:\Documents\python\asciitorast3.py", line 14, in <module>

    if InAsciiFile.rsplit(".")[-1] == "asc":

AttributeError: 'NoneType' object has no attribute 'rsplit'

Thakns for the help so far

0 Kudos
DanPatterson_Retired
MVP Emeritus

It still doesn't have the filename.  perhaps if you could post some revised code it could be check.

I will demonstrate by example using the following

>>> InAsciiFile = None
>>> InAsciiFile.rsplit(".")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'rsplit'
>>>

Spot the error?

0 Kudos
DanPatterson_Retired
MVP Emeritus

you have a dash in the folder path...try locating the data elsewhere

0 Kudos
SteveLynch
Esri Regular Contributor

in your Py script, print the value of

os.path.join(inDir,InAsciiFile)), os.path.join((OutRaster,InAsciiFile.rsplit(".")[0][:9])

once and in ArcMap paste the result into ASCIIToRaster and see if the tool runs.

-Steve

0 Kudos