Tuesday, 7 April 2015

rasterization - How can one create a binary image from a FeatureCollection in Google Earth Engine?


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(
[ee.Feature(
ee.Geometry.Polygon(
[[[8.845897007181179, 46.39336563254817],
[8.889842319681179, 45.890971350766165],
[9.878611850931179, 45.96738640737871],

[9.768748569681179, 46.453950430139365]]]),
{
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Polygon(
[[[10.186229038431179, 46.89117727437972],
[10.669627475931179, 46.544701395494236],
[11.570506382181179, 47.01116923861675],
[10.471873569681179, 47.33976045697917]]]),

{
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Polygon(
[[[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?



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...