fire2a.c2f_lookup_table_colors

Helper classes for transforming c2f lookup tables rgb to QGIS qml format

 1#!/usr/bin/env python3
 2"""Helper classes for transforming c2f lookup tables rgb to QGIS qml format"""
 3from colorsys import hls_to_rgb, rgb_to_hls
 4
 5import numpy as np
 6from pandas import read_csv
 7
 8
 9def rgb2hex_color(r, g, b) -> str:
10    """int 0-255 rgb to hex"""
11    return "#%02x%02x%02x" % (r, g, b)
12    # return '%02x%02x%02x'%(int(r*255), int(g*255), int(b*255))
13
14
15def fuel_lookuptable_colorconvert(afile="spain_lookup_table.csv"):
16    df = read_csv(afile, usecols=["grid_value", "r", "g", "b", "h", "s", "l"], dtype=np.int16)
17
18    for t in df.itertuples():
19        print((t.r, t.g, t.b), hls_to_rgb(t.h, t.l, t.s), rgb_to_hls(t.r, t.g, t.b), (t.h, t.l, t.s))
20        assert np.all(rgb_to_hls(t.r, t.g, t.b) == (t.h, t.l, t.s)) and np.all(
21            (t.r, t.g, t.b) == hls_to_rgb(t.h, t.l, t.s)
22        )
23
24
25def fuel_lookuptable_csv2qml(afile="kitral_lookup_table.csv", qmlfile="qml_lookup_table.qml"):
26    """convert csv to qml colorPalette section
27    printing results to stdout
28    TODO modify a qml file <colorPallete> section
29    columns : ['grid_value', 'export_value', 'descriptive_name', 'fuel_type', 'r', 'g', 'b', 'h', 's', 'l']
30    """
31    df = read_csv(afile)
32    # print('<paletteEntry color="#ffffff" label="NA" alpha="0" value="0"/>')
33    for row in df.itertuples():
34        hex_color = rgb2hex_color(row.r, row.g, row.b)
35        print(f'<paletteEntry color="{hex_color}" label="{row.fuel_type}" alpha="255" value="{row.grid_value}"/>')
def rgb2hex_color(r, g, b) -> str:
10def rgb2hex_color(r, g, b) -> str:
11    """int 0-255 rgb to hex"""
12    return "#%02x%02x%02x" % (r, g, b)
13    # return '%02x%02x%02x'%(int(r*255), int(g*255), int(b*255))

int 0-255 rgb to hex

def fuel_lookuptable_colorconvert(afile='spain_lookup_table.csv'):
16def fuel_lookuptable_colorconvert(afile="spain_lookup_table.csv"):
17    df = read_csv(afile, usecols=["grid_value", "r", "g", "b", "h", "s", "l"], dtype=np.int16)
18
19    for t in df.itertuples():
20        print((t.r, t.g, t.b), hls_to_rgb(t.h, t.l, t.s), rgb_to_hls(t.r, t.g, t.b), (t.h, t.l, t.s))
21        assert np.all(rgb_to_hls(t.r, t.g, t.b) == (t.h, t.l, t.s)) and np.all(
22            (t.r, t.g, t.b) == hls_to_rgb(t.h, t.l, t.s)
23        )
def fuel_lookuptable_csv2qml(afile='kitral_lookup_table.csv', qmlfile='qml_lookup_table.qml'):
26def fuel_lookuptable_csv2qml(afile="kitral_lookup_table.csv", qmlfile="qml_lookup_table.qml"):
27    """convert csv to qml colorPalette section
28    printing results to stdout
29    TODO modify a qml file <colorPallete> section
30    columns : ['grid_value', 'export_value', 'descriptive_name', 'fuel_type', 'r', 'g', 'b', 'h', 's', 'l']
31    """
32    df = read_csv(afile)
33    # print('<paletteEntry color="#ffffff" label="NA" alpha="0" value="0"/>')
34    for row in df.itertuples():
35        hex_color = rgb2hex_color(row.r, row.g, row.b)
36        print(f'<paletteEntry color="{hex_color}" label="{row.fuel_type}" alpha="255" value="{row.grid_value}"/>')

convert csv to qml colorPalette section printing results to stdout TODO modify a qml file section columns : ['grid_value', 'export_value', 'descriptive_name', 'fuel_type', 'r', 'g', 'b', 'h', 's', 'l']