@@ -130,36 +130,50 @@ class SummaryStrategy(Enum):
130
130
131
131
class Summarizer :
132
132
"""The Summarizer computes summaries from values, following the definition of fields
133
- to summarize provided in a json file .
133
+ to summarize.
134
134
135
- For more information about the structure of the fields json file, see:
135
+ The fields to summarize can be provided as a JSON file or as a dictionary of
136
+ field names and SummaryStrategys. If nothing is provided, a default JSON file
137
+ will be used.
136
138
139
+ For more information about the structure of the fields JSON file, see:
137
140
https://github.com/stac-utils/stac-fields
138
141
142
+ The default JSON file used is a snapshot of the following file at the time of
143
+ the pystac release:
144
+ https://cdn.jsdelivr.net/npm/@radiantearth/stac-fields/fields-normalized.json
145
+
139
146
Args:
140
- fields (str): the path to the json file with field descriptions.
141
- If no file is passed, a default one will be used.
147
+ fields: A string containing the path to the json file with field descriptions.
148
+ Alternatively, a dict with the field names as keys and SummaryStrategys
149
+ as values.
150
+ If nothing is passed, a default file with field descriptions will be used.
142
151
"""
143
152
144
153
summaryfields : Dict [str , SummaryStrategy ]
145
154
146
- def __init__ (self , fields : Optional [str ] = None ):
147
- jsonfields = _get_fields_json (fields )
148
- self ._set_field_definitions (jsonfields )
155
+ def __init__ (self , fields : Optional [Union [str , Dict [str , SummaryStrategy ]]] = None ):
156
+ if isinstance (fields , dict ):
157
+ self ._set_field_definitions (fields )
158
+ else :
159
+ jsonfields = _get_fields_json (fields )
160
+ self ._set_field_definitions (jsonfields ["metadata" ])
149
161
150
162
def _set_field_definitions (self , fields : Dict [str , Any ]) -> None :
151
163
self .summaryfields = {}
152
- for name , desc in fields ["metadata" ].items ():
153
- if isinstance (desc , dict ):
164
+ for name , desc in fields .items ():
165
+ strategy : SummaryStrategy = SummaryStrategy .DEFAULT
166
+ if isinstance (desc , SummaryStrategy ):
167
+ strategy = desc
168
+ elif isinstance (desc , dict ):
154
169
strategy_value = desc .get ("summary" , True )
155
170
try :
156
- strategy : SummaryStrategy = SummaryStrategy (strategy_value )
171
+ strategy = SummaryStrategy (strategy_value )
157
172
except ValueError :
158
- strategy = SummaryStrategy .DEFAULT
159
- if strategy != SummaryStrategy .DONT_SUMMARIZE :
160
- self .summaryfields [name ] = strategy
161
- else :
162
- self .summaryfields [name ] = SummaryStrategy .DEFAULT
173
+ pass
174
+
175
+ if strategy != SummaryStrategy .DONT_SUMMARIZE :
176
+ self .summaryfields [name ] = strategy
163
177
164
178
def _update_with_item (self , summaries : Summaries , item : Item ) -> None :
165
179
for k , v in item .properties .items ():
0 commit comments