Skip to content

gportal.search

search(dataset_ids=[], bbox=None, start_time=None, end_time=None, count=100, params={}, timeout=120)

Searches products on G-Portal with the given parameters.

Note that the return value is a Search instance. See its documentation for details on how to get the number of matches and iterate over results.

Parameters:

Name Type Description Default
dataset_ids Iterable[Union[str, int]]

List of dataset IDs.

[]
bbox Optional[Sequence[float]]

Bounding box of coordinates.

None
start_time Union[str, datetime, None]

Observation start time.

None
end_time Union[str, datetime, None]

Observation end time.

None
count int

Number of products per page.

100
params dict[str, Any]

Additional search parameters. See G-Portal User's Manual.

{}
timeout Optional[float]

Timeout in seconds.

120

Returns:

Type Description
Search

A Search instance that represents deferred query.

Source code in gportal/search.py
def search(
    dataset_ids: Iterable[Union[str, int]] = [],
    bbox: Optional[Sequence[float]] = None,
    start_time: Union[str, datetime, None] = None,
    end_time: Union[str, datetime, None] = None,
    count: int = 100,
    params: dict[str, Any] = {},
    timeout: Optional[float] = 120,
) -> "Search":
    """Searches products on G-Portal with the given parameters.

    Note that the return value is a [`Search`][gportal.search.Search] instance.
    See its documentation for details on how to get the number of matches and iterate over results.

    Args:
        dataset_ids: List of dataset IDs.
        bbox: Bounding box of coordinates.
        start_time: Observation start time.
        end_time: Observation end time.
        count: Number of products per page.
        params: Additional search parameters. See G-Portal User's Manual.
        timeout: Timeout in seconds.

    Returns:
        A [Search][gportal.search.Search] instance that represents deferred query.
    """
    params = params.copy()

    if dataset_ids:
        params["datasetId"] = ",".join(map(str, dataset_ids))

    if bbox is not None:
        params["bbox"] = ",".join(map(str, bbox))

    if isinstance(start_time, datetime):
        params["startTime"] = start_time.isoformat()
    elif start_time:
        params["startTime"] = start_time

    if isinstance(end_time, datetime):
        params["endTime"] = end_time.isoformat()
    elif end_time:
        params["endTime"] = end_time

    params["count"] = count

    return Search(params, timeout=timeout)

Search

Search query for G-Portal Catalogue Service API.

For API details, see G-Portal User's Manual Appendix 7: https://gportal.jaxa.jp/gpr/assets/mng_upload/COMMON/upload/GPortalUserManual_en.pdf

Attributes:

Name Type Description
params Mapping[str, Any]

Search parameters.

timeout Optional[float]

Timeout in seconds.

Source code in gportal/search.py
class Search:
    """Search query for G-Portal Catalogue Service API.

    For API details, see G-Portal User's Manual Appendix 7:
    https://gportal.jaxa.jp/gpr/assets/mng_upload/COMMON/upload/GPortalUserManual_en.pdf

    Attributes:
        params: Search parameters.
        timeout: Timeout in seconds.
    """

    def __init__(self, params: Mapping[str, Any], timeout: Optional[float] = None):
        self.params: Mapping[str, Any] = params
        self.timeout: Optional[float] = timeout

    def __repr__(self) -> str:
        return f"<gportal.Search params={self.params}>"

    @cache
    def matched(self) -> Optional[int]:
        """Gets the number of products matched the query.

        This method is cached, so it is safe to call it multiple times.

        Returns:
            The number of products matched the query.
        """
        response = self._request({"count": 0})
        return response.get("properties", {}).get("numberOfRecordsMatched")

    def products(self, convert_types: bool = True) -> Iterator[Product]:
        """Yields products matched the query.

        Args:
            convert_types: Whether to convert string values in properties into int, float, bool, or None.

        Yields:
            A [Product][gportal.product.Product] instance.
        """
        for page in self.pages():
            for product_dict in page.get("features", []):
                yield Product(product_dict, convert_types=convert_types)

    def pages(self) -> Iterator[dict[str, Any]]:
        """Iterates a search request with pagination.

        Yields:
            A dictionary of GeoJSON FeatureCollection.
        """
        start_index = 1

        while True:
            page = self._request({"startIndex": start_index})
            yield page

            properties = page["properties"]
            start_index += properties["numberOfRecordsReturned"]

            if start_index > properties["numberOfRecordsMatched"]:
                break

    def _request(self, extra_params: dict[str, Any]) -> dict[str, Any]:
        return http_client.get(
            "/csw/csw",
            params={
                **self.params,
                "service": "CSW",
                "version": "3.0.0",
                "request": "GetRecords",
                "outputFormat": "application/json",
                **extra_params,
            },
            timeout=self.timeout,
        )

matched() cached

Gets the number of products matched the query.

This method is cached, so it is safe to call it multiple times.

Returns:

Type Description
Optional[int]

The number of products matched the query.

Source code in gportal/search.py
@cache
def matched(self) -> Optional[int]:
    """Gets the number of products matched the query.

    This method is cached, so it is safe to call it multiple times.

    Returns:
        The number of products matched the query.
    """
    response = self._request({"count": 0})
    return response.get("properties", {}).get("numberOfRecordsMatched")

products(convert_types=True)

Yields products matched the query.

Parameters:

Name Type Description Default
convert_types bool

Whether to convert string values in properties into int, float, bool, or None.

True

Yields:

Type Description
Iterator[Product]

A Product instance.

Source code in gportal/search.py
def products(self, convert_types: bool = True) -> Iterator[Product]:
    """Yields products matched the query.

    Args:
        convert_types: Whether to convert string values in properties into int, float, bool, or None.

    Yields:
        A [Product][gportal.product.Product] instance.
    """
    for page in self.pages():
        for product_dict in page.get("features", []):
            yield Product(product_dict, convert_types=convert_types)

pages()

Iterates a search request with pagination.

Yields:

Type Description
Iterator[dict[str, Any]]

A dictionary of GeoJSON FeatureCollection.

Source code in gportal/search.py
def pages(self) -> Iterator[dict[str, Any]]:
    """Iterates a search request with pagination.

    Yields:
        A dictionary of GeoJSON FeatureCollection.
    """
    start_index = 1

    while True:
        page = self._request({"startIndex": start_index})
        yield page

        properties = page["properties"]
        start_index += properties["numberOfRecordsReturned"]

        if start_index > properties["numberOfRecordsMatched"]:
            break