0

I am trying to sample random 10 points from forested areas for different ecoregions as identified by RESOLVE Ecoregions 2017 (https://ecoregions.appspot.com/).

In the Google Earth Engine code below,I

  1. identify forested regions around the world using landcover classifications provided by MODIS
  2. create a mask to filter a specific ecoregion from the RESOLVE feature collection
  3. Clip the global forested regions raster using the ecoregion mask
  4. Use "stratifiedSample" to sample 10 random points on the clipped image
var geometry = ee.Geometry({
  type: 'Polygon',
  coordinates :
  [[[-165.0046128552152, -57.92312657774621], 
  [184.8000746447848, -57.92312657774621], 
  [184.8000746447848, 69.26619097225324], 
  [ -165.0046128552152, 69.26619097225324], 
  [-165.0046128552152, -57.92312657774621]]], 
  geodesic: false}// geodesic = false means it will take a straight path and not the shortest path around the world
); //hand-drawn geometry required for sampling random points

var landcover = ee.Image("MODIS/061/MCD12Q1/2022_01_01").select('LC_Type1');
print(landcover);

//creating a mask with different types of forest/ savanna cover LC_Type1=8 
var forest2022= landcover.lte(8); //identifying forested regions
print(forest2022);

var biomes=ee.FeatureCollection("RESOLVE/ECOREGIONS/2017");//Predefined ecoregions and biomes

//Identifying different ecoregions using RESOLVE and intersecting with forest landcover from 2022 to identify forested regions in each ecoregions.

var formask= biomes.filterMetadata("ECO_ID", 'equals',129);//creating a mask for a specific ecoregion

var combmap = forest2022.clip(formask);//clipping forest cover map to a specific ecoregion

Map.addLayer(combmap);

//sampling random points
var points1= combmap.updateMask(combmap.eq(1)).stratifiedSample({ //sampling points only when formask=1 values, i.e., regions with forests
  region: geometry,
  classBand: 'LC_Type1',
  numPoints: 10,
  geometries: true,
  seed: 234
});

Map.addLayer(points1);

The above code works for certain ecoregions (e.g. anything from ecoregion ID 1 to 120), but not for the others (e.g., 128, 168, 173, 176, 177).

When it does not work, I usually get an error that says " Layer error: Unable to transform edge (4622.000000, 30575.000000 to 4622.007828, 30575.000000) from SR-ORG:6974 PLANAR [463.3127165279165, 0.0, -2.0015109354E7, 0.0, -463.3127165279167, 1.0007554677003E7] to EPSG:4326."

What is the issue and is there a way to fix it? I want to be able to convert this to a function and extract similar data for several pre-identified ecoregions.

(Note: I am new to GEE and GIS stackexchange. If more information is needed, I can provide that as well)

1 Answer 1

0

I understand you want to sample 10 points in forest areas (classes 1 to 8 in MODIS dataset) across multiple ecoregions. The solution is straightforward:

// Load the land cover image
var landCover = ee.Image("MODIS/061/MCD12Q1/2022_01_01").select('LC_Type1');

// Create a mask for land cover types with values <= 8
var landCoverMask = landCover.lte(8).selfMask();

// Load the RESOLVE Ecoregions 2017 dataset
var ecoregions = ee.FeatureCollection("RESOLVE/ECOREGIONS/2017");

// Filter ecoregions by the ECO_ID values
// You can specify the desired ecoregion IDs to analyze
var ids = [10, 20];
var filteredEcoregions = ecoregions.filter(ee.Filter.inList("ECO_ID", ids));

// Generate stratified random sample points within each filtered ecoregion
var points = filteredEcoregions.map(function(ft) {
  var region = ft.geometry();
  
  var samples = landCoverMask.stratifiedSample({
    numPoints: 10,          // Number of points to generate per ecoregion
    classBand: "LC_Type1",  // Band used for stratification
    region: region,         // Geographic region to sample within
    dropNulls: true,        // Remove points with no data
    geometries: true        // Return points as geometries
  });
  
  return samples;
});

// Flatten the nested collections into a single FeatureCollection
// This combines all sample points from all ecoregions into one dataset
points = points.flatten();
print("points (2 IDs x 10 Points = 20 Features)", points);

Since you're new to GEE, here are some important tips:

  1. Avoid using clip() unless absolutely necessary. Most GEE reduction or sampling methods have a parameters (like region in stratifiedSample()) where you can define the processing area. Read more.

  2. The filterMetadata method is deprecated, as shown in the Code Editor's Docs tab. While it still works, using filter(ee.Filter.eq()) is recommended for better code readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.