python - 在没有 nodata 值的folium 地图上显示卫星数据
问题描述
通过使用下面的代码,我试图在 folium 地图上绘制/显示 .tif 文件(特定波段或 NCC 图像):
import rasterio as rio
import folium
from pyproj import Transformer
## LC08 RGB Image or particular Band Image
in_path = 'RGB.tif' or 'Band 1.tif'
dst_crs = 'EPSG:4326'
with rio.open(in_path) as src:
img = src.read()
src_crs = src.crs['init'].upper()
min_lon, min_lat, max_lon, max_lat = src.bounds
## Conversion from UTM to WGS84 CRS
bounds_orig = [[min_lat, min_lon], [max_lat, max_lon]]
bounds_fin = []
for item in bounds_orig:
#converting to lat/lon
lat = item[0]
lon = item[1]
proj = Transformer.from_crs(int(src_crs.split(":")[1]), int(dst_crs.split(":")[1]), always_xy=True)
lon_n, lat_n = proj.transform(lon, lat)
bounds_fin.append([lat_n, lon_n])
# Finding the centre latitude & longitude
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],
tiles='Stamen Terrain', zoom_start = 10)
# Overlay raster using add_child() function
m.add_child(folium.raster_layers.ImageOverlay(img.transpose(1, 2, 0), opacity=.7,
bounds = bounds_fin))
# Display map
m
运行上述代码后,我得到以下输出:
如上面附加的图像/输出所示,tiff 文件中的 nodata 值由黑色表示。
我还尝试将原始 tif 文件中表示为 0 的 nodata 值设置为 np.nan,然后绘制修改后的图像,但仍然得到与上述相同的结果。
有人可以帮我解决如何删除这些 nodata 值或使它们空心,同时在 folium 地图上显示栅格。
解决方案
Have a try with the following:
with rio.open(geotiff_geo) as src:
boundary = src.bounds
img = src.read(1)
nodata = src.nodata
img[img==nodata] = np.nan
推荐阅读
- python - 使用张量、lambda 和 python 变量绑定自定义学习率衰减
- css - 如何将 sass 和 scss 与 webpack 混合使用?
- react-native - React Native 的导航库
- facet-wrap - 如何在 iNext 函数中更改 facet.var 的字母顺序?
- exoplayer - 如何在 google IO18 audio App demo 中添加播放器控制 ui
- android - 没有overlayColor的Android GIF边框半径
- flask - 试图在本地主机上运行 Flask,拒绝连接
- python - 找到可用的端口来运行服务器
- machine-learning - LightGBM (lgb.cv) 的 CV 模型是什么,我该如何使用它?
- oracle - DBD 错误:错误可能在“TRUNCATE TABLE <*>MONTHLY_DATA”中字符 15 处的 <*> 指示符附近)