CoursesGIS Basics — A Complete Introduction18.3 GDAL/OGR — The Command-Line Swiss Army Knife
Module 18: Automation & Programming

18.3 GDAL/OGR — The Command-Line Swiss Army Knife

The command-line tools that underpin every other GIS library — essential for automation and pipelines.

Lesson 89 of 100·15 min read

Key takeaways

  • GDAL (raster) and OGR (vector) are the foundation libraries of open-source GIS.
  • Their command-line tools handle format conversion, reprojection, tiling, and more.
  • Learning a handful of commands replaces hours of clicking in a GUI.

Introduction

Under the hood, QGIS, GeoPandas, Rasterio, PostGIS loaders, and nearly every other open-source GIS tool rely on GDAL and OGR. Learning their CLI directly gives you a powerful scripting environment for data prep, conversion, and batch processing.

The vocabulary

  • GDAL — Geospatial Data Abstraction Library (rasters).
  • OGR — vector library (originally independent; now part of GDAL).
  • In practice, the distinction is mostly historical; "GDAL" refers to the whole.

Essential raster commands

gdalinfo

Inspect a raster:

Shell
gdalinfo -stats scene.tif

Always run first on unfamiliar data.

gdal_translate

Convert format or subset:

Shell
1# Convert BIL to GeoTIFF
2gdal_translate in.bil out.tif
3[object Object]
4[object Object]
5[object Object]
6[object Object]
7[object Object]
8

gdalwarp

Reproject, resample, mosaic:

Shell
1# Reproject to Web Mercator at 10 m resolution
2gdalwarp -t_srs EPSG:3857 -tr 10 10 -r bilinear in.tif out.tif
3[object Object]
4[object Object]
5[object Object]
6

gdal_rasterize

Convert vector to raster:

Shell
gdal_rasterize -a landuse_code -tr 30 30 landuse.gpkg landuse.tif

gdal_polygonize.py

Convert raster classes to polygons:

Shell
gdal_polygonize.py classified.tif out.gpkg

gdaldem

Terrain products:

Shell
1gdaldem slope dem.tif slope.tif
2gdaldem aspect dem.tif aspect.tif
3gdaldem hillshade dem.tif hillshade.tif -az 315 -alt 45
4gdaldem color-relief dem.tif colour_table.txt relief.tif

gdal_contour

Contours from a DEM:

Shell
gdal_contour -a elev -i 10 dem.tif contours.gpkg

Essential vector commands

ogrinfo

Inspect:

Shell
1ogrinfo -so data.gpkg                 # list layers
2ogrinfo -so data.gpkg buildings       # summary of one layer
3ogrinfo -al data.gpkg buildings       # full detail

ogr2ogr

Convert / transform:

Shell
1# Convert GeoJSON to GeoPackage
2ogr2ogr -f GPKG out.gpkg in.geojson
3[object Object]
4[object Object]
5[object Object]
6[object Object]
7[object Object]
8[object Object]
9[object Object]
10[object Object]
11[object Object]
12

Batch processing

Shell loops handle bulk conversion:

Shell
1for f in *.shp; do
2  ogr2ogr -f GPKG "${f%.shp}.gpkg" "$f"
3done

Or with GNU Parallel:

Shell
ls *.tif | parallel -j4 "gdal_translate -of COG {} cog/{}"

Python bindings

osgeo.gdal / osgeo.ogr give Python access; modern alternatives (rasterio, GeoPandas) wrap them with cleaner APIs. Use the CLI for quick scripts; use the libraries inside programs.

Tips and gotchas

  • Default driver: ogr2ogr guesses by extension. Use -f to be explicit.
  • Overwriting: -overwrite replaces; otherwise ogr2ogr errors on existing files.
  • Large files: use -progress to see progress.
  • Multithreading: gdalwarp -multi uses threads; --config GDAL_NUM_THREADS ALL_CPUS uses all cores.
  • Cache: --config GDAL_CACHEMAX 2048 increases read cache (MB).
  • CRS formats: EPSG:4326, "+proj=longlat +datum=WGS84", or WKT — all accepted.

A typical data-prep pipeline

Shell
1# 1. Inspect
2gdalinfo raw.tif
3ogrinfo -al raw.gpkg
4[object Object]
5[object Object]
6[object Object]
7[object Object]
8[object Object]
9

Four commands, no GUI, reproducible on any server.

Self-check exercises

1. You need to convert 500 shapefiles to GeoPackage overnight. How?

A shell loop with ogr2ogr:

Shell
1for f in shp/*.shp; do
2  ogr2ogr -f GPKG "gpkg/$(basename ${f%.shp}).gpkg" "$f"
3done

Or parallel:

Shell
ls shp/*.shp | parallel -j 8 "ogr2ogr -f GPKG gpkg/{/.}.gpkg {}"

With 8 cores, 500 files takes minutes.

2. How do you create a Cloud-Optimized GeoTIFF from an existing GeoTIFF?

gdal_translate -of COG -co COMPRESS=ZSTD input.tif output.tif. The -of COG driver creates properly structured internal tiles, overviews, and header ordering. Verify with python -m validate_cloud_optimized_geotiff output.tif or by running gdalinfo output.tif and looking for internal overviews.

3. OGR errors on a huge shapefile import with "too many features". What's probably wrong?

Shapefile's 2 GB file-size limit, or the 10-character field name limit truncating fields. The fix: convert to GeoPackage (ogr2ogr -f GPKG out.gpkg input.shp) which has neither limit, and use that as your working format. This is one of many reasons to retire shapefile for new data.

Summary

  • GDAL/OGR CLI tools handle every format, projection, and common transformation.
  • gdalinfo, gdalwarp, gdal_translate, ogrinfo, ogr2ogr are the daily tools.
  • Batch processing with shell loops and GNU Parallel scales to thousands of files.
  • Every Python GIS library wraps these commands under the hood.

Further reading

  • GDAL documentation (gdal.org).
  • GDAL Tutorial — official tutorial.
  • Frank Warmerdam's talks at FOSS4G.
  • awesome-gis GitHub — curated list of tools.