I have 2 ee.FeatureCollections
in GEE. The first is a square grid polygon layer. The second is a polygon layer made up of multiple irregular rectangles that overlap with one another. I want to count the number of times each grid square intersects with the rectangles and export this result as a Shapefile. I am used to performing spatial joins in QGIS, ArcGIS and R but would like to do it in GEE. However, I am facing some problems related to understanding the function of several code segments.
Here is a graphic demonstrating what I intend to achieve.
I was trying to work this out by following closely with the GEE tutorial on Spatial Joins but I am running into some errors which I am unsure how to solve. Primarily, I am unsure what to define in the code lines 31 (on 'matchesKey
') and 44 (on 'grid.get
') as I haven't yet understood their functions. After several failed attempts, I am wondering if I should be using another approach instead, for e.g. via ee.Geometry.Polygon.intersection().
Here is the link to my code (with the assets shared publicly) and the code appended in this thread too.
/*
Purpose of script:
For each grid square, count the number of times it intersects with another Feature Collection,
and return the count value as a new property
*/
// Load square grid layer
var grid = ee.FeatureCollection('users/jjohanness1992/SpatialJoin/grid');
print('1st grid square',grid.first());
// Load polygon layer
var rectangles = ee.FeatureCollection('users/jjohanness1992/SpatialJoin/rectangles');
print('1st rectangle',rectangles.first());
// Inspect in map
Map.addLayer(grid,{},'grid');
Map.addLayer(rectangles,{},'rectangles');
Map.centerObject(rectangles,15);
// Define spatial filter that controls the intersection of geometries
var spatialFilter = ee.Filter.intersects({
leftField: '.geo',
rightField: '.geo',
maxError: 0.01
});
// Define a save all join
var saveAllJoin = ee.Join.saveAll({
matchesKey: 'id', // I'm not sure what I should put here in matchesKey: 'XXX'
});
// **********
/* Here I get the following error message:
Error: Invalid property type: Property id has type List<Object>. (Error code: 3)
*/
// **********
// Apply the join
var intersectJoined = saveAllJoin.apply(grid, rectangles, spatialFilter);
// Count number of firepix in a grid and set as a property
intersectJoined = intersectJoined.map(function(grid) {
var nRectangles = ee.List(grid.get('id')).size(); // I'm not sure what to put in grid.get('XXX')
return grid.set('nCount',nRectangles);
});
// Export
Export.table.toDrive({
collection: intersectJoined,
description:'intersectJoined',
folder: '00_pilot',
fileFormat: 'SHP'
});