datacosmos 0.0.17__py3-none-any.whl → 0.0.18__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of datacosmos might be problematic. Click here for more details.

@@ -3,7 +3,19 @@
3
3
  from enum import Enum
4
4
 
5
5
 
6
- class ProcessingLevel(Enum):
6
+ class CaseInsensitiveEnum(Enum):
7
+ """An enum that can be initialized with case-insensitive strings."""
8
+
9
+ @classmethod
10
+ def _missing_(cls, value: object):
11
+ if isinstance(value, str):
12
+ for member in cls:
13
+ if member.value.lower() == value.lower():
14
+ return member
15
+ return super()._missing_(value)
16
+
17
+
18
+ class ProcessingLevel(CaseInsensitiveEnum):
7
19
  """Enum class for the processing levels of the data."""
8
20
 
9
21
  RAW = "RAW"
@@ -1,28 +1,111 @@
1
1
  """Model representing a datacosmos item."""
2
2
 
3
+ import math
3
4
  from datetime import datetime
5
+ from typing import Any
4
6
 
5
- from pydantic import BaseModel
7
+ from pydantic import BaseModel, ConfigDict, field_validator, model_validator
8
+ from shapely.errors import ShapelyError
9
+ from shapely.geometry import Polygon, shape
6
10
 
11
+ from datacosmos.exceptions.datacosmos_exception import DatacosmosException
7
12
  from datacosmos.stac.enums.processing_level import ProcessingLevel
8
13
  from datacosmos.stac.item.models.asset import Asset
9
14
 
15
+ _REQUIRED_DATACOSMOS_PROPERTIES = [
16
+ "datetime",
17
+ "processing:level",
18
+ "sat:platform_international_designator",
19
+ ]
20
+
10
21
 
11
22
  class DatacosmosItem(BaseModel):
12
- """Model representing a datacosmos item."""
23
+ """Model representing a flexible Datacosmos STAC item with mandatory business fields."""
24
+
25
+ model_config = ConfigDict(extra="allow")
13
26
 
14
27
  id: str
15
28
  type: str
16
- stac_version: str
17
- stac_extensions: list | None
18
- geometry: dict
19
- properties: dict
20
- links: list
29
+ geometry: dict[str, Any]
30
+ bbox: list[float]
31
+ properties: dict[str, Any]
32
+
33
+ links: list[dict[str, Any]]
21
34
  assets: dict[str, Asset]
22
- collection: str
23
- bbox: tuple[float, float, float, float]
24
35
 
25
- def get_property(self, key: str) -> str | None:
36
+ stac_version: str | None = None
37
+ stac_extensions: list[str] | None = None
38
+ collection: str | None = None
39
+
40
+ @field_validator("properties", mode="before")
41
+ @classmethod
42
+ def validate_datacosmos_properties(
43
+ cls, properties_data: dict[str, Any]
44
+ ) -> dict[str, Any]:
45
+ """Validates that Datacosmos-specific properties exist."""
46
+ missing_keys = [
47
+ key for key in _REQUIRED_DATACOSMOS_PROPERTIES if key not in properties_data
48
+ ]
49
+
50
+ if missing_keys:
51
+ raise DatacosmosException(
52
+ f"Datacosmos-specific properties are missing: {', '.join(missing_keys)}."
53
+ )
54
+ return properties_data
55
+
56
+ @field_validator("geometry", mode="before")
57
+ @classmethod
58
+ def validate_geometry_is_polygon(
59
+ cls, geometry_data: dict[str, Any]
60
+ ) -> dict[str, Any]:
61
+ """Validates that the geometry is a Polygon with coordinates and correct winding order."""
62
+ if geometry_data.get("type") != "Polygon" or not geometry_data.get(
63
+ "coordinates"
64
+ ):
65
+ raise DatacosmosException("Geometry must be a Polygon with coordinates.")
66
+
67
+ try:
68
+ # Use shape() for robust GeoJSON parsing and validation
69
+ polygon = shape(geometry_data)
70
+
71
+ if not polygon.is_valid:
72
+ raise ValueError(f"Polygon geometry is invalid: {polygon.geom_type}")
73
+
74
+ # right-hand rule validation:
75
+ # The right-hand rule means exterior ring must be counter-clockwise (CCW).
76
+ # Shapely's Polygon stores the exterior as CCW if the input is valid.
77
+ if not polygon.exterior.is_ccw:
78
+ raise ValueError(
79
+ "Polygon winding order violates GeoJSON Right-Hand Rule (Exterior ring is clockwise)."
80
+ )
81
+
82
+ except (KeyError, ShapelyError, ValueError) as e:
83
+ raise DatacosmosException(f"Invalid geometry data: {e}") from e
84
+
85
+ return geometry_data
86
+
87
+ @model_validator(mode="after")
88
+ def validate_bbox_vs_geometry(self) -> "DatacosmosItem":
89
+ """Validates that the bbox tightly encloses the geometry."""
90
+ if self.geometry and self.bbox:
91
+ try:
92
+ geom_shape = shape(self.geometry)
93
+ true_bbox = list(geom_shape.bounds)
94
+
95
+ # Check for floating point equality within a tolerance
96
+ if not all(
97
+ math.isclose(a, b, rel_tol=1e-9)
98
+ for a, b in zip(self.bbox, true_bbox)
99
+ ):
100
+ raise DatacosmosException(
101
+ "Provided bbox does not match geometry bounds."
102
+ )
103
+ except Exception as e:
104
+ # Catch any errors from Shapely or the comparison
105
+ raise DatacosmosException(f"Invalid bbox or geometry: {e}") from e
106
+ return self
107
+
108
+ def get_property(self, key: str) -> Any | None:
26
109
  """Get a property value from the Datacosmos item."""
