首页 > 解决方案 > 在没有 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 地图上显示栅格。

标签: pythonfoliumrasterio

解决方案


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

推荐阅读