安装地球引擎API和geemap 安装地球引擎的Python API和geemap。geemap Python包是建立在ipyleaflet和folium包之上的,它实现了几个与地球引擎数据层交互的方法,比如Map.addLayer()、Map.setCenter()和Map.centerObject()。下面的脚本检查geemap包是否已经安装。如果没有,它将安装geemap,它会自动安装其依赖项,包括earthengine-api、folium和ipyleaflet。
代码语言:javascript复制# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print('Installing geemap ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])
代码语言:javascript复制import ee
import geemap
使用的函数: **ee.Kernel.square(*args, kwargs) Generates a square-shaped boolean kernel.这里使用这个核函数创建一个布尔类型的内核。 参数类型的设定中我们可以使用半径和单位进行设定。 Args: radius: The radius of the kernel to generate. units: The system of measurement for the kernel (‘pixels’ or ‘meters’). If the kernel is specified in meters, it will resize when the zoom-level is changed. normalize: Normalize the kernel values to sum to 1. magnitude: Scale each value by this amount.
**image.convolve(*args, kwargs) Convolves each band of an image with the given kernel. 将每一个指定的影像波段转化为给定的核函数,通过与 Boxcar 内核卷积来平滑图像。
Args: image: The image to convolve. kernel: The kernel to convolve with.
**ee.Kernel.laplacian8(*args, kwargs) Generates a 3x3 Laplacian-8 edge-detection kernel.
Args: magnitude: Scale each value by this amount. normalize: Normalize the kernel values to sum to 1
生成 3x3 Laplacian-8 边缘检测内核。 参数: 大小:按此量缩放每个值。 标准化:将核值标准化为总和为 1
代码语言:javascript复制# Add Earth Engine dataset
# Load and display an image.
image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')
Map.setCenter(-121.9785, 37.8694, 11)
Map.addLayer(image, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'input image')
# Define a boxcar or low-pass kernel.
# boxcar = ee.Kernel.square({
# 'radius': 7, 'units': 'pixels', 'normalize': True
# })
boxcar = ee.Kernel.square(7, 'pixels', True)
# Smooth the image by convolving with the boxcar kernel.
smooth = image.convolve(boxcar)
Map.addLayer(smooth, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'smoothed')
# Define a Laplacian, or edge-detection kernel.
laplacian = ee.Kernel.laplacian8(1, False)
# Apply the edge-detection kernel.
edgy = image.convolve(laplacian)
Map.addLayer(edgy,
{'bands': ['B5', 'B4', 'B3'], 'max': 0.5},
'edges')