You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 12:02:44