admin管理员组

文章数量:1405379

I am working with Google Earth Engine (GEE) and need to perform a left join operation between two image collections:

  1. collection1 (lstDataset), which has 365 elements.
  2. collection2 (NTTempdataset), which has 320 elements.

I want to join the two collections based on the timestamp (system:time_start), keeping all images from collection1 (lstDataset) and adding the bands from collection2 (NTTempdataset) where the dates match. For the 45 dates in collection1 that do not have a corresponding match in collection2, I want to add null values for the bands from collection2. I am getting this error
ImageCollection (Error) Parameter 'value' is required and may not be null. Link:

var point = ee.Geometry.Point([-94.73665965557193, 35.915990354302]);

print('Point Geometry:', point);
var startDate = ee.Date('2016-01-01');
var endDate = ee.Date('2016-12-31');

  
var lstDataset = ee.ImageCollection('OREGONSTATE/PRISM/AN81d')
                  .select('tmean')
                  .filterDate(startDate, endDate)
                  .filterBounds(point)
                  .map(function(image) { return image.clip(point); });
print("lstDataset", lstDataset)    

var NTTempdataset = ee.ImageCollection('NASA/VIIRS/002/VNP21A1N')
    .select("LST_1KM") // Select the LST_1KM band
    .filterDate(startDate, endDate) // Filter by date
    .filterBounds(point) // Filter by region
    .map(function(image) {
        return image
            .clip(point) // Clip to the region
            .rename("LST_1KM_Night"); // Rename the band to LST_1KM_Night
    });
print("NTTempdataset", NTTempdataset)      


var leftJoin = function(collection1, collection2, dateProperty) {
  // Get the band names of collection2
  var collection2Bands = collection2.first().bandNames();

  return collection1.map(function(image) {
    var date = image.get(dateProperty); // Get the date of the current image in collection1
    
    // Find the image in collection2 with the same date
    var sameDateImage = collection2
      .filter(ee.Filter.eq(dateProperty, date)) // Filter for the same date
      .first(); // Get the first image (if any)
    
    // If no image is found, sameDateImage will be null.
    var mergedImage = ee.Algorithms.If({
      condition: ee.Algorithms.IsEqual(sameDateImage, null), // Check if no matching image is found
      trueCase: image.addBands(ee.Image.constant(null).rename(collection2Bands)), // Add null bands if no match
      falseCase: image.addBands(sameDateImage) // Add bands from collection2 if a match is found
    });

    return ee.Image(mergedImage); // Return the merged image
  });
};

// Example usage
var dateProperty = 'system:time_start'; // Use 'system:time_start' as the default date property

// Left join with missing values filled with null
var leftJoinedCollection = leftJoin(lstDataset, NTTempdataset, dateProperty);

print("Left Joined Collection", leftJoinedCollection);

I am working with Google Earth Engine (GEE) and need to perform a left join operation between two image collections:

  1. collection1 (lstDataset), which has 365 elements.
  2. collection2 (NTTempdataset), which has 320 elements.

I want to join the two collections based on the timestamp (system:time_start), keeping all images from collection1 (lstDataset) and adding the bands from collection2 (NTTempdataset) where the dates match. For the 45 dates in collection1 that do not have a corresponding match in collection2, I want to add null values for the bands from collection2. I am getting this error
ImageCollection (Error) Parameter 'value' is required and may not be null. Link: https://code.earthengine.google/53320bc6a2c7711b62fea1e7762195bb

var point = ee.Geometry.Point([-94.73665965557193, 35.915990354302]);

print('Point Geometry:', point);
var startDate = ee.Date('2016-01-01');
var endDate = ee.Date('2016-12-31');

  
var lstDataset = ee.ImageCollection('OREGONSTATE/PRISM/AN81d')
                  .select('tmean')
                  .filterDate(startDate, endDate)
                  .filterBounds(point)
                  .map(function(image) { return image.clip(point); });
print("lstDataset", lstDataset)    

var NTTempdataset = ee.ImageCollection('NASA/VIIRS/002/VNP21A1N')
    .select("LST_1KM") // Select the LST_1KM band
    .filterDate(startDate, endDate) // Filter by date
    .filterBounds(point) // Filter by region
    .map(function(image) {
        return image
            .clip(point) // Clip to the region
            .rename("LST_1KM_Night"); // Rename the band to LST_1KM_Night
    });
print("NTTempdataset", NTTempdataset)      


var leftJoin = function(collection1, collection2, dateProperty) {
  // Get the band names of collection2
  var collection2Bands = collection2.first().bandNames();

  return collection1.map(function(image) {
    var date = image.get(dateProperty); // Get the date of the current image in collection1
    
    // Find the image in collection2 with the same date
    var sameDateImage = collection2
      .filter(ee.Filter.eq(dateProperty, date)) // Filter for the same date
      .first(); // Get the first image (if any)
    
    // If no image is found, sameDateImage will be null.
    var mergedImage = ee.Algorithms.If({
      condition: ee.Algorithms.IsEqual(sameDateImage, null), // Check if no matching image is found
      trueCase: image.addBands(ee.Image.constant(null).rename(collection2Bands)), // Add null bands if no match
      falseCase: image.addBands(sameDateImage) // Add bands from collection2 if a match is found
    });

    return ee.Image(mergedImage); // Return the merged image
  });
};

// Example usage
var dateProperty = 'system:time_start'; // Use 'system:time_start' as the default date property

// Left join with missing values filled with null
var leftJoinedCollection = leftJoin(lstDataset, NTTempdataset, dateProperty);

print("Left Joined Collection", leftJoinedCollection);
Share Improve this question edited Mar 8 at 14:52 ryan asked Mar 8 at 14:51 ryanryan 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

How about using a ee.Join to do a join?

var point = ee.Geometry.Point([-94.73665965557193, 35.915990354302]);

print('Point Geometry:', point);
var startDate = ee.Date('2016-01-01');
var endDate = ee.Date('2016-12-31');

  
var lstDataset = ee.ImageCollection('OREGONSTATE/PRISM/AN81d')
                  .select('tmean')
                  .filterDate(startDate, endDate)
                  .filterBounds(point)
                  .map(function(image) { return image.clip(point); });
print("lstDataset", lstDataset)    

var NTTempdataset = ee.ImageCollection('NASA/VIIRS/002/VNP21A1N')
    .select("LST_1KM") // Select the LST_1KM band
    .filterDate(startDate, endDate) // Filter by date
    .filterBounds(point) // Filter by region
    .map(function(image) {
        return image
            .clip(point) // Clip to the region
            .rename("LST_1KM_Night"); // Rename the band to LST_1KM_Night
    });
print("NTTempdataset", NTTempdataset)      

var joined = ee.Join.saveBest({
  matchKey: 'other',
  measureKey: 'garbage',
  outer: true
}).apply({
  primary: lstDataset, 
  secondary: NTTempdataset, 
  condition: ee.Filter.maxDifference({
    difference: 100000000,
    leftField: 'system:time_start', 
    rightField: 'system:time_start'})
})

// Do something to these:
var withMatches = joined.filter(ee.Filter.neq('other', null))
print(withMatches.size())
// Do something else to these:
var withoutMatches = joined.filter(ee.Filter.eq('other', null))
print(withoutMatches.size())

本文标签: