Google Earth Engine(GEE) 03-矢量数据类型

Google Earth Engine(GEE) 03-矢量数据类型(GEE基本语法、Geometry, Dictionary, Feature和FeatureCollection之间的关系、Dictionary、Feature—常用函数、FeatureCollection—常用函数、实例1— 陆丝经济带与成渝经济圈、实例2— 一带一路沿线国家与城市、实例3— 影像可用性分析条带号(研究区的图幅范围)、实例4— 研究区分块运算原理(解决算力不足的问题))

代码:(直接搜下面的title可以直接定位到代码的位置)

Dictionary实例1:(报错实例)注意添加地图显示的时候,需要是GEE的object对象,因此需要强制转换一下类型
Dictionary实例2:字典的相关方法
Feature—常用函数实例
FeatureCollection—常用函数实例
实例1— 陆丝经济带与成渝经济圈
实例2— 一带一路沿线国家与城市
实例3— 影像可用性分析条带号(研究区的图幅范围)
实例4— 研究区分块运算原理(解决算力不足的问题)

这里需要特别说明,Google Earth Engine中引入的很多数据地图存在大量争议。

基本内容:

Geometry
Dictionary
Feature
FeatureCollection

GEE基本语法

var GEEStr = ee.String(‘Hello, GEE’);
var GEENum = ee.Number(42);
var GEEList = ee.List([0,1,2,3]);
var GEEDictionary = ee.Dictionary({
‘city’ : ‘Urumqi’,
‘code’ : ‘520’,
‘provinces’ : ‘Xinjiang’
});
function reflect(element){
return element;
}

//js变量
var city = 'Urumqi';
var code = 830000;
print(typeof city);
print(typeof code);

// GEE变量
var EEcity = ee.String('Urumqi');
var EEcode = ee.Number(830000);
print(typeof EEcity);
print(typeof EEcode);

GEE所有的数据在服务器端都是以Object运行:
在这里插入图片描述

Geometry, Dictionary, Feature和FeatureCollection之间的关系

矢量数据的承载都是ee.Feature来承载的,由点、线、面、多边形构成。
ee.Feature() = ee.Geometry.Point() + ee.Dictionary()
ee.Feature() = 空间特征 + 属性特征

空间特征是由ee.Geometry记录的。
属性信息是由ee.Dictionary记录的(例如:颜色、长度、大小、面积等)。

多个ee.Feature()就构成了ee.FeatureCollection()
ee.FeatureCollection() <= { Point
Line
Area
Polygon

如下图所示其相互关系:
在这里插入图片描述
实例:https://code.earthengine.google.com/7b8d61dab1eace0b9cf9bbbff3ce0d65

var Point = /* color: #d63000 */ee.Geometry.Point([104.06582705152093, 30.657468080942614]),
    
    Line = /* color: #0787ff */ee.Geometry.LineString(
        [[104.0646683372265, 30.657984919106863],
         [104.06471125257073, 30.656914322699983]]),

    Polygon = /* color: #0b4a8b */ee.Geometry.Polygon(
        [[[104.06586996686516, 30.656397478811318],
          [104.0659128822094, 30.652668737448874],
          [104.06900278699456, 30.65248414255618],
          [104.071062723518, 30.656471313821758]]]),
    
    Retangle = ee.Geometry.Polygon(
        [[[104.06059137952386, 30.65828025395996],
          [104.06059137952386, 30.656249808621276],
          [104.06423918378411, 30.656249808621276],
          [104.06423918378411, 30.65828025395996]]], null, false);
//设置点
var Feature_P = ee.Feature(Point).set({"color" :'red'}); 
print(Feature_P)
//
var Feature_L = ee.Feature(Line).set({"color" :'blue'});
print(Feature_L)

var Feature_Po = ee.Feature(Polygon).set({"color" :'red'});
print(Feature_Po)

var Feature_R = ee.Feature(Retangle).set({"color" :'yellow'});
print(Feature_R)

var FeatureColl = ee.FeatureCollection([Feature_P,Feature_L,Feature_Po,Feature_R]);
print(FeatureColl)

在这里插入图片描述

Dictionary

这里的字典,其实就是对矢量数据的属性信息进行操作。

Keys => Values

与字典相关的常用函数:
.toDictionary() 将矢量数据的属性信息取出
.get() 获取某个属性信息
.keys() 获取属性信息的名称
.values() 获取属性信息的值
.size() 获得这些属性信息的个数
.rename() 对这些属性信息进行重命名
.remove() 移除某个属性信息
.contain() 是否包含某个属性信息

https://code.earthengine.google.com/066053818a1dc31310412e9894572b03

Dictionary实例1:(报错实例)注意添加地图显示的时候,需要是GEE的object对象,因此需要强制转换一下类型。

var china_city = ee.FeatureCollection("users/geersl9502/china_city");

//选择成都市
var CD = china_city.filter(ee.Filter.eq('city','chengdu')).first();
print("CD:",CD);
print(typeof CD)
Map.centerObject(CD,8)
//这里会报错,因为Map.addLayer() Cannot add an object of type <Element>
Map.addLayer(CD) 

//因此需要强制转换一下类型,下面的ee.Feature(CD)可以强制转为GEE的对象Object类型,这样才能将矢量真确添加到地图上。
// CD = ee.Feature(CD)
// print(typeof CD)
// Map.centerObject(CD,8)
// Map.addLayer(CD)

在这里插入图片描述
强制转换类型后正常显示了:

var china_city = ee.FeatureCollection("users/geersl9502/china_city");

//选择成都市
var CD = china_city.filter(ee.Filter.eq('city','chengdu')).first();

//因此需要强制转换一下类型:
CD = ee.Feature(CD)

//可以正常显示添加矢量到地图中显示了
Map.centerObject(CD,8)
Map.addLayer(CD)

在这里插入图片描述

Dictionary实例2:字典的相关方法

var china_city = ee.FeatureCollection("users/geersl9502/china_city");

//选择成都市
var CD = china_city.filter(ee.Filter.eq('city','chengdu')).first();
//因此需要强制转换一下类型:
CD = ee.Feature(CD)
//可以正常显示添加矢量到地图中显示了
Map.centerObject(CD,8)
Map.addLayer(CD)

//将属性全部取出
var CD_dic = CD.toDictionary();
print("CD_dic:",CD_dic);

//获取对应需要获取内容city的值
var dic_city = CD_dic.get('city');
print("dic_city:",dic_city);

//获取键
var dic_keys = CD_dic.keys();
print("dic_keys:",dic_keys);

//获取值
var dic_values = CD_dic.values();
print("dic_values:",dic_values);

//获取属性长度
var dic_size = CD_dic.size();
print("dic_size:",dic_size);

//重命名属性名
var dic_rename = CD_dic.rename(['provinces'],['code']);
print("dic_rename:",dic_rename);

//移除某个属性
var dic_remove = CD_dic.remove(['c_code']);
print("dic_remove:",dic_remove);

//判断是否包含c_code这个属性?
//这里是针对CD_dic
var dic_contains = CD_dic.contains('c_code');
print("dic_contains:",dic_contains);
//这里是针对dic_remove
var dic_contains2 = dic_remove.contains('c_code');
print("dic_contains2:",dic_contains2);

在这里插入图片描述

Feature—常用函数

1、创建 ee.Feature() 绘图工具

2、编辑 .select()选择 .set()设置

3、几何 .centroid()获取中心点 / .simplify()内接多边形 / .bounds()外接矩形 / .convexHull()外接多边形 / .buffer()缓冲区
.union()合并 / .intersection()相交 / .difference()相差 / .distance()距离

4、提取 .geometry()获取空间属性 / .get()获得某个属性的值 / .area()获取面积 / .perimeter()获取周长

https://code.earthengine.google.com/1d25b00c9e8a90306ce12fa3975921a8

Feature—常用函数实例:

//1、添加数据
var table = ee.FeatureCollection("users/geersl9502/china_city"),
    geometry = /* color: #d63000 */ee.Geometry.MultiPoint();

//2、选取研究区,并且feature类型强制转换    
// 定义成都,北京和四川省为研究区
//ee.Filter.eq(name, value)
//这里的table是china_city矢量数据
var cd = table.filter(ee.Filter.eq('city','chengdu')).first();
var bj = table.filter(ee.Filter.eq('city','beijing')).first();
var sichuan = table.filter(ee.Filter.eq('provinces','sichuan'));
print("chengdu:",cd)
//将cd和bj强制转换成ee.Feature()
cd = ee.Feature(cd);
bj = ee.Feature(bj);
print("chengdu:",cd)

//3、定位feature,并添加显示出来
//显示成都所在范围
//定位到以cd为中心
Map.centerObject(cd);
//添加地图,{}这个里面应该填显示方式,这里我们没有填写。
//参数4表示是否加载后默认显示出来,true显示,false不显示。
//参数5表示透明度
Map.addLayer(cd,{},"chengdu",true,0.5);

//4、计算面积、周长、并添加到属性信息中。
//计算面积和周长,并将其添加在属性信息里面
var area = cd.area().divide(1000000);//这里除以100万,将平方m转为平方km
var perimeter = cd.perimeter().divide(1000);//这里除以1千,将m转为km
//设置面积和周长,到cd属性中
cd = cd.set({'area':area,'perimeter':perimeter});
print("cd_area:",cd)

//5、显示中心点,这里只显示成都的中心点
var cdcenterpoint = cd.centroid();
var bjcenterpoint = bj.centroid();
Map.addLayer(cdcenterpoint,{color:'red'},'cdcenterpoint');

//6、内接和外接多边形
//内接多边形,内接简化多边形
var simplify = cd.simplify(10000)
Map.addLayer(simplify,{color:'red'},'simplify');
//外接多边形
var convexHull = cd.convexHull(10000)
Map.addLayer(convexHull,{color:'black'},'convexHull');

//7、外接矩形(这个的应用是比较多的)
var bounds = cd.bounds()
Map.addLayer(bounds,{color:'blue'},'bounds');

//8、缓冲区(这个的应用是比较多的)
//往外缓存区范围10000米
var buffer = cd.buffer(10000);
Map.addLayer(buffer,{color:'red'},'buffer');
//往内缓存区范围10000米
var buffer = cd.buffer(-10000);
Map.addLayer(buffer,{color:'red'},'buffer');
//这个缓冲区可以对任何feature使用,包括上面的外接矩形
//这里我们可以注意到这个矩形是一个圆角矩形,如果我们想获得直角矩形,那就在bounds()一下,获得这个圆角矩形的外接矩形
//因为我们在做卷积运算的时候,需要考虑边缘的情况,需要往外边缘扩展一些距离
var buffer = bounds.buffer(10000).bounds();
Map.addLayer(buffer,{color:'red'},'buffer');

//9、合并四川所有市
var union = sichuan.union()
Map.addLayer(sichuan,{color:'red'},'sichuan');
Map.addLayer(union,{color:'black'},'union');

//10、距离计算
//cd中心到bj中心之间的表面距离
var distance = cdcenterpoint.distance(bjcenterpoint).divide(1000); //换算成km
print('distance:',distance)

在这里插入图片描述

FeatureCollection—常用函数

1、创建: 绘图工具可以创建FeatureCollection / ee.FeatureCollection()直接定义FeatureCollection / .randomPoints()随机生成的点
2、编辑: .filterBounds()编辑交叉的矢量范围 / .select()选择某一个矢量 / .union()合并内部所有矢量 / .merge()与另一个FeatureCollection合并 / .remap()重分类 / .sort()排序
3、转换: .geometry()变成空间属性 / .reduceToimage()矢量转栅格
4、循环: .map(function)循环
5、统计: .aggregate_min计算最小值 / .aggregate_max计算最大值 / .aggregate_mean计算均值 / .aggregate_first第一个值 / .aggregate_sum计算求和 / .aggregate_histogram计算直方图

https://code.earthengine.google.com/2622a0a9603f0a68e29ad867e2041940
FeatureCollection—常用函数实例:

//1、导入数据
var table = ee.FeatureCollection("users/geersl9502/china_city"),
    geometry = /* color: #d63000 */ee.Geometry.MultiPoint();
    
//2、通过字段名筛选出研究区
var cd = table.filter(ee.Filter.eq('city','chengdu'));
var sichuan = table.filter(ee.Filter.eq('provinces','sichuan'));
//这里并没有强制类型转换

//3、对cd范围内随机生成100个点
var randomP = ee.FeatureCollection.randomPoints(cd,100);
// 显示结果,显示的是成都区域范围
Map.centerObject(cd,8);
Map.addLayer(cd.geometry(),{},'chengdu',false);
Map.addLayer(randomP,{color:'blue'},'randomP',false);

//4、获取四川每个县市的面积
// 对四川省进行map循环,得到每个城市的面积
sichuan = sichuan.map(
		function(city){
			//city面积转为平方km后取整
  			var area = city.area().divide(1000000).toInt();
  			//并在city属性中新建一个属性area
  			return city.set({'area(km2)':area});
		}
	).sort('area(km2)'); //这里依据city面积排序
print('sichuan:',sichuan)

//5、统计操作
//对四川每个城市的面积进行统计分析
var area_min = sichuan.aggregate_min('area(km2)');
var area_max = sichuan.aggregate_max('area(km2)');
var area_mean = sichuan.aggregate_mean('area(km2)');
var area_first = sichuan.aggregate_first('area(km2)');
var area_sum = sichuan.aggregate_sum('area(km2)');
var area_histogram = sichuan.aggregate_histogram('area(km2)');
print("area_min:",area_min);
print("area_max:",area_max)
print("area_mean:",area_mean)
print("area_first:",area_first)
print("area_sum:",area_sum)
print("area_histogram:",area_histogram)

//6、根据sichuan面积属性,将矢量转栅格,并显示出来
// 利用面积属性,将矢量转成栅格,每个栅格的像素值是这个县市的面积大小
// 这里reduceToImage的参数2表示:reducer (Reducer): A Reducer to combine the properties of each intersecting feature into a final result to store in the pixel.将每个相交特征的属性组合成一个最终结果存储在像素中。
var sichuan_img = sichuan.reduceToImage(['area(km2)'],ee.Reducer.first())
                         .rename('area').toInt();
print('sichuan_img:',sichuan_img)
//21个城市定义了21个颜色
//面积最小值、面积最大值
var palette = {
    min:4387,
    max:149761,
  palette: ['05450a', '086a10', '54a708', '78d203', '009900', 'c6b044', 'dcd159',
    'dade48', 'fbff13', 'b6ff05', '27ff87', 'c24f44', 'a5a5a5', 'ff6d4c',
    '69fff8', 'f9ffa4', '1c0dff','#d63000', '#bbd605', '#0bd603', '#06d6be'],
};
//将这个img显示出来
Map.addLayer(sichuan_img,palette,'sichuan_img');

//7、重分类
//这里是对土地利用分类进行重分类
// 利用remap进行重分类
//使用这个数据'MODIS/006/MCD12Q1',该数据的'LC_Type1'的分类,一共17类,每一类代表类型不同。
//通过.clip()裁切出研究区域
var MCD12Q1 = ee.ImageCollection('MODIS/006/MCD12Q1').first().select('LC_Type1').clip(sichuan);
//我们是将17类重新定义成2类,一类是植被,另一类是非植被。
//ee.List.sequence(start, end, step, count)
var List = ee.List.sequence(1,17); //这个代表原数据的17类
var newList = ee.List([0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1]); //这个代表新的分类
//最小值1,最大值17,定义17个颜色
var igbpLandCoverVis = {
  min: 1.0, 
  max: 17.0,
  palette: [
    '05450a', '086a10', '54a708', '78d203', '009900', 'c6b044', 'dcd159',
    'dade48', 'fbff13', 'b6ff05', '27ff87', 'c24f44', 'a5a5a5', 'ff6d4c',
    '69fff8', 'f9ffa4', '1c0dff'
  ],
};
//最小值0,最大值1,定义2个颜色
var igbpLandCoverVis2 = {
  min: 0,
  max: 1,
  palette: ['#d63000','#d6d408'],
};
//对MCD12Q1进行remap
var MCD12Q1_remap = MCD12Q1.remap(List,newList);
//原始情况和重分类情况
Map.addLayer(MCD12Q1,igbpLandCoverVis,'MCD12Q1');
Map.addLayer(MCD12Q1_remap,igbpLandCoverVis2,'MCD12Q1_remap',true,0.8)

在这里插入图片描述
在这里插入图片描述

实例1— 陆丝经济带与成渝经济圈

https://code.earthengine.google.com/e2a430e48e1227537589e6b8c92a6049
在这里插入图片描述

//1、导入数据
var china_city = ee.FeatureCollection("users/geersl9502/china_city"),
    china = ee.FeatureCollection("users/geersl9502/china");
//2、查看一下导入的数据
Map.centerObject(china_city,4);
Map.addLayer(china_city)
print(china_city.limit(2))

//3、丝绸之路经济圈
//通过导入的数据选择出研究区,并将研究区合并,和添加新字段
//新丝绸之路经济带(陆丝)
var theBeltandRoad = china_city.filter(ee.Filter.eq('city','xian')).first().geometry()
            .union(china_city.filter(ee.Filter.eq('city','lanzhou')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','xining')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','chongqing')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','chengdu')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','zhenzhou')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','wuhan')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','changsha')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','nancang')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','hefei')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','wulumuqi')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','lianyungang')).first().geometry());
//强制类型转换,并且设置新的属性字段
theBeltandRoad = ee.Feature(theBeltandRoad).set({'范围': "新丝绸之路经济带(陆丝)"});
print('theBeltandRoad :',theBeltandRoad)
Map.addLayer(theBeltandRoad,{color:'red'},"theBeltandRoad");

//4、同样的对成渝经济圈进行选择
//成渝经济圈中心城city
var chengyu = china_city.filter(ee.Filter.eq('city','chengdu')).first().geometry()
            .union(china_city.filter(ee.Filter.eq('city','chongqing')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','mianyang')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','deyang')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','leshan')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','yibin')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','luzhou')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','nanchong')).first().geometry())
            .union(china_city.filter(ee.Filter.eq('city','dazhou')).first().geometry());
chengyu = ee.Feature(chengyu).set({'范围': "成渝经济圈中心城市"});
print('chengyu :',chengyu )
Map.addLayer(chengyu,{color:'blue'},"chengyu",true,0.6);

//5、相交、联合、相差处理
//既是新丝绸之路经济带,又是成渝经济圈的城市有哪些:相交后进行显示
var Intersection = theBeltandRoad.geometry().intersection(chengyu.geometry())
Map.addLayer(Intersection,{color:'green'},"Intersection",true,0.9);

//这个是做Union联合
var Union = theBeltandRoad.geometry().union(chengyu.geometry())
Map.addLayer(Union,{color:'yellow'},"Union");

//这个是做相差Difference
var Difference = theBeltandRoad.geometry().difference(chengyu.geometry())
Map.addLayer(Difference,{color:'orange'},"Difference");

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实例2— 一带一路沿线国家与城市

https://code.earthengine.google.com/ff69b8b0e98e03417bdd55e733a0759c
在这里插入图片描述

//1、导入数据
//全球矢量范围、丝绸之路路线、中国城市
var word = ee.FeatureCollection("users/geersl9502/world"),
    theBeltandRoad_line = ee.FeatureCollection("users/geersl9502/theBeltandRoad_line"),
    china_city = ee.FeatureCollection("users/geersl9502/china_city");

//2、定义三个不同的显示风格,都是中心镂空,边缘不同颜色
var styling1 = {fillColor:'00000000',color:'red'};
var styling2 = {fillColor:'00000000',color:'blue'};
var styling3 = {fillColor:'00000000',color:'yellow'};

//3、一带一路沿线及其经过国家
//通过.filterBounds(),获得word与theBeltandRoad_line相交的区域,实质就取交集
var theBeltandRoad_country = word.filterBounds(theBeltandRoad_line);
Map.addLayer(word,{},'word')
Map.addLayer(theBeltandRoad_line.style(styling1),{},'theBeltandRoad_line')
Map.addLayer(theBeltandRoad_country.style(styling2),{},'theBeltandRoad_country')

