@@ -90,29 +90,37 @@ def item_func(item: Item, parent_dir: str) -> str:
90
90
91
91
return CustomLayoutStrategy (collection_func = collection_func , item_func = item_func )
92
92
93
- def replace_asset_href (asset_key : str , asset : Asset ) -> Asset :
93
+ def replace_asset_href (asset_key : str , asset : Asset , collection_href : str ) -> Asset :
94
94
if urlparse (asset .href ).scheme not in ["" , "file" ]: # TODO: convenient place; move elsewhere?
95
95
raise NotImplementedError (f"only importing files on disk is supported, found: { asset .href } " )
96
96
97
97
# TODO: crummy way to export assets after STAC Collection has been written to disk with new asset hrefs;
98
98
# it ends up in the asset metadata on disk
99
- asset .extra_fields ["_original_absolute_href" ] = asset .get_absolute_href ()
100
- asset .href = Path (asset_key ).name # asset key matches the asset filename, becomes the relative path
99
+ asset_href = asset .get_absolute_href ()
100
+ asset .extra_fields ["_original_absolute_href" ] = asset_href
101
+ if asset_href .startswith ("s3" ):
102
+ asset .href = Path (asset_href ).name
103
+ else :
104
+ common_path = os .path .commonpath ([asset_href ,collection_href ])
105
+ asset .href = os .path .relpath (asset_href ,common_path )
101
106
return asset
102
107
108
+ collection_href = new_collection .get_self_href ()
103
109
if not existing_collection :
104
110
new_collection .normalize_hrefs (root_href = str (target .parent ), strategy = href_layout_strategy ())
105
- new_collection = new_collection .map_assets (replace_asset_href )
111
+ new_collection = new_collection .map_assets (lambda k , v : replace_asset_href ( k , v , collection_href ) )
106
112
new_collection .save (CatalogType .SELF_CONTAINED )
107
113
108
114
for new_item in new_collection .get_items ():
109
115
for asset in new_item .get_assets ().values ():
116
+ asset_path = Path (new_item .get_self_href ()).parent / Path (asset .href ).parent
117
+ asset_path .mkdir (parents = True )
110
118
file_operation (
111
- asset .extra_fields ["_original_absolute_href" ], str (Path ( new_item . get_self_href ()). parent )
119
+ asset .extra_fields ["_original_absolute_href" ], str (asset_path )
112
120
)
113
121
else :
114
122
merged_collection = _merge_collection_metadata (existing_collection , new_collection )
115
- new_collection = new_collection .map_assets (replace_asset_href )
123
+ new_collection = new_collection .map_assets (lambda k , v : replace_asset_href ( k , v , collection_href ) )
116
124
117
125
for new_item in new_collection .get_items ():
118
126
new_item .clear_links () # sever ties with previous collection
@@ -123,13 +131,15 @@ def replace_asset_href(asset_key: str, asset: Asset) -> Asset:
123
131
124
132
for new_item in new_collection .get_items ():
125
133
for asset in new_item .get_assets ().values ():
134
+ asset_path = Path (new_item .get_self_href ()).parent / Path (asset .href ).parent
135
+ asset_path .mkdir (parents = True )
126
136
file_operation (
127
- asset .extra_fields ["_original_absolute_href" ], Path ( new_item . get_self_href ()). parent
137
+ asset .extra_fields ["_original_absolute_href" ], str ( asset_path )
128
138
)
129
139
130
140
for item in new_collection .get_items ():
131
141
for asset in item .assets .values ():
132
- workspace_uri = f"file:{ Path (item .get_self_href ()).parent / Path ( asset .href ). name } "
142
+ workspace_uri = f"file:{ Path (item .get_self_href ()).parent / asset .href } "
133
143
asset .extra_fields ["alternate" ] = {"file" : workspace_uri }
134
144
135
145
return new_collection
0 commit comments