Manual Classification for Fractional Cover of Water Project 8d89ef6f4f7b42929c74bf68c4410d55

  • Compatability: Notebook currently compatible with the DEA Sandbox environment

  • Products used: This notebook uses no DEA products. It uses either example drone data or uploaded drone data, in WGS84 and GeoTIFF format

Background 49016dbbb01644ae971509158e39a8dc

The Fractional Cover of Water Project is a Geoscience Australia - Australian Rivers Institute project. The project requires the collection of drone field data to validate the development of a new algorithm that will classify a Landsat or Sentinel 2 image pixel into sub-pixel fractions of water and wet vegetation.

This notebook allows a user to manually classify collected drone imagery as input to the algorithm development and validation process. Load the data in, run all the cells, and a bokeh interactive plot allows the user to zoom in, click 1m x 1m cells, select the land cover type, and save the results to file. Open water cells can have a Forel-Ule water color recorded for the cell, where this was measured at the site using the EyeOnWater application.

The image at right shows the variations in wet vegetation fractional cover type that will be collected by drone. Floating, Emergent, OpenWater, GreenVeg(PV), DryVeg(NPV), BareSoil(BS), and Forel-Ule water colour (if the type is OpenWater) Water height is additionally recorded though may not be retrievable from the algorithm due to the inherent limitations of top-down satellite imagery.

Additional documentation is provided below.


The notebook uses drone imagery to classify the land cover type, including Floating, Emergent, OpenWater, GreenVeg(PV), DryVeg(NPV), BareSoil(BS), Forel-Ule water colour (if the type is OpenWater)

  • first bring in the drone data in resolution (1, -1) (unit meter) as a GeoTiff raster image. This can be copied into the DEA Sandbox by dragging and dropping the file.

  • classify the pixels into categories listed above

  • save the results as raster into geotiff

To use the notebook, please refer the instruction video and doc linked below - Written Workflow Instructions - Video Instructions ***

Get started

1. Upload drone image

Set the variable drone_tif_path accordingly e.g. drone_tif_path = "./DataAndFigures/drone_wetland_sml4.tif"

# change the file name if necessary
drone_tif_path = "./Data_and_figures/Drone_wetland_sml.tif"

2. Name your output file

The default file name is results_tif_path = ./test.tif

# This sends the output file to the current directory and a file named `test.tif`.
# You may want to rename this file
results_tif_path = './test.tif'

3. Run the rest of the notebook

Now you should be all set up, you can just run the cells below by pressing Shift + Enter until the drone image shows up near the end. Alternatively, you can select Run -->> Run All Cells from the drop-down menu in JupyterLab.

import sys
import os
from os import path
import datacube
import rasterio.features
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import shape
import xarray as xr
import re
from datetime import datetime
import urllib

from datacube.utils.cog import write_cog
from datacube.utils.geometry import assign_crs
from datacube.utils.geometry import GeoBox
from odc.algo import xr_reproject


from rasterio import warp
from rasterio.features import geometry_mask
from rasterio.transform import from_bounds
from shapely.geometry import Polygon

from import curdoc, output_notebook, show
from bokeh.layouts import layout, column, row
from bokeh.models import (CheckboxGroup, Select, ColumnDataSource, HoverTool, YearsTicker, Legend,
                          CustomJS, LegendItem, field, Range1d, Circle, Button, RadioGroup, TextInput, WheelZoomTool,
                          ResetTool, BoxZoomTool, SaveTool, LinearColorMapper, CategoricalColorMapper,
                          Label, PreText, FileInput, Toggle, MultiPolygons)
from bokeh.models.formatters import DatetimeTickFormatter
from import SelectionGeometry
from bokeh.models.glyphs import Text
from bokeh.palettes import Blues256
from bokeh.colors import RGB, named
from bokeh.plotting import figure
# only sandbox requires next two lines
from dea_dask import create_local_dask_cluster



  • Workers: 1
  • Cores: 8
  • Memory: 30.56 GB
# required by bokeh
Loading BokehJS ...
# drone imagery resample resolution
# note to user: don't change it unless required
# note to editor: change it whatever you want
drone_res_tgt = (1, 1)
# This is the Forel-Ule color scale
furgb = np.array([
        [21,161,44,4]], dtype='uint8')
