ee.FeatureCollection.flatten
Flattens collections of collections.
Usage | Returns |
---|
FeatureCollection.flatten() | FeatureCollection |
Argument | Type | Details |
---|
this: collection | FeatureCollection | The input collection of collections. |
Examples
// Counties in New Mexico, USA.
var counties = ee.FeatureCollection('TIGER/2018/Counties')
.filter('STATEFP == "35"');
// Monthly climate and climatic water balance surfaces for January 2020.
var climate = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
.filterDate('2020-01', '2020-02');
// Calculate mean climate variables for each county per climate surface
// time step. The result is a FeatureCollection of FeatureCollections.
var countiesClimate = climate.map(function(image) {
return image.reduceRegions({
collection: counties,
reducer: ee.Reducer.mean(),
scale: 5000,
crs: 'EPSG:4326'
});
});
// Note that a printed FeatureCollection of FeatureCollections is not
// recursively expanded, you cannot view metadata of the features within the
// nested collections until you isolate a single collection or flatten the
// collections.
print('FeatureCollection of FeatureCollections', countiesClimate);
print('Flattened FeatureCollection of FeatureCollections',
countiesClimate.flatten());
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
# Counties in New Mexico, USA.
counties = ee.FeatureCollection('TIGER/2018/Counties').filter('STATEFP == "35"')
# Monthly climate and climatic water balance surfaces for January 2020.
climate = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE').filterDate(
'2020-01', '2020-02')
# Calculate mean climate variables for each county per climate surface
# time step. The result is a FeatureCollection of FeatureCollections.
def reduce_mean(image):
return image.reduceRegions(**{
'collection': counties,
'reducer': ee.Reducer.mean(),
'scale': 5000,
'crs': 'EPSG:4326'
})
counties_climate = climate.map(reduce_mean)
# Note that a printed FeatureCollection of FeatureCollections is not
# recursively expanded, you cannot view metadata of the features within the
# nested collections until you isolate a single collection or flatten the
# collections.
print('FeatureCollection of FeatureCollections:', counties_climate.getInfo())
print('Flattened FeatureCollection of FeatureCollections:',
counties_climate.flatten().getInfo())
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[[["`flatten()` transforms a FeatureCollection of FeatureCollections into a single FeatureCollection."],["It's used to simplify nested collections for easier analysis and data access."],["This function is helpful when dealing with results from operations like `reduceRegions()` applied across image collections."],["The output of `flatten()` is a FeatureCollection with all the features from the nested collections combined."]]],["The `flatten()` method transforms a nested `FeatureCollection` of `FeatureCollections` into a single, flat `FeatureCollection`. It takes a `FeatureCollection` as input and returns a flattened `FeatureCollection`. This allows for the metadata of features within the nested collections to be viewed, which is not possible with unflattened collections. An example demonstrates calculating mean climate variables for counties per climate surface timestep and then flattening the resulting nested collection.\n"]]