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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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"),
            "geometry": body.pop("geometry", None),
            "bbox": body.pop("bbox"),
            "properties": {
                "datetime": None,
            },
            "links": body.pop("links", [])
            + [
                {
                    "rel": "self",
                    "type": "application/geo+json",
                    "href": f"{self.conf.stac_root_url}/collections/{body['collection']}/items/{body['id']}",
                },
                {
                    "rel": "parent",
                    "type": "application/json",
                    "href": f"{self.conf.stac_root_url}/collections/{body['collection']}",
                },
                {
                    "rel": "collection",
                    "type": "application/json",
                    "href": f"{self.conf.stac_root_url}/collections/{body['collection']}",
                },
                {
                    "rel": "root",
                    "type": "application/json",
                    "href": self.conf.stac_root_url,
                },
            ],
            "assets": body.pop("assets", {}),
            "collection": body.pop("collection"),
        }

        if title := body.pop("title", None):
            output["properties"]["title"] = title

        if description := body.pop("description", None):
            output["properties"]["description"] = description

        if version := body.pop("version", None):
            output["properties"]["version"] = version

        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 project := body.pop("project", None):
            output["properties"]["project"] = project

        if license := body.pop("license", None):
            output["properties"]["license"] = license

        if sci_doi := body.pop("sci:doi", None):
            output["properties"]["sci:doi"] = sci_doi

        if sci_citation := body.pop("sci:citation", None):
            output["properties"]["sci:citation"] = sci_citation

        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"),
        }

        if title := body.pop("title", None):
            output["title"] = title

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

        if keywords := body.pop("keywords", []):
            output["keywords"] = keywords

        if license := body.pop("license", None):
            output["properties"]["license"] = license

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

        if extent := body.pop(
            "extent",
            {
                "temporal": {
                    "interval": None,
                },
                "spatial": {
                    "bbox": None,
                },
            },
        ):
            output["extent"] = extent

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

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

        output["links"] = body.pop("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,
            },
        ]

        if assets := body.pop("assets", {}):
            output["assets"] = assets

        if item_assets := body.pop("item_assets", {}):
            output["item_assets"] = item_assets

        output["summaries"] |= body

        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