def load_drone_tif(fname, res_tgt):
        load drone imagery with given file name and resolution
            fname: file name of drone imagery
            res_tgt: resample resolution of output, type: tuple, e.g. res_tgt=(1, 1)
            xarray.DataSet of drone imagery

    drone = xr.open_rasterio(fname, parse_coordinates=True, chunks={'band': 1, 'x': 1024, 'y': 1024})
    drone = assign_crs(drone)
    affine, width, height = warp.calculate_default_transform(, 'EPSG:3577', drone.shape[1], drone.shape[2],
    tgt_affine, tgt_width, tgt_height = warp.aligned_target(affine, width, height, res_tgt)
    drone_geobox = GeoBox(tgt_width, tgt_height, tgt_affine, 'EPSG:3577')
    drone_tgt = xr_reproject(drone, drone_geobox, resampling= 'bilinear' )
    return drone_tgt.load()

def load_results_tif(fname):
        load geotiff of classification results
            fname: file name of classification results
            xarray.DataSet of geotiff
    results = xr.open_rasterio(fname, parse_coordinates=True, chunks={'band': 1, 'x': 1024, 'y': 1024})
    return results.load()
# note to user: just run it
# note to editor: load drone imagery, convert to rgba image, set up coordinates and affine
drone_tgt = load_drone_tif(drone_tif_path, drone_res_tgt)
rgba_image = np.empty((drone_tgt.shape[1], drone_tgt.shape[2]), dtype='uint32')
view = rgba_image.view(dtype='uint8').reshape(drone_tgt.shape[1], drone_tgt.shape[2], drone_tgt.shape[0])
for i in range(3):
    view[:, :, i] = ([i]).astype('uint8')
view[:, :, 3] = 255
scale_factor = np.array([,
                if > 0 else])
xr_results = xr.DataArray(data=[np.zeros(rgba_image.shape, dtype='uint8')] * 2, dims=['band', 'y', 'x'],
                     coords={'band': [1, 2],
                     'y': drone_tgt.coords['y'], 'x':drone_tgt.coords['x']}, attrs={'nodata':0})
select_poly_affine = from_bounds([0],
                                 drone_tgt.shape[2], drone_tgt.shape[1])
/g/data/u46/users/ea6141/opt_up2date/lib/python3.7/site-packages/pyproj/crs/ FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes:
  projstring = _prepare_from_string(projparams)
# note to user: run it
# note to editor: monstrous function required to redirect the bokeh interactive plot to a random jupyterlab port
# or the jupyter notebook port via proxy
def plot_doc(doc):
    drone_source = ColumnDataSource(data={'img': [np.flip(rgba_image, axis=0)]})
    results_source = ColumnDataSource(data={'category': [np.zeros(rgba_image.shape, dtype='uint8')],
                                           'forel_ule': [np.zeros(rgba_image.shape, dtype='uint8')]})

    drone_file_input = TextInput(value=drone_tif_path, title="Load drone imagery", height=50, width=600,
    def open_drone_tif(attrname, old, new):
        fname = drone_file_input.value
        if not path.exists(fname):
            print("file doesn't exist!")
        drone_tgt = load_drone_tif(fname, drone_res_tgt)
        rgba_image = np.empty((drone_tgt.shape[1], drone_tgt.shape[2]), dtype='uint32')
        view = rgba_image.view(dtype='uint8').reshape(drone_tgt.shape[1], drone_tgt.shape[2], drone_tgt.shape[0])
        for i in range(3):
            view[:, :, i] = ([i]).astype('uint8')
        view[:, :, 3] = 255
        scale_factor = np.array([,
                if > 0 else])['img'] = [np.flip(rgba_image, axis=0)]
        global xr_results
        xr_results = xr.DataArray(data=[np.zeros(rgba_image.shape, dtype='uint8')] * 2, dims=['band', 'y', 'x'],
                     coords={'band': [1, 2],
                     'y': drone_tgt.coords['y'], 'x':drone_tgt.coords['x']}, attrs={'nodata':0})['category'] = [np.flip(xr_results[0].data, axis=0)]['forel_ule'] = [np.flip(xr_results[1].data, axis=0)]
        global select_poly_affine
        select_poly_affine = from_bounds([0],
                                 drone_tgt.shape[2], drone_tgt.shape[1])

    drone_file_input.on_change('value', open_drone_tif)

    result_file_input = TextInput(value=results_tif_path, title="Results tiff file", height=50, width=600,
    result_save_button = Button(label="Save results", button_type="success")
    result_load_button = Button(label="Load results", button_type="success")
    def open_result_tif(event):
        fname = result_file_input.value
        if not path.exists(fname):
            print("file doesn't exist!")
        global xr_results
        xr_results = load_results_tif(fname)['category'] = [np.flip(xr_results[0].data, axis=0)]['forel_ule'] = [np.flip(xr_results[1].data, axis=0)]

    def save_result_tif(event):
        fname = result_file_input.value
        if path.exists(fname):
            time_now = str(':', '').replace(' ', '').replace('-', '')
            os.rename(fname, fname + '.bak' + time_now)
        write_cog(xr_results, fname)


    drone_fig = figure(tooltips=[('x-cood', "$x{0.0}"), ('y-coord', "$y{0.0}")], title="image %s" %("drone"),
            x_axis_type='auto', y_axis_type='auto', x_minor_ticks=10, y_minor_ticks=10,
            x_axis_label="x origin %s" % scale_factor[0],
            y_axis_label="y origin %s" % scale_factor[1],
            tools="box_zoom, wheel_zoom, pan, tap, poly_select, reset")
    drone_fig.toolbar.active_scroll = drone_fig.select_one(WheelZoomTool)
    drone_tag = ['drone', 1]
    drone_fig.image_rgba(image='img', source=drone_source,
               dw=drone_tgt.shape[2], dh=drone_tgt.shape[1],
    transparent_white = RGB(255, 255, 255, 0)
    cats_color = [named.violet.to_rgb(), named.indigo.to_rgb(), named.tomato.to_rgb(),
                  named.deepskyblue.to_rgb(), named.yellow.to_rgb(), named.beige.to_rgb(),
    cats_color_mapper = LinearColorMapper(cats_color, low=1, high=len(cats_color), low_color=transparent_white)
    water_color = [RGB(f[1], f[2], f[3], 255) for f in furgb]
    water_color_mapper = LinearColorMapper(water_color, low=1, high=21, low_color=transparent_white)
    water_tag = ['forel_ule', 21]
    cats_tag = ['cats', 10]
    water_image = drone_fig.image(image='forel_ule', source=results_source,[0],
                dw=drone_tgt.shape[2], dh=drone_tgt.shape[1],
                level="image", tags=water_tag)

    cats_image = drone_fig.image(image='category', source=results_source,[0],
                dw=drone_tgt.shape[2], dh=drone_tgt.shape[1],
                level="image", tags=cats_tag)

    coords_label = PreText(text="null", width=100, sizing_mode='fixed')
    select_poly_source = ColumnDataSource(data=dict(x=[[[[]]]], y=[[[[]]]]))
    js_code = """
        if ( == false)
        const geometry = cb_obj.geometry;
        const data = {'x': [[[[]]]], 'y': [[[[]]]]};
        if (geometry.type == "point")
            var ind_x = Math.floor(geometry.x);
            var ind_y = Math.floor(geometry.y);
            console.log("x:", ind_x);
            console.log("y:", ind_y);
            label.text = "x=" + ind_x.toString() +";" + "y=" + ind_y.toString();
        else if (geometry.type == "poly")
            var array_len = geometry.x.length;
            for (var i=0; i<array_len; i++)
            label.text = "null";
        } = data;
    js_callback = CustomJS(args={'label': coords_label, 'poly': select_poly_source},
    drone_fig.js_on_event(SelectionGeometry, js_callback)
    select_poly = MultiPolygons(xs="x", ys="y", line_width=2)
    drone_fig.add_glyph(select_poly_source, select_poly)

    def get_ind_from_coords():
        ind_list = []
        poly_list = []
        if coords_label.text != "null":
            coords = coords_label.text.split(';')
            ind_list += [[abs(int(coords[1].split('=')[1])), abs(int(coords[0].split('=')[1]))]]
            return ind_list
        elif len(['x'][0][0][0]) > 0:
            for x, y in zip(['x'][0][0][0],['y'][0][0][0]):
                poly_list += [[x, y]]
            poly_shape = Polygon(poly_list)
            poly_mask = geometry_mask([poly_shape], out_shape=xr_results[0].data.shape,
                                      transform=select_poly_affine, invert=True)
            return np.flip(poly_mask, axis=0)
            return None

    cats = ["NA", "Overstory","Emergent","Floating","OpenWater","GreenVeg","DryVeg","Bare"]
    radio_group = RadioGroup(labels=cats, active=0, height=800, height_policy="fixed", aspect_ratio=0.1)
    forel_ule_scale = TextInput(value="0", title="Forel-Ule Water Colour", width=100, sizing_mode='fixed')
    def choose_cat(attrname, old, new):
        ind_list = get_ind_from_coords()
        if ind_list is None:
        if attrname == 'active':
            if isinstance(ind_list, list):
                for ind_y, ind_x in ind_list:
                    xr_results[0][ind_y, ind_x] =
                xr_results[0].data[ind_list] =
  ['category'] = [np.flip(xr_results[0].data, axis=0)]
        if attrname == 'value':
            check_numbers = re.match(r'^[0-9]+$', forel_ule_scale.value)
            if check_numbers is None:
                print("only input numbers", forel_ule_scale.value)
                forel_ule_scale.value = "0"
            elif int(forel_ule_scale.value) < 0 or int(forel_ule_scale.value) > 21:
                print("invalid value, please check!")
                forel_ule_scale.value = "0"
            elif != 4 and int(forel_ule_scale.value) > 0:
                forel_ule_scale.value = "0"
                print("cannot set value for non-water")
            if isinstance(ind_list, list):
                for ind_y, ind_x in ind_list:
                    xr_results[1][ind_y, ind_x] = int(forel_ule_scale.value)
                xr_results[1].data[ind_list] = int(forel_ule_scale.value)
  ['forel_ule'] = [np.flip(xr_results[1].data, axis=0)]

    radio_group.on_change('active', choose_cat)
    forel_ule_scale.on_change('value', choose_cat)

    def coords_change(attrname, old, new):
        ind_list = get_ind_from_coords()
        if ind_list is None:
        if isinstance(ind_list, list):
   = xr_results[0].data[ind_list[0][0], ind_list[0][1]]
            forel_ule_scale.value = str(xr_results[1].data[ind_list[0][0], ind_list[0][1]])
   = xr_results[0].data[ind_list][0]
            forel_ule_scale.value = str(xr_results[1].data[ind_list][0])

    coords_label.on_change('text', coords_change)
    select_poly_source.on_change('data', coords_change)

    overlay_toggle_category = Toggle(label="Overlay category", button_type="success",
                                     height=50, width=150, sizing_mode='fixed', active=True)
    overlay_toggle_water_color = Toggle(label="Overlay water color", button_type="success",
                                        height=50, width=150, sizing_mode='fixed', active=True)

    def overlay_results(event):
        if (
            water_image.visible = True
            water_image.visible = False
        if (
            cats_image.visible = True
            cats_image.visible = False


    control_group = column(coords_label, forel_ule_scale, radio_group)
    result_group = row(result_file_input, column(result_load_button, result_save_button))
    layouts = layout([drone_file_input, result_group, [overlay_toggle_category, overlay_toggle_water_color],
                      [control_group, drone_fig]], sizing_mode='scale_height')
def remote_jupyter_proxy_url(port):
    Callable to configure Bokeh's show method when a proxy must be

    If port is None we're asking about the URL
    for the origin header.
    base_url = ""
    host = urllib.parse.urlparse(base_url).netloc
    # If port is None we're asking for the URL origin
    # so return the public hostname.
    if port is None:
        return host

    service_url_path = os.environ['JUPYTERHUB_SERVICE_PREFIX']
    proxy_url_path = 'proxy/%d' % port

    user_url = urllib.parse.urljoin(base_url, service_url_path)
    full_url = urllib.parse.urljoin(user_url, proxy_url_path)
    return full_url

4. If everything above worked, you should see your drone imagery below the next cell as well as an output file path and some green buttons.

  • When selecting polygons of multiple pixels, double-click to complete the polygon selection

  • Completing a new polygon will hide the old polygon

  • Hit the green Save results button frequently to make sure your results are saved during your session

  • You can switch between water color and category being displayed over your imagery by clicking Overlay category or Overlay water color buttons

  • You probably want to have your imagery open in a GIS on another screen to compare with this imagery while classifying, otherwise you’ll be scrolling in and out a lot

[ ]:
# if you know your url
# notebook_url = "http://localhost:8888"
show(plot_doc, notebook_url=remote_jupyter_proxy_url)

Additional information

License: The code in this notebook is licensed under the Apache License, Version 2.0. Digital Earth Australia data is licensed under the Creative Commons by Attribution 4.0 license.

Contact: If you need assistance, please post a question on the Open Data Cube Slack channel or on the GIS Stack Exchange using the open-data-cube tag (you can view previously asked questions here). If you would like to report an issue with this notebook, you can file one on Github.

Last modified: October 2020

Compatible datacube version:



Browse all available tags on the DEA User Guide’s Tags Index

Tags: sandbox compatible, bokeh, COG, classification, DEA Sandbox, drones, Forel-Ule, GeoTIFF, inland water,:index:water,