0

By using the below code I am trying to plot/display the .tif file (particular band or NCC image) on the folium map:

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

After running the above code I am getting the following output:

enter image description here

As seen in the above image/output attached, nodata values in the tiff file is represented by black color.

I also tried to make the nodata values represented as 0 in the original tif file to np.nan and then plot the modified image but still got the same above result.

Can someone please help me out how I can remove these nodata values or make them hollow while displaying the raster over the folium map.

RRSC
  • 257
  • 2
  • 15

1 Answers1

-1

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