예제 #1
0
 def test_no_url(self):
     old_stac_url = os.environ.get("STAC_URL")
     if old_stac_url:
         del os.environ["STAC_URL"]
     try:
         with pytest.raises(TypeError):
             Client.open()
     finally:
         if old_stac_url:
             os.environ["STAC_URL"] = old_stac_url
예제 #2
0
 def test_environment_variable(self):
     old_stac_url = os.environ.get("STAC_URL")
     os.environ["STAC_URL"] = ASTRAEA_URL
     try:
         client = Client.open()
         assert client.title == "Astraea Earth OnDemand"
     finally:
         if old_stac_url:
             os.environ["STAC_URL"] = old_stac_url
         else:
             del os.environ["STAC_URL"]
예제 #3
0
def stac_api_to_odc(
    dc: Datacube,
    update_if_exists: bool,
    config: dict,
    catalog_href: str,
    allow_unsafe: bool = True,
    rewrite: Optional[Tuple[str, str]] = None,
) -> Tuple[int, int]:
    doc2ds = Doc2Dataset(dc.index)
    client = Client.open(catalog_href)

    search = client.search(**config)
    n_items = search.matched()
    if n_items is not None:
        logging.info("Found {} items to index".format(n_items))
        if n_items == 0:
            logging.warning("Didn't find any items, finishing.")
            return 0, 0
    else:
        logging.warning("API did not return the number of items.")

    # Do the indexing of all the things
    success = 0
    failure = 0

    sys.stdout.write("\rIndexing from STAC API...\n")
    with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
        future_to_item = {
            executor.submit(
                process_item,
                item,
                dc,
                doc2ds,
                update_if_exists=update_if_exists,
                allow_unsafe=allow_unsafe,
                rewrite=rewrite,
            ): item.id
            for item in search.get_all_items()
        }
        for future in concurrent.futures.as_completed(future_to_item):
            item = future_to_item[future]
            try:
                _ = future.result()
                success += 1
                if success % 10 == 0:
                    sys.stdout.write(f"\rAdded {success} datasets...")
            except Exception as e:
                logging.exception(f"Failed to handle item {item} with exception {e}")
                failure += 1
    sys.stdout.write("\r")

    return success, failure
    def query_geom(self, geom_idx):
        geom = self.geoms[geom_idx]
        catalog = Client.open(
            "https://planetarycomputer.microsoft.com/api/stac/v1")

        search = catalog.search(
            collections=["sentinel-2-l2a"],
            intersects=geom,
            datetime=self.time_range,
            query={"eo:cloud_cover": {
                "lt": 10
            }},
        )

        items = list(search.get_items())
        return items[::-1]
예제 #5
0
        validate_core(root_body, warnings, errors)
    else:
        errors.append(
            "/ : 'conformsTo' must contain STAC API - Core conformance class.")

    if any(oaf_cc_regex.fullmatch(x) for x in conforms_to):
        print("STAC API - Features conformance class found.")
        validate_oaf(root_body, warnings, errors)

    if any(search_cc_regex.fullmatch(x) for x in conforms_to):
        print("STAC API - Item Search conformance class found.")
        validate_search(root_body, post, conforms_to, warnings, errors)

    if not errors:
        try:
            catalog = Client.open(root_url)
            catalog.validate()
            for collection in catalog.get_children():
                collection.validate()
        except STACValidationError as e:
            errors.append(f"pystac error: {str(e)}")

    return warnings, errors


def href_for(links: List, key: str) -> Dict[str, str]:
    return next((link for link in links if link.get("rel") == key), None)


def validate_core(root_body: Dict, warnings: List[str],
                  errors: List[str]) -> None: