Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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:

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!

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
Excellent and informative response. Thank you. <Gold star awarded to Luke> –  Brachabre Mar 19 '13 at 15:41

For Linux is something like

#!/bin/bash
for FILE in arcgis_folders_here/*
do
  echo "Processing $FILE file..."
  gdal_translate -a_srs "+proj=tmerc +lat_0=39.668258 +lon_0=-8.133108 +k=1 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs" -of GTiff $FILE $FILE.tif
done

just replace

"+proj=tmerc +lat_0=39.668258 +lon_0=-8.133108 +k=1 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs"

with the CRS of your data

share|improve this answer

You can use the batch_gdal_translate() (R package: gdalUtils) from QGIS using Qgis-Processing-R. Open the Processing toolbox, go to R scripts -> Tools -> Create new R script and paste the code below.

(Don't forget to install gdalUtils in R, otherwise it won't work)

##Input = folder
##Output =folder
library(gdalUtils)
batch_gdal_translate(infiles=Input,outdir=Output,outsuffix="")

Input -> is the folder where the ESRI rasters are located. Delete everything else inside this folder

Output -> folder where you want to store the newly created .tiff

The names of the rasters will be preserved.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.