GEE的数据类型第二讲:Dictionary,List,Array

***GEE的数据类型第二讲:Dictionary,List,Array

一、Dictionary 字典(关键词{身高}+内容{1.7米})

1.创建 ee.Dictiondary()

//创建时有ee.且D为大写字母
var Dictiondary_Profile= ee.Dictionary({
           Name:'Jan',
           Gender:'Male',
            Age:">20",
            Location:"Beijing"
})

print(Dictiondary_Profile)

2. 合并dictionary.combine()

//ee 是创建,dictionary是指所创建的字典文件名
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)  //发生冲突时,true表示第二个覆盖第一个、FALSE则相反
print(dict_comb)
//age: 26
//height: 160cm
//weight: 70kg

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)
//["age","wight"]

5.内容查询 dictionary.get()

var dict_1=ee.Dictionary({
        Name:'Jan',
        Wight: '70kg',
        Age:'>20'
          })
var dict_Name=dict_1.get('Name')
print(dict_Name) //Jan

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)
//["Jan","20"]

7.包含 dictionary.contains()

//判断是否包含关键字 true/false
var dict_1 = ee.Dictionary({
  weight:'50kg',
  height:'160cm'
})
var bool = dict_1.contains('name')
print(bool)
//false

8.大小 dictionary.size()

//是关键字的个数
var dict_1 = ee.Dictionary({
        Name:'Jan',
        Wight: '70kg',
        Age:'20'
})
var dict_size = dict_1.size()
print(dict_size)//3

二、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)
//[0,1,2,3,4,5,6,7,8,9]
//[0,3,6,9]

2.修改

2.1 改某一位的值 list.set ()

// (这里list是特定的列表)
var list1 = ee.List([1992,1,20,'hello'])
var list2 = list1.set(0,1993) //第一位为1993
var list3 = list2.set(-1, 'world')// 最后一位为'world'
print(list1, list2, list3)

//[1992,1,20,"hello"]
//[1993,1,20,"hello"]
//[1993,1,20,"world"]

2.2 替换 list.replace()

var list1 = ee.List([1992,1,20,'hello',1992,1,20,'hello'])
var list2 = list1.replace(1,5) //把1换成5
var list3 = list2.replaceAll(1,5)//把所有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)
//[1992,1,20,"hello"]
//[1992,1,20,"hello","earth"]
//[1992,1,20,"world","hello"]

2.4组合打包 list.zip()

//对应组成两个元素的列表,数量为两个列表最小数
var list1 = ee.List([1992,1,20,'NO1'])
var list2 = list1.zip(['Year','Month','Day'])
print(list1, list2)
//[1992,1,20,"NO1"]
//[[1992,"Year"],[1,"Month"],[20,"Day"]]

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)//指数值为5的放到首位,依次接后面顺序
print(list1, list2, list3)
//[0,1,2,3,4,5,6,7,8,9]
//[9,8,7,6,5,4,3,2,1,0]
//[5,6,7,8,9,0,1,2,3,4]

2.6 排序 list.sort()

//按首字母顺序排序
var list1 = ee.List(['Zhao','Wang','Ma','Liu','Bai','1','9'])
var list2 = list1.sort()
print(list1, list2)
//["Zhao","Wang","Ma","Liu","Bai","1","9"]
//["1","9","Bai","Liu","Ma","Wang","Zhao"]

2.7 交换 list.swap()

//交换的是两个位置的元素对调(位置从0 数)
var list1 = ee.List([1,2,3,4,5,6,7,8,9])
var list2 = list1.swap(5,8)
print(list1, list2)
//[1,2,3,4,5,6,7,8,9]
//[1,2,3,4,5,9,7,8,6]

2.8 展开 list.flatten()

//将多维数组展开为一维
var list1 = ee.List([[1,2,3,4],[5,6,7,8,9]])
var list2 = list1.flatten()
print(list1, list2)
//[[1,2,3,4],[5,6,7,8,9]]
//[1,2,3,4,5,6,7,8,9]

3、查询

