Skip to content

legends module

Module of sample legends for some commonly used geospatial datasets.

ee_table_to_legend(in_table, out_file)

Converts an Earth Engine color table to a dictionary

Parameters:

Name Type Description Default
in_table str

The input file path (*.txt) to the Earth Engine color table.

required
out_file str

The output file path (*.txt) to the legend dictionary.

required
Source code in geemap/legends.py
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
def ee_table_to_legend(in_table, out_file):
    """Converts an Earth Engine color table to a dictionary

    Args:
        in_table (str): The input file path (*.txt) to the Earth Engine color table.
        out_file (str): The output file path (*.txt) to the legend dictionary.
    """

    if not os.path.exists(in_table):
        print("The class table does not exist.")

    out_file = os.path.abspath(out_file)
    if not os.path.exists(os.path.dirname(out_file)):
        os.makedirs(os.path.dirname(out_file))

    legend_dict = {}
    with open(in_table) as f:
        lines = f.readlines()
        for index, line in enumerate(lines):
            if index > 0:
                items = line.split("\t")
                items = [item.strip() for item in items]
                color = items[1]
                key = items[0] + " " + items[2]
                legend_dict[key] = color

    out_lines = []
    out_lines.append("{\n")

    for key in legend_dict.keys():
        line = "\t'{}': '{}',\n".format(key, legend_dict[key])
        out_lines.append(line)

    out_lines[-1] = out_lines[-1].rstrip()[:-1] + "\n"
    out_lines.append("}\n")

    with open(out_file, "w") as f:
        f.writelines(out_lines)