0

In the Python GEE API I'd like to create a .tif file of the selected country boundaries. This file will then be the input of a R script.

I can easily select the country but I don't understand how to use the reduceToImage function to transform it into a raster.

country_code = "CG"
country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter(ee.Filter.eq('country_co', country_code))

country.getInfo()

#country = country.reduceToImage({
#    'properties': [],
#    'reducer': ee.Reducer.first()
#})

#task_config = {
#    'image': country, 
#    'description':'congo',
#    'assetId': ee.data.getAssetRoots()[0]['id'] + '/congo'
#}
#
#task = ee.batch.Export.image.toAsset(**task_config)
#task.start()

1 Answer 1

1

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

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.