किसी ImageCollection पर मैपिंग करना

ImageCollection में मौजूद हर Image पर कोई फ़ंक्शन लागू करने के लिए, imageCollection.map() का इस्तेमाल करें. map() का एक ही आर्ग्युमेंट एक ऐसा फ़ंक्शन है जो एक पैरामीटर लेता है: ee.Image. उदाहरण के लिए, यहां दिया गया कोड, संग्रह की हर इमेज में टाइमस्टैंप बैंड जोड़ता है.

कोड एडिटर (JavaScript)

// Load a Landsat 8 collection for a single path-row, 2021 images only.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
  .filterDate('2021', '2022')
  .filter(ee.Filter.eq('WRS_PATH', 44))
  .filter(ee.Filter.eq('WRS_ROW', 34));

// This function adds a band representing the image timestamp.
var addTime = function(image) {
  return image.addBands(image.getNumber('system:time_start'));
};

// Map the function over the collection and display the result.
print(collection.map(addTime));

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 collection for a single path-row, 2021 images only.
collection = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterDate('2021', '2022')
    .filter(ee.Filter.eq('WRS_PATH', 44))
    .filter(ee.Filter.eq('WRS_ROW', 34))
)


# This function adds a band representing the image timestamp.
def add_time(image):
  return image.addBands(image.getNumber('system:time_start'))


# Map the function over the collection and display the result.
display(collection.map(add_time))

ध्यान दें कि पहले से तय फ़ंक्शन में, किसी प्रॉपर्टी की संख्या वाली वैल्यू से नया Image बनाने के लिए, getNumber() तरीके का इस्तेमाल किया जाता है. कम करना और कंपोज़िट बनाना सेक्शन में बताए गए तरीके के मुताबिक, टाइम बैंड का इस्तेमाल, बदलाव की लीनियर मॉडलिंग और कंपोज़िट बनाने के लिए किया जाता है.

मैप किया गया फ़ंक्शन, सीमित ऑपरेशन कर सकता है. खास तौर पर, यह फ़ंक्शन के बाहर के वैरिएबल में बदलाव नहीं कर सकता. यह कुछ भी प्रिंट नहीं कर सकता. यह JavaScript और Python के 'if' या 'for' स्टेटमेंट का इस्तेमाल नहीं कर सकता. हालांकि, मैप किए गए फ़ंक्शन में कंडीशनल ऑपरेशन करने के लिए, ee.Algorithms.If() का इस्तेमाल किया जा सकता है. उदाहरण के लिए:

कोड एडिटर (JavaScript)

// Load a Landsat 8 collection for a single path-row, 2021 images only.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
  .filterDate('2021', '2022')
  .filter(ee.Filter.eq('WRS_PATH', 44))
  .filter(ee.Filter.eq('WRS_ROW', 34));

// This function uses a conditional statement to return the image if
// the solar elevation > 40 degrees. Otherwise it returns a "zero image".
var conditional = function(image) {
  return ee.Algorithms.If(ee.Number(image.get('SUN_ELEVATION')).gt(40),
                          image,
                          ee.Image(0));
};

// Map the function over the collection and print the result. Expand the
// collection and note that 7 of the 22 images are now "zero images'.
print('Expand this to see the result', collection.map(conditional));

Python सेटअप

Python API के बारे में जानकारी पाने और इंटरैक्टिव डेवलपमेंट के लिए geemap का इस्तेमाल करने के लिए, Python एनवायरमेंट पेज देखें.

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 collection for a single path-row, 2021 images only.
collection = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterDate('2021', '2022')
    .filter(ee.Filter.eq('WRS_PATH', 44))
    .filter(ee.Filter.eq('WRS_ROW', 34))
)


# This function uses a conditional statement to return the image if
# the solar elevation > 40 degrees. Otherwise it returns a "zero image".
def conditional(image):
  return ee.Algorithms.If(
      ee.Number(image.get('SUN_ELEVATION')).gt(40), image, ee.Image(0)
  )


# Map the function over the collection and print the result. Expand the
# collection and note that 7 of the 22 images are now "zero images'.
display('Expand this to see the result', collection.map(conditional))

आउटपुट ImageCollection में इमेज की सूची की जांच करें और ध्यान दें कि जब If() एल्गोरिदम से जांच की गई शर्त सही होती है, तो आउटपुट में एक स्थिर इमेज होती है. हालांकि, यह सर्वर-साइड का सशर्त फ़ंक्शन दिखाता है (Earth Engine में क्लाइंट बनाम सर्वर के बारे में ज़्यादा जानें), आम तौर पर If() का इस्तेमाल न करें और इसके बजाय फ़िल्टर का इस्तेमाल करें.