Batch process multiple ASCII to Raster files

16078
27
03-05-2012 11:46 AM
JenB
by
New Contributor
I have multiple (~50) ASCII files that I need to convert to raster. I???m very new to Python but have managed to create a simple script to do this for one file (and it worked- so exciting!). How can I modify this to process ALL of the files? They are all located in the same directory. Any advice would be appreciated. Thanks very much!

# Import system modules
import arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set local variables
InAsciiFile = "C:/Data/data_2001.asc"
OutRaster = "C:/Data/data_2001"
gp.outputCoordinateSystem = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

# Process: ASCIIToRaster_conversion
gp.ASCIIToRaster_conversion(InAsciiFile, OutRaster, "FLOAT")
Tags (2)
0 Kudos
27 Replies
BruceNielsen
Occasional Contributor III
I think this will work for you (it's untested):
# Import system modules
import arcgisscripting, os, glob

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set local variables
gp.workspace = "C:/Data"
gp.outputCoordinateSystem = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

# Process: ASCIIToRaster_conversion
for x in glob.glob(os.path.join(gp.workspace, "*.asc")):
    gp.ASCIIToRaster_conversion(x, os.path.split(os.path.basename(x))[0], "FLOAT")
0 Kudos
MathewCoyle
Frequent Contributor
Also untested
# Import system modules
import arcgisscripting, os

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set local variables
InAsciiFile = None
inDir = "C:/Data"
OutRaster = "C:/Data/data_2001"
gp.outputCoordinateSystem = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

for InAsciiFile in os.listdir(inDir):
    if InAsciiFile.rsplit(".")[-1] == "asc":
        print InAsciiFile
        try:
            # Process: ASCIIToRaster_conversion
            gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFile), OutRaster, "FLOAT")
        except:
            pass
0 Kudos
curtvprice
MVP Esteemed Contributor
One of the things that makes Arc 10 worth installing is model iterators, which avoid you all this drudgery.
0 Kudos
JenB
by
New Contributor
Thanks for the suggestions. I should have mentioned I'm using Python 2.5 and ArcGIS 9.3 (curtvprice, I can't upgrade to 10 yet as I am using a tool that was built exclusively for 9.3).

I tried both suggestions but unfortunately, I was not able to get either code to work.
mzcoyle- the ascii files read properly and printed in the log, but then nothing happened- I didn't get an error, but no files were converted. Any ideas?
bruce.nielsen- I got the following error- I'm not sure in this code where to specify the output?
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000859: The required parameter Output raster is empty, or is not the type of Raster Dataset.
Failed to execute (ASCIIToRaster).


Thanks again for your help.
0 Kudos
MathewCoyle
Frequent Contributor
I tested this one and it works. Make sure your file names are less than 13 characters.
# Import system modules
import arcgisscripting, os

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set local variables
InAsciiFile = None
inDir = r"C:\GIS\ascii"
OutRaster = "C:/GIS/Exports"
gp.outputCoordinateSystem = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

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]), "FLOAT")
0 Kudos
BruceNielsen
Occasional Contributor III
bruce.nielsen- I got the following error- I'm not sure in this code where to specify the output?
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000859: The required parameter Output raster is empty, or is not the type of Raster Dataset.
Failed to execute (ASCIIToRaster).


Thanks again for your help.


The ASCIIToRaster command as you wrote it required a valid workspace for creating GRID files. Try this instead:
gp.ASCIIToRaster_conversion(x, os.path.split(os.path.basename(x))[0] + ".tif", "FLOAT")
This will create TIF files, which can reside anywhere.
0 Kudos
JenB
by
New Contributor
I tested this one and it works. Make sure your file names are less than 13 characters.
# Import system modules
import arcgisscripting, os

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set local variables
InAsciiFile = None
inDir = r"C:\GIS\ascii"
OutRaster = "C:/GIS/Exports"
gp.outputCoordinateSystem = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

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]), "FLOAT")


This worked perfectly- thanks!!
0 Kudos
RumanaReaz
New Contributor
Hello mzcoyle,
I tried your code for batch processing of ASCII to Raster. It works but I got one problem.
How do I rename ASCII files or output Raster files, cause my ASCII files have more than 13 characters?

mzcoyle:
    I tested this one and it works. Make sure your file names are less than 13 characters.
    Code:

    # Import system modules
    import arcgisscripting, os

    # Create the Geoprocessor object
    gp = arcgisscripting.create()

    # Set local variables
    InAsciiFile = None
    inDir = r"C:\GIS\ascii"
    OutRaster = "C:/GIS/Exports"
    gp.outputCoordinateSystem = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

    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]), "FLOAT")
0 Kudos
MathewCoyle
Frequent Contributor
Hello mzcoyle,  
I tried your code for batch processing of ASCII to Raster. It works but I got one problem. 
How do I rename ASCII files or output Raster files, cause my ASCII files have more than 13 characters? 


To change the output name you could do something like this. Changing the output to only take the first 13 characters of the input file name. If you want it to be more dynamic, splitting on a specific string etc then you could incorporate that also. At that point I would probably create an output raster variable to create the name.
 gp.ASCIIToRaster_conversion(os.path.join(inDir, InAsciiFile), os.path.join(OutRaster, InAsciiFile.rsplit(".")[0][:13]), "FLOAT")
0 Kudos