Day 2

本文仅仅是将GEE官网上的代码整理一下,一方面自学,另一方面自我监督。文中有些问题是我想不通的,还需经历时间来理解。 

1 图像选择-image collection

1.1 clipped composite 图像裁剪

官网上给的代码截图:

clipped composite

逐行分析代码:

// Composite an image collection and clip it to a boundary.##此为案例目的,组合图像并裁剪至某范围。

// Load Landsat 7 raw imagery and filter it to April-July 2000.#此为加载影像的原始数据,时间限制

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1')

    .filterDate('2000-04-01', '2000-07-01');

// Reduce the collection by taking the median.##通过中指来减小

var median = collection.median();

// Load a table of state boundaries and filter.##找到美国的某两个州的边界。

var fc = ee.FeatureCollection('TIGER/2016/States')

    .filter(ee.Filter.or(

        ee.Filter.eq('NAME', 'Nevada'),

        ee.Filter.eq('NAME', 'Arizona')));

// Clip to the output image to the Nevada and Arizona state boundaries.##将输出的影像范围限制在美国的某两个州。

var clipped = median.clipToCollection(fc);

// Display the result.##显示结果

Map.setCenter(-110, 40, 5);##设置显示中心的三个参数,经度中心,纬度中心,放大比例。

var visParams = {bands: ['B3', 'B2', 'B1'], gain: [1.4, 1.4, 1.1]};##选取波段及对应组合增益的比例

Map.addLayer(clipped, visParams, 'clipped composite');##显示图层

问题:fc是边界,为什么裁剪它就行?日期筛选哪去了,跟mean有关系吗?有问题是好的,接下来的学习中,心中就会有一个目的,要把这些问题解决。

1.2 expression map 

官网上给的代码截图:

 expression map 

逐行分析代码:

// Map an expression over a collection.##在选择影像之后,通过表达式出图。

//

// Computes the mean NDVI and SAVI by mapping an expression over a collection

// and taking the mean.  This intentionally exercises both variants of

// Image.expression.##锻炼的是image表达式的变量

// Filter the L7 collection to a single month.##筛选一个月的影像。

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA')

    .filterDate('2002-11-01', '2002-12-01');

// A function to compute NDVI.##通过设置一个函数,来自定义NDVI。

var NDVI = function(image) {

  return image.expression('float(b("B4") - b("B3")) / (b("B4") + b("B3"))');

};

// A function to compute Soil Adjusted Vegetation Index.##通过设置一个函数,来自定义SAVI,与上面不同的是,这个函数中的自变量和定常数是额外定义的。

var SAVI = function(image) {

  return image.expression(

      '(1 + L) * float(nir - red)/ (nir + red + L)',

      {

        'nir': image.select('B4'),

        'red': image.select('B3'),

        'L': 0.2

      });

};

// Shared visualization parameters.##定义一个出图的可视化标准,这个比较常用,颜色范围就是棕黄绿。

var vis = {

  min: 0,

  max: 1,

  palette: [

      'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',

      '74A901', '66A000', '529400', '3E8601', '207401', '056201',

      '004C00', '023B01', '012E01', '011D01', '011301'

  ]

};

Map.setCenter(-93.7848, 30.3252, 11);

// Map the functions over the collection, reduce to mean and display.

Map.addLayer(collection.map(NDVI).mean(), vis, 'Mean NDVI');##显示图层

Map.addLayer(collection.map(SAVI).mean(), vis, 'Mean SAVI');

1.3 filtered composite

官网上给的代码截图:


 filtered composite

逐行分析代码:

// Filter an image collection by date and region to make a

// median pixel composite.##通过日期和范围筛选影像,做一个中值的像素组合。

//

// See also: ClippedComposite, which crops the output image##裁剪影像,而不是过滤输入。

// instead of filtering the input collection.

// Filter to only include images intersecting Colorado or Utah.##过滤遥感影像(与两州相交)

var polygon = ee.Geometry.Polygon({##定义面的边界。

  coords: [[[-109.05, 37.0], [-102.05, 37.0], [-102.05, 41.0], // Colorado

            [-109.05, 41.0], [-111.05, 41.0], [-111.05, 42.0], // Utah

            [-114.05, 42.0], [-114.05, 37.0], [-109.05, 37.0]]],

  geodesic: false

});

// Create a Landsat 7 composite for Spring of 2000, and filter by##创建影像,春季,边界过滤。

// the bounds of the FeatureCollection.

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1')

    .filterDate('2000-04-01', '2000-07-01')

    .filterBounds(polygon);

// Compute the median in each band, in each pixel.##计算每一个像元处的每一个波段的中值。

var median = collection.median();

// Select the red, green and blue bands.##选择结果的RGB三波段。

var result = median.select('B3', 'B2', 'B1');

Map.addLayer(result, {gain: [1.4, 1.4, 1.1]});

Map.setCenter(-110, 40, 5);

1.4 linear fit 线性拟合

官网上给的代码截图:

linear fit

逐行分析代码:

// Compute the trend of nighttime lights from DMSP. ##计算夜光的趋势,来于DMSP。

// Add a band containing image date as years since 1990.##添加波段,自1990年以来的。

function createTimeBand(img) {##创建时间波段。

  var year = img.date().difference(ee.Date('1990-01-01'), 'year');

  return ee.Image(year).float().addBands(img);

}

// Fit a linear trend to the nighttime lights collection.##夜光选择的线性趋势。

var collection = ee.ImageCollection('NOAA/DMSP-OLS/CALIBRATED_LIGHTS_V4')

    .select('avg_vis')

    .map(createTimeBand);##根据年份找到遥感影像。

var fit = collection.reduce(ee.Reducer.linearFit());##定义一个表达式,线性趋势就出来了,问号1.

// Display a single image

Map.addLayer(ee.Image(collection.select('avg_vis').first()),

        {min: 0, max: 63},

        'stable lights first asset');

// Display trend in red/blue, brightness in green.

Map.setCenter(30, 45, 4);

Map.addLayer(fit,

        {min: 0, max: [0.18, 20, -0.18], bands: ['scale', 'offset', 'scale']},

        'stable lights trend');

问题:这个fit是怎么回事?

1.5 simple cloud score 

官网上给的代码截图:

simple cloud score 

逐行分析代码:

// SimpleCloudScore, an example of computing a cloud-free composite with L8

// by selecting the least-cloudy pixel from the collection.##计算无云的组合。通过选择最小云像素。

// A mapping from a common name to the sensor-specific bands.

var LC8_BANDS = ['B2',  'B3',    'B4',  'B5',  'B6',    'B7',    'B10'];##波段的标志

var STD_NAMES = ['blue', 'green', 'red', 'nir', 'swir1', 'swir2', 'temp'];##波段的实际颜色

// Compute a cloud score.  This expects the input image to have the common##计算云分数,期待输入影像有相同的波段名字,然后通过传感器计算。

// band names: ["red", "blue", etc], so it can work across sensors.

var cloudScore = function(img) {##归一化值。函数的三个参数,影像,波段,值范围(上下限)。函数里面有一个小函数。

  // A helper to apply an expression and linearly rescale the output.

  var rescale = function(img, exp, thresholds) {

    return img.expression(exp, {img: img})

        .subtract(thresholds[0]).divide(thresholds[1] - thresholds[0]);

  };

  // Compute several indicators of cloudyness and take the minimum of them.##计算一些云的指数,然后取其最小值。

  var score = ee.Image(1.0);

  // Clouds are reasonably bright in the blue band.##云在蓝波段是亮的。

  score = score.min(rescale(img, 'img.blue', [0.1, 0.3]));

  // Clouds are reasonably bright in all visible bands.##云在可见波段是亮的。

  score = score.min(rescale(img, 'img.red + img.green + img.blue', [0.2, 0.8]));

  // Clouds are reasonably bright in all infrared bands.##云在红外线波段是亮的。

  score = score.min(

      rescale(img, 'img.nir + img.swir1 + img.swir2', [0.3, 0.8]));

  // Clouds are reasonably cool in temperature.##云在温度上是凉爽的。

  score = score.min(rescale(img, 'img.temp', [300, 290]));

  // However, clouds are not snow.##云不是学。

  var ndsi = img.normalizedDifference(['green', 'swir1']);

  return score.min(rescale(ndsi, 'img', [0.8, 0.6]));

};

