1

I would like add a feature collection to my map, with a color indicating the group it belongs. Here I show you a screenshot of the results in QGIS I would like to arrive to in google earth engine:

enter image description here

For example, I would like to color the features by country. As an example, imagine I start with the 'states' feature collection:

var table = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level2");

var states = table.select('ADM0_NAME')

Map.addLayer(states)
4

To display features in a collection with specific styling, use FeatureCollection.style.

var table = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level2");

var styled = table
  .map(function (feature) {
    return feature.set('style', {
      fillColor: feature.getNumber('ADM0_CODE').format('%06x')
    });
  })
  .style({
    styleProperty: 'style',
  });

Map.addLayer(styled);

https://code.earthengine.google.com/353d55f8f6e933c311baedded17d237f

However, you'll have to choose colors yourself — Earth Engine does not have a built-in method to pick colors based on distinct values of a property. In the above example, I've done it in a sloppy way by using the ADM0_CODE directly as a numeric color value, which will assign most countries shades of blue.

If you're interested in specific countries, you could hand write an ee.Dictionary that assigns colors to them. In this example, '777777' is the gray color that is used when no matching entry is present in the dictionary.

var table = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level2");

var colorTable = ee.Dictionary({
  'Spain': 'FF0000',
  'Germany': '00FF00',
  'Italy': '0000FF',
});

var styled = table
  .map(function (feature) {
    return feature.set('style', {
      fillColor: colorTable.get(feature.get('ADM0_NAME'), '777777')
    });
  })
  .style({
    styleProperty: 'style',
  });

Map.addLayer(styled);

https://code.earthengine.google.com/0c3231d141c97707e7b7685cfe7bb71e

3
  • Thanks so much for the answer. It is a possible solution, but in my case I have 156 different groups, so I need a easier way to assign each group a color, is it possible? Thanks so much! – Fabio Daniel Trinco Sep 25 '20 at 12:28
  • 1
    @FabioDanielTrinco There's no good way to do that that I know of. And with 156 items, any arbitrary assignment would be very likely to give you colors that were rather similar adjacent on the map. – Kevin Reid Sep 25 '20 at 16:04
  • That's partially true, take a look at the results obtained in QGIS. This is a detail of the zone. Its easy to view that all the clear blue items are part of the same group. – Fabio Daniel Trinco Sep 28 '20 at 11:45
1

did you try with image.paint() with the FeatureCollection as an argument? This is extremely powerful: https://developers.google.com/earth-engine/guides/feature_collections_visualizing

This is just en example using the ADM0_CODE codes of three countries in order to pass these numbers to the palette:

var table = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level2");

var states_list = ['Italy','Germany','France'] //ADM0_CODE = 122 93 85

var states = table.filter(ee.Filter.inList('ADM0_NAME',states_list));

var empty = ee.Image().byte();

var palette = ['FF0000', '00FF00', '0000FF'];

// Paint the interior of the polygons with different colors.

var fills = empty.paint({ featureCollection: states, color: 'ADM0_CODE', });

Map.addLayer(fills, {palette: palette,min: 85,max: 122}, 'colored countries');

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.