Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raster to ASCII - adding processing multiple files piece of code in Python

I wrote a piece of code in python that converts a raster file to ascii. Now, I need to make it handle possibly all files in the folder. Also, at the end save ascii files with the same name that the original with a suffix added. I am a total newbee in python and i promise i did my homework, i could just not make the batch processing work on my own. Any help will be so much appreciated!!

import arcpy
from arcpy import env
env.workspace = "C:/Data"
inRaster = ("test.img")
outASCII = "c:/output/test3.asc"
arcpy.RasterToASCII_conversion(inRaster, outASCII)
like image 919
mihoo Avatar asked Jul 03 '26 06:07

mihoo


1 Answers

Try this out:

import os
dir_name = ...
for filename in os.listdir(dir_name):
    if not filename.endswith(".img"): continue
    full_path = os.path.join(dir_name, filename)
    outASCII = '%s.asc' % (full_path,)
    arcpy.RasterToASCII_conversion(full_path, outASCII)

It gets all the filenames ending in .img in the directory dir_name and passes it to your conversion function.

like image 147
Claudiu Avatar answered Jul 04 '26 19:07

Claudiu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!