本次教程首先市选定我们合适的长时间序列影像,我们首先划定研究区,然后设定指定区域的NDVI计算这里用到的市mean合成的计算,设定我们的去云和可视化参数设定,我们定义一个佛如循环用来进行分别进行遍历,最后设定chart影像的格式,加载影像。我们这里需要用到一个函数:
split(regex)
按正则表达式拆分字符串,返回字符串列表。
方法参数:
- string(String)
要拆分的字符串。
- regex(String)
正则表达式,如果regex是空字符串,则将输入字符串拆分为单个字符。
返回值:String
当然这里我们还需要注意一点,当我们想要使用filterBounds来筛选我们所选择的影像时,我们首先要用一个函数,具体的转化的结果:
var geometry = pie.Geometry.Polygon([[ [ 120.64386203675974, 41.098996789722236 ], [ 120.57825490076209, 40.83858677068369 ], [ 120.91186251168273, 40.77854007464006 ], [ 120.97742944943161, 41.03986964756422 ], [ 120.64386203675974, 41.098996789722236 ] ]], null); var roi = pie.FeatureCollection(pie.Feature(geometry));
pie.Feature(geometry,properties)
通过Geometry构造Feature对象。
方法参数:
- feature(feature)
feature实例
- geometry(Geometry)
几何形体对象
- properties(Object)
属性信息JSON对象
返回值:Feature
pie.FeatureCollection()
通过矢量数据构造Feature集合。
方法参数:
- args(String|Geometry|Feature|List)
矢量数据路径或单个PIEGeometry对象或单个PIEFeature对象或PIEFeature对象Array
返回值:FeatureCollection
代码:
//第一步:划定研究区域 var geometry = pie.Geometry.Polygon([[ [ 120.64386203675974, 41.098996789722236 ], [ 120.57825490076209, 40.83858677068369 ], [ 120.91186251168273, 40.77854007464006 ], [ 120.97742944943161, 41.03986964756422 ], [ 120.64386203675974, 41.098996789722236 ] ]], null); var roi = pie.FeatureCollection(pie.Feature(geometry)); //在地图上显示研究区域 Map.addLayer(roi, { color: "ff0000ff", fillColor: "00000000" }, "分析区域"); Map.centerObject(geometry, 9); //第二步:筛选数据:调用Landsat8 TOA数据集,根据时间、空间、云量等筛选数据 var l8Col = pie.ImageCollection("LC08/01/T1") .filterBounds(geometry) .filterDate("2017-01-01", "2017-12-31") .filter(pie.Filter.lte('cloudCover', 5)); //查看符合筛选条件的影像个数 print("2017年Landsat影像集合", l8Col.size(), l8Col); //第三步:计算指定区域NDVI、NDVI均值并显示 //1、设置NDVI显示参数 var visParamNDVI = { min: -0.2, max: 0.8, palette: "FFFFFF, CE7E45, DF923D, F1B555, FCD163, " + "99B718, 74A901, 66A000, 529400, 3E8601, 207401," + " 056201, 004C00, 023B01, 012E01, 011D01, 011301" }; //2、定义指定区域NDVI计算方法 function imageNDVI(id) { var image = pie.Image(id); var b4 = image.select("B4"); var b5 = image.select("B5"); var ndvi = (b5.subtract(b4)).divide(b5.add(b4)); var qa = image.select("BQA"); var cloudMask = qa.bitwiseAnd(1 << 4).eq(0); ndvi = ndvi.updateMask(cloudMask); return ndvi; } //3、定义指定区域NDVI均值计算方法 function calcNDVIByRoi(ndvi, roi) { var result = ndvi.reduceRegion(pie.Reducer.mean(), roi, 300); return result; } //4、定义符合筛选条件的Landsat8 TOA数据ID集合 var imgIds = [ "LC08/01/T1/LC08_121031_20170101", "LC08/01/T1/LC08_121031_20170117", "LC08/01/T1/LC08_121031_20170202", "LC08/01/T1/LC08_121031_20170218", "LC08/01/T1/LC08_121031_20170306", "LC08/01/T1/LC08_121031_20170322", "LC08/01/T1/LC08_121031_20170407", "LC08/01/T1/LC08_121031_20170423", "LC08/01/T1/LC08_121031_20170509", "LC08/01/T1/LC08_121031_20170525", "LC08/01/T1/LC08_121031_20170610", "LC08/01/T1/LC08_121031_20170626", "LC08/01/T1/LC08_121031_20170712", "LC08/01/T1/LC08_121031_20170728", "LC08/01/T1/LC08_121031_20170813", "LC08/01/T1/LC08_121031_20170829", "LC08/01/T1/LC08_121031_20170914", "LC08/01/T1/LC08_121031_20170930", "LC08/01/T1/LC08_121031_20171101", "LC08/01/T1/LC08_121031_20171117", "LC08/01/T1/LC08_121031_20171203", "LC08/01/T1/LC08_121031_20171219" ]; //5、计算指定区域NDVI、NDVI均值并显示 var xSeries = []; var images = []; for (var i = 0; i < imgIds.length; i++) { var key = imgIds[i].split("_")[2]; var ndvi = imageNDVI(imgIds[i]).clip(geometry); Map.addLayer(ndvi, visParamNDVI, key); xSeries.push(key); var result = calcNDVIByRoi(ndvi, geometry); print(key + "的NDVI值为:", result); images.push(result); }; //第四步:绘制指定区域NDVI动态变化折线图 //1、配置折线图的样式 var line_options = { title: '锦州周边植被变化', legend: ['NDVI均值'], xAxisName: "日期", yAxisName: "植被指数", chartType: "line", smooth: true //是否平滑 }; //2、调用影像绘图的方法绘制锦州周边NDVI变化折线图 ChartImage(images, xSeries, line_options);
结果: