Professional Documents
Culture Documents
E-mail: j.vanderkwast@unesco-ihe.org
OpenCourseWare
1
Contents
1 Introduction ............................................................................................................................................. 3
1.1 Learning objectives .......................................................................................................................... 3
2 Prerequisites ............................................................................................................................................ 3
2.1 Installation ....................................................................................................................................... 3
3 Using the command line .......................................................................................................................... 4
3.1 Opening a command prompt ........................................................................................................... 4
3.2 Viewing the contents of a folder...................................................................................................... 5
3.3 Changing directories ........................................................................................................................ 6
3.4 Creating files .................................................................................................................................... 8
3.5 Copy, move, rename and delete files ............................................................................................ 11
3.6 Command history ........................................................................................................................... 12
4 GDAL - The Geospatial Data Abstraction Library ................................................................................... 13
5 Retrieving information from GIS data .................................................................................................... 14
5.1 Raster data ..................................................................................................................................... 14
5.2 Vector data..................................................................................................................................... 14
6 Reprojecting files ................................................................................................................................... 15
6.1 Introduction ................................................................................................................................... 15
6.2 Raster data ..................................................................................................................................... 15
6.3 Vector data..................................................................................................................................... 15
7 Change raster properties ....................................................................................................................... 16
7.1 Resize an image.............................................................................................................................. 16
7.2 Change raster format ..................................................................................................................... 16
8 Spatial queries of vectors ....................................................................................................................... 17
9 Convert Comma Separated Files ............................................................................................................ 18
10 Batch conversion .................................................................................................................................... 20
2
1 Introduction
During these exercises you will get familiar with the basics of the Geodata Abstraction Library (GDAL).
The first part of these exercises introduce you to the DOS command prompt. If you are already familiar
with DOS commands, you can skip chapter 3 and start immediately with chapter 4.
These exercises are provided as OpenCourseWare and come without support. For support, please
register to our related short courses that can be found at http://www.unesco-ihe.org/shortcourses.
2 Prerequisites
2.1 Installation
For these exercises GDAL needs to be installed, preferably using the OSGEO4W distribution package.
3
3 Using the command line
During the excersises we are going to use the Command Prompt. If you already know how to use the the
Command Prompt (aka DOS prompt), you can skip this section.
4
C:\> is called the command prompt. It shows your current working directory (or folder). In this case it
means that you are at drive C (which is called the root). You cannot delete or edit the command
prompt. You'll have to type the commands after the > of the command prompt.
How does your command prompt look like? Give drive name, directory and (sub)directories. It
may differ from the example in the screenshot.
This is called a directory list. A directory list is a list of all the files and subdirectories that a directory
contains. If you type dir /? you will see all options of the dir command. For example, if you type
dir /w you will see the directory listing in columns.
5
3.3 Changing directories
Look at the list on your screen. All the names that have <DIR> beside them are directories (or folders).
The others are files. The size of the files is also displayed. You can see a list of the files in another directory
by changing to that directory, and then using the dir command again.
These commands create subdirectories called John and Peter. Check if the directory has been created
by listing the directories.
Type:
dir
Also try:
dir /AD
cd John
cd means "change directory". You will see that the command prompt changes to C:\...\John>
6
DOS is not case sensitive (it interprets upper and lowercase in a similar way), so you could also have
typed cd john.
Now you'll see that the directory is not empty. There are to directories, namely . and ..
Actually, the directory is empty, but it also shows the relative paths:
Type:
cd ..
Check the command prompt: we're back in the directory where we started the exercise. Now
type :
cd .
.\John
7
Or the absolute path (in my case): cd C:\Users\jkw\John
cd \
cd ..\..\..
Go back to the directory called John. You can choose if you want to use the relative or absolute
path.
Type cd ..\Peter
Look at the prompt and explain what this command did.
Both ways of changing directories are equivalent. Why bother than? Well, if you move your files that are
organized in directories and subdirecties to another drive (e.g. drive D), than your absolute paths will not
work, while your relative paths will still refer to the same locations. This is specifically important when
writing scripts, which we'll do later.
Now we're going to delete the directory called Peter. First move one directory up, type:
cd ..
than type:
rd Peter
If you want to change to another drive, e.g. drive D, you use this
command:
d:
c:
Move back to the directory where you created John before continuing with the next exercise.
8
Type:
Type:
dir /AD
Type
<CTRL>-<Z>
Check if the file is in the directory list. What is the size of the file? Now display the contents of
the file on the screen. Type:
type listdirectories.bat
Now type:
listdirectories
Congratulations! You have created your first batch file. Batch files are scripts that can be used to
execute commands in batch. In this case the file executed dir /AD
Because batch files always have the file extension .bat, the computer knows that this is a batch file
and will execute the commands in the file. We'll come back to this later.
This command will not show the result of the dir command on the screen, but saves it to an ASCII
file called list.txt. So we can use > after a command to save its results to a file instead of
printing it to the screen.
9
If the list is larger than the Command Prompt screen, you have to scroll. With large text files it is
easier to use:
The result of list.txt is given to the more command which displays the results in pages as big as your
window. Press <ENTER> to see the next line. Press <SPACE BAR> to see the next page. Press
<CTRL-C> to stop. You can use this last key combination to stop any command if it isn't doing what
you like, it crashed or it takes too long.
Type:
more
Nothing happens, because the more command expects input from another command. So you can
wait forever. In this case you can stop the execution of the more command by using <CTRL-C>.
What happened?
In summary:
• > saves the result of a command to a new file. If the file on the right hand of > already exists,
it will be overwritten.
• | uses the result of the command on the left hand side in the command on the right hand side
We can also use these operators to create so called lock files. These are used in scripting. The script will
check if a certain file exists. If it exists, the program will wait, if it is removed, the program continues.
Therefore, these files can be empty. You can create a lockfile with: type NUL > lockfile.txt
10
3.5 Copy, move, rename and delete files
Now we can copy the file list.txt to a new file by typing:
Because it is a bit confusing to have a copy of list.txt in subdirectory John, we're going to rename
it:
Type dir
Type
dir *.txt
dir *john.*
dir *.??t
dir *.?xt
Explain the function of * and ?
del newlist.txt
11
Now remove the directory John.
Type <F3>
Clear the command prompt and press the right arrow button several times. You can see that the
characters of the previous command are repeated. If you press the up and down buttons, you
can browse through the previously used command.
When you use <TAB> while typing a path or a filename, it will try to automatically complete it.
doskey /h
This prints all the commands you typed during this session to the screen.
If you close the command prompt, the command history will be lost.
Save the command history to a text file using one of the commands previously learned.
In this way you can edit the command history file in e.g. notepad. If you remove all the wrong commands
and you save the file with the .bat extension, you can execute all of the commands in batch.
Try this for a few of the commands you have learned so far.
You can close a command prompt either by clicking on the cross in the corner, typing exit and pressing
enter, or choosing Close when right clicking the Command Prompt icon on the task bar.
12
4 GDAL - The Geospatial Data Abstraction Library
GDAL is a translator library for raster geospatial data formats that is released under an X/MIT style Open
Source license by the Open Source Geospatial Foundation. As a library, it presents a single abstract data
model to the calling application for all supported formats. It also comes with a variety of useful
commandline utilities for data translation and processing. See for more info: http://www.gdal.org
srtm_37_02.tif: tile of a Digital Elevation Model (DEM) from the Shuttle Radar
Topography Mission (SRTM) (http://www2.jpl.nasa.gov/srtm/)
Before continuing, make sure that you are in the right folder.
Open the OSGeo4W Shell (Start → All Programs → OSGeo4W → OSGeo4W Shell)
Change directory to
D:\gdalExercises
13
5 Retrieving information from GIS data
There exists a useful online tool to convert .prj format (as included by ESRI files) to EPSG code:
http://prj2epsg.org
14
6 Reprojecting files
6.1 Introduction
In this exercise we want to make a map of the community of Delft with the main roads and the relief.
Because the datasets are in different formats we have to reproject them to a common coordinate
system. Here we reproject all datasets to the Dutch Amersfoort/RD New projection.
The -t_srs argument specifies the target coordinate system. If the source coordinate system is
unknown it must be specified with the -s_srs argument. EPSG:... specifies the EPSG code of the
projection.
We are now going to reproject a Digital Elevation Model (DEM) acquired by the Shuttle Radar
Topography Mission (SRTM). You can use this website to download DEM's for your area of interest:
http://srtm.csi.cgiar.org/index.asp
In order to reproject the DEM from WGS-84 lat/lon to Amersfoort/RD New we use this command:
Replace XXXXX with the proper EPSG code for Amersfoort/RD New (see one of your previous
answers using ogrinfo).
Execute the command and visualize the result in QGIS.
Execute:
ogr2ogr -t_srs EPSG:XXXXX roadsreprojected.shp roads.shp
Replace XXXXX with the proper EPSG code.
Note that with ogr the output filename is typed before the input filename! With gdal and most other
tools this is different.
15
7 Change raster properties
7.1 Resize an image
The gdal_translate command gives us several options to modify raster images.
gdal_translate can be used simply to change the size of an image using the -outsize
parameter, which takes two integer values as the xsize and ysize respectively, or two percentage
values to scale the image. The syntax is:
gdal_translate -outsize newxsize newysize inputFile outputFile
Now we are going to convert the DEM from geoTiff to PCRaster format. PCRaster is open source
software for spatial dynamic modelling and has its own GIS format (http://pcraster.geo.uu.nl).
Execute:
gdal_translate -of PCRaster -ot Float64 dem_rd.tif dem.map
Visualize the result in QGIS
The -ot argument is needed to specifiy that it is continuous data, so PCRaster will interpret the map as
a scalar (continuous) data layer. Use -ot Int32 to convert to integer (thematic) maps. Use
-ot Byte to convert to a boolean PCRaster map.
16
8 Spatial queries of vectors
For our map of Delft we want to do the following GIS analysis:
Select the community of Delft from the community map and save it into a new shapefile;
Intersect the community boundaries of Delft with the road map of the Netherlands.
What is the attribute in the community map containing the names of the communities? You can
use either ogrinfo or QGIS to answer this question.
This will save the feature with GM_NAAM Delft to a new file called delft.shp. The argument -
a_srs EPSG:28992 is used to assign the Amersfoort/RD New projection to the output file. The
argument -f defines the output format.
Now open in QGIS the reprojected DEM (dem_rd.tif), the reprojected road map
(roadsreprojected.shp) and the community of Delft (delft.shp).
17
9 Convert Comma Separated Files
Sometimes you want to reproject coordinates in an ASCII file, e.g. which has been saved in a
spreadsheet program. Here we will convert the coordinates in a comma separated ASCII file
(locations.csv) to a new ASCII file (locations_reprojected.csv).
We can also convert the locations to Google Earth KML format. Type:
ogr2ogr -f KML locations.kml locations.vrt locations
18
Note that you don't need to specify an output projection (-t_srs), because for KML this is
always WGS 84 (EPSG:4326).
Visualize the results in Google Earth in Windows. If you don't have Google Earth yet, you can
download it from: http://www.google.com/earth/index.html
19
10 Batch conversion
Desktop GIS programmes are very useful for GIS operations, but are hard to use if we have to repeat the
same task for many GIS layers. Then scripting can be a solution.
Here we have an example dataset from a land-use model of Dublin. The data are in IDRISI raster format,
with one layer for each year between 1990 and 2030. Our task is to convert all layers to .tif format.
Try to understand the code. This is a for loop that loops over all *.rst files in the folder. %%f is the
variable that contains the filename of each file. With echo we can print something to the screen.
Here we print %%~nf , which is the part of the filename before the dot that separates it from the
extension. Then we use the gdal_translate command with output format GeoTiff. At the end of
the line we add the .tif extension to the filename.
Now you can execute the batch file and check the results.
20