toTable#

geetools.DictionaryAccessor.toTable(valueType='value')#

Convert a ee.Dictionary to a ee.FeatureCollection with no geometries (table).

There are 3 different type of values handled by this method:

  1. value (default): when values are a ee.String or ee.Number, the keys will be saved in the column system:index and the values in the column “value”.

  2. dict: when values are a ee.Dictionary, the keys will be saved in the column system:index and the values will be treated as each Feature’s properties.

  3. list: when values are a ee.List of numbers or strings, the keys will be saved in the column system:index and the values in as many columns as items in the list. The column name pattern is “value_{i}” where i is the position of the element in the list.

These are the only supported patterns. Other patterns should be converted to one of these. For example, the values of a reduction using the reducer ee.Reducer.frequencyHistogram() are of type ee.Array and the array contains lists.

Parameters:

valueType (Literal[dict, list, value]) – this will define how to process the values.

Returns:

a collection in which the keys of the ee.Dictionary are in the system:index and the values are in new columns.

Return type:

ee.FeatureCollection

Examples

import ee, geetools
from geetools.utils import initialize_documentation

initialize_documentation()

d = ee.Dictionary({"foo": 1, "bar": 2})
d.geetools.toTable().getInfo()
{'type': 'FeatureCollection',
 'columns': {'system:index': 'String', 'value': 'Integer'},
 'features': [{'type': 'Feature',
   'geometry': None,
   'id': 'bar',
   'properties': {'value': 2}},
  {'type': 'Feature',
   'geometry': None,
   'id': 'foo',
   'properties': {'value': 1}}]}
import ee, geetools
from geetools.utils import initialize_documentation

initialize_documentation()

d = ee.Dictionary({
  "Argentina": {"ADM0_CODE": 12, "Shape_Area": 278.289196625},
  "Armenia": {"ADM0_CODE": 13, "Shape_Area": 3.13783139285},
})
d.geetools.toTable('dict').getInfo()
{'type': 'FeatureCollection',
 'columns': {'ADM0_CODE': 'Integer',
  'Shape_Area': 'Float',
  'system:index': 'String'},
 'features': [{'type': 'Feature',
   'geometry': None,
   'id': 'Argentina',
   'properties': {'ADM0_CODE': 12, 'Shape_Area': 278.289196625}},
  {'type': 'Feature',
   'geometry': None,
   'id': 'Armenia',
   'properties': {'ADM0_CODE': 13, 'Shape_Area': 3.13783139285}}]}
import ee, geetools
from geetools.utils import initialize_documentation

initialize_documentation()

d = ee.Dictionary({
  "Argentina": [12, 278.289196625],
  "Armenia": [13, 3.13783139285],
})
d.geetools.toTable().getInfo()
{'type': 'FeatureCollection',
 'columns': {'system:index': 'String', 'value': 'List<Number>'},
 'features': [{'type': 'Feature',
   'geometry': None,
   'id': 'Argentina',
   'properties': {'value': [12, 278.289196625]}},
  {'type': 'Feature',
   'geometry': None,
   'id': 'Armenia',
   'properties': {'value': [13, 3.13783139285]}}]}
import ee, geetools
from geetools.utils import initialize_documentation

initialize_documentation()

# reduction
ran = ee.Image.random().multiply(10).reduceRegion(
    reducer=ee.Reducer.fixedHistogram(0, 1.1, 11),
    geometry=ee.Geometry.Point([0,0]).buffer(1000),
    scale=100
)

# process to get desired format
res = ee.Array(ee.Dictionary(ran).get('random'))
reslist = res.toList()
keys = reslist.map(lambda i: ee.Number(ee.List(i).get(0)).multiply(100).toInt().format())
values = reslist.map(lambda i: ee.Number(ee.List(i).get(1)).toInt())
final = ee.Dictionary.fromLists(keys, values)

# fetch
final.geetools.toTable().getInfo()
{'type': 'FeatureCollection',
 'columns': {'system:index': 'String', 'value': 'Long'},
 'features': [{'type': 'Feature',
   'geometry': None,
   'id': '0',
   'properties': {'value': 3}},
  {'type': 'Feature',
   'geometry': None,
   'id': '10',
   'properties': {'value': 4}},
  {'type': 'Feature',
   'geometry': None,
   'id': '100',
   'properties': {'value': 4}},
  {'type': 'Feature',
   'geometry': None,
   'id': '20',
   'properties': {'value': 4}},
  {'type': 'Feature',
   'geometry': None,
   'id': '30',
   'properties': {'value': 3}},
  {'type': 'Feature',
   'geometry': None,
   'id': '40',
   'properties': {'value': 1}},
  {'type': 'Feature',
   'geometry': None,
   'id': '50',
   'properties': {'value': 3}},
  {'type': 'Feature',
   'geometry': None,
   'id': '60',
   'properties': {'value': 0}},
  {'type': 'Feature',
   'geometry': None,
   'id': '70',
   'properties': {'value': 5}},
  {'type': 'Feature',
   'geometry': None,
   'id': '80',
   'properties': {'value': 1}},
  {'type': 'Feature',
   'geometry': None,
   'id': '90',
   'properties': {'value': 4}}]}