***GEE的数据类型第二讲:Dictionary,List,Array
一、Dictionary 字典(关键词{身高}+内容{1.7米})
1.创建 ee.Dictiondary()
var Dictiondary_Profile= ee.Dictionary({
Name:'Jan',
Gender:'Male',
Age:">20",
Location:"Beijing"
})
print(Dictiondary_Profile)
2. 合并dictionary.combine()
var dict_1 = ee.Dictionary({
weight:'50kg',
height:'160cm'
})
var dict_2 = ee.Dictionary({
weight:'70kg',
age:'26'
})
var dict_comb = dict_1.combine(dict_2,true)
print(dict_comb)
3.替换 dictionary.set()
var dict_1 = ee.Dictionary({
weight:'50kg',
Age:'>20'
})
var dict_change = dict_1.set('Age','30')
print(dict_change)
4.关键值 dictionary.keys()
var dict_1=ee.Dictionary({
wight: '70kg',
age:'>20'
})
var dict_keys=dict_1.keys()
print(dict_keys)
5.内容查询 dictionary.get()
var dict_1=ee.Dictionary({
Name:'Jan',
Wight: '70kg',
Age:'>20'
})
var dict_Name=dict_1.get('Name')
print(dict_Name)
6.多个字段的查询 dictionary.values()
var dict_1 = ee.Dictionary({
Name:'Jan',
Wight: '70kg',
Age:'20'
})
var dict_value = dict_1.values(['Name','Age'])
print(dict_value)
7.包含 dictionary.contains()
var dict_1 = ee.Dictionary({
weight:'50kg',
height:'160cm'
})
var bool = dict_1.contains('name')
print(bool)
8.大小 dictionary.size()
var dict_1 = ee.Dictionary({
Name:'Jan',
Wight: '70kg',
Age:'20'
})
var dict_size = dict_1.size()
print(dict_size)
二、list列表
1.1创建 ee.list()
var list = ee.List([1,2,3,'A','B','C',['Hello','Good','Bye']])
print(list)
1.2 创建重复的内容List.repeat()
var list = ee.List.repeat('hello',5)(重复内容,重复次数)
print(list)
1.3 创建序列List.sequence()
var list1 = ee.List.sequence(0,9,1,null)
var list2 = ee.List.sequence(0,9,null,4)
print(list1, list2)
2.修改
2.1 改某一位的值 list.set ()
var list1 = ee.List([1992,1,20,'hello'])
var list2 = list1.set(0,1993)
var list3 = list2.set(-1, 'world')
print(list1, list2, list3)
2.2 替换 list.replace()
var list1 = ee.List([1992,1,20,'hello',1992,1,20,'hello'])
var list2 = list1.replace(1,5)
var list3 = list2.replaceAll(1,5)
print(list1, list2, list3)
2.3 添加内容 list.add()
var list1 = ee.List([1992,1,20,'hello'])
var list2 = list1.add('earth')
var list3 = list1.insert(3, 'world')
print(list1, list2, list3)
2.4组合打包 list.zip()
var list1 = ee.List([1992,1,20,'NO1'])
var list2 = list1.zip(['Year','Month','Day'])
print(list1, list2)
2.5 反转:list.reverse() 和 list.rotate()
var list1 = ee.List([0,1,2,3,4,5,6,7,8,9])
var list2 = list1.reverse()
var list3 = list1.rotate(5)
print(list1, list2, list3)
2.6 排序 list.sort()
var list1 = ee.List(['Zhao','Wang','Ma','Liu','Bai','1','9'])
var list2 = list1.sort()
print(list1, list2)
2.7 交换 list.swap()
var list1 = ee.List([1,2,3,4,5,6,7,8,9])
var list2 = list1.swap(5,8)
print(list1, list2)
2.8 展开 list.flatten()
var list1 = ee.List([[1,2,3,4],[5,6,7,8,9]])
var list2 = list1.flatten()
print(list1, list2)
3、查询
3.1 查询 list.get()
var list1 = ee.List(['hello','world','GEE'])
var list2 = list1.get(1)
print(list1, list2)
3.2 删除list.remove()&list.removeall()
var list1 = ee.List([1,2,3,4,5,6])
var list2 = list1.remove(2)
var list3 = list1.removeAll([3,4])
print(list1, list2, list3)
4.1比较 list.equals()&list.contains()&list.containsALL()
var list1 = ee.List(['zhao','qian','sun','li'])
var list2 = ee.List(['zhao','qian','li','sun'])
var list3 = ee.List(['zhao','qian','li'])
var bool1 = list1.equals(list2)
var bool2 = list1.contains('li')
var bool3 = list1.containsAll(list3)
print(bool1, bool2, bool3)
4.2 查询位置list.indexOf()
var list_num = ee.List([1,2,3,4,5,6,4,5,6,5,5,5])
var index_num = list_num.indexOf(5)
var index_sub = list_num.indexOfSublist([4,5,6])
var index_last_sub = list_num.lastIndexOfSubList([4,5,6])
var freq_num = list_num.frequency(5)
print(index_num, index_sub, index_last_sub, freq_num)
5.2 迭代循环 list.iterate()
var list1= ee.List.sequence(1,100,1)
function Do(num1, num2){
return ee.Number(num1).add(num2)
}
var list2 = list1.iterate(Do, 0)
print(list1, list2)
5.3 列表元素数量 list.length()& list.size()
var list1= ee.List([[1,2],[3,4],[5,6],[7,8]])
var list_length = list1.length()
var list_size = list1.size()
print(list_length, list_size)
三、Array 向量
1.创建向量ee.Array()
1.1创建矩阵 ee.Array()
var array_1 = ee.Array([[1],[2],[3]])
var array_2 = ee.Array([[1,2,3]])
print(array_1, array_2)
1.2 单位矩阵 ee.Array.identity(n)
var Array_1=ee.Array.identity(5);
print(Array_1)
1.3 矩阵重复 ee.Array.repeat()
var array_1 = ee.Array([[1,3],[2,4]])
var array_2 = array_1.repeat(1,3)
print(array_1, array_2)
2.修改矩阵
2.1 掩膜 array.mask()
var array_1 = ee.Array([[1,2],[2,3],[3,4]])
var array_2 = ee.Array([[0],[1],[0]])
var array_3 = array_1.mask(array_2)
print(array_1, array_3)
2.2 转置 array.transpose()
var array_1 = ee.Array([[1,2],[2,3],[3,4]])
var array_2 = array_1.transpose()
print(array_1, array_2)
2.3 参数转换array.unit8()
var array_1 = ee.Array([[1.1,2.2,3.3],[4.4,5.5,6.6]])
var array_2 = array_1.uint8()
print(array_1,array_2)
3.数据处理
3.1 比较
var array_1 = ee.Array([[1,1],[2,2],[3,3],[4,4]])
var array_2 = ee.Array([[1,2],[3,4],[4,3],[4,4]])
var array_3 = array_1.eq(array_2)
print(array_3)
3.2与非结构
var array_1 = ee.Array([[1,0],[0,0],[4,1]])
var array_2 = ee.Array([[1,2],[0,1],[0,4]])
var array_and = array_1.and(array_2)
var array_or = array_1.or(array_2)
print(array_and,array_or)
3.3 数值运算
3.4 位运算与或非
var array_1 = ee.Array([3,2])
var array_2 = ee.Array([3,3])
var array_bit_and = array_1.bitwiseAnd(array_2)
var array_bit_or = array_1.bitwise_or(array_2)
print('Bitwise[11,10] and [11,11] =', array_bit_and)
print('Bitwise[11,10] or [11,11] =', array_bit_or)
3.5位运算平移
var array_1 = ee.Array([1,2,3])
var array_left = array_1.leftShift([1,1,1])
var array_right = array_1.rightShift([1,1,1])
print("[01,10,11]左移为[10,100,110]=",array_left)
print("[01,10,11]you右移动[00,01,01]=",array_right)