4

I am trying to measure the individual areas of many patches of water in a single image, rather than their total area. The connectedPixelCount function won't work for me because of its upper limit of 1024 pixels for the regions. However, using the reduceToVectors function seems to works to identify contiguous regions of water. The issue is that I am unable to calculate the areas of the resulting polygons.

This part of the code appears to work OK, setting the basin boundary, getting the image to be used, finding the water regions and plotting them.

// get basin boundary
var fc = 
ee.FeatureCollection('ft:v:1L6-BUK8GNDUU-335Ju8D9Gf9mXiOuPKigGSFcf0w');
var startDate = ee.Date.fromYMD(2015, 5, 1);
var endDate = ee.Date.fromYMD(2015, 5, 30);

// set region to be viewed
Map.setCenter(-106.086940, 52.207365, 13);   // St Denis 

// get imagecollection, filter by start and end date
var monthly=ee.ImageCollection('JRC/GSW1_0/MonthlyHistory')
  .filterBounds(fc)
  .filterDate(startDate, endDate);

// plot basin boundary
Map.addLayer({
   eeObject: fc,
   name: 'St Denis Basin',
});

// get single image, clip to basin boundary
var current = ee.Image(monthly.first())
 .clipToCollection(fc);

// get water, defined as value 2
var water = current.eq(2);
water = water.updateMask(water.neq(0));

// get water regions as vectors
var vectors = water.addBands(current).reduceToVectors({
  geometry: fc,
  scale: 20,
  crs: current.projection(),
  labelProperty: 'water',
  geometryType: 'polygon',
  eightConnected: true,
  reducer: ee.Reducer.mean(),
});

// plot water vectors
var empty = ee.Image().byte();
var fills = empty.paint({
   featureCollection: vectors,
});
Map.addLayer(fills, {palette: '0000FF'}, 'colored fills');

This section does NOT work:

var areas = function(feature) {
// Compute area from the geometry.
  var area = feature.geometry().area();
  return feature.set('area', area);
};

// Map the difference function over the collection.
var allAreas = ee.Number(vectors.map(areas))

// Print the result.
print(allAreas);

When I run the code, I get the error:

Number (Error)
Error in map(ID=+411164+143465):
Geometry.area: Unable to perform this geometry operation. Please specify a non-zero error margin.

The ID refers to the first polygon

: Feature +411164+143465 (Polygon, 2 properties)
type: Feature
id: +411164+143465
geometry: Polygon, 9 vertices
type: Polygon
coordinates: List (1 element)
0: List (9 elements)   
  0: [-106.13101008465881,52.22454718910092]
  1: [-106.12885412797692,52.22454718910092]
  2: [-106.12885412797692,52.22490651521456]
  3: [-106.1293931171474,52.22490651521456]
  4: [-106.1293931171474,52.22508617827138]
  5: [-106.13029143243152,52.22508617827138]
  6: [-106.13029143243152,52.22490651521456]
  7: [-106.13101008465881,52.22490651521456]
  8: [-106.13101008465881,52.22454718910092]
  geodesic: false
  properties: Object (2 properties)
    mean: 2
    water: 1
2
  • You will need to share your Fusion Table if you want others to reproduce your error. Commented Jul 11, 2017 at 20:25
  • Thanks very much - as you can see I'm a newbie. I've shared it as var fc = ee.FeatureCollection("users/kevinshook69/StDenis"); Commented Jul 11, 2017 at 21:59

1 Answer 1

8

In the documentation for Geometry.area(maxError, proj) says:

maxError (ErrorMargin, default: null): The maximum amount of error tolerated when performing any necessary reprojection.

so, I am not sure in which unit counts the amount of error, but this will work:

var areas = function(feature) {
// Compute area from the geometry.
  var area = feature.geometry().area(10);
  return feature.set('area', area);
};

// Map the difference function over the collection.
var allAreas = ee.Number(vectors.map(areas))

// Print the result.
print(allAreas);
2
  • Wow - thanks very much! It worked beautifully. I had not realized the necessity of setting the maxError Commented Jul 12, 2017 at 16:24
  • You are welcome. You can accept the answer as correct if you want. Commented Jul 12, 2017 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.