Converts simple features data between file formats. In this case we will convert from Shapefiles to GeoJSON.
Binaries exist for macOS, Linux and Windows. See GDAL.org for downloads.
brew install gdal
sudo add-apt-repository ppa:ubuntugis/ppa && sudo apt-get update sudo apt-get install gdal-bin
Test to verify install
ogr2ogr --version # sample response, the version number could be different # GDAL 2.3.1, released 2018/06/22
There are several great tools for the command line and API’s that you can use to wrangle your geodata. The example that we will be discussing is raster GeoTIFFs from the US Forest Service FSTopo Raster Gateway.
You can use
gdal-config to obtain the GDAL version
or you can list the supported formats in your GDAL installation
Learning with Mt Whitney
Since we are working straight from the command line, we can start by calling commands to fetch to our GeoTIFFs. We have examples for
curl. You can use the FSTopo Raster Gateway if you prefer, but you can also start with the page that shows FSTopos near Mt Whitney.
wget http://data.fs.usda.gov/geodata/rastergateway/data/36118/fstopo/363011815_Mount_Whitney_FSTopo.tif # or curl -O http://data.fs.usda.gov/geodata/rastergateway/data/36118/fstopo/363011815_Mount_Whitney_FSTopo.tif
gdalinfo you can obtain great metadata about your file
gdalinfo 363011815_Mount_Whitney_FSTopo.tif # output gdalinfo in json gdalinfo -json 363011815_Mount_Whitney_FSTopo.tif # histogram of palette colors gdalinfo -hist 363011815_Mount_Whitney_FSTopo.tif
Translating our GeoTIFF to other formats is useful, so we can use
gdal_translate to perform operations. One caveat is that when you translate out of GeoTIFF, then you lose all geospatial information.
gdal_translate -of PNG 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.png # or translate to JPEG gdal_translate -of JPEG 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.jpg gdal_translate -of JPEG -expand rgb 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.jpg # or translate to WEBP gdal_translate -of WEBP -expand rgb 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.webp # or translate to uncompressed TIFF gdal_translate -of GTIFF -expand rgb 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.TIFF
gdal-linux - http://www.sarasafavi.com/installing-gdalogr-on-ubuntu.html. A Good resource with step by step intro. ↩