1

I want to analyze Corine Land Cover data. It is raster data in a .tif file. Currently, I am using rasterio to handle the raster data.

Question: How can I plot only a single land use on a map? My goal is to plot every land use code in the study region on another map (e.g. only plot the urban area in the study region, then plot only cropland on another map).

I can plot the land use data for my study region with the following code, but I don't know how to plot only single land use codes.

import rasterio
from rasterio.plot import show
import numpy as np

# Open the land cover dataset using rasterio
land_cover_dataset_path = 'test_CLC.tif'
clc = rasterio.open(land_cover_dataset_path) 

band = clc.read(1)

#plot raster data
show(band, cmap="tab20")

With landuses = np.unique(band).tolist() I can extract all the existing land use codes in the study area.

I think I have to somehow "hide" all pixels which do not have the value of the investigated land use code. Please let me know if the question is not clear enough.

1 Answer 1

1

You can mask your raster using any condition. This is one way to do it:

import rasterio
from rasterio.plot import show
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

path = 'test_raster.tif'
dataset = rasterio.open(path)

band = dataset.read(1)

def plot_specific_land_use(land_use_code, band, title):
    land_use_mask = (band == land_use_code).astype(np.uint8)
    masked_band = np.where(land_use_mask, band, np.nan)
    plt.figure(figsize=(10, 10))
    plt.title(title)
    cmap = ListedColormap(['red'])
    show(masked_band, cmap=cmap)
    plt.show()

land_use_code = 1

plot_specific_land_use(land_use_code, band, f'Land Use Code: {land_use_code}')

2
  • Thank you so much! What are vmin and vmax for? I am now trying to set the color. rasterio.plot.show() only accepts a colormap. Do you know an easy way to set the color of the printed land use code easily to for example red?
    – tofri
    Commented Jul 11 at 12:56
  • You can ignore vmin vmax here, it defines the data range that the colormap covers. I dont know the most elegent way to color, but you can create a custom colormap. I edited my answer accordingly.
    – juto96
    Commented Jul 11 at 13:39

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.