OS raster

Optimising Raster For Web Mapping (on Linux)

OS raster colourThe previous post showed how to process panchromatic and colour TIFF images on Windows to optimise them for web mapping.  Here I’ll show you how to do the same thing using bash scripts on Linux.  This assumes you have GDAL installed (gdal-bin and python-gdal on Ubuntu/Debian/Mint).  The commands below will be available on your path.

$ gdalinfo --version
$ GDAL 1.9.2, released 2012/10/08

Using the same directory structure:

processing/
  gtiff/
  gtiff_rgb/
  gtiff_opt/

You could do this in one bash script or three:

1. Create RGB GeoTIFFS (full colour images)

#!/bin/bash
# Create geotiffs
for f in $(ls *.tif)
  do
    echo Processing $f
    gdal_translate -expand rgb -of GTiff -a_srs EPSG:27700 \
    -co "TILED=YES" $f ../gtiff_rgb/$f
  done
exit 0

2. Optimise GeoTIFFS

#!/bin/bash
# Optimise geotiffs
for f in $(ls *.tif)
  do
   echo Optimising $f
   gdal_translate -of Gtiff -a_srs EPSG:27700 -co "TILED=YES" \
   -co "TFW=YES" -co "PROFILE=Geotiff" -co "INTERLEAVE=PIXEL" \
   -co "PHOTOMETRIC=YCBCR" -co "COMPRESS=JPEG" \
   -co "JPEG_QUALITY=95" gtiff_rgb/$f gtiff_opt/$f
  done
exit 0

3. Create overviews

#!/bin/bash
# Add overviews
for f in $(ls *.tif)
  do
    echo Adding overviews to $f
    gdaladdo -r average --config COMPRESS_OVERVIEW JPEG \
    --config PHOTOMETRIC_OVERVIEW YCBCR \
    --config INTERLEAVE_OVERVIEW PIXEL $f 2 4 8 16 32 64 128 256
  done
exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *