Zero-1-Earth!

To content | To menu | To search

Saturday, April 24 2010

Building global gtopo30

GTOPO30, completed in late 1996, was developed othrough a collaborative effort led by the U.S. Geological Survey’s Center for Earth Resources Observation and Science (EROS). GTOPO30 is a global digital elevation model (DEM) with a horizontal grid spacing of 30 arc seconds (approximately 1 kilometer).

Gtopo30 is provided in tiles. Here is how to mosaic them into a single mosaic. First download from GTOPO30 the tiles you need to mosaic. Then write a script like the one I describe below (save it and change its permission to make it executable, chmod u+x make_gtopo30.sh)

First let’s define parameters.

inDir=/Volumes/TimeMachine/data/gtopo30/in
outDir=/Volumes/TimeMachine/data/gtopo30/out
outFile=$outDir/gtopo30_global.tif
tmpDir=/Volumes/TimeMachine/data/gtopo30/tmp
# ensure outDir and tmpDir are created
mkdir -p $outDir
mkdir -p $tmpDir

You must adapt those variables to the actual places where you saved the tiles (inDir), and where you want to have the result saved (outDir) and the temporary directory (tmpDir).

Then extract all files, with tar xvf (x=extract, v=verbose, f=file). tar command is a bit dull or old fashion. To force it to extract files into tmpDir while the tar files are in inDir, I saved the current directory (orgDir=$(pwd)) then moved to the target dir (tmpDir) and once all extracted, returned to the original directory:

orgDir=$(pwd)
cd $tmpDir
for tarFile in $inDir/*.tar
do
tar xvf $tarFile
done
cd $orgDir
Now, we are ready to build a mosaic. First get names of the files to process: DEM files have the .DEM extension. Save the list of these files into a variable, and pass it to gdal_merge.py
fileList=$(ls $tmpDir/*.DEM)
gdal_merge.py -o $outFile -of gtiff -co "compress=lzw" $fileList
Job done! Do  not forget to delete the temporary directory
\rm -r $tmpDir

For a global mosaic, I ended with a 2.2Gb file (with internal compression). You can of course force the output resolution by using the -ps option in gdal_merge.py.

You can download the script from here.


Wednesday, November 18 2009

install gdal on Mac OS X


Mac OS X is built on Unix BSD and thus offers terminal and bash for good old scripting. To add gdal and its executables (gdal_translate, gdal_merge.py etc.), go first downloading the framework prepared by KyngChaos http://www.kyngchaos.com/software:frameworks and download Gdal Complete.dmg.

The adaptation done for Mac OS X  is just perfect! To make it run: mount the dmg, then double click on the installation package. Once the installation is done, you must set the PATH variable for bash.

Launch Terminal, then type

export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH

This command adds /Library/Frameworks/GDAL.framework/Programs to the search PATH.

If you want to save this setting, edit the hidden file .bash_profile in your home directory, and add the above command line; then save. For beginners: the dot ‘.’ before bash_profile corresponds to a hidden file. To immediately set the path, type

source .bash_profile

Anyway, .bash_profile will be read again next time you run a terminal.

Enjoy!