Пример #1
0
    def __get_item_list(self, search_results, catalog):
        """Build STAC items list from EODAG search results

        :param search_results: EODAG search results
        :type search_results: :class:`eodag.api.search_result.SearchResult`
        :param catalog: STAC catalog dict used for parsing item metadata
        :type catalog: dict
        :returns: STAC item dicts list
        :rtype: list
        """
        if len(search_results) <= 0:
            return []

        item_model = self.__filter_item_model_properties(
            self.stac_config["item"], search_results[0].product_type)

        item_list = []
        for product in search_results:
            # parse jsonpath
            product_item = jsonpath_parse_dict_items(
                item_model, {"product": product.__dict__})
            # parse f-strings
            format_args = copy.deepcopy(self.stac_config)
            format_args["catalog"] = catalog
            format_args["item"] = product_item
            product_item = format_dict_items(product_item, **format_args)
            product_item["bbox"] = [float(i) for i in product_item["bbox"]]

            # remove empty properties
            product_item = self.__filter_item_properties_values(product_item)

            item_list.append(product_item)

        return item_list
Пример #2
0
    def build_locations_config(self):
        """Build locations config from stac_conf[locations_catalogs] & eodag_api.locations_config

        :returns: locations configuration dict
        :rtype: dict
        """
        user_config_locations_list = self.eodag_api.locations_config

        locations_config_model = copy.deepcopy(
            self.stac_config["locations_catalogs"])

        locations_config = {}
        for loc in user_config_locations_list:
            # parse jsonpath
            parsed = jsonpath_parse_dict_items(locations_config_model,
                                               {"shp_location": loc})

            # set default child/parent for this location
            parsed["location"]["parent_key"] = "{}_list".format(loc["name"])

            locations_config["{}_list".format(
                loc["name"])] = parsed["locations_list"]
            locations_config[loc["name"]] = parsed["location"]

        return locations_config
Пример #3
0
    def __get_collection_list(self, filters=None):
        """Build STAC collections list

        :param filters: additional filters for collections search
        :type filters: dict
        :returns: STAC collection dicts list
        :rtype: list
        """
        collection_model = copy.deepcopy(self.stac_config["collection"])

        product_types = self.__get_product_types(filters)

        collection_list = []
        for product_type in product_types:
            # get default provider for each product_type
            product_type_provider = (self.provider or next(
                self.eodag_api._plugins_manager.get_search_plugins(
                    product_type=product_type["ID"])).provider)

            # parse jsonpath
            product_type_collection = jsonpath_parse_dict_items(
                collection_model,
                {
                    "product_type":
                    product_type,
                    "provider":
                    self.eodag_api.providers_config[product_type_provider].
                    __dict__,
                },
            )
            # parse f-strings
            format_args = copy.deepcopy(self.stac_config)
            format_args["collection"] = dict(
                product_type_collection, **{
                    "url": self.url,
                    "root": self.root
                })
            product_type_collection = format_dict_items(
                product_type_collection, **format_args)

            collection_list.append(product_type_collection)

        return collection_list
Пример #4
0
    def get_stac_item_from_product(self, product):
        """Build STAC item from EODAG product

        :param product: EODAG product
        :type product: :class:`eodag.api.product._product.EOProduct`
        :returns: STAC item
        :rtype: list
        """
        product_type = product.product_type

        item_model = self.__filter_item_model_properties(
            self.stac_config["item"], product_type)

        catalog = StacCatalog(
            url=self.url.split("/items")[0],
            stac_config=self.stac_config,
            root=self.root,
            provider=self.provider,
            eodag_api=self.eodag_api,
            catalogs=[product_type],
        )

        # parse jsonpath
        product_item = jsonpath_parse_dict_items(item_model,
                                                 {"product": product.__dict__})
        # parse f-strings
        format_args = copy.deepcopy(self.stac_config)
        # format_args["collection"] = dict(catalog.as_dict(), **{"url": catalog.url})
        format_args["catalog"] = dict(
            catalog.as_dict(), **{
                "url": catalog.url,
                "root": catalog.root
            })
        format_args["item"] = product_item
        product_item = format_dict_items(product_item, **format_args)
        product_item["bbox"] = [float(i) for i in product_item["bbox"]]

        # remove empty properties
        product_item = self.__filter_item_properties_values(product_item)

        self.update_data(product_item)
        return self.as_dict()
Пример #5
0
    def get_stac_items(self, search_results, catalog):
        """Build STAC items from EODAG search results

        :param search_results: EODAG search results
        :type search_results: :class:`eodag.api.search_result.SearchResult`
        :param catalog: STAC catalog dict used for parsing item metadata
        :type catalog: dict
        :returns: STAC item dicts list
        :rtype: list

        :returns: items dictionnary
        :rtype: dict
        """
        items_model = copy.deepcopy(self.stac_config["items"])

        search_results.numberMatched = search_results.properties[
            "totalResults"]
        search_results.numberReturned = len(search_results)

        # next page link
        if "?" in self.url:
            # search endpoint: use page url as self link
            for i, _ in enumerate(items_model["links"]):
                if items_model["links"][i]["rel"] == "self":
                    items_model["links"][i]["href"] = catalog["url"]
            if "page=" not in self.url:
                search_results.next = "%s&page=%s" % (
                    self.url,
                    search_results.properties["page"] + 1,
                )
            else:
                search_results.next = re.sub(
                    r"^(.*)(page=[0-9]+)(.*)$",
                    r"\1page=%s\3" % (search_results.properties["page"] + 1),
                    self.url,
                )
        else:
            search_results.next = "%s?page=%s" % (
                self.url,
                search_results.properties["page"] + 1,
            )

        search_results.timeStamp = (datetime.datetime.now(
            datetime.timezone.utc).isoformat().replace("+00:00", "") + "Z")

        # parse jsonpath
        items = jsonpath_parse_dict_items(
            items_model, {"search_results": search_results.__dict__})
        # parse f-strings
        format_args = copy.deepcopy(self.stac_config)
        format_args["catalog"] = catalog
        items = format_dict_items(items, **format_args)

        # last page: remove next page link
        if (search_results.properties["itemsPerPage"] *
                search_results.properties["page"] >=
                search_results.properties["totalResults"]):
            items["links"] = [
                link for link in items["links"] if link["rel"] != "next"
            ]

        # provide static catalog to build features
        if "search?" in catalog["url"]:
            catalog["url"] = os.path.join(
                catalog["url"].split("search?")[0],
                "collections",
                catalog["id"],
            )
        else:
            catalog["url"] = catalog["url"].split("?")[0]
        items["features"] = self.__get_item_list(search_results, catalog)

        self.update_data(items)
        return self.as_dict()