0
点赞
收藏
分享

微信扫一扫

Google Earth Engine(GEE)——制作一个简易的panel的demo来展示地图

函数:

getDownloadURL(params, callback)

Get a download URL for small chunks of image data in GeoTIFF or NumPy format. Maximum request size is 32 MB, maximum grid dimension is 10000.

Use getThumbURL for RGB visualization formats PNG and JPG.

Returns returns a download URL, or undefined if a callback was specified.

获取GeoTIFF或NumPy格式的小块图像数据的下载URL。最大请求大小为32MB,最大网格尺寸为10000。

对于RGB可视化格式PNG和JPG,使用getThumbURL。

返回 返回一个下载URL,如果指定了回调,则未定义。

Arguments:

this:image (Image):

The Image instance.

params (Object):

An object containing download options with the following possible values:

- name: a base name to use when constructing filenames. Only applicable when format is "ZIPPED_GEO_TIFF" (default) or filePerBand is true. Defaults to the image id (or "download" for computed images) when format is "ZIPPED_GEO_TIFF" or filePerBand is true, otherwise a random character string is generated. Band names are appended when filePerBand is true.

- bands: a description of the bands to download. Must be an array of band names or an array of dictionaries, each with the following keys

(optional parameters apply only when filePerBand is true):

+ id: the name of the band, a string, required.

+ crs: an optional CRS string defining the band projection.

+ crs_transform: an optional array of 6 numbers specifying an affine transform from the specified CRS, in row-major order:

[xScale, xShearing, xTranslation, yShearing, yScale, yTranslation]

+ dimensions: an optional array of two integers defining the width and height to which the band is cropped.

+ scale: an optional number, specifying the scale in meters of the band; ignored if crs and crs_transform are specified.

- crs: a default CRS string to use for any bands that do not explicitly specify one.

- crs_transform: a default affine transform to use for any bands that do not specify one, of the same format as the crs_transform of bands.

- dimensions: default image cropping dimensions to use for any bands that do not specify them.

- scale: a default scale to use for any bands that do not specify one; ignored if crs and crs_transform are specified.

- region: a polygon specifying a region to download; ignored if crs and crs_transform is specified.

- filePerBand: whether to produce a separate GeoTIFF per band (boolean). Defaults to true. If false, a single GeoTIFF is produced and all band-level transformations will be ignored.

- format: the download format. One of: "ZIPPED_GEO_TIFF" (GeoTIFF file(s) wrapped in a zip file, default), "GEO_TIFF" (GeoTIFF file),

"NPY" (NumPy binary format). If "GEO_TIFF" or "NPY", filePerBand and all band-level transformations will be ignored. Loading a NumPy output results in a structured array.

callback (Function, optional):

An optional callback. If not supplied, the call is made synchronously.

Returns: Object|String

advance(delta, unit, timeZone)

Create a new Date by adding the specified units to the given Date.

Arguments:

this:date (Date)

delta (Float)

unit (String):

One of 'year', 'month' 'week', 'day', 'hour', 'minute', or 'second'.

timeZone (String, default: null):

The time zone (e.g. 'America/Los_Angeles'); defaults to UTC.

Returns: Date

ee.Date.fromYMD(year, month, day, timeZone)

Returns a Date given year, month, day.

Arguments:

year (Integer)

month (Integer)

day (Integer)

timeZone (String, default: null):

The time zone (e.g. 'America/Los_Angeles'); defaults to UTC.

Returns: Date

ee.Number.parse(input, radix)字符串转数字函数

Convert a string to a number.

Arguments:

input (String):

The string to convert to a number.

radix (Integer, default: 10):

An integer representing the base number system from which to convert. If input is not an integer, radix must equal 10 or not be specified.

Returns: Number

代码:

// 设置面板宽度
var mainPanel = ui.Panel({
  style: {width: '300px'}
});

//设置面板的标题和大小
var title = ui.Label({
  value: 'demo',
  style: {'fontSize': '24px'}
});
// 将标题添加到面板上
mainPanel.add(title)

