如何在Google Earth Engine中输出指定像元的聚合值?
Got it, here's a straightforward solution to compute the total precipitation sum for a given geographic point using Google Earth Engine, leveraging NASA's IMERG monthly precipitation dataset. Let's break it down step by step:
Step-by-Step Implementation
1. Define Your Target Point
First, specify the geographic coordinates of the point you're interested in. Remember, GEE uses the WGS84 (EPSG:4326) coordinate system, where you need to input longitude first, latitude second.
2. Fetch and Filter Precipitation Data
We'll use the NASA/GPM_L3/IMERG_MONTHLY_V06 collection, which provides reliable monthly precipitation estimates. We filter it by your desired time range and the bounds of your target point to narrow down the data we need (and optimize computation speed).
3. Compute Total Precipitation Sum
Use a sum reducer to accumulate precipitation values across all images in the filtered collection, giving us a single total value for the period.
4. Extract and Output the Result
Pull the computed sum from GEE's server to your local environment and print it out for review.
Full Working Code
// Define the target point (longitude, latitude) var loc_point = ee.Geometry.Point([-0.627983, 52.074361]); // Fetch and filter the IMERG monthly precipitation dataset var precip = ee.ImageCollection('NASA/GPM_L3/IMERG_MONTHLY_V06') .filterDate('2019-06-01', '2020-06-30') // Time range: June 2019 to June 2020 .filterBounds(loc_point) // Keep only images that cover the target point .select('precipitation'); // Select the precipitation band // Calculate the total precipitation sum across the collection var precip_sum = precip.reduce(ee.Reducer.sum()); // Extract the sum value and print it to the GEE console var totalPrecipitation = precip_sum.get('precipitation_sum').getInfo(); print('Total Precipitation (mm):', totalPrecipitation);
Key Details to Note
- Coordinate Order: Always stick to
[longitude, latitude]when defining points in GEE—this is a super common mistake that breaks location targeting! - Filtering:
filterBounds()ensures we only process images that include your point, which cuts down on unnecessary computation. - Reducer:
ee.Reducer.sum()aggregates precipitation values over all images in the filtered collection, storing the total in a new band namedprecipitation_sum. - getInfo(): This method pulls the server-side computation result to your local machine. For large datasets, this might take a moment, so avoid overusing it for massive computations.
内容的提问来源于stack exchange,提问作者Juba

