TransWikia.com

Earth engine fill color by group

Geographic Information Systems Asked by Fabio Daniel Trinco on December 8, 2020

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)

One Answer

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

Answered by Kevin Reid on December 8, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP