我正在尝试从传单中的netcdf文件绘制光栅图像,但在地图中不适合正确的光栅图像。感谢您的帮助,也许解决方法是使用rasterio或geopandas投影。我使用的代码:
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import folium
# geting Tmax
# you can download netcdf file here: https://www.dropbox.com/s/r7oxk8q3kh4ozs7/Tmax_daily_UT_Brazil_v2.1_19900101_19991231.nc?dl=0
path = '/home/'
data = xr.open_mfdataset(path + 'Tmax_daily_UT_Brazil_v2.1_19800101_19891231.nc', combine='by_coords').Tmax
data2plot = np.flipud(data.isel(time=0).values.astype(np.float64))
lon, lat = np.meshgrid(data.longitude.values.astype(np.float64), data.latitude.values.astype(np.float64))
cm = plt.cm.get_cmap('cubehelix')
normed_data = (data2plot - np.nanmin(data2plot)) / (np.nanmax(data2plot) - np.nanmin(data2plot))
colored_data = cm(normed_data)
m = folium.Map(location=[lat.mean(), lon.mean()], zoom_start=4)
folium.raster_layers.ImageOverlay(
image=colored_data,
bounds=[[lat.min(), lon.min()], [lat.max(), lon.max()]],
opacity=0.5
).add_to(m)
m.save('test_xarray_folium.html')
如您在下面看到的,图像在地图上不正确。
我在以下位置找到了解决方案的基础:https://github.com/ocefpaf/folium_notebooks/blob/master
import xarray as xr
import numpy as np
import folium
from cartopy.img_transform import warp_array
import cartopy.crs as ccrs
import matplotlib.cm
# based in https://github.com/ocefpaf/folium_notebooks/blob/master/test_image_overlay_geodetic.ipynb
# get data
path = '/home/'
data = xr.open_mfdataset(path + 'Tmax_daily_UT_Brazil_v2.1_19800101_19891231.nc', combine='by_coords').Tmax
data2plot = np.flipud(data.isel(time=0).values.astype(np.float64))
lon, lat = np.meshgrid(data.longitude.values.astype(np.float64), data.latitude.values.astype(np.float64))
source_extent = [lon.min(), lon.max(), lat.min(), lat.max()]
normed_data = (data2plot - np.nanmin(data2plot)) / (np.nanmax(data2plot) - np.nanmin(data2plot))
cm = matplotlib.cm.get_cmap('cubehelix')
colored_data = cm(normed_data)
m = folium.Map(location=[lat.mean(), lon.mean()], zoom_start=4)
folium.raster_layers.ImageOverlay(colored_data,
[[lat.min(), lon.min()], [lat.max(), lon.max()]],
mercator_project=True,
opacity=0.5).add_to(m)
m.save('test_xarray_folium.html')