最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Calculating percent coverage of 1000m raster by 30m raster - Stack Overflow

matteradmin6PV0评论

I have two rasters, one of restoration opportunity at 1000m and one of deforestation from 2019-2023 at 30m (Global Forest Watch data). At the end of this, I want to determine how many pixels of restoration opportunity have over 50% coverage by deforestation in this time period. Right now, I am tackling "how do I calculate percent coverage of a coarser resolution raster by a finer resolution raster?". See below for what I have tried which has resulted in two issues/questions.

  1. The reported coverage percents per polygon are plausible but strike me as a little small. How can I go about verifying these numbers are correct? Does this method seem sound?
  2. A more overarching issue is that adjacent pixels are aggregated together at the vectorizing stage. Ideally, I would calculate percent coverage per 1km pixel. Advice on how to achieve that in GEE?

General overview: I converted the 1km raster to polygons, got a pixel area raster of deforestation, created a function that sums up pixel area raster per polygon then divides that by polygon area, and map that over all the polygons. Function shown here, full code is available here

## Function to calculate fractional coverage for each polygon
var calculateCoverage = function(feature) {
  // Clip raster to current polygon
  var clippedImage = lossYear_2019_2023_area.clip(feature);
  
  // Mask raster to include only the pixels inside the polygon
  var imageMasked = clippedImage.updateMask(clippedImage.mask());

  // Calculate the area of the polygon (in square meters)
  var polygonArea = feature.area({maxError: 1e6}); // in square meters
  
  // Sum up the pixels (area of deforestation) for the polygon
  var coverageArea = imageMasked.reduceRegion({
    reducer: ee.Reducer.sum(), // Sum of 30m area pixels that are in polygon
    geometry: feature.geometry(),
    scale: scale, // The resolution of the image (~30m per pixel)
    maxPixels: 1e13
  });
  
  // Calculate the fractional coverage as a percentage
  var fractionalCoverage = ee.Number(coverageArea.get('lossyear')).divide(polygonArea).multiply(100);

  // Add the fractional coverage as a new property to the feature
  return feature.set('fractionalCoverage', fractionalCoverage);
};

// Apply the function to each polygon in the FeatureCollection
var polygonsWithCoverage = opportunity_vectors.map(calculateCoverage);

// Print the result to check
print('Polygons with fractional coverage:', polygonsWithCoverage.first());

Post a comment

comment list (0)

  1. No comments so far