3.1 查询 list.get()

//获得第n个位置的数
var list1 = ee.List(['hello','world','GEE'])
var list2 = list1.get(1)
print(list1, list2)
//["hello","world","GEE"]
//world

3.2 删除list.remove()&list.removeall()

var list1 = ee.List([1,2,3,4,5,6])
var list2 = list1.remove(2)//删除元素值为3
var list3 = list1.removeAll([3,4])//删除元素值为3和4
print(list1, list2, list3)
//[1,2,3,4,5,6]
//[1,3,4,5,6]
//[1,2,5,6]

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)
//false
//true
//true

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) //查找5在第几位-4
var index_sub = list_num.indexOfSublist([4,5,6])//查找456序列从第几位起
var index_last_sub = list_num.lastIndexOfSubList([4,5,6])//查找最后一组456序列从第几位起
var freq_num = list_num.frequency(5) //查找元素5 出现的次数
print(index_num, index_sub, index_last_sub, freq_num)

//4
//3
//6
//5

5.2 迭代循环 list.iterate()

var list1= ee.List.sequence(1,100,1)
//求1-100的和,将1和2加和后赋给0 ,再与下一个数相加
function Do(num1, num2){
  return ee.Number(num1).add(num2)
}
var list2 = list1.iterate(Do, 0)
print(list1, list2)

//5050

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)
//4
//4

三、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],[3]]
//0: [1]
//1: [2]
//2: [3]
//[[1,2,3]]
//0: [1,2,3]

1.2 单位矩阵 ee.Array.identity(n)

//n为单位矩阵的维数
var Array_1=ee.Array.identity(5);
print(Array_1)
//0: [1,0,0,0,0]
//1: [0,1,0,0,0]
//2: [0,0,1,0,0]
//3: [0,0,0,1,0]
//4: [0,0,0,0,1]

1.3 矩阵重复 ee.Array.repeat()

var array_1 = ee.Array([[1,3],[2,4]])//2*2
var array_2 = array_1.repeat(1,3)
//方向1为向右,2为向下,2*6
print(array_1, array_2)
//[[1,3],[2,4]]
//[[1,3,1,3,1,3],[2,4,2,4,2,4]]

2.修改矩阵

2.1 掩膜 array.mask()

var array_1 = ee.Array([[1,2],[2,3],[3,4]])
var array_2 = ee.Array([[0],[1],[0]])
//[0]掩膜没有矩阵,[1]有
var array_3 = array_1.mask(array_2)
print(array_1, array_3)
//[[1,2],[2,3],[3,4]]
//[[2,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)
//[[1,2],[2,3],[3,4]]
//[[1,2,3],[2,3,4]]

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()  
//8位无符号整型(uint6,uint32,unit64,long,float,double)
print(array_1,array_2)
//[[1.1,2.2,3.3],[4.4,5.5,6.6]]
//[[1,2,3],[4,6,7]]

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)
//[[1,0],[0,0],[0,1],[1,1]]
//相同为1 不同为0 
//.eq()   .neq()	.gt()	.gte()	.lt()	.lte()

3.2与非结构

var array_1 = ee.Array([[1,0],[0,0],[4,1]])
var array_2 = ee.Array([[1,2],[0,1],[0,4]])
//and都不为0则为1,or 有一个不为0 则为1
var array_and = array_1.and(array_2)
var array_or = array_1.or(array_2)
print(array_and,array_or)
//[[1,0],[0,0],[0,1]]
//[[1,1],[0,1],[1,1]]

3.3 数值运算

//.abs()	绝对值   
//.floor()	向下取整
//.round()	四舍五入
//.ceil()	向上取整
//.sqrt()	开方
//.exp()    .log()     .log10()

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)
//Bitwise[11,10] and [11,11] =[3,2]
//Bitwise[11,10] or [11,11] =[3,3]

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)

//[01,10,11]左移为[10,100,110]=[2,4,6]
//[01,10,11]you右移动[00,01,01]=[0,1,1]

你可能感兴趣的:(javascript,arcgis)