TransWikia.com

Dealing with NA-Values when plotting rasters with folium in python

Geographic Information Systems Asked by Robin Kohrs on December 24, 2020

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…

One Answer

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

Answered by Simigant on December 24, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP