Google Earth Engine tip 1 (clipping)

Published on May 26, 2021 | Bikesh Bade | 11194 Views

Clipping

 

Google Earth Engine supports vector operations in addition to raster manipulation. The basic use of vector data in Google Earth Engine is for clipping rasters. The user can either clip to a raster created using heads up digitizing in the GEE map interface, upload their own raster data, or clip to raster data provided by Google.

 

Before clipping, you need to understand the dataset and its nature. Basically, for clipping, you need to understand Image and Image collections. There are different functions in the google earth engine to clip image and image collections. 

 


GEE function for clipping Image are:

1. ee.Image.clip

2. ee.Image.clipToBoundsAndScale

3. ee.Image.clipToCollection

 

 

Clip an image to a Geometry or Feature

To clip an image to specific geometry, feature, or shapefile, you need to use ee.Image.clip

Example:

 

// Load an image.
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318');

// Define the visualization parameters.
var vizParams = {
  bands: ['B4', 'B3', 'B2'],
  min: 0,
  max: 0.5,
  gamma: [0.95, 1.1, 1]
};

// Center the map and display the image.
Map.setCenter(-122.1899, 37.5010, 10); // San Francisco Bay
Map.addLayer(image, vizParams, 'Original Image in True Color');

// Clipping image with geomerty
var clippedImage = image.clip(geometry) //ClipGeometry can any geometry or feature or shapfile
Map.addLayer(clippedImage, vizParams, 'Clipped Image');

 

 

Clips an image to the bounds of a Geometry, and scales

To clips an image to the bounds of a Geometry, and scales the clipped image to a particular size or scale, we use ee.Image.clipToBoundsAndScale

 

//height and width to scale image to 
var width = 512;//The width to scale the image to, in pixels. Must be provided along with "height".
var height = 512; //The height to scale the image to, in pixels. Must be provided along with "width"

//Clip the image with geometry/feature/sahpefile also with specific height and width
// along with widthheight there is two more parameter you can use that is  maxDimensionscale
var clippedToscale = image.clipToBoundsAndScale(geometry,width,height);
Map.addLayer(clippedToscale, vizParams, 'Clipped to scale Image');


 

Clip to Feature collections

Clips an image to a FeatureCollection is done with function ee.Image.clipToCollection. Sometimes you might need to clip an image with a number of features, in that case, you can use these clipping functions.

 

// Create an ee.Geometry.
var polygon = ee.Geometry.Polygon([
  [[-122.6197,37.4748], [-122.1830,37.4748], [-122.1830,37.8032], [-122.6197,37.8032], [-122.61974,37.47484]]]);

var polygon2 = ee.Geometry.Polygon([
  [[-122.06197,37.04748], [-122.1830,37.4748], [-122.1830,37.8032], [-122.6197,37.8032], [-122.061974,37.047484]]]);

// Create a FeatureCollection
var FeatureCollections = ee.FeatureCollection([polygon,polygon2]);

// clip to the feature collections
var ClippedToFeatureCollection = image.clipToCollection(FeatureCollections);
Map.addLayer(ClippedToFeatureCollection, vizParams, 'Clipped to Feature Collections');

 

Clipping Image Collections

Next, what if you want to clip image collections. There are different programming ideas to clip image collections. One of the most used ideas is to create a function and pass each image a time and clip. another method you can use is to filter the image collection with filterBounds.

 

Using Filterbounds

Filterbounds is not a clipping function, it's filtering the images based on the geometry, feature, feature collection, or shapefile. It most widely uses for filtering image collections. you can use it when defining image collection

 

// cllip imagecollection with geometry / feature / shapefile
var l8s = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA").filterBounds(geometry);
Map.addLayer(l8s, vizParams, 'Clipped Image collections');

 

 

Using functions to clip image collections

Clipping of image collection can be done with a simple function to loop through all the image collection and clip each individual image based on geometry, feature, or shapefile.

 

// cllip imagecollection with geometry / feature / shapefile
var l8s_collection = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA");

//Sample function to clip imagecollections
l8s_collection = l8s_collection.map(function(img){return img.clip(geometry)});

Map.addLayer(l8s_collection, vizParams, 'Clipped Image collections functions');

 

 

Get a link to the code

Responses

Frank Teye Oblim

Beautiful

  • May 27, 2021 |

Leave your comment