7
7
8
8
import pystac
9
9
from pystac import ExtensionTypeError , Item
10
- from pystac .extensions .classification import Classification , Bitfield , ClassificationExtension
10
+ from pystac .extensions .classification import (
11
+ Classification ,
12
+ Bitfield ,
13
+ ClassificationExtension ,
14
+ )
11
15
from pystac .utils import get_opt
12
16
import pytest
13
17
from tests .utils import TestCases
14
18
15
19
logging .basicConfig (level = logging .DEBUG )
16
20
logger = logging .getLogger ()
17
21
18
- LANDSAT_EXAMPLE_URI = TestCases .get_path ("data-files/classification/classification-landsat-example.json" )
19
- CLASSIFICATION_COLLECTION_URI = TestCases .get_path ("data-files/classification/collection-item-assets.json" )
22
+ LANDSAT_EXAMPLE_URI = TestCases .get_path (
23
+ "data-files/classification/classification-landsat-example.json"
24
+ )
25
+ CLASSIFICATION_COLLECTION_URI = TestCases .get_path (
26
+ "data-files/classification/collection-item-assets.json"
27
+ )
20
28
PLAIN_ITEM = TestCases .get_path ("data-files/item/sample-item.json" )
21
29
22
30
@@ -25,41 +33,50 @@ def item_dict() -> Dict[str, Any]:
25
33
with open (LANDSAT_EXAMPLE_URI ) as f :
26
34
return json .load (f )
27
35
36
+
28
37
@pytest .fixture
29
38
def landsat_item () -> Item :
30
39
return Item .from_file (LANDSAT_EXAMPLE_URI )
31
40
41
+
32
42
@pytest .fixture
33
43
def plain_item () -> Item :
34
44
return Item .from_file (PLAIN_ITEM )
35
45
46
+
36
47
def test_stac_extensions (landsat_item : Item ) -> None :
37
48
assert ClassificationExtension .has_extension (landsat_item )
38
49
50
+
39
51
def test_get_schema_uri (landsat_item : Item ) -> None :
40
52
assert ClassificationExtension .get_schema_uri () in landsat_item .stac_extensions
41
53
54
+
42
55
def test_ext_raises_if_item_does_not_conform (plain_item : Item ) -> None :
43
56
with pytest .raises (pystac .errors .ExtensionNotImplemented ):
44
57
ClassificationExtension .ext (plain_item )
45
58
59
+
46
60
def test_apply (plain_item : Item ) -> None :
47
61
ClassificationExtension .add_to (plain_item )
48
- ClassificationExtension .ext (plain_item ).apply (bitfields = [
49
- Bitfield .create (
50
- offset = 0 ,
51
- length = 1 ,
52
- classes = [
53
- Classification .create (name = "no" , value = 0 ),
54
- Classification .create (name = "yes" , value = 1 )
55
- ]
56
- )
57
- ])
62
+ ClassificationExtension .ext (plain_item ).apply (
63
+ bitfields = [
64
+ Bitfield .create (
65
+ offset = 0 ,
66
+ length = 1 ,
67
+ classes = [
68
+ Classification .create (name = "no" , value = 0 ),
69
+ Classification .create (name = "yes" , value = 1 ),
70
+ ],
71
+ )
72
+ ]
73
+ )
58
74
# plain_item.validate() ## THIS FAILS
59
- assert len (ClassificationExtension .ext (plain_item ).bitfields )== 1
60
- assert ClassificationExtension .ext (plain_item ).bitfields [0 ].offset == 0
61
- assert ClassificationExtension .ext (plain_item ).bitfields [0 ].length == 1
62
- assert len (ClassificationExtension .ext (plain_item ).bitfields [0 ].classes )== 2
75
+ assert len (ClassificationExtension .ext (plain_item ).bitfields ) == 1
76
+ assert ClassificationExtension .ext (plain_item ).bitfields [0 ].offset == 0
77
+ assert ClassificationExtension .ext (plain_item ).bitfields [0 ].length == 1
78
+ assert len (ClassificationExtension .ext (plain_item ).bitfields [0 ].classes ) == 2
79
+
63
80
64
81
def test_create () -> None :
65
82
field = Bitfield .create (
@@ -69,43 +86,32 @@ def test_create() -> None:
69
86
length = 2 ,
70
87
classes = [
71
88
Classification .create (
72
- name = "not_set" ,
73
- description = "No confidence level set" ,
74
- value = 0
89
+ name = "not_set" , description = "No confidence level set" , value = 0
75
90
),
76
91
Classification .create (
77
- name = "low" ,
78
- description = "Low confidence cloud" ,
79
- value = 1
92
+ name = "low" , description = "Low confidence cloud" , value = 1
80
93
),
81
94
Classification .create (
82
- name = "medium" ,
83
- description = "Medium confidence cloud" ,
84
- value = 2
95
+ name = "medium" , description = "Medium confidence cloud" , value = 2
85
96
),
86
97
Classification .create (
87
- name = "high" ,
88
- description = "High confidence cloud" ,
89
- value = 3
90
- )
91
- ]
98
+ name = "high" , description = "High confidence cloud" , value = 3
99
+ ),
100
+ ],
92
101
)
93
102
94
- assert field .__repr__ () == "<Bitfield offset=8 length=2 classes=[<Classification name=not_set value=0>, <Classification name=low value=1>, <Classification name=medium value=2>, <Classification name=high value=3>]>"
103
+ assert (
104
+ field .__repr__ ()
105
+ == "<Bitfield offset=8 length=2 classes=[<Classification name=not_set value=0>, <Classification name=low value=1>, <Classification name=medium value=2>, <Classification name=high value=3>]>"
106
+ )
95
107
96
108
logger .info (field )
97
109
110
+
98
111
def test_incomplete_fields ():
99
112
with pytest .raises (Exception ):
100
113
field = Bitfield .create (
101
- offset = 2 ,
102
- length = 1 ,
103
- classes = [
104
- Classification .create (
105
- name = "dummy" ,
106
- value = 1
107
- )
108
- ]
114
+ offset = 2 , length = 1 , classes = [Classification .create (name = "dummy" , value = 1 )]
109
115
)
110
116
111
117
@@ -130,6 +136,7 @@ def _parse_times(a_dict: Dict[str, Any]) -> None:
130
136
_parse_times (d2 )
131
137
assert d1 == d2 , f"Mismatch between dictionaries: \n { d1 } \n { d2 } "
132
138
139
+
133
140
def test_add_to (plain_item : Item ) -> None :
134
141
assert ClassificationExtension .get_schema_uri () not in plain_item .stac_extensions
135
142
@@ -143,12 +150,16 @@ def test_add_to(plain_item: Item) -> None:
143
150
ClassificationExtension .add_to (plain_item )
144
151
145
152
classification_uris = [
146
- uri for uri in plain_item .stac_extensions if uri == ClassificationExtension .get_schema_uri ()
153
+ uri
154
+ for uri in plain_item .stac_extensions
155
+ if uri == ClassificationExtension .get_schema_uri ()
147
156
]
148
157
assert len (classification_uris ) == 1
149
158
159
+
150
160
def test_validate_classification (landsat_item : Item ) -> None :
151
161
landsat_item .validate ()
152
162
163
+
153
164
def test_set_field (landsat_item : Item ) -> None :
154
- logger .warning (landsat_item .assets [' qa_pixel' ][ ' raster:bands' ])
165
+ logger .warning (landsat_item .assets [" qa_pixel" ][ " raster:bands" ])
0 commit comments