GEE学习笔记 二十九:下载文件方式一
GEE学习笔记 二十九:下载文件方式一
详细信息
今天首先探讨一下在实际工作中最为常用的一个功能,下载GEE中的数据或者结果,关于GEE中下载数据方式我了解到的目前主要有以下两种方式,如果有新的方式请大家留言探讨一下。
- 第一种方式:getDownloadURL方式,这是这篇文章主要介绍的内容。
- 第二种方式:Export方式,这种方式在之前的文章中也提起过,只不过没有详细介绍。篇幅所限,下一篇文章中会详细介绍各种Export方式。
首先,我们查看GEE的API,getDownloadURL方法总共有两个:一个是FeatureCollection,一个是Image的。这个也非常好理解,我们在GEE需要下载的也就是矢量数据和栅格数据,那么也就是这两种数据集合。需要明确的一点是GEE不支持直接下载ImageCollection,非常重要。下面我会依次介绍这两个方法:
1. ee.FeatureCollection.getDownloadURL()参数
Get a download URL.
Returns a download URL or undefined if a callback was specified.
Arguments:
this:featurecollection (FeatureCollection):
The FeatureCollection instance.
format (String, optional):
The format of download, one of:
"csv", "json", "kml", "kmz".
selectors (List<String>|String, optional):
Selectors that should be used to determine which attributes will be downloaded.
filename (String, optional):
Name of the file to be downloaded.
callback (Function, optional):
An optional callback. If not supplied, the call is made synchronously.
Returns: Object|String
参数说明:
- format:生成的下载格式,支持四种各种csv、json、kml、kmz。如果需要ESRI shapefile格式,那么需要生成比如kml格式,然后使用QGIS或者Arcgis本地转换一下;
- selectors:筛选下载的矢量数据中包含哪些属性,默认是包含全部属性;
- filename:下载的文件名称;
- callback:这个参数几乎不会用到,回调方法。
上传的Assert文件下载示例:
//使用的是GEE中世界地区边界这个数据集,展示的是中国台湾省
var fc = ee.FeatureCollection('USDOS/LSIB/2013');
//筛选台湾省
var sfc = fc.filter(ee.Filter.eq("cc", "TW"));
//这一步非常重要,直接使用sfc下载数据那么只能获取相关数据结构,数据内容会全部丢失
var taiwan = ee.FeatureCollection(sfc.toList(sfc.size()));
//地图加载台湾省
Map.addLayer(taiwan, {color:"00ff00"}, 'taiwan');
Map.centerObject(taiwan, 7);
print(taiwan);
//输出的kml包含全部属性
var url1 = taiwan.getDownloadURL({
format: "kml",
filename: "taiwan1"
});
//在输出面板中展示URL
print("url1 is: ", url1);
//输出的kml只包含cc属性
var url2 = taiwan.getDownloadURL({
format: "kml",
selectors: ["cc"],
filename: "taiwan2"
});
print("url2 is: ", url1);
输出结果:
使用QGIS查看两个文件中的属性:
- 包含全部属性的文件
- 只包含cc属性的文件
对于直接使用代码生成的FeatureCollection下载示例:
var roi = /* color: #d63000 */ee.Geometry.Polygon(
[[[115.46424865722656, 39.47489550075251],
[115.54664611816406, 39.47171528483254],
[115.55419921875, 39.524699902077586],
[115.47111511230469, 39.52734807268146]]]);
//featureCollection生成下载的URL
var roi_col = ee.FeatureCollection(roi);
var url3 = roi_col.getDownloadURL({
format: "json",
filename: "bounds"
});
print("url3 is: ", url3);
2. ee.Image.getDownloadURL() 参数说明:
Get a Download URL
Returns returns a download URL, or undefined if a callback was specified.
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.
- bands: a description of the bands to download. Must be a list of dictionaries, each with the following keys:
+ id: the name of the band, a string, required.
+ crs: an optional CRS string defining the band projection.
+ crs_transform: an optional list of 6 numbers specifying an affine transform from the specified CRS, in row-major order:
[xScale, xShearing, xTranslation, yShearing, yScale, yTranslation]
+ dimensions: an optional list 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 is 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 is specified.
- region: a polygon specifying a region to download; ignored if crs and crs_transform is specified.
callback (Function, optional):
An optional callback. If not supplied, the call is made synchronously.
Returns: Object|String
参数说明:
- params各种参数,这里说一下常用的,其他的参数用的机会不多(bands波段参数列表,这个参数不会用, )
- name 下载文件的名称
- crs 投影信息,一般来讲都是使用默认的投影,然后本地使用其他软件转换投影信息
- scale 分辨率
- region 下载的区域
- callback回调方法,几乎不用
下载示例:
//download image url demo
var roi = /* color: #d63000 */ee.Geometry.Polygon(
[[[115.46424865722656, 39.47489550075251],
[115.54664611816406, 39.47171528483254],
[115.55419921875, 39.524699902077586],
[115.47111511230469, 39.52734807268146]]]);
var l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT_TOA")
.filterBounds(roi)
.filterDate("2017-01-01", "2017-12-31")
.map(ee.Algorithms.Landsat.simpleCloudScore)
.map(function(img) {
img = img.updateMask(img.select("cloud").lt(1));
return img;
})
.sort("system:time_start");
var landsat = l8.mosaic().clip(roi);
Map.addLayer(landsat, {bands: ["B3", "B2", "B1"], min:0, max:0.3}, "landsat");
Map.centerObject(roi, 12);
Map.addLayer(roi, {color: 'red'}, 'roi');
//第一种获取geometry的json字符串方式
var roiInfo = roi.getInfo();
var region1 = JSON.stringify(roiInfo);
var url1 = landsat.select('B1').getDownloadURL({scale: 30,
region: region1,
name:"landsat8-B1"});
print("url1 is: ", url1);
var url2 = landsat.select('B2').getDownloadURL({scale: 100,
region: region1,
name:"landsat8-B2"});
print("url2 is: ", url2);
//第二种获取geometry的json字符串方式(推荐)
var region2 = ee.Geometry(roi).toGeoJSONString();
var url3 = landsat.select('B3').getDownloadURL({scale: 30,
region: region2,
name:"landsat8-B3"});
print("url3 is: ", url3);
//筛选多个波段同时下载
var url4 = landsat.select(["B3", "B2", "B1"]).getDownloadURL({scale: 30,
region:region2,
name:"landsat8-RGB"});
print("url4 is: ", url4);
运行结果:
下载的文件,单波段的文件
多波段文件: