Para enumerar todas as correspondências entre os elementos de duas coleções, use um
ee.Join.inner()
. A saída de uma união interna é uma FeatureCollection
, mesmo que una uma ImageCollection
a outra ImageCollection
. Cada elemento na saída representa uma correspondência, em que os elementos correspondentes são armazenados em duas propriedades do elemento. Por
exemplo, feature.get('primary')
é o elemento na coleção principal
que corresponde ao elemento da coleção secundária armazenado em
feature.get('secondary')
. Nomes diferentes para essas propriedades podem ser
especificados como argumentos para inner()
, mas ‘primary’
e
‘secondary’
são os padrões. As relações de um para muitos são representadas por vários recursos na saída. Se um elemento em qualquer uma das coleções não tiver uma correspondência, ele não estará presente na saída.
Junte exemplos usando entradas ImageCollection
aplicadas sem modificações às entradas FeatureCollection
. Também é possível unir um
FeatureCollection
a um ImageCollection
e vice-versa. Considere
o seguinte exemplo de mesclagem interna:
Editor de código (JavaScript)
// Create the primary collection. var primaryFeatures = ee.FeatureCollection([ ee.Feature(null, {foo: 0, label: 'a'}), ee.Feature(null, {foo: 1, label: 'b'}), ee.Feature(null, {foo: 1, label: 'c'}), ee.Feature(null, {foo: 2, label: 'd'}), ]); // Create the secondary collection. var secondaryFeatures = ee.FeatureCollection([ ee.Feature(null, {bar: 1, label: 'e'}), ee.Feature(null, {bar: 1, label: 'f'}), ee.Feature(null, {bar: 2, label: 'g'}), ee.Feature(null, {bar: 3, label: 'h'}), ]); // Use an equals filter to specify how the collections match. var toyFilter = ee.Filter.equals({ leftField: 'foo', rightField: 'bar' }); // Define the join. var innerJoin = ee.Join.inner('primary', 'secondary'); // Apply the join. var toyJoin = innerJoin.apply(primaryFeatures, secondaryFeatures, toyFilter); // Print the result. print('Inner join toy example:', toyJoin);
import ee import geemap.core as geemap
Colab (Python)
# Create the primary collection. primary_features = ee.FeatureCollection([ ee.Feature(None, {'foo': 0, 'label': 'a'}), ee.Feature(None, {'foo': 1, 'label': 'b'}), ee.Feature(None, {'foo': 1, 'label': 'c'}), ee.Feature(None, {'foo': 2, 'label': 'd'}), ]) # Create the secondary collection. secondary_features = ee.FeatureCollection([ ee.Feature(None, {'bar': 1, 'label': 'e'}), ee.Feature(None, {'bar': 1, 'label': 'f'}), ee.Feature(None, {'bar': 2, 'label': 'g'}), ee.Feature(None, {'bar': 3, 'label': 'h'}), ]) # Use an equals filter to specify how the collections match. toy_filter = ee.Filter.equals(leftField='foo', rightField='bar') # Define the join. inner_join = ee.Join.inner('primary', 'secondary') # Apply the join. toy_join = inner_join.apply(primary_features, secondary_features, toy_filter) # Print the result. display('Inner join toy example:', toy_join)
No exemplo anterior, observe que a relação entre as tabelas é definida no filtro, o que indica que os campos ‘foo’
e ‘bar’
são os campos de mesclagem. Uma mesclagem interna é especificada e aplicada às coleções. Inspecione
a saída e observe que cada correspondência possível é representada como um
Feature
.
Para um exemplo motivado, considere unir objetos ImageCollection
do MODIS. Os dados de qualidade do MODIS às vezes são armazenados em uma coleção separada dos dados de imagem. Portanto, uma união interna é conveniente para unir as duas coleções e aplicar os dados de qualidade. Nesse caso, os tempos de aquisição de imagem são idênticos, então um filtro de igualdade
processa o trabalho de especificar essa relação entre as duas coleções:
Editor de código (JavaScript)
// Make a date filter to get images in this date range. var dateFilter = ee.Filter.date('2014-01-01', '2014-02-01'); // Load a MODIS collection with EVI data. var mcd43a4 = ee.ImageCollection('MODIS/MCD43A4_006_EVI') .filter(dateFilter); // Load a MODIS collection with quality data. var mcd43a2 = ee.ImageCollection('MODIS/006/MCD43A2') .filter(dateFilter); // Define an inner join. var innerJoin = ee.Join.inner(); // Specify an equals filter for image timestamps. var filterTimeEq = ee.Filter.equals({ leftField: 'system:time_start', rightField: 'system:time_start' }); // Apply the join. var innerJoinedMODIS = innerJoin.apply(mcd43a4, mcd43a2, filterTimeEq); // Display the join result: a FeatureCollection. print('Inner join output:', innerJoinedMODIS);
import ee import geemap.core as geemap
Colab (Python)
# Make a date filter to get images in this date range. date_filter = ee.Filter.date('2014-01-01', '2014-02-01') # Load a MODIS collection with EVI data. mcd43a4 = ee.ImageCollection('MODIS/MCD43A4_006_EVI').filter(date_filter) # Load a MODIS collection with quality data. mcd43a2 = ee.ImageCollection('MODIS/006/MCD43A2').filter(date_filter) # Define an inner join. inner_join = ee.Join.inner() # Specify an equals filter for image timestamps. filter_time_eq = ee.Filter.equals( leftField='system:time_start', rightField='system:time_start' ) # Apply the join. inner_joined_modis = inner_join.apply(mcd43a4, mcd43a2, filter_time_eq) # Display the join result: a FeatureCollection. display('Inner join output:', inner_joined_modis)
Para usar as imagens combinadas na saída FeatureCollection
,
map()
uma função de combinação sobre a saída. Por exemplo, as imagens correspondentes
podem ser empilhadas para que as faixas de qualidade sejam adicionadas aos dados da imagem:
Editor de código (JavaScript)
// Map a function to merge the results in the output FeatureCollection. var joinedMODIS = innerJoinedMODIS.map(function(feature) { return ee.Image.cat(feature.get('primary'), feature.get('secondary')); }); // Print the result of merging. print('Inner join, merged bands:', joinedMODIS);
import ee import geemap.core as geemap
Colab (Python)
# Map a function to merge the results in the output FeatureCollection. joined_modis = inner_joined_modis.map( lambda feature: ee.Image.cat( feature.get('primary'), feature.get('secondary') ) ) # Print the result of merging. display("Inner join, merged 'bands':", joined_modis)
Embora essa função seja mapeada em um FeatureCollection
, o resultado é um
ImageCollection
. Cada imagem na ImageCollection
resultante
tem todas as bandas das imagens na coleção principal (neste exemplo, apenas
‘EVI’
) e todas as bandas da imagem correspondente na coleção secundária
(as bandas de qualidade).