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
var fc = ee.FeatureCollection("users/kevinshook69/StDenis");