def hrefs_from_catalog(catalog: Catalog, N: int = None) -> Tuple[str, str]: def find_label_collection(c): return 'label' in str.lower(c.description) catalog.make_all_asset_hrefs_absolute() labels = next(filter(find_label_collection, catalog.get_children())) label_items = list(labels.get_items()) label_hrefs = [] imagery_hrefs = [] for item in label_items: try: imagery = item.get_links('source')[0] imagery.resolve_stac_object() imagery_href = imagery.target.assets.get('cog').href except: imagery_href = None label_href = pystac_workaround(item.assets.get('data').href) label_hrefs.append(label_href) if imagery_href.startswith('./'): imagery_href = re.sub('^\.\/[0-9]+-', './', imagery_href) imagery_href = imagery_href.replace('14060at01p00r17', '140603t01p00r17') imagery_hrefs.append(imagery_href) if N is not None: N = int(N) label_hrefs = [label_hrefs[N]] imagery_hrefs = [imagery_hrefs[N]] return (label_hrefs, imagery_hrefs)
def validate_catalog(self, catalog: pystac.Catalog) -> int: catalog.validate() validated_count = 1 for child in catalog.get_children(): validated_count += self.validate_catalog(child) for item in catalog.get_items(): item.validate() validated_count += 1 return validated_count
def test_clear_children_removes_from_cache(self): catalog = Catalog(id='test', description='test') subcat = Catalog(id='subcat', description='test') catalog.add_child(subcat) children = list(catalog.get_children()) self.assertEqual(len(children), 1) self.assertEqual(children[0].description, 'test') catalog.clear_children() subcat = Catalog(id='subcat', description='test2') catalog.add_child(subcat) children = list(catalog.get_children()) self.assertEqual(len(children), 1) self.assertEqual(children[0].description, 'test2') catalog.remove_child('subcat') subcat = Catalog(id='subcat', description='test3') catalog.add_child(subcat) children = list(catalog.get_children()) self.assertEqual(len(children), 1) self.assertEqual(children[0].description, 'test3')
def hrefs_from_catalog(catalog: Catalog) -> Tuple[str, str]: catalog.make_all_asset_hrefs_absolute() catalog = next(catalog.get_children()) children = list(catalog.get_children()) imagery = next(filter(lambda child: \ "image" in str.lower(child.description), children)) imagery_item = next(imagery.get_items()) imagery_assets = list(imagery_item.assets.values()) imagery_href = pystac_workaround(imagery_assets[1].href) labels = next(filter(lambda child: \ "label" in str.lower(child.description), children)) labels_item = next(labels.get_items()) labels_href = pystac_workaround( next(iter(labels_item.assets.values())).href) label_dict = labels_item.to_dict() label_dict['properties']['class_id'] = None aoi_geometries = [label_dict] return (imagery_href, labels_href, aoi_geometries)
def test_clear_children_sets_parent_and_root_to_None(self): catalog = Catalog(id='test', description='test') subcat1 = Catalog(id='subcat', description='test') subcat2 = Catalog(id='subcat2', description='test2') catalog.add_children([subcat1, subcat2]) self.assertIsNotNone(subcat1.get_parent()) self.assertIsNotNone(subcat2.get_parent()) self.assertIsNotNone(subcat1.get_root()) self.assertIsNotNone(subcat2.get_root()) children = list(catalog.get_children()) self.assertEqual(len(children), 2) catalog.clear_children() self.assertIsNone(subcat1.get_parent()) self.assertIsNone(subcat2.get_parent()) self.assertIsNone(subcat1.get_root()) self.assertIsNone(subcat2.get_root())