Google Earth Engine 阶段1—代码—初识

简介

这里的例子都来自官网脚本demos,因为主要用到的是Modis数据,所以就罗列了一些Modis有关的脚本。根据下面脚本可以组合出适合于自己需求的脚本。

GEE教程

https://www.cnblogs.com/yhpan/p/12161091.html

批量下载

方法1(推荐使用,简单)

var batch = require('users/fitoprincipe/geetools:batch');
batch.Download.ImageCollection.toDrive(Albedo,"MCD43A3", {
    
    
scale: 1000,
crs: "EPSG: 4326",
maxPixels: 1e10,
type:'int16',
region: roi});

方法2(不推荐,麻烦)

var numTS = Albedo.size();
var list = dataset.toList(numTS); 
print(list);
for (var i=0; i<numTS; i++){
    
    
  var image = ee.Image(list.get(i));
  if (i < 9){
    
    
    var nameOut = ee.String('MCD43A3_2018_0')
                    .cat(ee.String(ee.Number(i+1)))
                    .getInfo();
  }
  else {
    
    
    var nameOut = ee.String('MCD43A3_2018_')
                    .cat(ee.String(ee.Number(i+1)))
                    .getInfo();
  }
 
  // 将数据下载到Google云盘上
  Export.image.toDrive({
    
    
    image: image, // 要下载的影像
    description: nameOut, // 下载任务描述,也是文件的默认名称
    folder: 'MCD43A3_2018', // 选择要下载到云盘的哪个文件夹
    region: roi, // 裁剪区域
    crs: "EPSG: 4326",
    scale: 1000, // 分辨率,默认值是1000m
    maxPixels: 1e10 // 下载数据的最大像元数
  });
}

批量Run

批量执行GEE导出任务
首先,您需要生成导出任务。并显示了运行按钮。
然后按F12进入控制台,然后将这些脚本粘贴到其中,然后按回车。
所有任务都将自动启动。 (支持Firefox和Chrome。其他浏览器我没有测试过。)
@Author: Dongdong Kong,2017年8月28日 中山大学

function runTaskList() {
    
    
    var tasklist = document.getElementsByClassName('awaiting-user-config');
    for (var i = 0; i < tasklist.length; i++)
        tasklist[i].children[2].click();
}
function confirmAll() {
    
    
    var ok = document.getElementsByClassName('goog-buttonset-default goog-buttonset-action');
    for (var i = 0; i < ok.length; i++)
        ok[i].click();
}
runTaskList();
confirmAll();

取消前面的Run

function runTaskList() {
    
    
var tasklist = document.getElementsByClassName('indicator');
for (var i = 0; i < tasklist.length; i++)
tasklist[i].click();
}
function confirmAll() {
    
    
var ok = document.getElementsByClassName('goog-buttonset-default goog-buttonset-action');
for (var i = 0; i < ok.length; i++)
ok[i].click();
}
runTaskList();
confirmAll();

Image

1 From Name

// Display an image given its ID.

var image = ee.Image('CGIAR/SRTM90_V4');
// Center the Map.
Map.setCenter(-110, 40, 5);
// Display the image.
Map.addLayer(image, {
    
    min: 0, max: 3000}, 'SRTM');

在这里插入图片描述

2 Normalized Difference

// NormalizedDifference example.
//
// Compute Normalized Difference Vegetation Index over MOD09GA product.
// NDVI = (NIR - RED) / (NIR + RED), where
// RED is sur_refl_b01, 620-670nm
// NIR is sur_refl_b02, 841-876nm

// Load a MODIS image.
var img = ee.Image('MODIS/006/MOD09GA/2012_03_09');

// Use the normalizedDifference(A, B) to compute (A - B) / (A + B)
var ndvi = img.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']);

// Make a palette: a list of hex strings.
var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
               '74A901', '66A000', '529400', '3E8601', '207401', '056201',
               '004C00', '023B01', '012E01', '011D01', '011301'];

// Center the map
Map.setCenter(-94.84497, 39.01918, 8);

// Display the input image and the NDVI derived from it.
Map.addLayer(img.select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03']),
         {
    
    gain: [0.1, 0.1, 0.1]}, 'MODIS bands 1/4/3');
Map.addLayer(ndvi, {
    
    min: 0, max: 1, palette: palette}, 'NDVI');

在这里插入图片描述

3 Expression

// Compute Enhanced Vegetation Index (EVI) over the MODIS MOD09GA product
// using an expression.

// Load a MODIS image and apply the scaling factor.
var img = ee.Image('MODIS/006/MOD09GA/2012_03_09').multiply(0.0001);

// Compute EVI using an expression.  The second argument is a map from
// variable name to band name in the input image.
var evi = img.expression(
    '2.5 * (nir - red) / (nir + 6 * red - 7.5 * blue + 1)',
    {
    
    
        red: img.select('sur_refl_b01'),    // 620-670nm, RED
        nir: img.select('sur_refl_b02'),    // 841-876nm, NIR
        blue: img.select('sur_refl_b03')    // 459-479nm, BLUE
    });

