The Challenge: To batch convert 1000 rasters in ESRI Grid binary to Geotiff using either using a batch file or Python 2.7 without ArcGIS/Arcpy module.
The Tools: Win7Pro, GDAL 1.9.2 (via OSGeo4W), python 2.6/2.7, 1000 binary ESRI Grid rasters (see image)
File structure:
Single file conversion: (piece of cake)
gdal_translate GRID1 -of GTiff GRID1.tif
Batch Route:
for %i in (*.img) do gdal_translate -of GTiff %i %~SourceFilename.tif
The Problem: ESRI Grids don't have a file extension and are instead treated as file directories by the operating system. I need to find an alternative way to iterate. My only option it seems is to iterate by directory as (to my knowledge) there is no method (to my knowledge) to read the GRIDs into a variable like using the ArcPy method: rasters = arcpy.listRasters("*", "GRID")
Does anyone have any suggestions about how to modify the (*.img) portion of the code so that the loop will read each GRID directory as a file object so I can iterate using the for loop posted above? Note: I'm also aware that I will need to add the parameter for recursion to the batch file and possibly ignore all directories with the name "info".
I know I could just use arcpy and python to accomplish this task, but I wasn't satisfied with the results on google of how to batch convert ESRI GRIDs with only GDAL and maybe python. I appreciate any and all advice!
Answer
You could script it in python with os.walk, fnmatch and subprocess.Popen. Alternatively, here's a batch file to do the same thing.
The key is that gdal can use either the grid name (directory) or grid name\hdr.adf to open the grid. To tell the difference between grid and vector coverage directories, the for loop searches for w*.adf as they are only found in grids.
@echo off
SET startpath=%1
FOR /R %startpath% %%f IN (w*1.adf) do (call :translate "%%f")
pause
GOTO :eof
:translate
SET var=%~dp1
ECHO Translating %var%hdr.adf to %var:~0,-1%.tif
gdal_translate %var%hdr.adf %var:~0,-1%.tif
Edit: just tested and found that gdal can also read the grid when passed the w001001.adf file as well as the directory name and the hdr.adf file.
No comments:
Post a Comment