首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Landsat8时态数据将Google Earth引擎脚本转换为Python

使用Landsat8时态数据将Google Earth引擎脚本转换为Python
EN

Stack Overflow用户
提问于 2021-04-02 21:52:32
回答 1查看 285关注 0票数 2

我正在为我的工作做一项研究,我需要用python修改我的google earth引擎脚本,但我遇到了一些问题。他们能帮我吗?

这是一个google earth引擎脚本:

代码语言:javascript
复制
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中的错误:

代码语言:javascript
复制
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而不是每月/每年,但我没有在谷歌地球引擎上得到它。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-09-14 13:14:25

我强烈推荐使用eemont,它让这一切在python中变得轻而易举。您可以直接在他们的自述文件中找到适合您的用例的示例:

代码语言:javascript
复制
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计算中使用了LandSat -2,但是可以很容易地将其更改为使用sentinel。

另请注意:您的脚本不是Python。如果你真的需要我们的帮助来调试你当前的脚本,请删除所有的分号,坚持PEP8格式,并包括你的导入。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66920356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档