File: planetary_computer_06_map_legends.py

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (76 lines) | stat: -rw-r--r-- 2,347 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------

"""
FILE: planetary_computer_06_map_legends.py

DESCRIPTION:
    This sample demonstrates map legend operations from the Azure Planetary Computer Pro SDK.

USAGE:
    python planetary_computer_06_map_legends.py

    Set the environment variable PLANETARYCOMPUTER_ENDPOINT with your endpoint URL.
"""

import os
from azure.planetarycomputer import PlanetaryComputerProClient
from azure.identity import DefaultAzureCredential
from azure.planetarycomputer.models import ColorMapNames

import logging

# Enable HTTP request/response logging
logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(
    logging.ERROR
)
logging.basicConfig(level=logging.INFO)


def get_class_map_legend(client: PlanetaryComputerProClient):
    """Get a class map legend (categorical color map)."""
    result = client.data.get_class_map_legend(
        classmap_name=ColorMapNames.MTBS_SEVERITY,
    )
    logging.info(result)


def get_interval_legend(client: PlanetaryComputerProClient):
    """Get an interval legend (continuous color map)."""
    result = client.data.get_interval_legend(classmap_name=ColorMapNames.MODIS64_A1)
    logging.info(result)


def get_legend(client: PlanetaryComputerProClient):
    """Get a legend as a PNG image and save it locally."""
    legend_response = client.data.get_legend(color_map_name="rdylgn")

    # Save the legend to a file
    legend_bytes = b"".join(legend_response)
    filename = "legend_rdylgn.png"
    with open(filename, "wb") as f:
        f.write(legend_bytes)

    logging.info(f"Legend saved as: {filename} ({len(legend_bytes)} bytes)")


def main():
    endpoint = os.environ.get("PLANETARYCOMPUTER_ENDPOINT")

    if not endpoint:
        raise ValueError("PLANETARYCOMPUTER_ENDPOINT environment variable must be set")

    client = PlanetaryComputerProClient(
        endpoint=endpoint, credential=DefaultAzureCredential()
    )

    get_class_map_legend(client)
    get_interval_legend(client)
    get_legend(client)


if __name__ == "__main__":
    main()