【发布时间】:2021-06-29 09:17:03
【问题描述】:
我正在为我的工作进行研究,我需要在 python 中调整我的谷歌地球引擎脚本,但我遇到了一些问题。他们能帮我吗?
这是一个谷歌地球引擎脚本:
import ee
ee.Initialize()
//Importing image and geometry:
var l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
// geometry = /* color: #d63000 */ee.Geometry.Polygon(
// [[[-80.92489760146748, 25.433457120928352],
// [-80.64474623427998, 25.488013471687964],
// [-80.57882826552998, 25.710940372707608],
// [-81.02377455459248, 25.770317250349557],
// [-80.95236342177998, 25.552457242621447]]]);
//Filtering date, polygon, and cloudiness
var image = l8.filterDate ('2010-09-01', '2021-12-31')
.filterBounds (geometry)
.filterMetadata ('CLOUD_COVER', 'less_than', 1);
//NDVI calculation:
var ndvi_func = function (i) {
var ndvi = i.normalizedDifference (['B5', 'B4']).rename ('NDVI')
return i.addBands(ndvi);
}
var image_ndvi = image.map(ndvi_func);
//Calculating year wise NDVI
var year = ee.List.sequence(2010,2021);
var year_func = function(y){
var range = ee.Filter.calendarRange (y, y, 'year');
return image_ndvi.select('NDVI').filter(range).mean().set ('Year', y)
};
var yearwise_ndvi = ee.ImageCollection(year.map(year_func));
print (yearwise_ndvi);
Map.addLayer (yearwise_ndvi)
//Creating time-series chart:
var chart = ui.Chart.image.series ({
imageCollection: image_ndvi.select('NDVI'),
region: geometry,
reducer: ee.Reducer.mean(),
scale: 30})
print(chart);
上面的脚本显示了一个地区的 NDVI 时间序列,我需要在 python 中完成。这是一个显示python错误的脚本:
l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
geometry = ee.Geometry.Polygon([[[-80.92489760146748, 25.433457120928352],
[-80.64474623427998, 25.488013471687964],
[-80.57882826552998, 25.710940372707608],
[-81.02377455459248, 25.770317250349557],
[-80.95236342177998, 25.552457242621447]]]);
#Filtering date, polygon, and cloudiness
image = l8.filterDate ('2010-09-01', '2021-12-31')
image = image.filterBounds (geometry)
image = image.filterMetadata ('CLOUD_COVER', 'less_than', 1);
#NDVI calculation:
def ndvi_func(i):
ndvi = i.normalizedDifference (['B5', 'B4']).rename ('NDVI')
return i.addBands(ndvi)
image_ndvi = ndvi_func(image.map)
#Calculating year wise NDVI
year = ee.List.sequence(2010,2021);
def year_func(y):
range = ee.Filter.calendarRange (y, y, 'year');
return image_ndvi.select('NDVI').filter(range).mean().set ('Year', y)
yearwise_ndvi = ee.ImageCollection(year.map(year_func));
print (yearwise_ndvi);
Map.addLayer (yearwise_ndvi)
#Creating time-series chart:
chart = ui.Chart.image.series ({imageCollection: image_ndvi.select('NDVI'),
region: geometry,
reducer: ee.Reducer.mean(),
scale: 30})
print(chart)
问题是函数没有加载数据。
您愿意帮我解决这个问题并将 NDVI 数据按日期加载到 DataFrame 中吗?
我还希望获得每日 NDVI 而不是每月/每年,但我无法在 Google 地球引擎上获得它。
谢谢!
【问题讨论】:
-
我错过了什么吗?您在哪里访问 Google 地球 api 以请求数据?
-
来自“import ee”和“ee.Initialize()”。有什么问题吗?
-
您说“这是一个显示 python 错误的脚本:”,但我找不到错误消息,也看不到您在哪里完成了
import ee或 @ 987654326@ 在您的 Python 代码中执行步骤。另外,在进行ee.initialize()之前是否需要进行身份验证 -
好的,我进行了身份验证,但是当加载函数“ndvi_func”时出现错误:AttributeError: 'function' object has no attribute 'normalizedDifference'
-
您的函数 nvdi_func(i) 使用 image.map 调用。 image.map 是否支持 .normalizeDifference 方法?
标签: python-3.x dataframe google-api-python-client google-earth-engine