forked from stac-utils/stac-pydantic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.py
159 lines (132 loc) · 5.16 KB
/
shared.py
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
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
from datetime import timezone
from enum import Enum, auto
from typing import Annotated, Any, Dict, List, Optional, Tuple, Union
from warnings import warn
from pydantic import (
AfterValidator,
AwareDatetime,
BaseModel,
ConfigDict,
Field,
PlainSerializer,
)
from stac_pydantic.utils import AutoValueEnum
NumType = Union[float, int]
BBox = Union[
Tuple[NumType, NumType, NumType, NumType], # 2D bbox
Tuple[NumType, NumType, NumType, NumType, NumType, NumType], # 3D bbox
]
SEMVER_REGEX = r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
# Allows for some additional flexibility in the input datetime format. As long as
# the input value has timezone information, it will be converted to UTC timezone.
UtcDatetime = Annotated[
# Input value must be in a format which has timezone information
AwareDatetime,
# Convert the input value to UTC timezone
AfterValidator(lambda d: d.astimezone(timezone.utc)),
# Use `isoformat` to serialize the value in an RFC3339 compatible format
PlainSerializer(lambda d: d.isoformat()),
]
class MimeTypes(str, Enum):
"""
https://github.com/radiantearth/stac-spec/blob/v1.0.0/item-spec/item-spec.md#media-types
"""
# Raster
geotiff = "image/tiff; application=geotiff"
cog = "image/tiff; application=geotiff; profile=cloud-optimized"
jp2 = "image/jp2"
png = "image/png"
jpeg = "image/jpeg"
# Vector
geojson = "application/geo+json"
geopackage = "application/geopackage+sqlite3"
kml = "application/vnd.google-earth.kml+xml"
kmz = "application/vnd.google-earth.kmz"
# Others
hdf = "application/x-hdf"
hdf5 = "application/x-hdf5"
xml = "application/xml"
json = "application/json"
html = "text/html"
text = "text/plain"
openapi = "application/vnd.oai.openapi+json;version=3.0"
class AssetRoles(str, AutoValueEnum):
"""
https://github.com/radiantearth/stac-spec/blob/v1.0.0/extensions/asset/README.md
"""
thumbnail = auto()
overview = auto()
data = auto()
metadata = auto()
class ProviderRoles(str, AutoValueEnum):
licensor = auto()
producer = auto()
processor = auto()
host = auto()
class StacBaseModel(BaseModel):
def to_dict(
self, by_alias: bool = True, exclude_unset: bool = True, **kwargs: Any
) -> Dict[str, Any]:
warn(
"`to_dict` method is deprecated. Use `model_dump` instead",
DeprecationWarning,
)
return self.model_dump(by_alias=by_alias, exclude_unset=exclude_unset, **kwargs)
def to_json(
self, by_alias: bool = True, exclude_unset: bool = True, **kwargs: Any
) -> str:
warn(
"`to_json` method is deprecated. Use `model_dump_json` instead",
DeprecationWarning,
)
return self.model_dump_json(
by_alias=by_alias, exclude_unset=exclude_unset, **kwargs
)
def model_dump(
self, *, by_alias: bool = True, exclude_unset: bool = True, **kwargs: Any
) -> Dict[str, Any]:
return super().model_dump(
by_alias=by_alias, exclude_unset=exclude_unset, **kwargs
)
def model_dump_json(
self, *, by_alias: bool = True, exclude_unset: bool = True, **kwargs: Any
) -> str:
return super().model_dump_json(
by_alias=by_alias, exclude_unset=exclude_unset, **kwargs
)
class Provider(StacBaseModel):
"""
https://github.com/radiantearth/stac-spec/blob/v1.0.0/collection-spec/collection-spec.md#provider-object
"""
name: str = Field(..., alias="name", min_length=1)
description: Optional[str] = None
roles: Optional[List[str]] = None
url: Optional[str] = None
class StacCommonMetadata(StacBaseModel):
"""
https://github.com/radiantearth/stac-spec/blob/v1.0.0/item-spec/common-metadata.md#date-and-time-range
"""
title: Optional[str] = Field(None, alias="title")
description: Optional[str] = Field(None, alias="description")
start_datetime: Optional[UtcDatetime] = Field(None, alias="start_datetime")
end_datetime: Optional[UtcDatetime] = Field(None, alias="end_datetime")
created: Optional[UtcDatetime] = Field(None, alias="created")
updated: Optional[UtcDatetime] = Field(None, alias="updated")
platform: Optional[str] = Field(None, alias="platform")
instruments: Optional[List[str]] = Field(None, alias="instruments")
constellation: Optional[str] = Field(None, alias="constellation")
mission: Optional[str] = Field(None, alias="mission")
providers: Optional[List[Provider]] = Field(None, alias="providers")
gsd: Optional[float] = Field(None, alias="gsd", gt=0)
class Asset(StacCommonMetadata):
"""
https://github.com/radiantearth/stac-spec/blob/v1.0.0/item-spec/item-spec.md#asset-object
"""
href: str = Field(..., alias="href", min_length=1)
type: Optional[str] = None
title: Optional[str] = None
description: Optional[str] = None
roles: Optional[List[str]] = None
model_config = ConfigDict(
populate_by_name=True, use_enum_values=True, extra="allow"
)