با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
reduceRegion
برای بدست آوردن آماری از مقادیر پیکسل در یک ناحیه از ee.Image ، از image.reduceRegion() استفاده کنید. این همه پیکسل ها در منطقه (ها) را به یک نمایش آماری یا دیگر نمایش فشرده از داده های پیکسل در منطقه (مانند هیستوگرام) کاهش می دهد. این ناحیه به صورت Geometry نمایش داده میشود که ممکن است یک چند ضلعی باشد که دارای پیکسلهای زیادی است، یا ممکن است یک نقطه واحد باشد، در این صورت فقط یک پیکسل در منطقه وجود خواهد داشت. در هر صورت، همانطور که در شکل 1 نشان داده شده است، خروجی آماری است که از پیکسل های منطقه بدست می آید.
شکل 1. تصویری از ee.Reducer اعمال شده بر روی یک تصویر و یک منطقه.
برای مثالی از به دست آوردن آمار پیکسل در یک منطقه از یک تصویر با استفاده از reduceRegion() ، یافتن مقادیر میانگین طیفی یک ترکیب 5 ساله Landsat را در محدوده جنگل مخروطی سیرا نوادا (نشان داده شده در شکل 2) در نظر بگیرید:
شکل 2. ترکیب رنگ نادرست داده های تصویر Landsat برای کالیفرنیا و نوادا. ناحیه ای که باید کاهش یابد با رنگ سفید نشان داده شده است.
توجه داشته باشید که در این مثال کاهش با ارائه reducer ( ee.Reducer.mean() )، geometry ( region.geometry() )، scale (30 متر) و maxPixels برای حداکثر تعداد پیکسل های ورودی به کاهنده مشخص می شود. همیشه باید در فراخوانی های reduceRegion() یک مقیاس مشخص شود. این به این دلیل است که در جریانهای پردازش پیچیده، که ممکن است شامل دادههایی از منابع مختلف با مقیاسهای متفاوت باشد، مقیاس خروجی به طور واضح از ورودیها تعیین نمیشود. در این حالت، مقیاس به طور پیش فرض روی 1 درجه قرار می گیرد که به طور کلی نتایج رضایت بخشی ایجاد می کند. برای اطلاعات بیشتر در مورد نحوه کنترل ترازو توسط Earth Engine به این صفحه مراجعه کنید.
دو راه برای تنظیم مقیاس وجود دارد: با تعیین پارامتر scale ، یا با تعیین تبدیل CRS و CRS. (برای اطلاعات بیشتر در مورد تبدیل CRS و CRS به واژه نامه مراجعه کنید). به عنوان مثال، کاهش meanDictionary (در بالا) معادل زیر است:
به طور کلی، مشخص کردن مقیاس کافی است و منجر به کد خواندن بیشتر می شود. Earth Engine تعیین می کند که کدام پیکسل ها را با شطرنجی کردن منطقه به کاهنده وارد کند. اگر مقیاسی بدون CRS مشخص شده باشد، ناحیه در طرح اصلی تصویر با وضوح مشخص شده، شطرنجی می شود. اگر هم یک CRS و هم مقیاس مشخص شده باشند، منطقه بر اساس آنها شطرنجی می شود.
پیکسل ها در منطقه
طبق قوانین زیر که در مقیاس و طرح ریزی مشخص اعمال می شود، پیکسل ها در منطقه (و وزن) تعیین می شوند:
کاهنده های بدون وزن (به عنوان مثال ee.Reducer.count() یا ee.Reducer.mean().unweighted() ): پیکسل ها در صورتی که مرکز آنها در ناحیه باشد و ماسک تصویر غیر صفر باشد، گنجانده می شوند.
کاهش دهنده های وزنی (به عنوان مثال ee.Reducer.mean() ): اگر حداقل (تقریباً) 0.5٪ پیکسل در منطقه باشد و ماسک تصویر غیر صفر باشد، پیکسل ها گنجانده می شوند. وزن آنها حداقل ماسک تصویر و کسری (تقریبی) پیکسل پوشیده شده توسط ناحیه است.
پارامتر maxPixels برای رسیدن به موفقیت در محاسبات مورد نیاز است. اگر این پارامتر از مثال حذف شود، یک خطا برگردانده می شود که چیزی شبیه به:
چندین گزینه برای عبور از این خطاها وجود دارد: افزایش maxPixels ، مانند مثال، افزایش scale ، یا تنظیم bestEffort روی true، که به طور خودکار یک مقیاس جدید (بزرگتر) را محاسبه می کند به طوری که از maxPixels فراتر نمی رود. اگر maxPixels مشخص نکنید، از مقدار پیشفرض استفاده میشود.
تاریخ آخرین بهروزرسانی 2025-07-25 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-25 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003e\u003ccode\u003ereduceRegion\u003c/code\u003e calculates statistics of pixel values within a specified region of an \u003ccode\u003eee.Image\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe region can be a polygon or a single point, and the output is a statistic derived from the pixels within that region.\u003c/p\u003e\n"],["\u003cp\u003eA scale should always be specified in \u003ccode\u003ereduceRegion\u003c/code\u003e calls to ensure accurate results, either using the \u003ccode\u003escale\u003c/code\u003e parameter or by defining a CRS and CRS transform.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003emaxPixels\u003c/code\u003e parameter may need to be adjusted or \u003ccode\u003ebestEffort\u003c/code\u003e set to true to handle large regions and avoid computation errors.\u003c/p\u003e\n"],["\u003cp\u003ePixels are included in the calculation based on whether their centroid is within the region (unweighted reducers) or the fraction of the pixel covered by the region (weighted reducers), along with the image's mask.\u003c/p\u003e\n"]]],["`reduceRegion()` calculates statistics of pixel values within a specified region of an `ee.Image`. It uses a `Geometry` to define the region, which can range from a single point to a polygon. The key actions involve setting a `reducer` (e.g., `ee.Reducer.mean()`), `geometry`, `scale` or `crs`, and `maxPixels`. Pixel inclusion is determined by centroid location for unweighted reducers or pixel coverage for weighted ones, considering the image's mask. The output is a `Dictionary` of statistics derived from pixel values.\n"],null,["# Statistics of an Image Region\n\n`reduceRegion`\n--------------\n\nTo get statistics of pixel values in a region of an `ee.Image`, use\n[`image.reduceRegion()`](/earth-engine/apidocs/ee-image-reduceregion).\nThis reduces all the pixels in the region(s) to a\nstatistic or other compact representation of the pixel data in the region (e.g.\nhistogram). The region is represented as a `Geometry`, which might be a\npolygon, containing many pixels, or it might be a single point, in which case there will\nonly be one pixel in the region. In either case, as illustrated in Figure 1, the output\nis a statistic derived from the pixels in the region.\nFigure 1. An illustration of an `ee.Reducer` applied to an image and a region.\n\nFor an example of getting pixel statistics in a region of an image using\n`reduceRegion()`, consider finding the mean spectral\nvalues of a 5-year Landsat composite within the boundaries of the Sierra Nevada\nConiferous Forest (illustrated by Figure 2): \n\n```gdscript\n// Load input imagery: Landsat 7 5-year composite.\nvar image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012');\n\n// Load an input region: Sierra Nevada.\nvar region = ee.Feature(ee.FeatureCollection('EPA/Ecoregions/2013/L3')\n .filter(ee.Filter.eq('us_l3name', 'Sierra Nevada'))\n .first());\n\n// Reduce the region. The region parameter is the Feature geometry.\nvar meanDictionary = image.reduceRegion({\n reducer: ee.Reducer.mean(),\n geometry: region.geometry(),\n scale: 30,\n maxPixels: 1e9\n});\n\n// The result is a Dictionary. Print it.\nprint(meanDictionary);\n```\n\nTo force the computation, it suffices to print the result, which the Code Editor will\ndisplay as a `Dictionary` in the console. The output should look something like: \n\n```\nB1: 25.406029716816853\nB2: 23.971497014238988\nB3: 22.91059593763103\nB4: 54.83164133293403\nB5: 38.07655472573677\nB6_VCID_2: 198.93216428012906\nB7: 24.063261634961563\n```\nFigure 2. False color composite of the Landsat image data for California and Nevada. The region over which to reduce is shown in white.\n\nNote that in this example the reduction is specified by providing the `reducer`\n(`ee.Reducer.mean()`), the `geometry` (`region.geometry()`),\nthe `scale` (30 meters) and `maxPixels` for the maximum number of\npixels to input to the reducer. A scale should always be specified in\n`reduceRegion()` calls. This is because in complex processing flows, which\nmay involve data from different sources with different scales, the scale of the output\nwill not be unambiguously determined from the inputs. In that case, the scale defaults\nto 1 degree, which generally produces unsatisfactory results. See [this\npage](/earth-engine/guides/scale) for more information about how Earth Engine handles scale.\n\nThere are two ways to set the scale: by specifying the `scale` parameter,\nor by specifying a CRS and CRS transform. (See [the glossary](/earth-engine/glossary) for\nmore information about CRS's and CRS transforms). For example, the\n`meanDictionary` reduction (above) is equivalent to the following: \n\n```gdscript\n// As an alternative to specifying scale, specify a CRS and a CRS transform.\n// Make this array by constructing a 4326 projection at 30 meters,\n// then copying the bounds of the composite, from composite.projection().\nvar affine = [0.00026949458523585647, 0, -180, 0, -0.00026949458523585647, 86.0000269494563];\n\n// Perform the reduction, print the result.\nprint(image.reduceRegion({\n reducer: ee.Reducer.mean(),\n geometry: region.geometry(),\n crs: 'EPSG:4326',\n crsTransform: affine,\n maxPixels: 1e9\n}));\n```\n\nIn general, specifying the scale is sufficient and results in more readable code.\nEarth Engine determines which pixels to input to the reducer by first rasterizing the region.\nIf a scale is specified without a CRS, the region is rasterized in the image's native\nprojection scaled to the specified resolution. If both a CRS and scale are specified, the\nregion is rasterized based on them.\n\nPixels in the region\n--------------------\n\nPixels are determined to be in the region (and weighted) according to the following rules,\napplied in the specified scale and projection:\n\n- **Unweighted reducers** (e.g. `ee.Reducer.count()` or `ee.Reducer.mean().unweighted()`): pixels are included if their centroid is in the region and the image's mask is non-zero.\n- **Weighted reducers** (e.g. `ee.Reducer.mean()`): pixels are included if at least (approximately) 0.5% of the pixel is in the region and the image's mask is non-zero; their weight is the minimum of the image's mask and the (approximate) fraction of the pixel covered by the region.\n\nThe `maxPixels` parameter is needed to get the computation to succeed. If this\nparameter is left out of the example, an error is returned, which looks something like:\n| Dictionary (Error) \n| Image.reduceRegion: Too many pixels in the region. Found 527001545, but only 10000000 allowed. \n\nThere are multiple options to get past these errors: increase `maxPixels`,\nas in the example, increase the `scale`, or set `bestEffort` to\ntrue, which automatically computes a new (larger) scale such that `maxPixels`\nis not exceeded. If you do not specify `maxPixels`, the default value is used."]]