Tag Archives: GDAL

TuiView

TuiView is an open source viewer for remote sensing data, named after the New Zealand bird (Tui). It’s primarily for viewing raster data but it will also display vectors. I’ve been using it as my main remote sensing viewer for over a year and I’d highly recommend giving it a try.

tuview_nlcd_alaska

It works on windows, OS X and Linux, if you’re on windows, you can grab an installer from here and you should be good to go (don’t worry about there only being 32 bit installers it’s pretty light on memory).

If you’re on Linux / OS X you binaries are available through conda from binstar.org/osgeo or you can install from source.

To install from source you need to have GDAL, numpy and PyQt installed. I’d recommend installing GDAL from source and making sure you build with python bindings (–with-python). For Linux you can install numpy and PyQt from the package manager. Under OS X, assuming you already have numpy, you need to install Qt4, then install sip and PyQt4. If you want to view vectors you also need turbogdal.

After the prerequisites have been installed you can install tuiview using:

hg clone https://bitbucket.org/chchrsc/tuiview tuiview
cd tuiview
sudo python setup.py install

You should then be able to bring up TuiView by typing tuiview in a terminal window. You can load in an image by clicking on the first icon on the left.

TuiView can handle very large (> 50 GB) datasets, very quickly if you build pyramids for the dataset and pre-calculate statistics. You can use gdalcalcstats from gdalutils, or the popimgstats option in RSGISLib

gdalcalcstats image.kea -ignore 0
# or
rsgiscmd --algor imageutils --option popimgstats \
   --image image.kea --ignore 0 \
   --pyramids yes

Note: The rsgiscmd interface is shown here, you could also use XML or the Python bindings.
It’s a good habit to create pyramids and calculate statistics for outputs at the end of your scripts as other programs (such as ArcGIS and ERDAS Imagine) can also use them.

TuiView has lots of options, including the ability to load multiple images in the same or linked windows, and excellent support for raster attribute tables. A comprehensive guide to the functionality is available on the TuiView wiki.

Generate a distance-to-class raster

A raster, where each pixel provides the distance to a class is useful for lots of classification / modelling applications. Using the gdal_proximity command it’s really easy to generate one.

Assuming your classification is already a raster (you can use gdal_rasterize to convert a vector to a raster if needed), the command to calculate the proximity to class 1 (-values 1), in geographic units (-distunits GEO), up to a maximum distance of 1000 m (-maxdist 1000; assuming your input is in m), setting no-data values to 1000 (maximum distance; -nodata 1000) and saving as a KEA file (-of KEA), is:

 gdal_proximity.py -of KEA -ot UInt16 -distunits GEO \
-maxdist 1000 -nodata 1000 -values 1 \
classification.kea \
proximity2class.kea
 

Resize image using another image

With the gdalwarp command it is possible to resize an image. Combining with python, to get the size of an existing image, it is possible to create a script to resize and reproject one image to another image.

1. Import GDAL


import os

import osgeo.gdal as gdal

2. Get bounding box and projection from input image

# Get information from image
dataset = gdal.Open(inBaseFile, gdal.GA_ReadOnly )
projection = dataset.GetProjection()
geotransform = dataset.GetGeoTransform()
xSize = dataset.RasterXSize
ySize = dataset.RasterYSize

# Get bounding box
minX = geotransform[0]
maxY = geotransform[3]
pixSizeX = geotransform[1]
pixSizeY = geotransform[5]
maxX = minX + (xSize * pixSizeX)
minY = maxY + (ySize * pixSizeY)

3. Create gdalwarp command from information.

bbStr = ' -te %s %s %s %s '%(minX, minY, maxX, maxY)
resStr = ' -tr %s %s '%(pixSizeX, pixSizeY)
projectionStr = ''' -t_srs '%s' ''' %(projection)
warpOptions = ' -of KEA ' + bbStr + resStr + projectionStr
warpCMD = 'gdalwarp ' + warpOptions + inLargeImage + ' ' + \
    outSubsetImage
os.system(warpCMD)

A full script is available in the RSGIS_Scripts repository, and can be run using:

python subsetImage2Image.py baseImage inImage outImage

Quick look image of a multi-image mosaic

For large mosaics it’s common for images to be delivered / processed in smaller tiles. Creating a quick look is a good way of visually checking the data and making sure there are no missing tiles. The combination of the GDAL Virtual Raster Format (VRT; http://www.gdal.org/gdal_vrttut.html), a text file containing links to the individual tiles and the -outsize option in GDAL translate provides a quick way of producing a mosaic. For recursively finding files the find command is used.

Therefore, to create the mosaic a total of three commands are used:

# 1) Recursively find files with the extension tif
# (you could use ls if there are in one directory)

find ./ -name '.*tif' > filelist.txt

# 2) Create a GDAL virtual raster
gdalbuildvrt -input_file_list filelist.txt virtualmosaic.vrt

# 2) Create a PNG 10  % of the size, scaled from 0 - 255
gdal_translate -of PNG -ot Byte -scale -outsize 10% 10% \
virtualmosaic.vrt quicklookmosaic.png

How successfully the pixel values scale will depend on the range of values in your input image. It your image looks odd try leaving of the scale option and setting the data type (-ot) to the same as the input image. You can then open with your normal viewer for remote sensing data and stretch the image in that (if you need a remote sensing data viewer TuiView is our recommendation).

If you remove the -outsize flag, gdal_translate can also be used to create the full size mosaic. As virtual rasters are handled like standard datasets in anything that uses GDAL you can use them as part of your processing chain, like you would a real dataset.

Simplified raster handling in Python

Raster I/O Simplification (RIOS) is a set of python modules, built on top of GDAL which makes reading and writing raster datasets much simpler (as the name promises). In a couple of lines of code it’s possible to apply a python function, taking numpy arrays as input and output, to a single or multiple images.

RIOS can be downloaded from https://bitbucket.org/chchrsc/rios/downloads. Assuming the GDAL bindings for python are already installed, RIOS is installed by unzipping the file, navigating to it in terminal (or windows command line) and typing:


python setup.py install

Depending on where python is installed you may need to add sudo before. If you don’t have permission to install there you can specify another directory using:


python setup.py install --prefix=~/python/rios

Then add this to your PYTHONPATH (There will be a post on installing software off the main path and managing paths soon).

You can test RIOS was installed OK by opening a python console and typing:


import rios

Examples of using RIOS for calculating NDVI and performing a rule-based classification are provided in our Python Tutorial for Spatial Data Processing (available to download from https://bitbucket.org/petebunting/python-tutorial-for-spatial-data-processing).