1

I am seeking a solution that provides me a range of values rather than the provided 0 and 255 from rasterio. That most likely meant that I had to set the photometric and bands and ColorInterp to something related to RGB. You can download the dataset to better understand.

I am pretty new to GIS.

I have this dataset: https://ghsl.jrc.ec.europa.eu/download.php?ds=pop. 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.

How would I change the gray band to RGB bands to work with RGB-valued data with numpy?

I am using the full dataset (Global dataset) which is located in the hyperlink below the map

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)}

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.

Here is some sample data (at array 10427 there are several 255s that COULD be made differently. In QGIS, I used the single band pseudo color rendering type on continuous mode to see some rich data on the GUI.):

>>> data[10427].shape
(72164,)
>>> 
  • 1
    Hi there, please integrate your edits into the question so it is updated in its total, not requiring one to jump back and forth ;) – inc42 Mar 21 at 8:58
  • 1
    Which dataset exactly are we talking about? – inc42 Mar 21 at 9:04
  • 1
    And are you really talking about the mask or do you want the actual data? – inc42 Mar 21 at 9:05
2

If you are saying you want a grayscale 3-band image array, then np.repeat() could do that for you.

# bogus 1-band image array
array_2d = np.reshape(np.random.random(100), (10, 10))

# duplicate the band, inserting a new axis to repeat along
array_3d = np.repeat(array_2d[:, :, np.newaxis], repeats=3, axis=2)

array_2d.shape  # (10, 10)
array_3d.shape  # (10, 10, 3)
array_2d[0, 3]  # 0.7311774956241371
array_3d[0, 3]  # array([0.7311775, 0.7311775, 0.7311775])
| improve this answer | |
  • When I meant 2D, I meant a numpy array of [ [2,2], [1,1] ]. I've updated my question with some more information and more specifically what I'm after. Mind taking a look? Additionally, do you mind actually getting that dataset and changing the bands from the GeoTiff file? Because all the numbers are either 0 or 255. Changing the shape of the array and inputting different values (with np.random.random) doesn't really help since MY numbers are still only going to be 0 and 255. – acwpython Feb 22 at 0:14
  • 1
    src.dataset_mask() returns a binary mask of nodata, but as a byte data type instead of a boolean. That's why you're seeing either 0 or 255. You need to use src.read(1) to read the actual band – mikewatt Feb 24 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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