Skip to content

Stac

STACConf

Bases: BaseModel

STAC mapping config model.

Parameters:

Name Type Description Default
stac_root_url str

STAC root URL.

required
stac_version str

STAC version.

required
stac_extensions list[str]

STAC extensions.

[]
Source code in stac_generator/plugins/mappings/stac.py
21
22
23
24
25
26
27
28
29
30
31
32
33
class STACConf(BaseModel):
    """STAC mapping config model."""

    stac_root_url: str = Field(
        description="STAC root URL.",
    )
    stac_version: str = Field(
        description="STAC version.",
    )
    stac_extensions: list[str] = Field(
        default=[],
        description="STAC extensions.",
    )

STACMapping

Bases: BaseMapping

Map metadata into STAC.

Mapping Name**: stac_mapping

Description:

Example Configuration:

.. code-block:: yaml

    - name: stac_mapping
      conf:
        stac_root_url: http://stac.catalog
        stac_version: 0.0.1
Source code in stac_generator/plugins/mappings/stac.py
 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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class STACMapping(BaseMapping):
    """Map metadata into STAC.

    Mapping Name**: ``stac_mapping``

    Description:


    Example Configuration:

        .. code-block:: yaml

            - name: stac_mapping
              conf:
                stac_root_url: http://stac.catalog
                stac_version: 0.0.1

    """

    config_class = STACConf

    def datetime_field(self, date_str: str) -> str:
        dt = parser.parse(date_str)
        return dt.strftime("%Y-%m-%dT%H:%M:%SZ")

    def item(self, body: dict) -> dict:
        output = {
            "type": "Feature",
            "stac_version": self.conf.stac_version,
            "stac_extensions": body.pop("stac_extensions", []) + self.conf.stac_extensions,
            "id": body.pop("id"),
            "collection": body.pop("collection"),
            "geometry": body.pop("geometry", None),
            "assets": body.pop("assets", {}),
            "properties": {
                "datetime": None,
            },
        }

        if "datetime" in body:
            output["properties"]["datetime"] = self.datetime_field(body.pop("datetime"))

        if "start_datetime" in body:
            output["properties"]["start_datetime"] = self.datetime_field(body.pop("start_datetime"))

        if "end_datetime" in body:
            output["properties"]["end_datetime"] = self.datetime_field(body.pop("end_datetime"))

        if "bbox" in body:
            output["bbox"] = body.pop("bbox")

        output["links"] = body.pop("links", []) + [
            {
                "rel": "self",
                "type": "application/geo+json",
                "href": f"{self.conf.stac_root_url}/collections/{output['collection']}/items/{output['id']}",
            },
            {
                "rel": "parent",
                "type": "application/json",
                "href": f"{self.conf.stac_root_url}/collections/{output['collection']}",
            },
            {
                "rel": "collection",
                "type": "application/json",
                "href": f"{self.conf.stac_root_url}/collections/{output['collection']}",
            },
            {
                "rel": "root",
                "type": "application/json",
                "href": self.conf.stac_root_url,
            },
        ]

        output["properties"] |= body

        return output

    def collection(self, body: dict) -> dict:
        output = {
            "type": "Collection",
            "stac_version": self.conf.stac_version,
            "stac_extensions": self.conf.stac_extensions,
            "id": body.pop("id"),
            "extent": {
                "temporal": {
                    "interval": None,
                },
                "spatial": {
                    "bbox": None,
                },
            },
            "summaries": {},
            "assets": {},
            "providers": [],
            "license": "",
        }

        if "description" in body:
            output["description"] = body.pop("description")

        if "interval" in body:
            output["extent"]["temporal"]["interval"] = body.pop("interval")

        if "bbox" in body:
            output["extent"]["spatial"]["bbox"] = body.pop("bbox")

        if "license" in body:
            output["license"] = body.pop("license")

        if "providers" in body:
            output["providers"] = body.pop("providers")

        if "member_of_recipes" in body:
            output["member_of_recipes"] = body.pop("member_of_recipes")

        output["summaries"] |= body

        output["links"] = [
            {
                "rel": "self",
                "type": "application/geo+json",
                "href": f"{self.conf.stac_root_url}/collections/{output['id']}",
            },
            {
                "rel": "parent",
                "type": "application/json",
                "href": f"{self.conf.stac_root_url}/",
            },
            {
                "rel": "queryables",
                "type": "application/json",
                "href": f"{self.conf.stac_root_url}/collections/{output['id']}/queryables",
            },
            {
                "rel": "items",
                "type": "application/geo+json",
                "href": f"{self.conf.stac_root_url}/collections/cmip6/{output['id']}",
            },
            {
                "rel": "root",
                "type": "application/json",
                "href": self.conf.stac_root_url,
            },
        ]

        return output

    def run(
        self,
        body: dict,
        recipe: Recipe,
        **kwargs,
    ) -> dict:
        if kwargs["GENERATOR_TYPE"] == "item":
            return self.item(body)

        if kwargs["GENERATOR_TYPE"] == "collection":
            return self.collection(body)

        return body