在谷歌地球引擎中提取多边形中所有像素的值



我定义了一个多边形

var polygon = ee.Geometry.Polygon([114, 0.37, 114, 2.04, 112, 2.04, 112, 0.37]);

以及上述多边形需要处理的数据集

var dataset = ee.ImageCollection('NASA/NEX-GDDP');

对于选定日期

var startDate = ee.Date('1980-01-01');
var endDate = ee.Date('1980-01-02');

数据集有三个波段prtasmaxtasmin,我正在选择需要处理的波段

var dataset = ee.ImageCollection('NASA/NEX-GDDP')
.filter(ee.Filter.date(startDate,endDate))
.filter(ee.Filter.bounds(polygon))
.select('tasmax');

Map.addLayer(dataset)

我想导出多边形下所有网格的数据以及他们各自的latlong。由于一天有21个特征(GCM(,我希望最终数据的行数等于多边形X 21特征(GCM(中的网格数

var dailyImg = dataset.toBands();
Export.table.toDrive({
collection: dailyImg,
description: 'hist_tx',
fileFormat: 'CSV',
});

当我尝试这样做时,我会得到一个错误

错误:无效参数:"collection"必须是FeatureCollection。

我该如何解决这个问题?此外,即使将我的空间区域限制为多边形,地图仍然显示整个地球的数据?为什么会发生这种情况?

错误:无效参数:"collection"必须是FeatureCollection。

Export.table用于导出表,也称为FeatureCollections。您有一个映像,而不是表。

从地球引擎获取数据的最有效方法是使用Export.image,然后转换下载的GeoTIFF以适合您的R程序。然而,由于该数据集非常小,因此将其作为CSV下载会很好,而用于下载的工具是ee.Image.sample,它将Image的区域转换为FeatureCollection

var collection = dailyImg.sample({
region: polygon,
geometries: true,  // This specifies that you want the lat-long, rather
// than image samples without any position information.
});

如果您导出它,您将获得GeoJSON格式的单列中的位置。这可能不是你想要的,所以我们可以将其转换为列:

var collection_with_latlon = collection.map(function (feature) {
var coordinates = feature.geometry().transform('epsg:4326').coordinates();
return feature.set('lon', coordinates.get(0), 'lat', coordinates.get(1));
});

以下是作为工作示例的所有内容:

var polygon = ee.Geometry.Polygon([114, 0.37, 114, 2.04, 112, 2.04, 112, 0.37]);
var startDate = ee.Date('1980-01-01');
var endDate = ee.Date('1980-01-02');
var dataset = ee.ImageCollection('NASA/NEX-GDDP')
.filter(ee.Filter.date(startDate,endDate))
.filter(ee.Filter.bounds(polygon))
.select('tasmax');
Map.addLayer(polygon);
var dailyImg = dataset.toBands();
var collection = dailyImg.sample({
region: polygon,
geometries: true,  // This specifies that you want the lat-long.
});
// Break point coordinates up into properties (table columns) explicitly.
var collection_with_latlon = collection.map(function (feature) {
var coordinates = feature.geometry().transform('epsg:4326').coordinates();
return feature.set('lon', coordinates.get(0), 'lat', coordinates.get(1));
});
print(collection_with_latlon);
Export.table.toDrive({
collection: collection_with_latlon,
description: 'hist_tx',
fileFormat: 'CSV',
});

此外,即使将我的空间区域限制为多边形,地图仍然显示整个地球的数据?为什么会发生这种情况?

仅将集合过滤为几何体会忽略与几何体不相交的图像。在这种情况下,图像覆盖了整个地球,因此没有图像被过滤掉。为了将图像剪辑到集合中,您必须指定,例如:

var dailyImg = dataset.toBands().clip(polygon);

但是,如果使用.sample(),则没有必要这样做,因为该操作有自己的区域参数,并且不会使用多边形之外的任何像素。

最新更新