// Center the map.
Map.setCenter(-94.84497, 39.01918, 8);

// Display the input image and the EVI computed from it.
Map.addLayer(img.select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03']),
         {
    
    min: 0, max: 0.2}, 'MODIS bands 1/4/3');
Map.addLayer(evi, {
    
    min: 0, max: 1}, 'EVI');

在这里插入图片描述

4 Landcover Cleanup

// Morphological processing of land cover.  This example
// includes spatial smoothing (neighborhood mode) followed by
// dilation, erosion and dilation again.  Reprojection is
// used to force these operations to be performed at the
// native scale of the input (rather than variable pixel
// sizes based on zoom level).

// Force projection of 500 meters/pixel, which is the native MODIS resolution.
var SCALE = 500;

// Load a 2001 MODIS land cover image.
var image1 = ee.Image('MODIS/051/MCD12Q1/2001_01_01');
// Select the classification band of interest.
var image2 = image1.select(['Land_Cover_Type_1']);
// Reproject to WGS84 to force the image to be reprojected on load.
// This is just for display purposes, to visualize the input to
// the following operations.  The next reproject is sufficient
// to force the computation to occur at native scale.
var image3 = image2.reproject('EPSG:4326', null, SCALE);
// Smooth with a mode filter.
var image4 = image3.focal_mode();
// Use erosion and dilation to get rid of small islands.
var image5 = image4.focal_max(3).focal_min(5).focal_max(3);
// Reproject to force the operations to be performed at SCALE.
var image6 = image5.reproject('EPSG:4326', null, SCALE);

// Define display paramaters with appropriate colors for the MODIS
// land cover classification image.
var PALETTE = [
    'aec3d4', // water
    '152106', '225129', '369b47', '30eb5b', '387242', // forest
    '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40', // shrub, grass, savannah
    '111149', // wetlands
    'cdb33b', // croplands
    'cc0013', // urban
    '33280d', // crop mosaic
    'd7cdcc', // snow and ice
    'f7e084', // barren
    '6f6f6f'  // tundra
].join(',');

var vis_params = {
    
    min: 0, max: 17, palette: PALETTE};

// Display each step of the computation.
Map.setCenter(-113.41842, 40.055489, 6);
Map.addLayer(image2, vis_params, 'IGBP classification');
Map.addLayer(image3, vis_params, 'Reprojected');
Map.addLayer(image4, vis_params, 'Mode');
Map.addLayer(image5, vis_params, 'Smooth');
Map.addLayer(image6, vis_params, 'Smooth');

在这里插入图片描述

5 Connect Pixel Count

// Image.ConnectedPixelCount example.

// Split pixels of band 01 into "bright" (arbitrarily defined as
// reflectance > 0.3) and "dim". Highlight small (<30 pixels)
// standalone islands of "bright" or "dim" type.
var img = ee.Image('MODIS/006/MOD09GA/2012_03_09')
              .select('sur_refl_b01')
              .multiply(0.0001);

// Create a threshold image.
var bright = img.gt(0.3);

// Compute connected pixel counts; stop searching for connected pixels
// once the size of the connected neightborhood reaches 30 pixels, and
// use 8-connected rules.
var conn = bright.connectedPixelCount({
    
    
  maxSize: 30,
  eightConnected: true
});

// Make a binary image of small clusters.
var smallClusters = conn.lt(30);

Map.setCenter(-107.24304, 35.78663, 8);
Map.addLayer(img, {
    
    min: 0, max: 1}, 'original');
Map.addLayer(smallClusters.updateMask(smallClusters),
         {
    
    min: 0, max: 1, palette: 'FF0000'}, 'cc');

在这里插入图片描述

6 Modis Qa Bands

// Extract MODIS QA information from the "state_1km" QA band
// and use it to mask out cloudy and deep ocean areas.
//
// QA Band information is available at:
// https://lpdaac.usgs.gov/products/modis_products_table/mod09ga
// Table 1: 1-kilometer State QA Descriptions (16-bit)


/**
 * Returns an image containing just the specified QA bits.
 *
 * Args:
 *   image - The QA Image to get bits from.
 *   start - The first bit position, 0-based.
 *   end   - The last bit position, inclusive.
 *   name  - A name for the output image.
 */
var getQABits = function(image, start, end, newName) {
    
    
    // Compute the bits we need to extract.
    var pattern = 0;
    for (var i = start; i <= end; i++) {
    
    
       pattern += Math.pow(2, i);
    }
    return image.select([0], [newName])
                  .bitwiseAnd(pattern)
                  .rightShift(start);
};

// Reference a single MODIS MOD09GA image.
var image = ee.Image('MODIS/006/MOD09GA/2012_10_11');

// Select the QA band
var QA = image.select('state_1km');

// Get the cloud_state bits and find cloudy areas.
var cloud = getQABits(QA, 0, 1, 'cloud_state')
                    .expression("b(0) == 1 || b(0) == 2");

// Get the land_water_flag bits.
var landWaterFlag = getQABits(QA, 3, 5, 'land_water_flag');

// Create a mask that filters out deep ocean and cloudy areas.
var mask = landWaterFlag.neq(7).and(cloud.not());

// Add a map layer with the deep ocean and clouds areas masked out.
Map.addLayer(image.updateMask(mask),
  {
    
    
    bands: ['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03'],
    min: -100,
    max: 2000
  }, 'MOD09GA 143'
);

// Add a semi-transparent map layer that displays the clouds.
Map.addLayer(
    cloud.updateMask(cloud),
    {
    
    palette: 'FFFFFF', opacity: 0.8},
    'clouds'
);

在这里插入图片描述

扫描二维码关注公众号,回复: 12759046 查看本文章

7 Polynomial

// Applies a non-linear contrast enhancement to a MODIS image using
// function -0.2 + 2.4x - 1.2x^2.

// Load a MODIS image and apply the scaling factor.
var img = ee.Image('MODIS/006/MOD09GA/2012_03_09')
              .select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03'])
              .multiply(0.0001);

// Apply the polynomial enhancement.
var adj = img.polynomial([-0.2, 2.4, -1.2]);

Map.setCenter(-107.24304, 35.78663, 8);
Map.addLayer(img, {
    
    min: 0, max: 1}, 'original');
Map.addLayer(adj, {
    
    min: 0, max: 1}, 'adjusted');

在这里插入图片描述

8 Animated Thumbnail

// Simple ImageCollection preview via animated GIF.

// 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.
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.
var im = ee.Image(collection.first());
Map.addLayer(im, {
    
    }, "first 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);

在这里插入图片描述

MODIS V006 MCD43数据

MCD43A1

var dataset = ee.ImageCollection('MODIS/006/MCD43A1')
                  .filter(ee.Filter.date('2018-05-01', '2018-07-01'));
var defaultVisualization = dataset.select([
  'BRDF_Albedo_Parameters_Band1_iso', 'BRDF_Albedo_Parameters_Band4_iso',
  'BRDF_Albedo_Parameters_Band3_iso'
]);
var defaultVisualizationVis = {
    
    
  min: 0.0,
  max: 1400.0,
  gamma: 2.0,
};
Map.setCenter(6.746, 46.529, 6);
Map.addLayer(
    defaultVisualization, defaultVisualizationVis, 'Default visualization');

MCD43A2

var dataset = ee.ImageCollection('MODIS/006/MCD43A2')
                  .filter(ee.Filter.date('2018-01-01', '2018-05-01'));
var defaultVisualization = dataset.select('Snow_BRDF_Albedo');
var defaultVisualizationVis = {
    
    
  min: 0.0,
  max: 1.0,
};
Map.setCenter(6.746, 46.529, 6);
Map.addLayer(
    defaultVisualization, defaultVisualizationVis, 'Default visualization');

MCD43A3

var dataset = ee.ImageCollection('MODIS/006/MCD43A3')
                  .filter(ee.Filter.date('2018-01-01', '2018-05-01'));
var blackSkyAlbedo = dataset.select('Albedo_BSA_Band1');
var blackSkyAlbedoVis = {
    
    
  min: 0.0,
  max: 400.0,
};
Map.setCenter(6.746, 46.529, 6);
Map.addLayer(blackSkyAlbedo, blackSkyAlbedoVis, 'Black-Sky Albedo');

MCD43A4

var dataset = ee.ImageCollection('MODIS/006/MCD43A4')
                  .filter(ee.Filter.date('2018-04-01', '2018-06-01'));
var trueColor = dataset.select([
  'Nadir_Reflectance_Band1', 'Nadir_Reflectance_Band4',
  'Nadir_Reflectance_Band3'
]);
var trueColorVis = {
    
    
  min: 0.0,
  max: 4000.0,
  gamma: 1.4,
};
Map.setCenter(-7.03125, 31.0529339857, 2);
Map.addLayer(trueColor, trueColorVis, 'True Color');

MCD43C3

var dataset = ee.ImageCollection('MODIS/006/MCD43C3')
                  .filter(ee.Filter.date('2018-01-01', '2018-05-01'));
var blackSkyAlbedo = dataset.select('Albedo_BSA_Band1');
var blackSkyAlbedoVis = {
    
    
  min: 0.0,
  max: 400.0,
};
Map.setCenter(6.746, 46.529, 6);
Map.addLayer(blackSkyAlbedo, blackSkyAlbedoVis, 'Black-Sky Albedo');

参考
https://blog.csdn.net/qq_21567935/article/details/89061114
https://www.cnblogs.com/yhpan/p/13370954.html
https://code.earthengine.google.com/d30e104e4d5e3320eb28d4c291b3ede5?noload=true
https://blog.csdn.net/weixin_36396470/article/details/107281153

猜你喜欢

转载自blog.csdn.net/wokaowokaowokao12345/article/details/110733056