In order to use country.reduceToImage()
for this purpose, you would need each feature to have a property (e.g. an ID number 1, 2, 3, ...) that becomes the corresponding pixel value. In this (JavaScript for interactive convenience) example, I've used the feature ID which seems to be a hexadecimal string and converted it to a number. JS example for Code Editor:
var country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
var image = country.map(function(feature) {
return feature.set('n', ee.Number.parse(feature.id(), 16))
}).reduceToImage({
'properties': ['n'],
'reducer': ee.Reducer.first()
});
Map.addLayer(image, {min: 0, max: 500});
https://code.earthengine.google.com/6a522690d6d371799a441548f1814070
Corresponding Python (not tested):
country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
def addNumberProperty(feature):
return feature.set('n', ee.Number.parse(feature.id(), 16))
image = country.map(addNumberProperty).reduceToImage(
properties=['n'],
reducer=ee.Reducer.first());
On the other hand, if you don't want to distinguish between countries, you can just set the property to a constant:
var country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
var image = country.map(function(feature) {
return feature.set('n', 1)
}).reduceToImage({
'properties': ['n'],
'reducer': ee.Reducer.first()
});
Map.addLayer(image, {min: 0, max: 500});
https://code.earthengine.google.com/c1d024444341f88c3d9b174099d89526
Another option would be to use country.style()
; the difference is that this works with RGB colors, making it more suitable for visualization (or working with tools that expect "ordinary" RGB images). Relatedly, it understands hex codes for colors and can draw boundary lines. In this case, if you want to change the colors of individual countries, you add a property with a dictionary of style options to each country feature.
var country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
var image = country.style({
color: 'FF0000',
fillColor: 'FFAAAA',
width: 1,
});
Map.addLayer(image);
https://code.earthengine.google.com/0c97dcad603d2f3a902ee26a7f21fa00