I have been trying to find a solution for this for quite some time now based on ee.FeatureCollection.reduceToImage, but I don't seem to be able to find it...

Suppose you have a region of interest such as

var ROI = ee.Geometry.Polygon(
    [[[7.263865757181179, 48.32795920242072],
      [7.549510288431179, 45.47651062594895],
      [11.768260288431179, 45.58425825326147],
      [11.614451694681179, 48.284113758015984]]]);

and within this ROI a featureCollection consisting of polygons that designate the same class, e.g.

var example = ee.FeatureCollection(
            [[[8.845897007181179, 46.39336563254817],
              [8.889842319681179, 45.890971350766165],
              [9.878611850931179, 45.96738640737871],
              [9.768748569681179, 46.453950430139365]]]),
          "system:index": "0"
            [[[10.186229038431179, 46.89117727437972],
              [10.669627475931179, 46.544701395494236],
              [11.570506382181179, 47.01116923861675],
              [10.471873569681179, 47.33976045697917]]]),
          "system:index": "1"
            [[[7.813182163431179, 47.2950722806053],
              [8.801951694681179, 46.86113724601222],
              [9.483104038431179, 47.36953157846809],
              [8.604197788431179, 47.725475439232184]]]),
          "system:index": "2"

How can you create a binary image, where one pixel value (e.g. 1) indicates the areas of the polygons, while another pixel value (e.g. 0) indicates the background class?


var foreground = 1;
var background = 0;

Start by defining an image in the shape of the ROI with the background value.

var roi_image = ee.Image(background).clip(ROI);

Create a second image with the foreground value within the example polygons.

var example_image = ee.Image(foreground).clip(example);

Finally, replace the background image with the foreground value where the example polygons occur, using ee.Image.where().

var binary_image = roi_image.where({test:example_image, value:example_image});
Map.addLayer(binary_image, {min:0, max:1}, 'binary_image');

enter image description here

  • 1
    Great solution, works like a charm. Can you also tell what the scale (pixel size) of the thus-created image will be? – Michael Dec 5 '18 at 7:00
  • 1
    I am also wondering how this can be used with ee.Image.stratifiedSample() to gather training data for a binary classifier. Here is a summary of what I am doing: code.earthengine.google.com/3cbd9c722f8ceca84281327cecd8cdb8 You'll see that this code throws the following error: "FeatureCollection (Error) Remote request too large (134217728 > 83886080) for output: [<Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>, <Object>]." – Michael Dec 5 '18 at 10:18
  • 2
    The computed images do not have scale. However, when you export them (using something like Export.image or ee.Image.getDownloadURL) or perform a spatial aggregation (using something like ee.Image.reduceRegion), you can specify a scale for the computation to be run at. For more info on scale: developers.google.com/earth-engine/scale#scale-of-analysis – Tyler Erickson Dec 5 '18 at 13:49
  • 2
    For the ee.Image.stratifiedSample() comment, could you create a new question for this? (StackExchange requests each post should be on one specific question.) – Tyler Erickson Dec 5 '18 at 13:59
  • 1
    I have added a dedicated question here: gis.stackexchange.com/questions/305079/… – Michael Dec 5 '18 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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