Shaun Levick https://www.geospatialecology.com

Environmental Monitoring and Modelling

Lab 9 - Working with vector datasets

Objective

The objective of this lab is to familiarise yourself with Features and FeatureCollections in Earth Engine. Vector/table datasets are useful for stratifying study sites, and for defining regions of interest (roi) for reducing and clipping images and image collections.

Acknowledgements

  • Google Earth Engine Developers
  • Google Earth Engine Guides

Visualising Features and FeatureCollections

A Feature in Earth Engine is defined as a GeoJSON Feature. Specifically, a Feature is an object with a geometry property storing a Geometry object (or null) and a properties property storing a dictionary of other properties

GeoJSON is a format for encoding a variety of geographic data structures.GeoJSON supports the following geometry types: Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon. Geometric objects with additional properties are Feature objects. Sets of features are contained by FeatureCollection objects.

Mapping FeatureCollections

  • As with images, geometries and features, feature collections can be added to the map directly with Map.addLayer(). The default visualization will display the vectors with solid black lines and semi-opaque black fill. To render the vectors in color, specify the color parameter. The following displays the ‘RESOLVE’ ecoregions (Dinerstein et al. 2017) as the default visualization and in red:
// Load a FeatureCollection from a table dataset: 'RESOLVE' ecoregions.
var ecoregions = ee.FeatureCollection('RESOLVE/ECOREGIONS/2017');

// Display as default and with a custom color.
Map.addLayer(ecoregions, {}, 'default display');
Map.addLayer(ecoregions, {color: 'FF0000'}, 'colored');
  • For additional display options, use featureCollection.draw(). Specifically, parameters pointRadius and strokeWidth control the size of points and lines, respectively, in the rendered FeatureCollection:
Map.addLayer(ecoregions.draw({color: '006600', strokeWidth: 5}), {}, 'drawn');
  • The output of draw() is an image with red, green and blue bands set according to the specified color parameter.

  • For more control over how a FeatureCollection is displayed, use image.paint() with the FeatureCollection as an argument. Unlike draw(), which outputs a three-band, 8-bit display image, image.paint() outputs an image with the specified numeric value ‘painted’ into it. Alternatively, you can supply the name of a property in the FeatureCollection which contains the numbers to paint. The width parameter behaves the same way: it can be a constant or the name of a property with a number for the line width. For example:

// Create an empty image into which to paint the features, cast to byte.
var empty = ee.Image().byte();

// Paint all the polygon edges with the same number and width, display.
var outline = empty.paint({
  featureCollection: ecoregions,
  color: 1,
  width: 3
});
Map.addLayer(outline, {palette: 'FF0000'}, 'edges');
  • Note that the empty image into which you paint the features needs to be cast prior to painting. This is because a constant image behaves as a constant: it is clamped to the initialization value. To color the feature edges with values set from a property of the features, set the color parameter to the name of the property with numeric values:
// Paint the edges with different colors, display.
var outlines = empty.paint({
  featureCollection: ecoregions,
  color: 'BIOME_NUM',
  width: 4
});
var palette = ['FF0000', '00FF00', '0000FF'];
Map.addLayer(outlines, {palette: palette, max: 14}, 'different color edges');
  • Both the color and width with which the boundaries are drawn can be set with properties. For example:
// Paint the edges with different colors and widths.
var outlines = empty.paint({
  featureCollection: ecoregions,
  color: 'BIOME_NUM',
  width: 'NNH'
});
Map.addLayer(outlines, {palette: palette, max: 14}, 'different color, width edges');
  • If the width parameter is not provided, the interior of the features is painted:
// Paint the interior of the polygons with different colors.
var fills = empty.paint({
  featureCollection: ecoregions,
  color: 'BIOME_NUM',
});
Map.addLayer(fills, {palette: palette, max: 14}, 'colored fills');
  • To render both the interior and edges of the features, paint the empty image twice:
// Paint both the fill and the edges.
var empty = ee.Image().byte();
var palette = ['FF0000', '00FF00', '0000FF'];
var filledOutlines = empty.paint(ecoregions, 'BIOME_NUM').paint(ecoregions, 0, 2);
Map.addLayer(filledOutlines, {palette: ['000000'].concat(palette), max: 14}, 'edges and fills');

Filtering a FeatureCollection

Filtering a FeatureCollection is analogous to filtering an ImageCollection. (See the Filtering an ImageCollection section). There are the featureCollection.filterDate(), and featureCollection.filterBounds() convenience methods and the featureCollection.filter() method for use with any applicable ee.Filter. For example:

// Load watersheds from a data table.
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')
  // Convert 'areasqkm' property from string to number.
  .map(function(feature){
    var num = ee.Number.parse(feature.get('areasqkm'));
    return feature.set('areasqkm', num);
  });

// Define a region roughly covering the continental US.
var continentalUS = ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29);

// Filter the table geographically: only watersheds in the continental US.
var filtered = sheds.filterBounds(continentalUS);

// Check the number of watersheds after filtering for location.
print('Count after filter:', filtered.size());

// Filter to get only larger continental US watersheds.
var largeSheds = filtered.filter(ee.Filter.gt('areasqkm', 25000));

// Check the number of watersheds after filtering for size and location.
print('Count after filtering by size:', largeSheds.size());

Exercise

Repeat the steps above, using the hydrosheds database for Australia. Clicking this link will import the hydrosheds table into Earth Engine


GEARS - Geospatial Ecology and Remote Sensing - https://www.geospatialecology.com

(c) Shaun R Levick