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.
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:
gdalinfo -stats scene.tifAlways run first on unfamiliar data.
gdal_translate
Convert format or subset:
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]
8gdalwarp
Reproject, resample, mosaic:
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]
6gdal_rasterize
Convert vector to raster:
gdal_rasterize -a landuse_code -tr 30 30 landuse.gpkg landuse.tifgdal_polygonize.py
Convert raster classes to polygons:
gdal_polygonize.py classified.tif out.gpkggdaldem
Terrain products:
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.tifgdal_contour
Contours from a DEM:
gdal_contour -a elev -i 10 dem.tif contours.gpkgEssential vector commands
ogrinfo
Inspect:
1ogrinfo -so data.gpkg # list layers
2ogrinfo -so data.gpkg buildings # summary of one layer
3ogrinfo -al data.gpkg buildings # full detailogr2ogr
Convert / transform:
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]
12Batch processing
Shell loops handle bulk conversion:
1for f in *.shp; do
2 ogr2ogr -f GPKG "${f%.shp}.gpkg" "$f"
3doneOr with GNU Parallel:
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:
ogr2ogrguesses by extension. Use-fto be explicit. - Overwriting:
-overwritereplaces; otherwiseogr2ogrerrors on existing files. - Large files: use
-progressto see progress. - Multithreading:
gdalwarp -multiuses threads;--config GDAL_NUM_THREADS ALL_CPUSuses all cores. - Cache:
--config GDAL_CACHEMAX 2048increases read cache (MB). - CRS formats:
EPSG:4326,"+proj=longlat +datum=WGS84", or WKT — all accepted.
A typical data-prep pipeline
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]
9Four 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:
1for f in shp/*.shp; do
2 ogr2ogr -f GPKG "gpkg/$(basename ${f%.shp}).gpkg" "$f"
3doneOr parallel:
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,ogr2ograre 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-gisGitHub — curated list of tools.