出版质量地图使用地球引擎和卡通!
cartoee的Python项目详细描述
卡通
使用Earth Engine和Cartopy发布质量映射!
安装
cartoee
可通过pip
安装。要安装软件包,可以在python环境中使用pip install:
pip install cartoee
或者,可以使用以下命令从源代码手动安装包:
git clone https://github.com/kmarkert/cartoee.git
cd cartoee
python setup.py install
有关安装依赖项的说明,请参阅documentation。
使用卡通
cartoee
只想做好一件事:将来自地球引擎的处理结果放入发布质量映射接口。cartoee
只需从地球引擎获取结果,并用正确的地理投影绘制出来,留下ee
和cartopy
来进行更多的处理和可视化。
一个简单的例子
下面是在地图上可视化SRTM数据的简单工作流:
import cartoee as cee
import ee
ee.Initialize()
# get an earth engine image
srtm = ee.Image("CGIAR/SRTM90_V4")
# plot the result using cartoee
ax = cee.getMap(srtm,region=[-180,-90,180,90],visParams={'min':0,'max':3000})
ax.coastlines()
plt.show()
现在我们已经将ee图像作为cartopy/matplotlib对象,可以开始使用cartopy
api为发布设置绘图样式。
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER
# set gridlines and spacing
xticks = [-180,-120,-60,0,60,120,180]
yticks = [-90,-60,-30,0,30,60,90]
ax.gridlines(xlocs=xticks, ylocs=yticks,linestyle='--')
# set custom formatting for the tick labels
ax.xaxis.set_major_formatter(LONGITUDE_FORMATTER)
ax.yaxis.set_major_formatter(LATITUDE_FORMATTER)
# set tick labels
ax.set_xticks([-180,-120,-60, 0, 60, 120, 180], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
做更多…
现在我们已经掌握了一个简单的例子,我们可以使用地球引擎来进行一些处理,并制作一个漂亮的地图。
# function to add NDVI band to imagery
def calc_ndvi(img):
ndvi = img.normalizedDifference(['Nadir_Reflectance_Band2','Nadir_Reflectance_Band1'])
return img.addBands(ndvi.rename('ndvi'))
# MODIS Nadir BRDF-Adjusted Reflectance with NDVI band
modis = ee.ImageCollection('MODIS/006/MCD43A4')\
.filterDate('2010-01-01','2016-01-01')\
.map(calc_ndvi)
# get the cartopy map with EE results
ax = cee.getMap(modis.mean(),cmap='YlGn'
visParams={'min':-0.5,'max':0.85,'bands':'ndvi',},
region=[-180,-90,180,90])
ax.coastlines()
cb = cee.addColorbar(ax,loc='right',cmap='YlGn',visParams={'min':0,'max':1,'bands':'ndvi'})
从这个例子可以看出,我们计算了2010-2015年modis图像上的ndvi,并创建了一个每像素平均值的全球地图。
如果我们想用不同的投影制作多个地图呢?我们可以通过创建图形并提供轴来绘制。
# get land mass feature collection
land = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
# get seasonal averages and clip to land features
djf = modis.filter(ee.Filter.calendarRange(12,3,'month')).mean().clip(land)
mam = modis.filter(ee.Filter.calendarRange(3,6,'month')).mean().clip(land)
jja = modis.filter(ee.Filter.calendarRange(6,9,'month')).mean().clip(land)
son = modis.filter(ee.Filter.calendarRange(9,12,'month')).mean().clip(land)
fig,ax = plt.subplots(ncols=2,nrows=2,subplot_kw={'projection': ccrs.Orthographic(-80,35)})
imgs = np.array([[djf,mam],[jja,son]])
titles = np.array([['DJF','MAM'],['JJA','SON']])
for i in range(len(imgs)):
for j in range(len(imgs[i])):
ax[i,j] = cee.addLayer(imgs[i,j],ax=ax[i,j],
region=bbox,dims=500,
visParams=ndviVis,cmap='YlGn'
)
ax[i,j].coastlines()
ax[i,j].gridlines(linestyle='--')
ax[i,j].set_title(titles[i,j])
cax = fig.add_axes([0.9, 0.2, 0.02, 0.6])
cb = cee.addColorbar(ax[i,j],cax=cax,cmap='YlGn',visParams=ndviVis)
要查看更多示例,请转到https://cartoee.readthedocs.io上的文档!