// Filter the TOA collection to a time-range and add the cloudscore band.##根据时间范围过滤,添加云指数波段。

var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')

    .filterDate('2017-05-01', '2017-07-01')

    .map(function(img) {

      // Invert the cloudscore so 1 is least cloudy, and rename the band.##倒转云指数,本来最小值是云,现在最大值1是最少的云了。重命名这个波段。

      var score = cloudScore(img.select(LC8_BANDS, STD_NAMES));

      score = ee.Image(1).subtract(score).select([0], ['cloudscore']);

      return img.addBands(score);

    });

// Define visualization parameters for a true color image.

var vizParams = {bands: ['B4', 'B3', 'B2'], max: 0.4, gamma: 1.6};

Map.setCenter(-120.24487, 37.52280, 8);

Map.addLayer(collection.qualityMosaic('cloudscore'), vizParams);qualitymosaic不懂

问题:qualitymosaic是什么意思?

1.6 animated thumbnail 动态展示。

官网上给的代码截图:


animated thumbnail

逐行分析代码:

// Simple ImageCollection preview via animated GIF.##通过动态的GIF展示选择的image。

// The region of interest - a planar rectangle around Australia.##选择的地区是澳大利亚的正方形。

var rect = ee.Geometry.Rectangle({##设置面的边界。

  coords: [[110, -44], [155, -10]],

  geodesic: false

});

Map.addLayer(rect);##添加这个正方形的图层。

Map.centerObject(rect, 3);

// Select MODIS vegetation composites from 2018.##选择某时的某image的植被内容。

var collection = ee.ImageCollection("MODIS/006/MOD13Q1")

  .filterDate('2018-01-01', '2019-01-01')

  .select('NDVI');

// Add the first image to the map, just as a preview.##添加第一个image,来提前浏览一下。

var im = ee.Image(collection.first());

Map.addLayer(im, {}, "first image");##显示图层三个参数,第一个image本身,第二个颜色板,第三个是名字。

// Visualization parameters.##设置可视化的颜色板。

var args = {

  crs: 'EPSG:3857',  // Maps Mercator

  dimensions: '300',

  region: rect,

  min: -2000,

  max: 10000,

  palette: 'black, blanchedalmond, green, green',

  framesPerSecond: 12,

};

// Create a video thumbnail and add it to the map.##创建一个小视屏窗,然后添加到地图上。

var thumb = ui.Thumbnail({

  // Specifying a collection for "image" animates the sequence of images.##设置一个选集,作为动画的播放顺序。

  image: collection,##选择的影像集。

  params: args,##可视化的要求

  style: {

    position: 'bottom-right',##动画位置

    width: '320px'##动画窗的大小。

  }});

Map.add(thumb);

1.7 landsat simple composite

官网上给的代码截图:

landsat simple composite

逐行分析代码:

// Composite 6 months of Landsat 8.

// Note that the input to simpleComposite is raw data.##输入原始数据集。

var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1');

// The asFloat parameter gives floating-point TOA output instead of##输出的是浮点型,而不是整型的。

// the UINT8 outputs of the default simpleComposite().

var composite = ee.Algorithms.Landsat.simpleComposite({

  collection: l8.filterDate('2015-1-1', '2015-7-1'),

  asFloat: true

});

// Pick a spot with lots of clouds.##挑选一个多云的点。

Map.setCenter(-47.6735, -0.6344, 12);

// Display a composite with a band combination chosen from:

// https://landsat.usgs.gov/how-do-landsat-8-band-combinations-differ-landsat-7-or-landsat-5-satellite-data

Map.addLayer(composite, {bands: ['B6', 'B5', 'B4'], max: [0.3, 0.4, 0.3]});##显示图层,设置RGB显示比例。

这次的学习就到这里了,希望帮助自己学到知识, 也可以帮助到需要的朋友。不足之处,还望见谅。

我是小白,生产不了代码。copyright from Google earth engine。

你可能感兴趣的:(Day 2)