27
110
  return self.properties.get(key)
28
111
 
@@ -43,12 +126,13 @@ class DatacosmosItem(BaseModel):
43
126
  @property
44
127
  def sat_int_designator(self) -> str:
45
128
  """Get the satellite international designator of the Datacosmos item."""
46
- property = self.get_property("sat:platform_international_designator")
47
- if property is None:
48
- raise ValueError(
49
- "sat:platform_international_designator is missing in STAC item"
50
- )
51
- return property
129
+ return self.properties["sat:platform_international_designator"]
130
+
131
+ @property
132
+ def polygon(self) -> Polygon:
133
+ """Returns the polygon of the item."""
134
+ coordinates = self.geometry["coordinates"][0]
135
+ return Polygon(coordinates)
52
136
 
53
137
  def to_dict(self) -> dict:
54
138
  """Converts the DatacosmosItem instance to a dictionary."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datacosmos
3
- Version: 0.0.17
3
+ Version: 0.0.18
4
4
  Summary: A library for interacting with DataCosmos from Python code
5
5
  Author-email: Open Cosmos <support@open-cosmos.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -16,6 +16,7 @@ Requires-Dist: pystac==1.12.1
16
16
  Requires-Dist: pyyaml==6.0.2
17
17
  Requires-Dist: structlog==24.4.0
18
18
  Requires-Dist: tenacity>=8.2.3
19
+ Requires-Dist: shapely>=1.8.0
19
20
  Provides-Extra: dev
20
21
  Requires-Dist: black==22.3.0; extra == "dev"
21
22
  Requires-Dist: ruff==0.9.5; extra == "dev"
@@ -29,7 +29,7 @@ datacosmos/stac/collection/models/collection_update.py,sha256=XC6-29nLz1VGWMxYAw
29
29
  datacosmos/stac/constants/__init__.py,sha256=dDRSsF7CKqNF44yIlNdE-PD1sp0Q5mhTEPT7hHIK7YE,26
30
30
  datacosmos/stac/constants/satellite_name_mapping.py,sha256=EJqNdO9uW5B-sIeDF72AjnW7va5BM9mm4oNwijtl51w,575
31
31
  datacosmos/stac/enums/__init__.py,sha256=GUEL2xGtdjsrszrxivs0X6daxkaZs2JsTu2JoBtsvB4,22
32
- datacosmos/stac/enums/processing_level.py,sha256=kWGhpMXgzoyTzwR4yoSFd3UQ5IBw3cUf3TaM1FaOtUg,263
32
+ datacosmos/stac/enums/processing_level.py,sha256=_k4FO818VlZWtlm-rULhg-CYkbewkOdTUfqeCfHN8h4,641
33
33
  datacosmos/stac/enums/product_type.py,sha256=7lL0unJ1hxevW8Pepn9rmydUUWIORu2x4MEtp6rSFbA,196
34
34
  datacosmos/stac/enums/season.py,sha256=QvUzXBYtPEfixhlbV0SAw2u_HK3tRFEnHKshJyIatdg,241
35
35
  datacosmos/stac/item/__init__.py,sha256=lRuD_yp-JxoLqBA23q0XMkCNImf4T-X3BJnSw9u_3Yk,200
@@ -37,7 +37,7 @@ datacosmos/stac/item/item_client.py,sha256=HCHl3cHp0u2qxbwLxPk0xkujC1D4uwIBIFI-f
37
37
  datacosmos/stac/item/models/__init__.py,sha256=bcOrOcIxGxGBrRVIyQVxSM3C3Xj_qzxIHgQeWo6f7Q8,34
38
38
  datacosmos/stac/item/models/asset.py,sha256=mvg_fenYCGOTMGwXXpK2nyqBk5RMsUYxl6KhQTWW_b0,631
39
39
  datacosmos/stac/item/models/catalog_search_parameters.py,sha256=3HrUm37VezujwuCR45jhMryS5m1FGc1XmX8-fdTy4jU,4870
40
- datacosmos/stac/item/models/datacosmos_item.py,sha256=AImz0GRxrpZfIETdzzNfaKX35wpr39Q4f4u0z6r8eys,1745
40
+ datacosmos/stac/item/models/datacosmos_item.py,sha256=MJXUJu_kNJIoaTfIG05OlSys9th3KI4hgbzj2m2xI0Q,5027
41
41
  datacosmos/stac/item/models/eo_band.py,sha256=YC3Scn_wFhIo51pIVcJeuJienF7JGWoEv39JngDM6rI,309
42
42
  datacosmos/stac/item/models/item_update.py,sha256=_CpjQn9SsfedfuxlHSiGeptqY4M-p15t9YX__mBRueI,2088
43
43
  datacosmos/stac/item/models/raster_band.py,sha256=CoEVs-YyPE5Fse0He9DdOs4dGZpzfCsCuVzOcdXa_UM,354
@@ -54,8 +54,8 @@ datacosmos/utils/http_response/check_api_response.py,sha256=dKWW01jn2_lWV0xpOBAB
54
54
  datacosmos/utils/http_response/models/__init__.py,sha256=Wj8YT6dqw7rAz_rctllxo5Or_vv8DwopvQvBzwCTvpw,45
55
55
  datacosmos/utils/http_response/models/datacosmos_error.py,sha256=Uqi2uM98nJPeCbM7zngV6vHSk97jEAb_nkdDEeUjiQM,740
56
56
  datacosmos/utils/http_response/models/datacosmos_response.py,sha256=oV4n-sue7K1wwiIQeHpxdNU8vxeqF3okVPE2rydw5W0,336
57
- datacosmos-0.0.17.dist-info/licenses/LICENSE.md,sha256=vpbRI-UUbZVQfr3VG_CXt9HpRnL1b5kt8uTVbirxeyI,1486
58
- datacosmos-0.0.17.dist-info/METADATA,sha256=HyL2q5BSp_lua5GsQbDpAXRV5c7CE9jBX_Yy1iIOHYc,970
59
- datacosmos-0.0.17.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
60
- datacosmos-0.0.17.dist-info/top_level.txt,sha256=ueobs5CNeyDbPMgXPcVV0d0yNdm8CvGtDT3CaksRVtA,11
61
- datacosmos-0.0.17.dist-info/RECORD,,
57
+ datacosmos-0.0.18.dist-info/licenses/LICENSE.md,sha256=vpbRI-UUbZVQfr3VG_CXt9HpRnL1b5kt8uTVbirxeyI,1486
58
+ datacosmos-0.0.18.dist-info/METADATA,sha256=rTT3QYoHGLf1LxLT7i11oLG68l0KUjXaYeC07hFvjOs,1000
59
+ datacosmos-0.0.18.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
60
+ datacosmos-0.0.18.dist-info/top_level.txt,sha256=ueobs5CNeyDbPMgXPcVV0d0yNdm8CvGtDT3CaksRVtA,11
61
+ datacosmos-0.0.18.dist-info/RECORD,,