在Folium中旋转GeoTIFF数据图层的问题求助
解决Folium中Earth Engine下载影像显示方向错误问题
我从Earth Engine下载了指定CRS为EPSG:4326的GeoTIFF影像,已知Folium要求输入数据采用EPSG:4326坐标系,但影像在Folium中显示方向错误——本该在东侧的道路出现在了南侧,需要调整为正确的垂直显示状态。原代码如下:
import folium import rasterio in_path = r"filepath" with rasterio.open(in_path) as src: img = src.read([4, 3, 2]).astype(np.float32) min_lon, min_lat, max_lon, max_lat = src.bounds src_crs = src.crs['init'].upper() bounds_fin = [[min_lat, min_lon], [max_lat, max_lon]] centre_lon = bounds_fin[0][1] + (bounds_fin[1][1] - bounds_fin[0][1])/2 centre_lat = bounds_fin[0][0] + (bounds_fin[1][0] - bounds_fin[0][0])/2 m = folium.Map(location=[centre_lat, centre_lon], zoom_start = 10,) m.add_child(folium.raster_layers.ImageOverlay(img.transpose(2, 1, 0), opacity=1.0, bounds = bounds_fin))
修复方案
问题根源是Earth Engine导出的影像数组维度顺序与Folium的ImageOverlay渲染要求不匹配,通过对影像数组进行旋转和翻转即可纠正方向。修改后的完整代码:
import folium import rasterio import numpy as np # 补充缺失的numpy导入 in_path = r"filepath" with rasterio.open(in_path) as src: img = src.read([4, 3, 2]).astype(np.float32) min_lon, min_lat, max_lon, max_lat = src.bounds src_crs = src.crs['init'].upper() bounds_fin = [[min_lat, min_lon], [max_lat, max_lon]] centre_lon = bounds_fin[0][1] + (bounds_fin[1][1] - bounds_fin[0][1])/2 centre_lat = bounds_fin[0][0] + (bounds_fin[1][0] - bounds_fin[0][0])/2 m = folium.Map(location=[centre_lat, centre_lon], zoom_start=10,) # 旋转并翻转影像,纠正显示方向 img = np.rot90(img, k=3, axes=(2, 1)) img = np.flip(img, axis=1) m.add_child(folium.raster_layers.ImageOverlay(img.transpose(2, 1, 0), opacity=1.0, bounds=bounds_fin))
关键修复细节
np.rot90(img, k=3, axes=(2, 1)):将影像数组绕(2,1)轴逆时针旋转270度(等价于顺时针旋转90度),修正整体方向偏移np.flip(img, axis=1):沿水平轴翻转影像,解决镜像问题- 补充
import numpy as np:原代码依赖numpy但未导入,需添加避免运行报错
内容的提问来源于stack exchange,提问作者Josiah Hulsey
相关产品推荐
相关产品推荐

