简介
GEE提供了大量的Jave脚本代码,然而对于常用的Python例子并没有太多的讲解。这里对在线Python环境搭建进行一个简介。
配置
1 关联Colaboratory
Colaboratory位置在Google Drive中,是它的一个关联应用,所以第一步我们先要登录到Google Drive中(https://drive.google.com/)
2 Python测试
新建的流程也是:点击新建 —> 更多 -> Colaboratory
+代码,输入测试代码后,点左侧“播放图标”运行代码。
import sys
print(sys.version)
!pip list
3 GEE运行授权
使用在线Python方法运行GEE需要Gmail授权,授权代码如下,包括安装了一个自动代码补齐库jieba。
import ee
print(ee.__version__)
!pip install jieba
!pip install earthengine-api
!earthengine authenticate
ee.Authenticate()
ee.Initialize()
点开链接后,有授权的操作,复制授权码,粘贴回下面的空行处回车即可。
授权结束,就能正常运行了。
例子
例子1:
import matplotlib.pyplot as plt
# Fetch a Landsat image.
img = ee.Image('LANDSAT/LT05/C01/T1_SR/LT05_034033_20000913')
# Select Red and NIR bands, scale them, and sample 500 points.
samp_fc = img.select(['B3','B4']).divide(10000).sample(scale=30, numPixels=500)
# Arrange the sample as a list of lists.
samp_dict = samp_fc.reduceColumns(ee.Reducer.toList().repeat(2), ['B3', 'B4'])
samp_list = ee.List(samp_dict.get('list'))
# Save server-side ee.List as a client-side Python list.
samp_data = samp_list.getInfo()
# Display a scatter plot of Red-NIR sample pairs using matplotlib.
plt.scatter(samp_data[0], samp_data[1], alpha=0.2)
plt.xlabel('Red', fontsize=12)
plt.ylabel('NIR', fontsize=12)
plt.show()
例子2
import folium
Peru = ee.Image("USGS/SRTMGL1_003")\
.clip(ee.Geometry.Rectangle(-85, -20, -65,0))
# Define the URL format used for Earth Engine generated map tiles.
EE_TILES = 'https://earthengine.googleapis.com/map/{mapid}/{
{z}}/{
{x}}/{
{y}}?token={token}'
# Use folium to visualize the imagery.
mapid = Peru.getMapId({
'pallete': ['black', 'white'], 'min': 0, 'max': 3000})
map = folium.Map(location=[-10, -75],zoom_start=5)
folium.TileLayer(
tiles=EE_TILES.format(**mapid),
attr='Google Earth Engine',
overlay=True,
name='median composite',
).add_to(map)
map.add_child(folium.LayerControl())
map
例子3——数据下载
import ee
ee.Initialize()
# Get a download URL for an image.
image1 = ee.Image('srtm90_v4')
path = image1.getDownloadUrl({
'scale': 30,
'crs': 'EPSG:4326',
'region': '[[-120, 35], [-119, 35], [-119, 34], [-120, 34]]'
})
print(path)
例子3
本例子可能会提示缺少两个库:tornado 或 folium,如果缺少就安装。
pip install -v tornado>=6.1.0
pip install -v folium==0.2.1
import ee
import geemap
# Create a map centered at (lat, lon).
Map = geemap.Map(center=[40, -100], zoom=4)
# traditional python string
print('Hello world!')
# Earth Eninge object
print(ee.String('Hello World from Earth Engine!').getInfo())
print(ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318').getInfo())
# Display the map.
Map
例子4
参考
入门
https://blog.csdn.net/wokaowokaowokao12345/article/details/110733056
安装
https://zhuanlan.zhihu.com/p/150175719
https://zhuanlan.zhihu.com/p/41591886
https://zhuanlan.zhihu.com/p/41672480
例子
https://github.com/giswqs/earthengine-py-examples
Python指定版本包的安装方法
https://www.crifan.com/python_install_specific_designated_version_of_lib_package/