Re: Convert gray band from dataset to RGB with rasterio for population density

Sean Gillies


Sorry for the slow response.

On Fri, Feb 21, 2020 at 3:10 PM <acwangpython@...> wrote:

I'm pretty new to GIS so please correct and terminology or logical fallacies:
I have this dataset: (I am using the full dataset (Global dataset) which is located in the hyperlink below the map). When I unzip it (the .tif.ovr file) and access it via rasterio, there is only one band. On QGIS GUI, I've managed to open the .tif.ovr file and change some of the colors (since I'm working on population density). Unfortunately, I have no clue on how to change this dataset to have RGB bands.
Right now, when I do:
with open(pathtodata, "r+", **profile) as src:
I only get a 2D numpy array with what seems like the gray band only values (0 and 255), but I'd like to have the RGB values so I can work with the RGB values in Python (not for visualization). The meta values show that there is only one band (count) and no photometric. Doing `src.colorinterp` shows only `ColorInterp.gray: 1` which is the issue.

I downloaded a small portion of the data, GHS_POP_E2015_GLOBE_R2019A_4326_9ss_V1_0_18_4.tif, a block that covers some of the South of France and North Africa. It's the .tif file that you want to read, not the .tif.ovr file (I suspect QGIS created one for you, there are no ovr files in the downloads).

The dataset has one single float64 band. To visualize it, use a matplotlib colormap with normalization. For example:

import rasterio
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

with"GHS_POP_E2015_GLOBE_R2019A_4326_9ss_V1_0_18_4.tif") as dataset:
    data =, masked=True)
    plt.imshow(data, norm=LogNorm(vmin=1e-6, vmax=data.max()))
<matplotlib.image.AxesImage object at 0x12f865710>

Results in

Sean Gillies

Join to automatically receive all group messages.