// 同样加载其他的东西
var dropdownPanel = ui.Panel({
  layout: ui.Panel.Layout.flow('horizontal', true),
});
mainPanel.add(dropdownPanel);

var yearSelector = ui.Select({
  placeholder: 'please wait..',
  })

var monthSelector = ui.Select({
  placeholder: 'please wait..',
  })

var button = ui.Button('Load')
var clear = ui.Button('Remove layer')
// var download_button = ui.Button('Download_button')
var download_button = ui.Label({
  value:'Load to download',
  style:{border:'2px solid black', padding:'4px'}
  })
dropdownPanel.add(yearSelector)
dropdownPanel.add(monthSelector)
dropdownPanel.add(button)
dropdownPanel.add(clear)
dropdownPanel.add(download_button)

//设置一个时间序列来完成时间的选择
var years = ee.List.sequence(2021, 2022)
var months = ee.List.sequence(1, 12)

// 遍历以上的列表
var yearStrings = years.map(function(year){
  return ee.Number(year).format('%04d')
})
var monthStrings = months.map(function(month){
  return ee.Number(month).format('%02d')
})

// 评估结果并填入下拉菜单
yearStrings.evaluate(function(yearList) {
  yearSelector.items().reset(yearList)
  yearSelector.setPlaceholder('select a year')
})

monthStrings.evaluate(function(monthList) {
  monthSelector.items().reset(monthList)
  monthSelector.setPlaceholder('select a month')

})
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // 第10和11位分别是云和卷云。
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // 这两个标志都应设为零,来位移运算去云。
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000);
}
// 定义一个函数,当任何值发生变化时都会触发

var loadComposite = function() {
  var col = ee.ImageCollection("COPERNICUS/S2_SR");
  var year = yearSelector.getValue()
  var month = monthSelector.getValue()
  var startDate = ee.Date.fromYMD(
    ee.Number.parse(year), ee.Number.parse(month), 1)
//这里设置的是结束时间就是根据已经设置号的月份再往前推算一个月
  var endDate = startDate.advance(1, 'month')
//正常的影像时间和去云筛选
  var filtered = col.filterDate(startDate,endDate)
                    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',10))
                    .map(maskS2clouds);
  //选择我们要加载的区域
  var Beijing = ee.FeatureCollection("users/bqt2000204051/Beijing")
  var geometry = Beijing.geometry()

//根据矢量边界进行影像裁剪
  var image = ee.Image(filtered.median().clip(geometry))
  var vis = {
    min: 0,
    max: 0.3,
    bands: ['B4','B3','B2'],
  };
  var name = startDate.format('yyyy-MM')
  var layerName = 'Cangio_S2_RGB_' + name.getInfo()
  Map.addLayer(image, vis, layerName)
  Map.centerObject(Beijing,11)
  
  // return [image,cangio]
image.getDownloadURL({
  params:{name:layerName,
    bands:['B4', 'B3', 'B2'],
    region:geometry,
    scale:30
    }, 
  callback:function(URL) {
    download_button.setUrl(URL)
    download_button.style().set({backgroundColor:'#90EE90'})
    download_button.setValue(layerName)
    //download_button.getImageUrl(URL)
  }
})

}
// var download_function = function(){
//     Export.image.toDrive({
//       image: loadComposite()[0].select(['B4', 'B3', 'B2']),
//       description: 'multi_band_S2',
//       folder:'S2',
//       fileNamePrefix:'multi_band_S2',
//       region:loadComposite()[1],//ee.Geometry(image.get('system:footprint')),
//       scale:10,
//       crs:'EPSG:4326',
//       maxPixels:10e13,
//       fileFormat:'GeoTIFF'
//     });
//     var download = image.getDownloadURL({
//     name: 'multi_band',
//     bands: ['R', 'G', 'B'],
//     region: geometry,
//     scale: 30,
//     filePerBand: false
//   })
//   download_button.onClick(download_function)

// }
//download_button.onClick(function(){loadComposite()})
button.onClick(function(){loadComposite()})

clear.onClick(function(){Map.clear()})

ui.root.add(mainPanel);

结果:

 

举报

相关推荐

0 条评论