2

I have some tiffs that I'd like to plot interactively in python using folium (or any other package, but I don't know of any). My problem is, that I have many na-values in the raster. At the moment I read in the raster with rasterio, read the data into a 2d-numpy-array, and then I write it back out as .png. Finally I use folium with:

rasterlayer = folium.FeatureGroup(name = "raster")
rasterlayer.add_child(folium.raster_layers.ImageOverlay(
                        image=ndvi,
                        bounds=[[bottom, left], [top, right]],
                        interactive=True,
                        cross_origin=False,
                        zindex=1,
                        colormap=matplotlib.pyplt.cm.Greens
                            ))
m.add_child(rasterlayer)
m.add_child(folium.LayerControl())
m

But I get all the NA-Values printed in some colour. And I didn't find any solution to that problem. I had a look here:

https://stackoverflow.com/questions/50689068/display-raster-data-in-folium-handling-no-data-values

but also did not really help. I also did set all NA-values in the "numpy"-array to 0, but this also was not the solution. The NA-Values always got plotted. Is there some, relatively easy way, to omit the plotting of na values. And more general: What is a good way of plotting raster with folium? It seems that in R it's so easy with packages like mapview or tmap and in python it feels way more complicated...

1 Answer 1

0

I couldn't find a solution that worked apart from building my own colormap. This function will make the lowest 100 values in the scale transparent

    def cmap(cm, scale=9999):
        from matplotlib.colors import ListedColormap
        ncm = np.zeros((scale, 4))

        pcm = ListedColormap(ncm)

        ncm = np.vstack((pcm(np.linspace(0, 1, 100)), cm(np.linspace(0, 1, 2000)))
           )

        return ListedColormap(ncm)
    cmap(matplotlip.colormap.viridis)

Worked off this document https://matplotlib.org/3.1.0/tutorials/colors/colormap-manipulation.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.