12
12
STACValidationError ,
13
13
)
14
14
15
- from typing import Any , Dict , Optional , Union
15
+ from typing import Any , Dict , Optional
16
16
from pystac .version import (
17
17
__version__ ,
18
18
get_stac_version ,
72
72
)
73
73
74
74
75
- def read_file (href : str ) -> Union [ STACObject , ItemCollection ] :
75
+ def read_file (href : str ) -> STACObject :
76
76
"""Reads a STAC object from a file.
77
77
78
78
This method will return either a Catalog, a Collection, or an Item based on what the
@@ -86,15 +86,18 @@ def read_file(href: str) -> Union[STACObject, ItemCollection]:
86
86
Returns:
87
87
The specific STACObject implementation class that is represented
88
88
by the JSON read from the file located at HREF.
89
+
90
+ Raises:
91
+ STACTypeError : If the file at ``href`` does not represent a valid
92
+ :class:`~pystac.STACObject`. Note that an :class:`~pystac.ItemCollection` is not
93
+ a :class:`~pystac.STACObject` and must be read using
94
+ :meth:`ItemCollection.from_file <pystac.ItemCollection.from_file>`
89
95
"""
90
- try :
91
- return STACObject .from_file (href )
92
- except STACTypeError :
93
- return ItemCollection .from_file (href )
96
+ return STACObject .from_file (href )
94
97
95
98
96
99
def write_file (
97
- obj : Union [ STACObject , ItemCollection ] ,
100
+ obj : STACObject ,
98
101
include_self_link : bool = True ,
99
102
dest_href : Optional [str ] = None ,
100
103
) -> None :
@@ -113,51 +116,44 @@ def write_file(
113
116
Args:
114
117
obj : The STACObject to save.
115
118
include_self_link : If ``True``, include the ``"self"`` link with this object.
116
- Otherwise, leave out the self link. Ignored for :class:~ItemCollection`
117
- instances.
119
+ Otherwise, leave out the self link.
118
120
dest_href : Optional HREF to save the file to. If ``None``, the object will be
119
- saved to the object's ``"self"`` href (for :class:`~STACObject` sub-classes)
120
- or a :exc:`~STACError` will be raised (for :class:`~ItemCollection`
121
- instances).
121
+ saved to the object's ``"self"`` href.
122
122
"""
123
- if isinstance (obj , ItemCollection ):
124
- if dest_href is None :
125
- raise STACError ("Must provide dest_href when saving and ItemCollection." )
126
- obj .save_object (dest_href = dest_href )
127
- else :
128
- obj .save_object (include_self_link = include_self_link , dest_href = dest_href )
123
+ obj .save_object (include_self_link = include_self_link , dest_href = dest_href )
129
124
130
125
131
126
def read_dict (
132
127
d : Dict [str , Any ],
133
128
href : Optional [str ] = None ,
134
129
root : Optional [Catalog ] = None ,
135
130
stac_io : Optional [StacIO ] = None ,
136
- ) -> Union [ STACObject , ItemCollection ] :
131
+ ) -> STACObject :
137
132
"""Reads a :class:`~STACObject` or :class:`~ItemCollection` from a JSON-like dict
138
133
representing a serialized STAC object.
139
134
140
135
This method will return either a :class:`~Catalog`, :class:`~Collection`,
141
- :class`~Item`, or :class:`~ItemCollection ` based on the contents of the dict.
136
+ or :class`~Item ` based on the contents of the dict.
142
137
143
138
This is a convenience method for either
144
- :meth:`stac_io.stac_object_from_dict <stac_io.stac_object_from_dict>` or
145
- :meth:`ItemCollection.from_dict <ItemCollection.from_dict>`.
139
+ :meth:`stac_io.stac_object_from_dict <stac_io.stac_object_from_dict>`.
146
140
147
141
Args:
148
142
d : The dict to parse.
149
143
href : Optional href that is the file location of the object being
150
- parsed. Ignored if the dict represents an :class:`~ItemCollection`.
144
+ parsed.
151
145
root : Optional root of the catalog for this object.
152
146
If provided, the root's resolved object cache can be used to search for
153
- previously resolved instances of the STAC object. Ignored if the dict
154
- represents an :class:`~ItemCollection`.
147
+ previously resolved instances of the STAC object.
155
148
stac_io: Optional :class:`~StacIO` instance to use for reading. If ``None``,
156
149
the default instance will be used.
150
+
151
+ Raises:
152
+ STACTypeError : If the ``d`` dictionary does not represent a valid
153
+ :class:`~pystac.STACObject`. Note that an :class:`~pystac.ItemCollection` is not
154
+ a :class:`~pystac.STACObject` and must be read using
155
+ :meth:`ItemCollection.from_dict <pystac.ItemCollection.from_dict>`
157
156
"""
158
157
if stac_io is None :
159
158
stac_io = StacIO .default ()
160
- try :
161
- return stac_io .stac_object_from_dict (d , href , root )
162
- except STACTypeError :
163
- return ItemCollection .from_dict (d )
159
+ return stac_io .stac_object_from_dict (d , href , root )
0 commit comments