TransWikia.com

Calculate intersection for two FeatureCollections containing points in GEE

Geographic Information Systems Asked on March 28, 2021

I have a FeatureCollection of points that I want to sample data from. However, I only want data for the points where information is available from both the winter and summer image. The solution I’ve come up with is to sample all data from both and then intersect those point classes. I’ve tried implementing the intersection code from here, which was written for intersections of polygons, but I’m getting two incorrect results that I don’t understand.

First, depending on the order that I nest the functions in, the intersection FeatureCollection will have a different number of elements. I don’t understand why that is, since it seems like it shouldn’t matter what order the mapping happens in. In my specific code, winterOutside produces 7 features and summerOutside produces 5 features (5 was the correct number).

Secondly, on the map, both FeatureCollections look identical, but incorrect – there are only four points on the map, and they’re both missing a fifth point that should be included.

var winter = landsat8.filterDate('2017-12-01', '2017-12-10').mosaic()
var summer = landsat8.filterDate('2017-06-01', '2017-06-10').mosaic()
var visParams = {bands: ['B4', 'B3', 'B2'], max:20000};

Map.addLayer(winter, visParams, 'winter')
Map.addLayer(summer, visParams, 'summer')

// makes FeatureCollection with band values of winter values for invariant points
var winterSamples = winter.sampleRegions({
  collection: points,
  scale: 6.5,
  geometries: true
});
// makes FeatureCollection with band values of summer values for invariant points
var summerSamples = summer.sampleRegions({
  collection: points,
  scale: 6.5,
  geometries: true
});

// with winter on the outside:
var winterOutside = winterSamples.map(function(feat1){
  feat1 = ee.Feature(feat1);
  var mapped1 = summerSamples.map(function(feat2){
    feat2 = ee.Feature(feat2);
    var intersection = feat1.intersection(feat2, ee.ErrorMargin(1));
    return ee.Feature(intersection);
  });
  return mapped1;
}).flatten().distinct(['B1', 'B2', 'B3', 'B4', 'B5'])

print(winterOutside);
Map.addLayer(winterOutside, {color: 'blue'}, 'winterOutside')

// with summer on the outside:
var summerOutside = summerSamples.map(function(feat1){
  feat1 = ee.Feature(feat1);
  var mapped1 = winterSamples.map(function(feat2){
    feat2 = ee.Feature(feat2);
    var intersection = feat1.intersection(feat2, ee.ErrorMargin(1));
    return ee.Feature(intersection);
  });
  return mapped1;
}).flatten().distinct(['B1', 'B2', 'B3', 'B4', 'B5'])

print(summerOutside);
Map.addLayer(summerOutside, {color: 'red'}, 'summerOutside')

Here’s the link to my code if you want to grab my imports. My question is really just "How do you intersect two FeatureCollections that only have points in them?" but also "Why isn’t the above working?"

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