Skip to content

Commit 7daeb60

Browse files
Merge branch 'develop' into mah/faster_generate_subcats
2 parents 5c16701 + a29705f commit 7daeb60

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Added
66

7-
- HIERARCHICAL_LINKS array constant of all the types of hierarchical links (self is not included)
7+
- HIERARCHICAL_LINKS array constant of all the types of hierarchical links (self is not included) ([#290](https://github.com/stac-utils/pystac/pull/290))
88

99
### Fixed
1010

@@ -13,11 +13,13 @@
1313

1414
### Changed
1515

16-
- Link behavior - link URLs can be either relative or absolute. Hierarchical (e.g., parent, child) links are made relative or absolute based on the value of the root catalog's `catalog_type` field
16+
- Link behavior - link URLs can be either relative or absolute. Hierarchical (e.g., parent, child) links are made relative or absolute based on the value of the root catalog's `catalog_type` field ([#290](https://github.com/stac-utils/pystac/pull/290))
17+
- Internal self hrefs are set automatically when adding Items or Children to an existing catalog. This removes the need to call `normalize_hrefs` or manual setting of the hrefs for newly added STAC objects ([#294](https://github.com/stac-utils/pystac/pull/294))
18+
- Catalog.generate_subcatalogs is an order of magnitude faster ([#295](https://github.com/stac-utils/pystac/pull/295))
1719

1820
### Removed
1921

20-
- Removed LinkType class and the `link_type` field from links
22+
- Removed LinkType class and the `link_type` field from links ([#290](https://github.com/stac-utils/pystac/pull/290))
2123

2224
## [v0.5.5]
2325

pystac/catalog.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def set_root(self, root):
151151
def is_relative(self):
152152
return self.catalog_type in [CatalogType.RELATIVE_PUBLISHED, CatalogType.SELF_CONTAINED]
153153

154-
def add_child(self, child, title=None):
154+
def add_child(self, child, title=None, strategy=None):
155155
"""Adds a link to a child :class:`~pystac.Catalog` or :class:`~pystac.Collection`.
156156
This method will set the child's parent to this object, and its root to
157157
this Catalog's root.
@@ -165,8 +165,18 @@ def add_child(self, child, title=None):
165165
if isinstance(child, pystac.Item):
166166
raise STACError('Cannot add item as child. Use add_item instead.')
167167

168+
if strategy is None:
169+
strategy = BestPracticesLayoutStrategy()
170+
168171
child.set_root(self.get_root())
169172
child.set_parent(self)
173+
174+
# set self link
175+
self_href = self.get_self_href()
176+
if self_href:
177+
child_href = strategy.get_href(child, os.path.dirname(self_href))
178+
child.set_self_href(child_href)
179+
170180
self.add_link(Link.child(child, title=title))
171181

172182
def add_children(self, children):
@@ -180,7 +190,7 @@ def add_children(self, children):
180190
for child in children:
181191
self.add_child(child)
182192

183-
def add_item(self, item, title=None):
193+
def add_item(self, item, title=None, strategy=None):
184194
"""Adds a link to an :class:`~pystac.Item`.
185195
This method will set the item's parent to this object, and its root to
186196
this Catalog's root.
@@ -194,8 +204,18 @@ def add_item(self, item, title=None):
194204
if isinstance(item, pystac.Catalog):
195205
raise STACError('Cannot add catalog as item. Use add_child instead.')
196206

207+
if strategy is None:
208+
strategy = BestPracticesLayoutStrategy()
209+
197210
item.set_root(self.get_root())
198211
item.set_parent(self)
212+
213+
# set self link
214+
self_href = self.get_self_href()
215+
if self_href:
216+
item_href = strategy.get_href(item, os.path.dirname(self_href))
217+
item.set_self_href(item_href)
218+
199219
self.add_link(Link.item(item, title=title))
200220

201221
def add_items(self, items):
@@ -563,6 +583,11 @@ def generate_subcatalogs(self, template, defaults=None, parent_ids=None, **kwarg
563583
result.append(subcat)
564584
curr_parent = subcat
565585

586+
# resolve collection link so when added back points to correct location
587+
link = item.get_single_link('collection')
588+
if link is not None:
589+
link.resolve_stac_object()
590+
566591
curr_parent.add_item(item)
567592

568593
# keep only non-item links and item links that have not been moved elsewhere

0 commit comments

Comments
 (0)