//4、一带一路沿线经过的中国城市
//依然是使用.filterBounds()这个函数
var theBeltandRoad_city = china_city.filterBounds(theBeltandRoad_line);
Map.addLayer(china_city,{},"china_city");
Map.addLayer(theBeltandRoad_city.style(styling3),{},'theBeltandRoad_city')

在这里插入图片描述

实例3— 影像可用性分析条带号(研究区的图幅范围)

https://code.earthengine.google.com/e6ff434039350f533366ca29b34cfad9
在这里插入图片描述

//1、加载一个中国省份的数据和条代号的数据
var table = ee.FeatureCollection("users/cduthes1991/boundry/China_province_2019"),
    PathRow = ee.FeatureCollection("users/cduthes1991/PathAndRow/LandsatPathRow");

//2、通过筛选,选择出广东省的矢量范围
// set study area
var roi = table.filter(ee.Filter.eq('provinces','guangdong'))

//3、利用位运算进行研磨,定义去云函数
// reomove cloud for Landsat-8
function rmL8Cloud(image) { 
  //<<表示二进制左移几位
  //二进制第三位表示有云阴影,第五位表示有云。
  var cloudShadowBitMask = (1 << 3); 
  var cloudsBitMask = (1 << 5); 
  // 选择质量评估波段
  var qa = image.select('pixel_qa'); 
  // 按位与运算,将检测出云以及云阴影像元值为0
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) 
                 .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image.updateMask(mask).toFloat().divide(10000)
              .copyProperties(image)
              .copyProperties(image, ['system:time_start','system:time_end','system:footprint']);
}

