GEE(Google Earth Engine) 代码学习笔记一 快速入门
GEE 代码学习笔记一
(GEE 基于JavaScript语言和python语言,我记录的是JavaScript语言)
1.GEE 快速入门 quick start.
2.基本语句
- 简单输出
print('Hello world!');
- 输出影像的元数据
print(ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318'));
- 加载影像到地图上(将影像加载到地图上,便可可视化地理数据)
用Map.addLayer() 方法实现。
// 加载影像
var image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318');// Center the map on the image.
Map.centerObject(image, 9);// Display the image.
Map.addLayer(image);
Map.centerObject(),是一个放大或缩小的水平,数字越大,尺度越大(zoom in)。
还可以附加参数配置显示参数Map.addLayer图层(). 例如:
// Load the image from the archive.
var image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318');// Define visualization parameters in an object literal.
var vizParams = {bands: ['B5', 'B4', 'B3'], min: 5000, max: 15000, gamma: 1.3};// Center the map on the image and display.
Map.centerObject(image, 9);
Map.addLayer(image, vizParams, 'Landsat 8 false color');
- 用Map.addLayer()加载矢量数据
var counties = ee.FeatureCollection('TIGER/2016/Counties');
Map.addLayer(counties, {}, 'counties');
- 加载栅格数据
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1');
注意:e.Image()加载单幅栅格影像,ee.ImageCollection ()加载一定时间段的即连续的影像,后面会细细记录。
- 确定研究区域,当研究区是一个点时,比如站点
var point = ee.Geometry.Point(-122.262, 37.8719);
括号里内容是经纬度。
- 根据时间筛选数据
var start = ee.Date('2014-06-01');
var finish = ee.Date('2014-10-01');
- 综上,筛选满足自己需求的数据
var filteredCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1').filterBounds(point).filterDate(start, finish).sort('CLOUD_COVER', true);
.sort()的内容根据数据元数据写
ee.ImageCollection()加载的影像很多,可能速度慢。且影像集通过list存储在feature中,因此可以通过List()选择。
var first = filteredCollection.first();
以下操作将创建一个过滤器,使用它过滤FeatureCollection并显示结果。
// Load a feature collection.
var featureCollection = ee.FeatureCollection('TIGER/2016/States');// Filter the collection.
var filteredFC = featureCollection.filter(ee.Filter.eq('NAME', 'California'));// Display the collection.
Map.addLayer(filteredFC, {}, 'California');
- 波段运算
eg:计算NDVI
// This function gets NDVI from Landsat 5 imagery.
var getNDVI = function(image) {return image.normalizedDifference(['B4', 'B3']);
};// Load two Landsat 5 images, 20 years apart.
var image1 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_19900604');
var image2 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_20100611');// Compute NDVI from the scenes.
var ndvi1 = getNDVI(image1);
var ndvi2 = getNDVI(image2);// Compute the difference in NDVI.
var ndviDifference = ndvi2.subtract(ndvi1);
- 循环
GEE没有循环操作,GEE中用map()来循环数据集集合中的项。map()函数可应用于ImageCollection、FeatureCollection或List,并接受函数作为其参数。函数的参数是它映射到的集合的元素。这对于以相同的方式修改集合的每个元素非常有用。
以下代码将NDVI波段添加到ImageCollection中的每个图像
// This function gets NDVI from Landsat 8 imagery.
var addNDVI = function(image) {return image.addBands(image.normalizedDifference(['B5', 'B4']));
};// Load the Landsat 8 raw data, filter by location and date.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1').filterBounds(ee.Geometry.Point(-122.262, 37.8719)).filterDate('2014-06-01', '2014-10-01');// Map the function over the collection.
var ndviCollection = collection.map(addNDVI);
- FeatureCollection中添加新属性
一个常见任务是向FeatureCollection中的要素添加新属性(或“属性”或“字段”)。在以下示例中,新属性是一个包含两个现有属性的计算:
// This function creates a new property that is the sum of two existing properties.
var addField = function(feature) {var sum = ee.Number(feature.get('property1')).add(feature.get('property2'));return feature.set({'sum': sum});
};// Create a FeatureCollection from a list of Features.
var features = ee.FeatureCollection([ee.Feature(ee.Geometry.Point(-122.4536, 37.7403),{property1: 100, property2: 100}),ee.Feature(ee.Geometry.Point(-118.2294, 34.039),{property1: 200, property2: 300}),
]);// Map the function over the collection.
var featureCollection = features.map(addField);// Print a selected property of one Feature.
print(featureCollection.first().get('sum'));// Print the entire FeatureCollection.
print(featureCollection);
ee.number()属性值必须被识别为数字才能使用add(),collection的类型可以通过map()改,比如:
// This function returns the image centroid as a new Feature.
var getGeom = function(image) {return ee.Feature(image.geometry().centroid(), {foo: 1});
};// Load a Landsat 8 collection.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1').filterBounds(ee.Geometry.Point(-122.262, 37.8719)).filterDate('2014-06-01', '2014-10-01');// Map the function over the ImageCollection.
var featureCollection = ee.FeatureCollection(collection.map(getGeom));// Print the collection.
print(featureCollection);
- Reduce
Reduce是在地球引擎中,通过时间、空间、波段、阵列和其他数据结构聚合数据的方法。例如,要合成ImageCollection,请使用reduce()将集合中的图像减少为一个图像。
比如可以通过Reduce(),算得某个时间段的均值(我自己的理解)。
// Load a Landsat 8 collection.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1')// Filter by date and location..filterBounds(ee.Geometry.Point(-122.262, 37.8719)).filterDate('2014-01-01', '2014-12-31')// Sort by increasing cloudiness..sort('CLOUD_COVER');// Compute the median of each pixel for each band of the 5 least cloudy scenes.
var median = collection.limit(5).reduce(ee.Reducer.median());
Reduce()也是在由特征或特征集合定义的区域中获取图像统计信息的方法。假设任务是计算感兴趣区域内的平均像素值,请使用reduceRegion()。
- reduceRegion
计算感兴趣区域内的平均像素值
// Load and display a Landsat TOA image.
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318');
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], max: 0.3});// Create an arbitrary rectangle as a region and display it.
var region = ee.Geometry.Rectangle(-122.2806, 37.1209, -122.0554, 37.2413);
Map.addLayer(region);// Get a dictionary of means in the region. Keys are bandnames.
var mean = image.reduceRegion({reducer: ee.Reducer.mean(),geometry: region,scale: 30
});
- Masking(相当于质量控制,把质量不好的设为空值)
ee.Image图像中的每个像素都有一个值和mask,mask的范围为0(无数据)到1。mask后的像素(其中mask==0)被视为无数据。0<mask≤1的像素有一个值,但它是由数值计算的mask加权的。
可以使用mask使像素透明或将其从分析中排除。当mask值为零时,将mask像素。继续图像差异示例,使用遮罩显示差异间隔内NDVI增加和减少的区域:
// This function gets NDVI from Landsat 5 imagery.
var getNDVI = function(image) {return image.normalizedDifference(['B4', 'B3']);
};// Load two Landsat 5 images, 20 years apart.
var image1 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_19900604');
var image2 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_20100611');// Compute NDVI from the scenes.
var ndvi1 = getNDVI(image1);
var ndvi2 = getNDVI(image2);// Compute the difference in NDVI.
var ndviDifference = ndvi2.subtract(ndvi1);
// Load the land mask from the SRTM DEM.
var landMask = ee.Image('CGIAR/SRTM90_V4').mask();// Update the NDVI difference mask with the land mask.
var maskedDifference = ndviDifference.updateMask(landMask);// Display the masked result.
var vizParams = {min: -0.5, max: 0.5, palette: ['FF0000', 'FFFFFF', '0000FF']};
Map.setCenter(-122.2531, 37.6295, 9);
Map.addLayer(maskedDifference, vizParams, 'NDVI difference');
在本例中,请注意,NDVI差异的mask由具有updateMask()的陆地mask更新。这会将NDVI差异像素的mask设置为陆地mask,只要NDVI差异mask不为零。
mask对于从分析中排除数据也很有用。请考虑reduceRegion()一节中的示例。假设任务是计算加州圣克拉拉县的季节平均NDVI,不包括多云像素。下面的示例演示了多个概念:过滤、映射、减少和云mask的使用:
// This function gets NDVI from a Landsat 8 image.
var addNDVI = function(image) {return image.addBands(image.normalizedDifference(['B5', 'B4']));
};// This function masks cloudy pixels.
var cloudMask = function(image) {var clouds = ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud']);return image.updateMask(clouds.lt(10));
};// Load a Landsat collection, map the NDVI and cloud masking functions over it.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA').filterBounds(ee.Geometry.Point([-122.262, 37.8719])).filterDate('2014-03-01', '2014-05-31').map(addNDVI).map(cloudMask);// Reduce the collection to the mean of each pixel and display.
var meanImage = collection.reduce(ee.Reducer.mean());
var vizParams = {bands: ['B5_mean', 'B4_mean', 'B3_mean'], min: 0, max: 0.5};
Map.addLayer(meanImage, vizParams, 'mean');// Load a region in which to compute the mean and display it.
var counties = ee.FeatureCollection('TIGER/2016/Counties');
var santaClara = ee.Feature(counties.filter(ee.Filter.eq('NAME', 'Santa Clara')).first());
Map.addLayer(santaClara);// Get the mean of NDVI in the region.
var mean = meanImage.select(['nd_mean']).reduceRegion({reducer: ee.Reducer.mean(),geometry: santaClara.geometry(),scale: 30
});// Print mean NDVI for the region.
mean.get('nd_mean').evaluate(function(val){print('Santa Clara spring mean NDVI:', val);
});
春天好美噢~
这是我的wx公众号,要来看看不,似乎挺有趣哒
塞翁的读书笔记
GEE(Google Earth Engine) 代码学习笔记一 快速入门相关推荐
- GEE(Google Earth Engine) 最基础代码学习笔记二 —— JavaScript 语言
GEE(Google Earth Engine) 学习笔记二 Javascript 语言 1. 注释 print('Hello World!'); 如果要注释,则在代码前面加//,比如: // pri ...
- Google Earth Engine(GEE)最基础代码学习笔记6——计算坡度坡向
1.Google Earth Engine 计算坡度 计算坡度坡向使用ee.Terrain包计算. // 加载 SRTM 影像. var srtm = ee.Image('CGIAR/SRTM90_V ...
- GEE(Google earth engine)中的Landsat影像的选择和去云(附代码)
1.获取校正过的Landsat 影像 在这里可以看到GEE提供的全部Landsat数据:Landsat Collections in Earth Engine | Earth Engine Dat ...
- 学习笔记:快速入门ZooKeeper技术
学习视频:黑马程序员 ZooKeeper 视频教程,快速入门 ZooKeeper 技术 学习资料:黑马程序员 公众号提供的文档资料链接 | 提取码:dor4) 本文最后更新于 2022-04-25,若 ...
- stm32单片机c语言入门 pdf,STM32学习笔记(初学者快速入门).pdf
STM32 学习笔记 从51 开始,单片机玩了很长时间了,有51,PIC,AVR 等等,早就想跟潮 流玩玩ARM ,但一直没有开始,原因不知道玩了ARM 可以做什么(对我自 己而言).如果为学习而学习 ...
- mybatis学习笔记——mybatis-plus快速入门
一.快速入门 MyBatis-plus (简称mp)是一款 Mybatis 增强工具,用来简化开发.增强效率.本文结合Spring Boot来实现mp的快速入门. 注:本文演示mp版本为当前最新的3. ...
- GEE (Google Earth Engine)高阶学习一 影像分割
影像分割就是把影像分成若干个特定.具有独特性质区域的技术和过程,是面向对象分类的重要基础. 下面介绍几种GEE中自带的影像分割的算法.具体的算法原理,在这里不多介绍,主要还是展示算法的应用. 使用例子 ...
- GEE(Google Earth Engine)学习——常用筛选器Filter操作
目录 一.筛选器Filter (1)关系比较型筛选器 (2)差值筛选器 (3)字符筛选器 (4)详细时间筛选 (5)筛选器叠加筛选 二.Join配合Filter进行两数据集联合筛选 (1)仅保留左侧数 ...
- google earth engine随缘学习(六)通过格网导出图像
GEE在栅格图像导出方面,限制了导出像素的最大数量.因此如果研究区域很大或者对分辨率要求比较高,就不能一次导出整个栅格图像,而需要拆成多部分单个导出,但是在gee上拆分矢量数据非常的不方便. 可以通过 ...
最新文章
- nero结果,对应的分析。如下。
- 自动驾驶定位技术-粒子滤波实践
- python【数据结构与算法】PriorityQueue and Huffuman树
- 大数据的逆袭:传统数据库市场的变革
- 用JAVA SOCKET编程,读服务器几个字符,再写入本地显示
- Spring面试,IoC和AOP的理解
- 初探 performance – 监控网页与程序性能
- 软件测试视频教程下载:APP测试类型和方法
- phpcms v9模板制作教程
- python 对比文件内容差异_Python-文件差异对比
- react项目 上线配置流程
- 论window和Linux之长短
- Nebula Graph - 全文索引
- 9GAG客户端,五一3天尽心之作,Just Android Design!(开源)+毛玻璃效果
- populate auto detected configs
- 3dsmax-uv展开
- windows linux 共享鼠标,在Ubuntu/Windows下配置Synergy-键盘鼠标共享
- [译] APT分析报告:01.Linux系统下针对性的APT攻击概述
- UltraEdit mac版
- 计算机毕业设计springboot+uniapp点餐外卖系统源码