DEV Community 👩‍💻👨‍💻

Gabriel Wu
Gabriel Wu

Posted on • Updated on

My GDAL CLI snippets

To crop a raster file according to a shapefile

gdalwarp -overwrite -s_srs EPSG:32649 -q -cutline shapefile.shp -of GTiff raw.tiff crop.tiff

To merge several bands into a raster file -o target.tiff -of GTiff -ps 10 10 -separate B1.tiff B2.tiff B3.tiff

Here 10 10 means 10 meters in the x-axis and 10 meters in the y-axis. If -ps is not specified, the spatial resolution of the first raster file will be considered to be the target resolution.

To merge several files spatially -o target.tiff -of GTiff TL.tiff BL.tiff TR.tiff BR.tiff

To resample a raster file

gdal_translate -outsize 50 50 raw.jp2 target.jp2

Here 50 50 means 50% in x-axis, and 50% in y-axis.

Appendix: Install GDAL for Python on Ubuntu

# Install gdal library
sudo apt-get install -y gdal-bin libgdal-dev
# gdal-bin is not necessary, but you will definitely need it to run the CLI commands above

# Export include path
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal

# Install the exact version of Python package
gdal-config --version | xargs -0 -I {} pip install gdal=={}

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.