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
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.
    """
    # pkg_dir = os.path.dirname(pkg_resources.resource_filename("geemap", "geemap.py"))
    # ee_legend_table = os.path.join(pkg_dir, "data/template/ee_legend_table.txt")

    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)

Last update: 2020-08-22
Created: 2020-08-22