之前,您已了解如何通过执行类似以下操作来获取各个 Landsat 场景,其中 l8 和 point 是表示 Landsat 8 TOA 集合和感兴趣区域几何图形的导入项:
<x0A>
代码编辑器 (JavaScript)
// Define a point of interest. Use the UI Drawing Tools to import a point// geometry and name it "point" or set the point coordinates with the// ee.Geometry.Point() function as demonstrated here.varpoint=ee.Geometry.Point([-122.292,37.9018]);// Import the Landsat 8 TOA image collection.varl8=ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA');// Get the least cloudy image in 2015.varimage=ee.Image(l8.filterBounds(point).filterDate('2015-01-01','2015-12-31').sort('CLOUD_COVER').first());
[null,null,["最后更新时间 (UTC):2025-07-27。"],[[["\u003cp\u003eThis guide explains how to calculate and add an NDVI (Normalized Difference Vegetation Index) band to Landsat 8 imagery in Earth Engine.\u003c/p\u003e\n"],["\u003cp\u003eIt demonstrates how to use the \u003ccode\u003emap()\u003c/code\u003e function to apply a custom function, like NDVI calculation, to every image in a collection.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial explores creating a "greenest-pixel" composite using \u003ccode\u003equalityMosaic()\u003c/code\u003e to highlight areas with peak vegetation.\u003c/p\u003e\n"],["\u003cp\u003eIt highlights the potential issues with using \u003ccode\u003equalityMosaic()\u003c/code\u003e for compositing, such as cloud contamination in water bodies due to NDVI values.\u003c/p\u003e\n"],["\u003cp\u003eUsers are encouraged to further explore data extraction and visualization techniques in subsequent tutorials.\u003c/p\u003e\n"]]],[],null,["# NDVI, Mapping a Function over a Collection, Quality Mosaicking\n\nPreviously, you learned how to get individual Landsat scenes by doing something like this,\nwhere `l8` and `point` are imports representing the Landsat 8 TOA\ncollection and an area-of-interest geometry:\n\n\n### Code Editor (JavaScript)\n\n```javascript\n// Define a point of interest. Use the UI Drawing Tools to import a point\n// geometry and name it \"point\" or set the point coordinates with the\n// ee.Geometry.Point() function as demonstrated here.\nvar point = ee.Geometry.Point([-122.292, 37.9018]);\n\n// Import the Landsat 8 TOA image collection.\nvar l8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA');\n\n// Get the least cloudy image in 2015.\nvar image = ee.Image(\n l8.filterBounds(point)\n .filterDate('2015-01-01', '2015-12-31')\n .sort('CLOUD_COVER')\n .first()\n);\n```\n\nSuppose now that you want to compute a Normalized Difference Vegetation Index (NDVI)\nimage from the Landsat image. Vegetation reflects light in the\nnear-infrared (NIR) part of the electromagnetic spectrum and absorbs light in the red\npart ([Learn more about NIR\nreflectance from vegetation](https://science.nasa.gov/ems/08_nearinfraredwaves)). NDVI uses this to create a single value roughly\nreflecting the photosynthetic activity occurring at a pixel. The calculation is\n(NIR - red) / (NIR + red). This results in a number between 1 and -1, where pixels with\nhigh photosynthetic activity have a high NDVI. This is one way to compute NDVI in Earth\nEngine:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Compute the Normalized Difference Vegetation Index (NDVI).\nvar nir = image.select('B5');\nvar red = image.select('B4');\nvar ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');\n\n// Display the result.\nMap.centerObject(image, 9);\nvar ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};\nMap.addLayer(ndvi, ndviParams, 'NDVI image');\n```\n\nThe result should look something like Figure 8. Note that we use the `select()`\nfunction you learned about in the [previous section on\nmasking](/earth-engine/tutorials/tutorial_api_05#masking) to get the NIR and red bands, then compute NDVI using image mathematical\noperators that that you have also [seen before in\nthe section on `Image` math](/earth-engine/tutorials/tutorial_api_03#image-math). Finally, display the image with a palette. Here\nwe used color names instead of hex strings in the palette. (See\n[this external\nreference about CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) for details.)\nFigure 8. NDVI for a single Landsat scene. Blue is low and green is high NDVI.\n\nThe normalized difference operation is so ubiquitous in remote sensing, there is a\n[shortcut function](/earth-engine/apidocs/ee-image-normalizeddifference) on an\n`ee.Image` that is useful for simplifying the code in the previous example:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');\n```\n\nMapping a Function over a Collection\n------------------------------------\n\nSuppose now that you want to add NDVI to *every* image in an image collection. The\nway to do that in Earth Engine is to `map()` a function over the collection.\nDon't confuse `map()` with the `Map` object. The former is a method\non a collection, and uses *map* in the\n[parallel computing sense](https://en.wikipedia.org/wiki/Map_(parallel_pattern))\nof applying a function to every element in a collection. The function defines the\noperations that will be applied to every element in the collection. You have seen\n[a simple function in the JavaScript tutorial](/earth-engine/tutorials/tutorial_js_01#functions), but\nnow we're going to make a function that includes Earth Engine functionality. For example,\ncopy the previous NDVI code into a function which returns the input image with an NDVI band:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar addNDVI = function(image) {\n var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');\n return image.addBands(ndvi);\n};\n\n// Test the addNDVI function on a single image.\nvar ndvi = addNDVI(image).select('NDVI');\n```\n\nThis code might not be as efficient for computing NDVI for a single image, but this\nfunction can be used as an argument to `map()` in order to add an NDVI band\nto every image in the collection. It's often useful to first test a function on a single\nimage, to make sure the function is behaving as you expect. Once you've tested the function\non an individual image and have determined that it does what you want, you can map it\nover the collection:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar withNDVI = l8.map(addNDVI);\n```\n\nTo verify that this is indeed putting an NDVI band in every image in this collection,\nyou can add the `withNDVI` collection to the map and query a random location\nwith the **Inspector** tab. You should notice that each image in the\ncollection now has a band called `NDVI`.\n\nMake a greenest pixel composite\n-------------------------------\n\nNow that you've made an image collection in which each image has an NDVI band, we can\nexplore a new way to make composites: `qualityMosaic()`. You may have noticed\ndiscontinuities between Landsat paths, even in the median pixel composite. Part of the\nreason for that may be due to differences in\n[phenology](https://en.wikipedia.org/wiki/Phenology) as a result of images in\nadjacent paths being collected at different times (specifically, 8 days apart). One way to\nminimize this is to try to set pixel values in the composite from roughly the same\nphenological stage, for example the time of maximum greenness of plants (when the leaves\nare on and photosynthetically active). If we let max greenness be defined by the maximum\nNDVI, we can use\n[`qualityMosaic()`](/earth-engine/apidocs/ee-imagecollection-qualitymosaic) to\nmake a composite in which each pixel contains the maximum NDVI pixel from the collection.\nNow you can make use of the added NDVI band in your `withNDVI` collection:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Make a \"greenest\" pixel composite.\nvar greenest = withNDVI.qualityMosaic('NDVI');\n\n// Display the result.\nvar visParams = {bands: ['B4', 'B3', 'B2'], max: 0.3};\nMap.addLayer(greenest, visParams, 'Greenest pixel composite');\n```\n\nThe result of this code should look something like Figure 9. Comparing Figure 9 to the\nmedian composite shown in [Figure 6](/earth-engine/tutorials/tutorial_api_05#compositing-with-reducers),\nobserve that the greenest pixel composite is indeed much greener. However, close\nexamination of water bodies should make a different problem apparent. Specifically,\nwater bodies now appear cloudy. This is due to the way the `qualityMosaic()`\nmethod works: at each location, the entire time series is examined and the pixel with the\nmaximum value in the NDVI band is set as the composite value. Because NDVI is higher over\nclouds than water, water areas get cloudy pixels, while vegetated areas all appear green\nbecause NDVI is highest when the vegetation in the pixel is photosynthetically active.\nFigure 9. Landsat 8 greenest pixel composite.\n\nNow you've seen several ways to composite and mosaic images in Earth Engine. You can make\nrecent-value, median, or greenest-pixel composites from images filtered by time and place\nor all images in the collection. You've learned how to do computations on the imagery and\nextract information. The next page covers ways to get information out of Earth Engine, for\nexample as a chart or a dataset exported to your Google Drive folder. \n[arrow_backPrevious page](/earth-engine/tutorials/tutorial_api_05) [Next pagearrow_forward](/earth-engine/tutorials/tutorial_api_07)"]]