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
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).
OUTPUT NAME.prj, and
OUTPUT NAME.dbf in the current directory
name="OUTPUT NAME" ;
rm "$name.shx" "$name.shp" "$name.prj" "$name.dbf" ;
gdalbuildvrt "$name".dem.vrt PATH_TO_DEMS/*.dem ;
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
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.
gdal_translate -of XYZ "$SOURCE" EDIT_ME.xyz
EDIT_ME.xyz in a text editor and fix the broken values.
gdal_translate -of GTiff "$SOURCE" .original.tif
listgeo .original.tif > .original.metadata
gdal_translate -of GTiff EDIT_ME.xyz .not_georeferenced.tif
geotifcp -g .original.metadata .not_georeferenced.tif .georeferenced.tif
gdal_translate -of HFA .georeferenced.tif "$DEST"
rm .original.tif .original.metadata .not_georeferenced.tif .georeferenced.tif
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
Other GIS pages on this site
Slippy map tile generator for QGIS
Python script to generate slippy map tiles from within QGIS.
Similar in functionality to the QTiles plugin but faster and
without the annoying rendering errors at tile edges.
Google/OSM to TMS tile renamer
Simple BASH script to rename map tiles from Google Maps/OpenStreetMap
format to Tile Map Service format.
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!
Last modified 2016-12-22
© 2009-2018 Alexander Hajnal