//4、对影像进行去云操作
// set time parameter
var year_start = 2018;
var year_end = 2020;
//设置时间参数,并通过roi控件过滤,时间过滤,循环操作去云函数
var imgCol = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
              .filterBounds(roi)
              .filter(ee.Filter.calendarRange(year_start, year_end,'year'))
              .map(rmL8Cloud);

//5、计算一下一共用了多少图幅
// compute the image total size
var imgSize = imgCol.size();
print("imgSize",imgSize); // result is 805

//6、选择波段并统计可用像素数量
//compute the image avaibility
var imgCount = imgCol.select("B1").count().clip(roi);

//7、定义渲染参数,并显示出来
// define image render parameters
var visParam = {
 min: 10,
 max: 40,
 palette: 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
   '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'
};
Map.addLayer(imgCount,visParam,"imgCount",false);

//方法1:这里8-10就是通过空间相交的区域,以属性连接的方式,获取研究区的条带号:
//8、实例化一个空间筛选器,该筛选器是通过左后空间坐标进行筛选,其误差最大为10
// 定义空间筛选:利用边界进行叠加筛选.即空间相交
var spatialFilter = ee.Filter.intersects({
leftField: '.geo',
rightField: '.geo',
maxError: 10
});

//9、实例化SaveAll-Join.属性连接
//返回将第一个集合中的每个元素与第二个集合中的一组匹配元素配对的连接。
//匹配列表作为附加属性添加到每个结果中。
var saveAllJoin = ee.Join.saveAll({
matchesKey: 'scenes',
});

//10、运用SaveAll-Join.属性连接,连接的是感兴趣区广东与条带号,连接的方式spatialFilter空间相交 
var intersectJoined = saveAllJoin.apply(roi, PathRow, spatialFilter);
// 显示结果.就是研究区所需要的条带号结果
var intersected = ee.FeatureCollection(ee.List(intersectJoined.first().get('scenes')));

//方法2:(简单明了)一步就可以实现8-10步的内容:
//11、利用filterBounds,即条带号与感兴趣区交叉区域
// var intersected = PathRow.filterBounds(roi);

//12、显示出研究区和研究区对应的条带区域
var styling = {color:'red',fillColor:'00000000'}
var styling1 = {color:'black',fillColor:'00000000'}
//以roi为显示的中心
Map.centerObject(roi);
//显示研究区对应的条带区域
Map.addLayer(intersected.style(styling1), {}, 'WRS-2 polygons',false);
//显示研究区
Map.addLayer(roi.style(styling),{}, 'guangdong',false);

//13、导出
Export.image.toDrive({
  image:imgCount,
  description:'avaibility_Landsat',
  folder:'avaibility_Landsat',
  region:roi,
  scale:30,
  crs:'EPSG:4326',
  maxPixels:1e13
});
Export.table.toDrive({
  collection:intersected,
  description:'PathRow',
  folder:'avaibility_Landsat'
})

