Batch process multiple ASCII to Raster files

16076
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
RaphaelR
Occasional Contributor II
looks like i forgot a bracket at the end, sorry!

os.rename(os.path.join(inDir,InAsciiFile), os.path.join(inDir,InAsciiFilename))
0 Kudos
RumanaReaz
New Contributor
looks like i forgot a bracket at the end, sorry!

os.rename(os.path.join(inDir,InAsciiFile), os.path.join(inDir,InAsciiFilename))


Thank you for getting back to me.
I am really sorry to bother you again but there is again error and this time is with ascii2raster conversion:

Traceback (most recent call last):
  File "C:\Users\rumana\Desktop\My EFDC\Data\AVHRR\BatchAscii2Raster5.py", line 23, in <module>
    gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFilename), os.path.join(OutRaster,InAsciiFilename.rsplit(".")[0][:11]), "FLOAT")
ExecuteError: ERROR 000875: Output raster: C:/Users/rumana/Desktop/My EFDC/Data/AVHRR/SST_Feb11/RasterUp_Feb11\SST2011_037's workspace is an invalid output workspace.
ERROR 000581: Invalid parameters.
Failed to execute (ASCIIToRaster).
0 Kudos
RumanaReaz
New Contributor
No worries, seems like the code is working now.
Thanks a trillion for your outstanding help.
0 Kudos
Madan_KrishnaSuwal
New Contributor
Dear all
I am trying to convert many ASCII files to Raster from Python
I edited following codes from different sources
but I don't know where it went wrong, but not working
it just list out different ASCII file names present in the folder
any our help be appreciated
thanking you

here is my code
# Import system modules
import arcgisscripting, os

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

# Set local variables
InAsciiFile = None
inDir = r'J:/BIOCLIM_Data/a1b_scenario/csiro_mk3_0_sres_a1b_2020s_bio_30s_no_tile_asc'
OutRaster = 'J:/BIOCLIM_Data/a1b_scenario/csiro_mk3_0_sres_a1b_2020s_bio_30s_no_tile_asc/Raster format'
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:
            full_path = os.path.join(inDir, filename)
            inASCII = '%s.asc' % (full_path,)
            outRaster = '%s.img' % (full_path,)
            # Process: ASCIIToRaster_conversion
            gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFile), OutRaster, "FLOAT")
        except:
            pass

0 Kudos
RaphaelR
Occasional Contributor II
if you just need to convert all the .asc files in one folder and TIF-Rasters are ok, you could try the script below.

import os,arcpy, glob

# Path to ascii files
filepath = r"E:\test\ascdirectory"
# Path where to put rasters
outFolder = r"E:\test\ascdirectory\rasters"

ascList = glob.glob(filepath + "/*.asc")
print ascList

for ascFile in ascList:
    outRaster = outFolder + "/" + os.path.split(ascFile)[1][:-3] + "tif"
    print outRaster
    arcpy.ASCIIToRaster_conversion(ascFile, outRaster, "FLOAT")
RodrigoTorres
New Contributor

Hi, I tried out your script and it worked perfectly for a tif, but I requiere a GRID-raster, what do you recomend I should do?

0 Kudos
curtvprice
MVP Esteemed Contributor

just set the path to not end with ".tif" 

Esri grids need to be named < 14 characters and need to start with a letter and no funny characters

# remove file extension and path
grid_name = os.path.splitext(os.path.basename(ascFile))[0][:13]
# clean up funky characters
grid_name = arcpy.ValidateTableName(grid_name, outFolder)
# set output Esri grid path
outRaster = outFolder + "/" + grid_name
0 Kudos
Madan_KrishnaSuwal
New Contributor
Thanks for the codes,
I Think its working now, I will post more latter

thanking you

# Import system modules
import arcgisscripting, os

# Create the Geoprocessor object
gp = arcgisscripting.create()
import arcpy
import os
import glob

# Path to ascii files
filepath = r"J:/BIOCLIM_Data/a1b_scenario/csiro_mk3_0_sres_a1b_2020s_bio_30s_no_tile_asc/new folder"
# Path where to put rasters
outFolder = r"J:/BIOCLIM_Data/a1b_scenario/csiro_mk3_0_sres_a1b_2020s_bio_30s_no_tile_asc/new folder"

ascList = glob.glob(filepath + "/*.asc")
print ascList

for ascFile in ascList:
    outRaster = outFolder + "/" + os.path.split(ascFile)[1][:-3] + "tif"
    print outRaster
    arcpy.ASCIIToRaster_conversion(ascFile, outRaster, "FLOAT")
0 Kudos