[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThis tutorial demonstrates how to apply computations to images in Earth Engine, such as calculating slope and performing trigonometric operations on bands.\u003c/p\u003e\n"],["\u003cp\u003eIt explains the use of reducers for computing image statistics within regions, like finding the mean elevation within a polygon.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial highlights the importance of scale in Earth Engine and how to handle errors related to exceeding the pixel limit in computations.\u003c/p\u003e\n"],["\u003cp\u003eIt also introduces the concept of band math, chaining methods for complex operations, and accessing image metadata like native resolution.\u003c/p\u003e\n"],["\u003cp\u003eUsers are guided on how to draw polygons for region-based calculations and provided with links to relevant documentation for further exploration.\u003c/p\u003e\n"]]],[],null,["# Computations using Images\n\nNow that you know how to load and display an image, it's time to apply a computation to it.\nFor example, you can compute the slope of terrain, by passing the SRTM elevation image to the\n[`slope` method of the `ee.Terrain`\npackage](/earth-engine/apidocs/ee-terrain-slope).\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load the SRTM image.\nvar srtm = ee.Image('CGIAR/SRTM90_V4');\n\n// Apply an algorithm to an image.\nvar slope = ee.Terrain.slope(srtm);\n\n// Display the result.\nMap.setCenter(-112.8598, 36.2841, 9); // Center on the Grand Canyon.\nMap.addLayer(slope, {min: 0, max :60}, 'slope');\n```\n\nNote that in the code `ee.Terrain.slope(srtm)`, the `srtm` image is\nprovided as an argument to the slope algorithm. The result should look something like\nFigure 3.\nFigure 3. Slope image.\n\nImage math\n----------\n\nThere are also methods in the `ee.Image` class that can be invoked on an\nimage object. For example, suppose you'd like to do some math using image bands (sometimes\ncalled *band math* or *map algebra* ). For example, you may be interested in\ntrigonometric operations on an aspect image. To accomplish that, first convert an\naspect image to radians, then call `sin()` on it. Reusing our `srtm`\nimage,\n\n### Code Editor (JavaScript)\n\n```javascript\n// Get the aspect (in degrees).\nvar aspect = ee.Terrain.aspect(srtm);\n\n// Convert to radians, compute the sin of the aspect.\nvar sinImage = aspect.divide(180).multiply(Math.PI).sin();\n\n// Display the result.\nMap.addLayer(sinImage, {min: -1, max: 1}, 'sin');\n```\n\nThe result should look something like Figure 4. It's worth taking a closer look at the\n`aspect.divide(180).multiply(Math.PI).sin()` code. By chaining multiple methods\nlike this, the code says, 'divide the aspect by 180, multiply the result of that by\nπ, and finally take the sin'. You can perform complex mathematical operations on\nimages by combining methods in this manner. See the\n[`Image` docs](/earth-engine/apidocs/ee-image) for a complete list of mathematical\noperations, `add()`, `subtract()`, `multiply()`, etc.\nFigure 4. Sin of terrain aspect.\n\nImage statistics\n----------------\n\nAnother useful class of operations on images involves computing pixel statistics in image\nregions, or raster-vector overlays. To compute statistics in Earth Engine, use a\n*reducer* as represented by classes in the\n[`ee.Reducer` package](/earth-engine/guides/reducers_intro). For example, suppose\nyou're interested in the mean of elevation in some region. You can define a region by\ndrawing a polygon using the [geometry drawing tools](/earth-engine/guides/playground#geometry-tools).\nTo interactively draw a region, get the polygon drawing tool\n(), then digitize a polygon over your\narea of interest and click **Exit** when you're done. Note that the resultant\n`ee.Geometry` object is automatically named `geometry` and added\nas an import at the top of your script. Rename that variable to 'polygon' by clicking\non the variable name in the imports and typing the new name.\n\nNext, get the mean pixel value in the polygon using the following code:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Compute the mean elevation in the polygon.\nvar meanDict = srtm.reduceRegion({\n reducer: ee.Reducer.mean(),\n geometry: polygon,\n scale: 90\n});\n\n// Get the mean from the dictionary and print it.\nvar mean = meanDict.get('elevation');\nprint('Mean elevation', mean);\n```\n\nThere are several things to note here. First, observe that `reduceRegion()` is\na method available for `Image` objects ([learn\nmore about reducing regions here](/earth-engine/guides/reducers_reduce_region)). Second, the method arguments are provided in a\nJavaScript object that is passed as a single argument. (Specifically, the keys of the\nobject are the names of the method parameters. The values are the arguments to the\nmethod). Third, the `reducer` parameter specifies the type of statistic to\ncompute and the `geometry` parameter specifies the region in which to compute\nthe statistic. The `scale` parameter is the pixel size in meters to use. To\navoid ambiguity, you should always specify scale when doing reductions as Earth Engine\nmay not be able to automatically determine the appropriate scale from the inputs.\n([Learn more about scale in Earth Engine](/earth-engine/guides/scale)).\n\nLastly, the return value of `reduceRegion()` is a dictionary\nin which keys are band names and values are the pixel statistics for the bands. The\n`get()` method on a dictionary returns the value corresponding to the key\nprovided as an argument. In this case, the `srtm` image has one band,\n'elevation', so the example code gets that statistic from the dictionary and prints it.\n\nWhen you run this code, if you get an error that looks like:\n| ComputedObject (Error) \n| Image.reduceRegion: Too many pixels in the region. Found 527001545, but only 10000000 allowed. \n\nFear not! There are several things you can do to resolve the error. The\n`reduceRegion()` method has a check to make sure you consider whether you\nreally want to include so many pixels in your computation. This is intended to prevent\nyou from accidentally doing something silly, like trying compute the mean of every\none-meter pixel in the world (don't do that). To resolve the error, either set the\n`bestEffort` parameter to `true` by adding `bestEffort: true`\nto the dictionary of parameters, or set the `maxPixels` parameter to a value\nhigher than the default of 10 million pixels, or both. If `bestEffort` is\ntrue, Earth Engine will automatically recompute the scale such that `maxPixels`\nis not exceeded.\n\nDigression: Scale in Earth Engine\n---------------------------------\n\nIn the previous example, `scale` is set to approximately the native resolution\nof the SRTM image. You can discover the native resolution of an image with:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar scale = srtm.projection().nominalScale();\nprint('SRTM scale in meters', scale);\n```\n\nIf you specify a scale smaller than the native resolution, Earth Engine will happily\nresample the input image using nearest neighbor, then include all those smaller pixels in the\ncomputation. If you set the scale to be larger, Earth Engine will use input pixels from\nan aggregated version of the input (i.e. get pixels from a higher level of the image\npyramid). Learn more about how Earth Engine handles scale in\n[this doc](/earth-engine/guides/scale).\n\nSo far, you've been working with a single image with a single band. In the next page, you'll\nlearn about multi-band images and image collections. \n[arrow_backPrevious page](/earth-engine/tutorials/tutorial_api_02) [Next pagearrow_forward](/earth-engine/tutorials/tutorial_api_04)"]]