TransWikia.com

Calculating intersect for two featurecollections

Geographic Information Systems Asked by oamram on July 19, 2021

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

One Answer

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

Answered by Rodrigo E. Principe on July 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP