普通的字典对象为{key: value}格式,在GEE中则需要加入ee.Dictionary()修饰,具体常见的方法有构建、获取字典信息、重构、合并、判断、删除、强制转换等。
//ee.Dictionary 字典
var ee_dict1 = ee.Dictionary({
name: "AA",
age: 10,
desc: "this is a boy"
});
//获取字典的大小、key列表、值列表
print("size is", ee_dict1.size());
print("keys is", ee_dict1.keys());
print("values is", ee_dict1.values());
//根据键值取值
print("age is ", ee_dict1.get("age"));
print("name is ", ee_dict1.get("name"));
var keys = ["name", "year", "sex"];
var values = ["BB", 1990, "girl"];
//生成新的字典
var ee_dict2 = ee.Dictionary.fromLists(keys, values);
print("ee_dict2 is", ee_dict2);
//合并两个字典
var ee_dict3 = ee_dict1.combine(ee_dict2);
print("ee_dict3 is", ee_dict3);
var ee_dict4 = ee_dict1.combine(ee_dict2, false);
print("ee_dict4 is", ee_dict4);
//判断是否包含key
var flag = ee_dict4.contains("name");
print("flag is", flag);
//删除
var ee_dict5 = ee.Dictionary({
a: 1,
b: 2,
c: 3
});
ee_dict5 = ee_dict5.remove(["a", "c"]);
print("ee_dict5 is", ee_dict5);
//添加忽略可以防止键值不存在造成删除错误
print(ee_dict5.remove(["a", "c"], true));
//添加,需要注意的使用set后字典会变为GEE的object对象类型,
//所以需要强制转换一下
var ee_dict6 = ee.Dictionary({
a: 1,
b: 2,
c: 3
});
ee_dict6 = ee_dict6.set("d", 4);
ee_dict6 = ee.Dictionary(ee_dict6);
print("ee_dict6 is", ee_dict6);