Useful GIS commands


This is a collection of commands for common GIS tasks using GDAL, OGR, and QGIS. Hope you find them useful.
In the commands below, bold items should be changed as appropriate. Commands are indicated like this. Optional items are indicated [ like this ].
All commandlines assume a Unix-like environment.
Comments are welcome, see the email link at the bottom of the page.

GDAL (mostly raster)

Combine a set of GeoTIFF tagged scans into a single image

No data areas in the source are black ("0 0 0") in this example.
gdalwarp -srcnodata "0 0 0" SOURCE [SOURCE ...] DEST.tif

Backup GeoTIFF metadata

Run this so that a GeoTIFF can be edited using an application that is not GeoTIFF-aware (e.g. GIMP).
listgeo IMAGE.tif > IMAGE.metadata

Restore GeoTIFF metadata

Run this after having a GeoTIFF's metadata being clobbered by an application that is not GeoTIFF-aware.
mv IMAGE.tif tmp.tif ; geotifcp -g IMAGE.metadata tmp.tif IMAGE.tif

Generate contour lines from DEM (from USGS, etc.) files

Input files are combined into a virtual file to avoid discontinuities at tile boundaries.
Source files can be in any format supported by GDAL (not just DEM), e.g. GeoTiff or Erdas Imagine Images (.img).
Ouputs to OUTPUT NAME.shp, OUTPUT NAME.shx, OUTPUT NAME.prj, and OUTPUT NAME.dbf in the current directory
name="OUTPUT NAME" ; METERS=NN.n ; rm "$name.shx" "$name.shp" "$name.prj" "$name.dbf" ; gdalbuildvrt "$name".dem.vrt PATH_TO_DEMS/*.dem ; shp="$name.shp" ; gdal_contour -a ELEV -i $METERS "$name.dem.vrt" "$shp"

Manually edit raster data

Useful for manually tweaking files that are e.g. causing gdal_contour to fail
Set ORIGINAL.img and OUTPUT.img to the input and desired output filenames.
This example is for .img files (as used e.g. by New York state for LIDAR data); the commands given will need to be tweaked for other formats.
export SOURCE="ORIGINAL.img" # Set the input filename export DEST="OUTPUT.img" # Set the output filename gdal_translate -of XYZ "$SOURCE" # Convert the data to plain text
Open in a text editor and fix the broken values.
gdal_translate -of GTiff "$SOURCE" .original.tif # Copy source data to a GeoTiff file (listgeo can't read .img files) listgeo .original.tif > .original.metadata # Extract metadata from temp file gdal_translate -of GTiff .not_georeferenced.tif # Convert edited text file to a GeoTiff geotifcp -g .original.metadata .not_georeferenced.tif .georeferenced.tif # Add saved metadata to the GeoTiff gdal_translate -of HFA .georeferenced.tif "$DEST" # Convert back to .img file rm .original.tif .original.metadata .not_georeferenced.tif .georeferenced.tif # Clean up temporary files

OGR (vector)

Clip a shapefile to another shapefile's area

The CRS of IN.shp and CLIP_AREA.shp must be the same
ogr2ogr -clipsrc CLIP_AREA.shp OUT.shp IN.shp

Clip a shapefile by corner coordinates

All coordinates are the units appropriate to the input file's CRS
X0 < X1, Y0 < Y1
ogr2ogr -clipsrc X0 Y0 X1 Y1 OUT.shp IN.shp


Show all datasources in a QGIS project

grep datasource PROJECT.qgs | sed 's/^.*[<]datasource[>]//;s/[<][/]datasource[>]//;s/[|]layername\=.*$//;s/\?type=.*$//;' | sort -u
Did you find this page useful? Want to say thanks? Consider leaving a tip via PayPal:

Other GIS pages on this site

I make no warranty or representation, either express or implied, with respect the behavior of the code presented on this page, its quality, performance, accuracy, merchantability, or fitness for a particular purpose. This code is provided 'as is', and you, by making use thereof, are assuming the entire risk. That said, I hope you this code useful. Have fun!