在这里插入图片描述

实例4— 研究区分块运算原理(解决算力不足的问题)

由于云平台分配给用户的算力有限,因此我们划分每一块来进行计算,这样就可以解决算力不足的问题。
https://code.earthengine.google.com/be255b6c46621aad6c2e1c174456615b
在这里插入图片描述

//1、导入筛选出研究区
var table = ee.FeatureCollection("users/geersl9502/china_province");
var roi = table.filter(ee.Filter.eq('provinces','sichuan'));

//2、获取四川省的最小外接矩形
var bounds = roi.geometry().bounds();

//3、获取最小外接矩形的顶点坐标,这里一共是5个,即先从左下、左上、右上、右下、最后在回到左下,这样一共五个。
//这里需要特别注意一下,.coordinates()获取的是两层的list,如果要获取坐标,需要到list的第二层。
var lonlat = bounds.coordinates()
print(lonlat)

var styling = {color:'red',fillColor:'00000000'}

// Map.addLayer(bounds)

//4、定义一个生成格网的函数:
// 参数是:x轴最小值、y轴最小值、x轴最大值、y轴最大值、x轴划分的最小单元、y轴划分的最小单元
var generateGrid = function(xmin, ymin, xmax, ymax, dx, dy) {
  //定义x列表,从x轴最小开始,到x轴最大结束,步长是x轴划分的最小单元
  var xx = ee.List.sequence(xmin, ee.Number(xmax).subtract(0.0001), dx);
  //定义y列表,从x轴最小开始,到x轴最大结束,步长是x轴划分的最小单元
  var yy = ee.List.sequence(ymin, ee.Number(ymax).subtract(0.0001), dy);
  //双循环
  var cells = xx.map(function(x) {
    return yy.map(function(y) {
      var x1 = ee.Number(x);
      var x2 = ee.Number(x).add(ee.Number(dx));
      var y1 = ee.Number(y);
      var y2 = ee.Number(y).add(ee.Number(dy));
      var coords = ee.List([x1, y1, x2, y2]);
      //生成矩形,传入的是列个列表,这个列表包含的是左下角和右上角生成矩形的坐标
      var rect = ee.Algorithms.GeometryConstructors.Rectangle(coords);   
      return ee.Feature(rect);
    });
  }).flatten(); //这里打平 
  
  //该generateGrid函数的返回值:
  return ee.FeatureCollection(cells);
};

//5、设置generateGrid函数所需要的参数
//获取四川省的最小外接矩形
var bounds = roi.geometry().bounds();
//获取外接矩形的四个顶点坐标,并赋予x轴的最小值和最大值,以及y轴的最小值和最大值
//.get(0)的作用是获取bounds.coordinates()返回list的第二层。因为bounds.coordinates()返回的list有两层。
var coords = ee.List(bounds.coordinates().get(0));
var xmin = ee.List(coords.get(0)).get(0);
var ymin = ee.List(coords.get(0)).get(1);
var xmax = ee.List(coords.get(2)).get(0);
var ymax = ee.List(coords.get(2)).get(1);
//这是步长,x轴长度除以4,y轴长度除以4
var dx = (ee.Number(xmax).subtract(xmin)).divide(4);
var dy = (ee.Number(ymax).subtract(ymin)).divide(4);
//这里需要注意的是dx和dy可以按照1度来设置步长,这样划分也是可以的:
//var dx = 1
//var dy = 1

//6、将参数带入函数中,并获取对应的矩形。
var grid = generateGrid(xmin, ymin, xmax, ymax, dx, dy);

//7、通过.filterBounds(roi)只有与研究区相交的矩形,我们才保留,显示的时候只显示相交的矩形。
var grid = grid.filterBounds(roi); // filter out out-of-boundary tiles
print(grid.size()); //查看生成所有格网数量

//7、地图显示
Map.addLayer(grid, {color:'orange'}, 'grid');
Map.addLayer(roi.style(styling),{},'sichuan_boundary')
Map.centerObject(bounds,5)

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Henrik698

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值