Topics

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

@pyaw
 

https://gis.stackexchange.com/questions/351614/convert-gray-band-from-dataset-to-rgb-with-rasterio-for-population-density

I'm pretty new to GIS so please correct and terminology or logical fallacies:
 
I have this dataset: https://ghsl.jrc.ec.europa.eu/download.php?ds=pop (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:
    src.meta
    src.dataset_mask()
 
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.
 
So how would I change the gray band to RGB bands to work with RGB-valued data with numpy? Thank you!

@pyaw
 

Here are some edits from stack that came about since my question may not be interpreted correctly: 

Edit: I am using the full dataset (Global dataset) which is located in the hyperlink below the map
 
Edit 2: When I mean 2D array, I meant a numpy array that looks like this: `[[0, 255, 0], [0, 0, 255]]`.
 
Additionally, this is the meta data:
`{'driver': 'GTiff', 'dtype': 'float32', 'nodata': -200.0, 'width': 72164, 'height': 36000, 'count': 3, 'crs': None, 'transform': Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0)}`
 
Thanks to mike for a slight correction: I am actually after just increasing the number of bands so that I can differentiate the two given values which are 0 and 255.
 
Note that when you do `x = src.dataset_mask()` to grab the numpy array, 0 and 255 are the only two values. Like any population density map, I'm after values that are between a range rather than simply having two numbers... e.g. numbers between 0-255 or float values.

@pyaw
 

This is a re worded question that probably sounds a bit better: https://gis.stackexchange.com/questions/351639/rasterio-change-singelband-gray-to-singleband-pseudo-color-for-multiple-colors

Quoted: This is in reference to: Convert gray band from dataset to RGB with rasterio for population density however there is a difference. I have been playing around with QGIS which I had opened the aforementioned .tif.ovr file and finally got some different hues of gray.

Instead of having multiple bands like the original question, simply having multiple colors is what I'm seeking with rasterio (or gdal. Honestly either one works). In QGIS, I set the rendering type for the .tif.ovr file to singleband pseudocolor with a magma color ramp and on continuous mode with linear interpolation. (Setting the mode to equal interval also works well).

Looks neat! So how would I do the same (probably best to use RGB instead though with black being 0 statistic minimum) with the rasterio data that I used from the aforementioned question?

Recalling, the dataset I have from src.dataset_mask() is a 2D numpy array (e.g. [[0,255],[255,255]]) in which the only values in the entire dataset is either 0 or 255. I'm seeking a way to make the values either Floats or a range between 0 and 255. Thank you! 

Sean Gillies
 

Hi,

Sorry for the slow response.

On Fri, Feb 21, 2020 at 3:10 PM <acwangpython@...> wrote:
https://gis.stackexchange.com/questions/351614/convert-gray-band-from-dataset-to-rgb-with-rasterio-for-population-density

I'm pretty new to GIS so please correct and terminology or logical fallacies:
 
I have this dataset: https://ghsl.jrc.ec.europa.eu/download.php?ds=pop (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:
    src.meta
    src.dataset_mask()
 
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 rasterio.open("GHS_POP_E2015_GLOBE_R2019A_4326_9ss_V1_0_18_4.tif") as dataset:
    data = src.read(1, masked=True)
    plt.imshow(data, norm=LogNorm(vmin=1e-6, vmax=data.max()))
<matplotlib.image.AxesImage object at 0x12f865710>
    plt.show()

Results in


Figure_1.png
--
Sean Gillies