GEE python 使用gee colab进行指定研究区的平均值、最大值、最小值等
使用colab之前一定要进行下面代码的加载:
安装 Earth Engine API 和 geemap
安装 Earth Engine Python API 和 geemap。 geemap Python 包基于 ipyleaflet 和 folium 包构建,并实现了与 Earth Engine 数据层交互的多种方法,例如 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'])
import ee
import geemap
真彩色影像加载如下面的代码所示:
这里就是我们加载影像,然后使用中位数合成,然后限定至少5景影像,然后就是对影像可是参数的设定,然后就是设定地图中心点和进行地图的加载。
# Add Earth Engine dataset
# Load a Landsat 8 collection.
collection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \
.filterBounds(ee.Geometry.Point(-122.262, 37.8719)) \
.filterDate('2014-01-01', '2014-12-31') \
.sort('CLOUD_COVER')
# Compute the median of each pixel for each band of the 5 least cloudy scenes.
median = collection.limit(5).reduce(ee.Reducer.median())
# Define visualization parameters in an object literal.
vizParams = {'bands': ['B5_median', 'B4_median', 'B3_median'],
'min': 5000, 'max': 15000, 'gamma': 1.3}
Map.setCenter(-122.262, 37.8719, 10)
Map.addLayer(median, vizParams, 'Median image')
这里加载完单景影像,然后设定我们的研究区,然后加载矢量,
# Add Earth Engine dataset
# Load and display a Landsat TOA image.
image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')
Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'Landsat 8')
# Create an arbitrary rectangle as a region and display it.
region = ee.Geometry.Rectangle(-122.2806, 37.1209, -122.0554, 37.2413)
Map.centerObject(ee.FeatureCollection(region), 13)
Map.addLayer(ee.Image().paint(region, 0, 2), {}, 'Region')
# Get a dictionary of means in the region. Keys are bandnames.
mean = image.reduceRegion(**{
'reducer': ee.Reducer.mean(),
'geometry': region,
'scale': 30
})
print(mean.getInfo())
这里函数:
**image.reduceRegion(*args, kwargs)
Apply a reducer to all the pixels in a specific region. Either the reducer
must have the same number of inputs as the input image has bands, or it
must have a single input and will be repeated for each band. Returns a
dictionary of the reducer’s outputs.
将缩减器应用于特定区域中的所有像素。 无论是减速机
输入的数量必须与输入图像的带数相同,否则
必须有一个输入,并且将为每个频段重复。 返回一个
减速器输出的字典。
Args:
image: The image to reduce.
reducer: The reducer to apply.
geometry: The region over which to reduce data. Defaults to
the footprint of the image’s first band.
scale: A nominal scale in meters of the projection to work in.
crs: The projection to work in. If unspecified, the projection of
the image’s first band is used. If specified in addition to
scale, rescaled to the specified scale.
crsTransform: The list of CRS transform values. This is
a row-major ordering of the 3x2 transform matrix.
This option is mutually exclusive with ‘scale’, and
replaces any transform already set on the projection.
bestEffort: If the polygon would contain too many pixels at
the given scale, compute and use a larger scale which
would allow the operation to succeed.
maxPixels: The maximum number of pixels to reduce.
tileScale: A scaling factor between 0.1 and 16 used to
adjust aggregation tile size; setting a larger tileScale
(e.g. 2 or 4) uses smaller tiles and may enable
computations that run out of memory with the default.
image:要缩小的图像。
减速器:要应用的减速器。
几何:要减少数据的区域。 默认为
图像第一条带的足迹。
比例:要使用的投影的标称比例(以米为单位)。
crs:要使用的投影。如果未指定,则投影
使用图像的第一个波段。 如果除了指定
缩放,重新缩放到指定的缩放比例。
crsTransform:CRS 变换值的列表。 这是
3x2 变换矩阵的行优先顺序。
此选项与“scale”互斥,并且
替换投影上已设置的任何变换。
bestEffort:如果多边形在以下位置包含太多像素
给定的比例,计算并使用更大的比例
将使操作成功。
maxPixels:要减少的最大像素数。
tileScale:0.1 到 16 之间的缩放因子,用于
调整聚合图块大小; 设置更大的tileScale
(例如 2 或 4)使用较小的图块并且可以启用
默认情况下内存不足的计算。
最后的结果:
{‘B1’: 0.10156660976055135, ‘B10’: 288.38945983522234, ‘B11’: 287.449363371539, ‘B2’: 0.0777191134617773, ‘B3’: 0.05530029791297399, ‘B4’: 0.036708278784827325, ‘B5’: 0.2213974520811337, ‘B6’: 0.07888247177758294, ‘B7’: 0.03508945487734233, ‘B8’: 0.04710008874158145, ‘B9’: 0.0015609697454817123, ‘BQA’: 2720.119445772292}
{‘B1’: 0.10057091121347973, ‘B10’: 288.31267832028374, ‘B11’: 287.4380035810285, ‘B2’: 0.0770883840019017, ‘B3’: 0.05370450386558887, ‘B4’: 0.03416041768795379, ‘B5’: 0.22070476984580337, ‘B6’: 0.07128097690030873, ‘B7’: 0.028315342903770746, ‘B8’: 0.04588400150438728, ‘B9’: 0.0014895948115736248, ‘BQA’: 2720.0000000000396}
{‘B1’: 0.09194143861532211, ‘B10’: 282.551513671875, ‘B11’: 282.04803466796875, ‘B2’: 0.0669216737151146, ‘B3’: 0.03696415200829506, ‘B4’: 0.02074405923485756, ‘B5’: 0.010509952902793884, ‘B6’: 0.0009378909599035978, ‘B7’: 0.0006896242848597467, ‘B8’: 0.030233364552259445, ‘B9’: -2.7590731406235136e-05, ‘BQA’: 2720}
{‘B1’: 0.09194143861532211, ‘B10’: 282.551513671875, ‘B11’: 282.04803466796875, ‘B2’: 0.0669216737151146, ‘B3’: 0.03696415200829506, ‘B4’: 0.02074405923485756, ‘B5’: 0.010509952902793884, ‘B6’: 0.0009378909599035978, ‘B7’: 0.0006896242848597467, ‘B8’: 0.030233364552259445, ‘B9’: -2.7590731406235136e-05, ‘BQA’: 2720}
{‘B10_max’: 302.4209289550781, ‘B10_min’: 282.551513671875, ‘B11_max’: 300.42791748046875, ‘B11_min’: 282.04803466796875, ‘B1_max’: 0.42826008796691895, ‘B1_min’: 0.09194143861532211, ‘B2_max’: 0.43499088287353516, ‘B2_min’: 0.0669216737151146, ‘B3_max’: 0.44122514128685, ‘B3_min’: 0.03696415200829506, ‘B4_max’: 0.4735274016857147, ‘B4_min’: 0.02074405923485756, ‘B5_max’: 0.5558140277862549, ‘B5_min’: 0.010509952902793884, ‘B6_max’: 0.4359287917613983, ‘B6_min’: 0.0009378909599035978, ‘B7_max’: 0.3699174225330353, ‘B7_min’: 0.0006896242848597467, ‘B8_max’: 0.4776651859283447, ‘B8_min’: 0.030233364552259445, ‘B9_max’: 0.005710129160434008, ‘B9_min’: -2.7590731406235136e-05, ‘BQA_max’: 2976, ‘BQA_min’: 2720}
方差标准差自己可以通过上面的函数进行加载,然后进行计算。这里我们输入ee.Reducer.后后面会自动给你提示相应的函数,然后你直接点击后就可以直接加载,这个相较于JavaScript中的函数没有提示的结果还是非常好的。
- 点赞
- 收藏
- 关注作者
评论(0)