Skip to content

Commit 3f37e95

Browse files
committed
added raster migration
1 parent 90f37ee commit 3f37e95

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

pystac/extensions/mlm.py

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,36 +2178,67 @@ def migrate(props_obj: dict[str, Any]) -> None:
21782178
if "mlm:input" not in props_obj:
21792179
return
21802180

2181-
bands_objs_present = any("bands" in inp for inp in props_obj["mlm:input"])
2181+
# check if mlm:input.bands is present and contains items
2182+
bands_objs_present = [
2183+
"bands" in inp and len(inp["bands"]) > 0
2184+
for inp in props_obj["mlm:input"]
2185+
]
21822186

2183-
if not bands_objs_present:
2187+
if not any(bands_objs_present):
21842188
return
21852189

2186-
if "raster:bands" not in props_obj:
2190+
if "eo:bands" in props_obj or "bands" in props_obj:
21872191
return
2188-
raster_bands = props_obj["raster:bands"]
21892192

2190-
# make sure all raster_bands have a name prop with length>0
2191-
names_properties_valid = all(
2192-
"name" in band and len(band["name"]) > 0 for band in raster_bands
2193-
)
2194-
if not names_properties_valid:
2195-
raise STACError(
2196-
"Error migrating stac:mlm version: In mlm>=1.3, each band in "
2197-
'raster:bands is required to have a property "name"'
2193+
if (
2194+
"raster:bands" in props_obj
2195+
and "eo:bands" not in props_obj
2196+
and "bands" not in props_obj
2197+
):
2198+
raster_bands = props_obj["raster:bands"]
2199+
2200+
bands_valid = all(
2201+
"name" in band and len(band["name"]) > 0 for band in raster_bands
21982202
)
21992203

2200-
# no need to perform the actions below if props_obj is an asset
2201-
# this is checked by the presence of "roles" prop
2202-
if "roles" in props_obj:
2203-
return
2204+
if not bands_valid:
2205+
raise STACError(
2206+
"Error migrating stac:mlm version: In mlm>=1.3, each band in "
2207+
'raster:bands is required to have a property "name" with '
2208+
"length > 0"
2209+
)
22042210

2205-
# copy the raster:bands to assets
2206-
for inner_asset_name in obj["assets"]:
2207-
inner_asset = obj["assets"][inner_asset_name]
2208-
if "mlm:model" not in inner_asset["roles"]:
2209-
continue
2210-
inner_asset["raster:bands"] = raster_bands
2211+
# no need to perform the actions below if props_obj is not an asset
2212+
# this is checked by the presence of "roles" prop
2213+
if "roles" in props_obj:
2214+
return
2215+
2216+
# move raster:bands to assets that contain "mlm:model" role
2217+
for inner_asset_name in obj["assets"]:
2218+
inner_asset = obj["assets"][inner_asset_name]
2219+
if "mlm:model" not in inner_asset["roles"]:
2220+
continue
2221+
inner_asset["raster:bands"] = raster_bands
2222+
props_obj.pop("raster:bands")
2223+
2224+
# create new bands object from mlm:input.bands if none exist
2225+
if (
2226+
"raster:bands" not in props_obj
2227+
and "eo:bands" not in props_obj
2228+
and "bands" not in props_obj
2229+
):
2230+
i = bands_objs_present.index(True)
2231+
bands = [
2232+
{"name": band if type(band) is str else band["name"]}
2233+
for band in props_obj["mlm:input"][i]["bands"]
2234+
]
2235+
2236+
# copy the raster:bands to assets
2237+
for inner_asset_name in obj["assets"]:
2238+
inner_asset = obj["assets"][inner_asset_name]
2239+
if "mlm:model" not in inner_asset["roles"]:
2240+
continue
2241+
inner_asset["raster:bands"] = bands
22112242

22122243
if obj["type"] == "Feature" and "mlm:input" in obj["properties"]:
22132244
migrate(obj["properties"])
@@ -2307,7 +2338,7 @@ def migrate(props_obj: dict[str, Any]) -> None:
23072338

23082339
# add new REQUIRED proretie mlm:artifact_type to asset
23092340
if "mlm:model" in obj["assets"][asset]["roles"]:
2310-
obj["assets"][asset]["mlm:artifact_type"] = ""
2341+
obj["assets"][asset]["mlm:artifact_type"] = "asdf"
23112342

23122343
def migrate(
23132344
self, obj: dict[str, Any], version: STACVersionID, info: STACJSONDescription

0 commit comments

Comments
 (0)