6

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!

4 Answers 4

5

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.

1
  • Excellent and informative response. Thank you. <Gold star awarded to Luke> Commented Mar 19, 2013 at 15:41
1

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

0

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.

0

This can be done using gdal translate within QGIS. To convert an ESRI GRID file the user has to enter the folder and select the hdr.adf as input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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