gee python:使用colab进行NDVI和NDWI的计算和加载

举报
此星光明 发表于 2023/06/30 17:42:20 2023/06/30
【摘要】 安装地球引擎API和geemap安装地球引擎的Python API和geemap。geemap Python包是建立在ipyleaflet和folium包之上的,它实现了几个与地球引擎数据层交互的方法,比如Map.addLayer()、Map.setCenter()和Map.centerObject()。下面的脚本检查geemap包是否已经安装。如果没有,它将安装geemap,它会自动安装其...

安装地球引擎API和geemap
安装地球引擎的Python API和geemap。geemap Python包是建立在ipyleaflet和folium包之上的,它实现了几个与地球引擎数据层交互的方法,比如Map.addLayer()、Map.setCenter()和Map.centerObject()。下面的脚本检查geemap包是否已经安装。如果没有,它将安装geemap,它会自动安装其依赖项,包括earthengine-api、folium和ipyleaflet。

# Installs geemap package
import subprocess

try:
    import geemap
except ImportError:
    print('Installing geemap ...')
    subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])

这里我们需要进行倒入两个包,分别是官网GEE的的包,另外就是geemap的包

import ee
import geemap

创建一个交互式地图
默认的基图是谷歌地图。可以使用Map.add_basemap()函数来添加额外的基图。

Map = geemap.Map(center=[40,-100], zoom=4)
Map

授权后加载的geemap的结果
这里我们需要进行我们gee账户的获取然后这里成功后会显示你已经经过授权了
这里我们开始进行NDVI的计算
这里加载landsat 数据集,然后使用normalizedDifference()函数进行NDVI计算,当然我们也可以进行NDWI的计算,或者其他归一化指数的计算,但是如果其他波段运算,那我们就得进行expression的公式计算。

# Add Earth Engine dataset
# This function gets NDVI from Landsat 8 imagery.


def addNDVI(image):
    return image.addBands(image.normalizedDifference(['B5', 'B4']))
def addNDWI(image):
    return image.addBands(image.normalizedDifference(['B5', 'B3']))

# Load the Landsat 8 raw data, filter by location and date.
collection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \
    .filterBounds(ee.Geometry.Point(-122.262, 37.8719)) \
    .filterDate('2014-06-01', '2014-10-01')

# Map the function over the collection.
ndviCollection = collection.map(addNDVI).map(addNDWI)

first = ndviCollection.first()
print(first.getInfo())

bandNames = first.bandNames()
print(bandNames.getInfo())

结果:
{‘type’: ‘Image’, ‘bands’: [{‘id’: ‘B1’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B2’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B3’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B4’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B5’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B6’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B7’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B8’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [15341, 15601], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [15, 0, 463792.5, 0, -15, 4264507.5]}, {‘id’: ‘B9’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B10’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘B11’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘BQA’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘int’, ‘min’: 0, ‘max’: 65535}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘nd’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘float’, ‘min’: -1, ‘max’: 1}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}, {‘id’: ‘nd_1’, ‘data_type’: {‘type’: ‘PixelType’, ‘precision’: ‘float’, ‘min’: -1, ‘max’: 1}, ‘dimensions’: [7671, 7801], ‘crs’: ‘EPSG:32610’, ‘crs_transform’: [30, 0, 463785, 0, -30, 4264515]}], ‘id’: ‘LANDSAT/LC08/C01/T1/LC08_044034_20140606’, ‘version’: 1497493928601000.0, ‘properties’: {‘RADIANCE_MULT_BAND_5’: 0.005937200039625168, ‘RADIANCE_MULT_BAND_6’: 0.0014764999505132437, ‘RADIANCE_MULT_BAND_3’: 0.011505999602377415, ‘RADIANCE_MULT_BAND_4’: 0.009702100418508053, ‘RADIANCE_MULT_BAND_1’: 0.012192999944090843, ‘RADIANCE_MULT_BAND_2’: 0.01248599961400032, ‘K2_CONSTANT_BAND_11’: 1201.1441650390625, ‘K2_CONSTANT_BAND_10’: 1321.078857421875, ‘system:footprint’: {‘type’: ‘LinearRing’, ‘coordinates’: [[-120.79200539048736, 38.12706906512293], [-120.79323597868374, 38.12758439698958], [-120.82683301978153, 38.13425518072935], [-122.57369124774934, 38.465867462644404], [-122.91132538951987, 38.52663370240754], [-122.91414613702007, 38.526635850439405], [-122.9189327723941, 38.510718361283075], [-123.40419439796977, 36.80678576741027], [-121.36227701906473, 36.41476296352091], [-121.32989516455781, 36.40824848906167], [-121.20432618246714, 36.815494543804164], [-121.07428782575109, 37.232255532839595], [-120.95966651326353, 37.59672218968956], [-120.90596782826022, 37.76651090203559], [-120.86494805861443, 37.895947164272634], [-120.83393920808882, 37.993514542680224], [-120.82433446488996, 38.02375043851124], [-120.79204501354904, 38.125755061557996], [-120.79200539048736, 38.12706906512293]]}, ‘REFLECTIVE_SAMPLES’: 7671, ‘SUN_AZIMUTH’: 124.43635559082031, ‘CPF_NAME’: ‘LC08CPF_20140401_20140630_01.01’, ‘DATE_ACQUIRED’: ‘2014-06-06’, ‘ELLIPSOID’: ‘WGS84’, ‘google:registration_offset_x’: 0, ‘google:registration_offset_y’: 0, ‘STATION_ID’: ‘LGN’, ‘RESAMPLING_OPTION’: ‘CUBIC_CONVOLUTION’, ‘ORIENTATION’: ‘NORTH_UP’, ‘WRS_ROW’: 34, ‘RADIANCE_MULT_BAND_9’: 0.002320399973541498, ‘TARGET_WRS_ROW’: 34, ‘RADIANCE_MULT_BAND_7’: 0.0004976699710823596, ‘RADIANCE_MULT_BAND_8’: 0.010979999788105488, ‘IMAGE_QUALITY_TIRS’: 9, ‘TRUNCATION_OLI’: ‘UPPER’, ‘CLOUD_COVER’: 35.709999084472656, ‘GEOMETRIC_RMSE_VERIFY’: 2.7200000286102295, ‘COLLECTION_CATEGORY’: ‘T1’, ‘GRID_CELL_SIZE_REFLECTIVE’: 30, ‘CLOUD_COVER_LAND’: 3.930000066757202, ‘GEOMETRIC_RMSE_MODEL’: 5.419000148773193, ‘COLLECTION_NUMBER’: 1, ‘IMAGE_QUALITY_OLI’: 9, ‘LANDSAT_SCENE_ID’: ‘LC80440342014157LGN01’, ‘WRS_PATH’: 44, ‘google:registration_count’: 0, ‘PANCHROMATIC_SAMPLES’: 15341, ‘PANCHROMATIC_LINES’: 15601, ‘GEOMETRIC_RMSE_MODEL_Y’: 3.4519999027252197, ‘REFLECTIVE_LINES’: 7801, ‘TIRS_STRAY_LIGHT_CORRECTION_SOURCE’: ‘TIRS’, ‘GEOMETRIC_RMSE_MODEL_X’: 4.177000045776367, ‘system:asset_size’: 1264461529, ‘system:index’: ‘LC08_044034_20140606’, ‘REFLECTANCE_ADD_BAND_1’: -0.10000000149011612, ‘REFLECTANCE_ADD_BAND_2’: -0.10000000149011612, ‘DATUM’: ‘WGS84’, ‘REFLECTANCE_ADD_BAND_3’: -0.10000000149011612, ‘REFLECTANCE_ADD_BAND_4’: -0.10000000149011612, ‘RLUT_FILE_NAME’: ‘LC08RLUT_20130211_20150302_01_11.h5’, ‘REFLECTANCE_ADD_BAND_5’: -0.10000000149011612, ‘REFLECTANCE_ADD_BAND_6’: -0.10000000149011612, ‘REFLECTANCE_ADD_BAND_7’: -0.10000000149011612, ‘REFLECTANCE_ADD_BAND_8’: -0.10000000149011612, ‘BPF_NAME_TIRS’: ‘LT8BPF20140606181212_20140606190417.01’, ‘GROUND_CONTROL_POINTS_VERSION’: 4, ‘DATA_TYPE’: ‘L1TP’, ‘UTM_ZONE’: 10, ‘LANDSAT_PRODUCT_ID’: ‘LC08_L1TP_044034_20140606_20170305_01_T1’, ‘REFLECTANCE_ADD_BAND_9’: -0.10000000149011612, ‘google:registration_ratio’: 0, ‘GRID_CELL_SIZE_PANCHROMATIC’: 15, ‘RADIANCE_ADD_BAND_4’: -48.51054000854492, ‘REFLECTANCE_MULT_BAND_7’: 1.9999999494757503e-05, ‘system:time_start’: 1402080344240, ‘RADIANCE_ADD_BAND_5’: -29.6860294342041, ‘REFLECTANCE_MULT_BAND_6’: 1.9999999494757503e-05, ‘RADIANCE_ADD_BAND_6’: -7.382649898529053, ‘REFLECTANCE_MULT_BAND_9’: 1.9999999494757503e-05, ‘PROCESSING_SOFTWARE_VERSION’: ‘LPGS_2.7.0’, ‘RADIANCE_ADD_BAND_7’: -2.4883499145507812, ‘REFLECTANCE_MULT_BAND_8’: 1.9999999494757503e-05, ‘RADIANCE_ADD_BAND_1’: -60.96493148803711, ‘RADIANCE_ADD_BAND_2’: -62.428829193115234, ‘RADIANCE_ADD_BAND_3’: -57.52762985229492, ‘REFLECTANCE_MULT_BAND_1’: 1.9999999494757503e-05, ‘RADIANCE_ADD_BAND_8’: -54.90058135986328, ‘REFLECTANCE_MULT_BAND_3’: 1.9999999494757503e-05, ‘RADIANCE_ADD_BAND_9’: -11.601969718933105, ‘REFLECTANCE_MULT_BAND_2’: 1.9999999494757503e-05, ‘REFLECTANCE_MULT_BAND_5’: 1.9999999494757503e-05, ‘REFLECTANCE_MULT_BAND_4’: 1.9999999494757503e-05, ‘THERMAL_LINES’: 7801, ‘TIRS_SSM_POSITION_STATUS’: ‘NOMINAL’, ‘GRID_CELL_SIZE_THERMAL’: 30, ‘NADIR_OFFNADIR’: ‘NADIR’, ‘RADIANCE_ADD_BAND_11’: 0.10000000149011612, ‘REQUEST_ID’: ‘0501703043447_00036’, ‘EARTH_SUN_DISTANCE’: 1.014767050743103, ‘TIRS_SSM_MODEL’: ‘ACTUAL’, ‘FILE_DATE’: 1488689158000, ‘SCENE_CENTER_TIME’: ‘18:45:44.2439160Z’, ‘SUN_ELEVATION’: 67.10252380371094, ‘BPF_NAME_OLI’: ‘LO8BPF20140606171321_20140606190324.01’, ‘RADIANCE_ADD_BAND_10’: 0.10000000149011612, ‘ROLL_ANGLE’: -0.0010000000474974513, ‘K1_CONSTANT_BAND_10’: 774.8853149414062, ‘SATURATION_BAND_1’: ‘Y’, ‘SATURATION_BAND_2’: ‘Y’, ‘SATURATION_BAND_3’: ‘Y’, ‘SATURATION_BAND_4’: ‘Y’, ‘SATURATION_BAND_5’: ‘Y’, ‘MAP_PROJECTION’: ‘UTM’, ‘SATURATION_BAND_6’: ‘Y’, ‘SENSOR_ID’: ‘OLI_TIRS’, ‘SATURATION_BAND_7’: ‘Y’, ‘K1_CONSTANT_BAND_11’: 480.8883056640625, ‘SATURATION_BAND_8’: ‘N’, ‘SATURATION_BAND_9’: ‘N’, ‘TARGET_WRS_PATH’: 44, ‘RADIANCE_MULT_BAND_11’: 0.00033420001273043454, ‘RADIANCE_MULT_BAND_10’: 0.00033420001273043454, ‘GROUND_CONTROL_POINTS_MODEL’: 549, ‘SPACECRAFT_ID’: ‘LANDSAT_8’, ‘ELEVATION_SOURCE’: ‘GLS2000’, ‘THERMAL_SAMPLES’: 7671, ‘GROUND_CONTROL_POINTS_VERIFY’: 192}}
[‘B1’, ‘B2’, ‘B3’, ‘B4’, ‘B5’, ‘B6’, ‘B7’, ‘B8’, ‘B9’, ‘B10’, ‘B11’, ‘BQA’, ‘nd’, ‘nd_1’]

大家注意,当我们进行上面的代码运行后,我们发现ndvi的结果为 ‘nd’,而ndwi为 ‘nd_1’,我们想要改变这个模式的时候,就需要用到另外一个函数rename()

最后我们可以进行相应影像的加载:

Map.addLayer(first)
Map

在这里插入图片描述

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。