1

I am trying to calculate the area of watershed within each polygon for a set of square polygons (1km*1km) in my study area. I converted a water image to a featureCollection of water polygons, it works great. I get an error when I try to intersect the square polygon layer to the water featureCollection. I also would like to export the result to a csv.

//my study area

var studyArea = ee.FeatureCollection('users/oamram/ABM_Kenya/StudyArea');

//square polygons 1km in dimensions

var polygons = ee.FeatureCollection('users/oamram/ABM_Kenya/Polygons1km');


Map.centerObject(studyArea,13);




var start = '2014-01-01';

var finish = '2014-01-31';

//get water dataset

var dataset = ee.ImageCollection('JRC/GSW1_0/MonthlyHistory')
                  .filter(ee.Filter.date(start, finish));

 //select water layer

  var water = dataset.select('water');

//identify water area

   var sumWater = water.reduce(ee.Reducer.sum());

//clip to my study area

  var clipped = sumWater.clip(studyArea);



var waterVis = {
  min: 0.0,
  max: 2.0,
  palette: ['ffffff', 'fffcb8', '0905ff'],
};

//Apply watermask- water values =2 in this layer//;

var maskedComposite = clipped.updateMask(clipped.eq(2));



// Convert the water areas  to vectors.

var vectors = maskedComposite.reduceToVectors({
  geometry: studyArea,
  crs: maskedComposite.projection(),
  scale: 30,
  geometryType: 'polygon',
  eightConnected: false,
  labelProperty: 'water'
});


**//everything works up to here**


// Compute the intersection, for each feature in the feature collection.

var polyIntersect = polygons.map(function(feature){
  var intersection = vectors.intersection(feature, ee.ErrorMargin(1));

  return feature.set({'Intersect': intersection.area().divide(1000 * 1000)});
});

The error is get is this:

vectors.intersection is not a function

  • The error can't be reproduced because the features are not publicly available. – JepsonNomad Apr 2 at 0:29
2

vectors is a FeatureCollection and hasn't got an intersection method. That method is for the class Feature. This is what I'd do:

// cast vectors
vectors = ee.FeatureCollection(vectors)

// make vectors a list
var vectorList = vectors.toList(vectors.size())

var polyIntersect = polygons.iterate(function(feature, list){
  list = ee.List(list)
  feature = ee.Feature(feature)

  var intersection = vectorList.map(function(feat) {
    feat = ee.Feature(feat)
    var intersection = feat.intersection(feature, ee.ErrorMargin(1));
    return ee.Feature(intersection).set({'Intersect': intersection.area().divide(1000 * 1000)})
  })

  return list.add(intersection)
}, ee.List([]));

polyIntersect = ee.FeatureCollection(ee.List(polyIntersect).flatten())
Map.addLayer(polyIntersect)

link: https://code.earthengine.google.com/cef9d625cce4f0078c258e38ceff46f6

  • Isn't it faster to map the features over both feature collection, instead of using itereate? code.earthengine.google.com/deb4434e507cfcac9f45efc7e3e0ce33. Anyhow, both produce the same result. Don't forget to filter out the features which do not have an intersection area. – Kuik Apr 2 at 8:29
  • That's a good option too @Kuik, the code is a bit cleaner. Yes, may be map is a little faster than iterate. I made a test: code.earthengine.google.com/4d342ffe2f00aee69e11d5dbd7e33f1f. Also, I don't have to filter out because I create a new list where intersections go. But you can make an answer with your option so readers have more options, this isn't a competition but a help forum =) – Rodrigo E. Principe Apr 2 at 10:54
  • Thanks so much everybody. all the answers works great. – oamram Apr 2 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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