GDAL

From NCEAS Knowledge Base

Jump to: navigation, search

The Geospatial Data Abstraction Library (GDAL/OGR) is an open-source, cross-platform set of libraries and low-level tools for working with spatial data in many formats. Although GDAL itself is geared towards raster data, it provides vector support via the OGR Simple Features Library. The term GDAL is often used to refer jointly to the full suite of GDAL and OGR functionality. GDAL/OGR plays a particular important role in the open-source geospatial software world as a file import/export/translation engine, as it can read (and often write) many spatial file formats. The associated command-line utilities can also be used to accomplish a variety of common tasks such as reprojecting, merging, and subsetting spatial data.

Contents

Installation guides

As of March 2008, the latest release is GDAL/OGR 1.5.1.

Binary installers for recent releases can be downloaded here. Windows and Linux users may find it useful to simply install the FWTools kit, which includes the GDAL/OGR tools plus several other useful utility programs. Note that for the most recent version, it is often necessary to build from source.

Note for Ubuntu users: Up-to-date packages of GDAL and other useful GIS tools are available at this third-party UbuntuGIS repository maintained by Jachym Cepicky.


How can I...

See what formats are supported by my version of GDAL/OGR

ogrinfo --formats
gdalinfo --formats


Obtain vector projection and other information

Get projection info and list of attribute columns

# Shapefile
ogrinfo -al -so myshapefile.shp
# PostGIS table
ogrinfo -al -so PG:dbname=mydatabase mytable
# ...or simply list the spatial layers in a PostGIS database
ogrinfo -so PG:dbname=mydatabase


Obtain raster projection and other information

Get projection info, bounding box coordinates, and other info

gdalinfo myraster


Reproject a vector

When using ogr2ogr, remember that the output filename should come before the input filename!

Option 1: Use the EPSG code (if known) to specify the projection

ogr2ogr -t_srs EPSG:2784 output_vector input_vector

Option 2: Use an appopriate projection file (e.g. the *.prj file associated with a shapefile) to specify the new projection

ogr2ogr -t_srs some_shapefile.prj output_vector input_vector

Option 3: Manually specify full Proj.4 definition string

ogr2ogr -t_srs '+proj=tmerc +lat_0=21.16666666666667 +lon_0=-158 \
                +k=0.999990 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs \
                output_vector input_vector


Reproject a raster

Because a square grid in one projection is not necessarily a square grid in a nother, reprojecting a raster layer often requires "warping". Fortunately, this can usually be done quite easily using gdalwarp. As with other GDAL/OGR tools, the output projection can be specified using the EPSG code, an appropriate file, or the full Proj.4 definition string. Using EPSG code 2784 as an example, the general syntax is as below. In contrast to ogr2ogr, the output filename should come after the input filename

gdalwarp -t_srs EPSG:2784 input_raster output_raster


Assign a "nodata" value to a raster

You may occasionally have a raster dataset in which a particular value indicates lack of information. Prior to analysis and display, it is often useful to designate this nodata value explicitly. This can be done using gdal_translate:

gdal_translate -a_nodata 255 input_raster output_raster

(Note that nodata values are not supported by all raster formats.)


"Burn" polygons into an raster

The gdal_rasterize function will (re)assign a specified value to all cells of the input raster that overlap any polygons of the input vector. The example below assumes you have a polygon shapefile mypolygons.shp, and a single band raster layer myraster in the same projection. (Other supported vector and raster formats can be substituted.)

gdal_rasterize -b 1 -burn 255 -l mypolygons mypolygons.shp myraster

Note that if the raster layer has a nodata value, specifying this value as the -burn value can be a convenient way to "clip out" portions of the raster.


GDAL/OGR resources on the web

Resource portals

Online tutorials and demos

Personal tools