【问题标题】:Transform Google Earth Engine script to Python with landsat 8 temporal data使用 landsat 8 时态数据将 Google 地球引擎脚本转换为 Python
【发布时间】: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


【解决方案1】:

我强烈建议您查看eemont,这使得这一切在 python 中变得轻而易举。 您可以直接在他们的 README 中找到适合您用例的示例:

import ee, eemont

f1 = ee.Feature(ee.Geometry.Point([3.984770,48.767221]).buffer(50),{'ID':'A'})
f2 = ee.Feature(ee.Geometry.Point([4.101367,48.748076]).buffer(50),{'ID':'B'})
fc = ee.FeatureCollection([f1,f2])

S2 = (ee.ImageCollection('COPERNICUS/S2_SR')
   .filterBounds(fc)
   .filterDate('2020-01-01','2021-01-01')
   .maskClouds()
   .scale()
   .index(['EVI','NDVI']))

# By Region
ts = S2.getTimeSeriesByRegion(reducer = [ee.Reducer.mean(),ee.Reducer.median()],
                              geometry = fc,
                              bands = ['EVI','NDVI'],
                              scale = 10)

请注意,他们在NDVI 计算中使用的是 sentinel-2,但可以轻松更改为使用 LandSat。

另外说明:您的脚本不是 Python。如果您真的希望我们帮助调试您当前的脚本,请删除所有分号,坚持 PEP8 格式并包括您的导入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2022-07-03
    • 2018-10-04
    • 1970-01-01
    • 2019-05-21
    • 2019-09-28
    • 2022-06-21
    相关资源
    最近更新 更多