QGIS Color Map
This notebook allows you to create a color map readable by QGIS.
You must first create a Excel file with your RGB code.
This is an example for the Scene Classification map (see Sentinel-2 preprocessing).
[4]:
import pandas as pd
import os
print('Pandas library successfully imported!')
Pandas library successfully imported!
Set directory
[8]:
computer_path = '/export/miro/ndeffense/LBRAT2104/'
grp_nb = '99'
work_path = f'{computer_path}STUDENTS/GROUP_{grp_nb}/TP/'
cmap_path = f'{work_path}COLOR_MAP/'
print(f'The folder where your color map is located is : \n {cmap_path}')
The folder where your color map is located is :
/export/miro/ndeffense/LBRAT2104/STUDENTS/GROUP_99/TP/COLOR_MAP/
Set parameters
[9]:
col_with_name = 'scl'
col_with_code = f'{col_with_name}_nb'
# Input file (.xlsx or .xlsm)
cmap_xls = f'{cmap_path}cmap_{col_with_name}.xlsm'
# Output file (.clr)
cmap_clr = f'{cmap_path}cmap_{col_with_name}.clr'
Check if Excel table color map exists
[11]:
if not os.path.isfile(cmap_xls):
print(f'Check if the excel file exists and is stored in the right foled \n {cmap_path}')
else:
print(f'OK excel file exists : \n {cmap_xls}')
OK excel file exists :
/export/miro/ndeffense/LBRAT2104/STUDENTS/GROUP_99/TP/COLOR_MAP/cmap_scl.xlsm
Read Excel color map
[12]:
df = pd.read_excel(cmap_xls, usecols=[col_with_name,'R','G','B','T',col_with_code])
df.dropna(how='all', inplace=True)
df = df.astype({'R': 'int32', 'G': 'int32', 'B': 'int32', 'T': 'int32', col_with_code : 'int32'})
df
[12]:
scl_nb | R | G | B | T | scl | |
---|---|---|---|---|---|---|
0 | 1 | 0 | 0 | 0 | 255 | No Data (missing data) |
1 | 2 | 59 | 0 | 0 | 255 | Saturated or defective pixel |
2 | 3 | 47 | 47 | 47 | 255 | Dark features / Shadows |
3 | 4 | 100 | 50 | 0 | 255 | Cloud shadows |
4 | 5 | 0 | 161 | 0 | 255 | Vegetation |
5 | 6 | 255 | 231 | 90 | 255 | Not-vegetated |
6 | 7 | 0 | 0 | 255 | 255 | Water |
7 | 8 | 129 | 129 | 129 | 255 | Unclassified |
8 | 9 | 193 | 193 | 193 | 255 | Cloud medium probability |
9 | 10 | 255 | 255 | 255 | 255 | Cloud high probability |
10 | 11 | 100 | 201 | 255 | 255 | Thin cirrus |
11 | 12 | 255 | 151 | 255 | 255 | Snow or ice |
Write color map in QGIS style
[13]:
f = open(cmap_clr, "w")
f.write(df.to_string(index=False, header=False))
f.close()
Congratulations !
Now, you can open your GeoTIFF in QGIS and load the color map file (.clr)