Source code for descarteslabs.scenes.search_api

# Copyright 2018-2023 Descartes Labs.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from descarteslabs.exceptions import NotFoundError
from ..common.dotdict import DotDict, DotList
from ..client.deprecation import deprecate
from ..catalog import (
    Product,
    Band,
    SpectralBand,
    DerivedBand,
    Image,
    properties,
)

from ..common.geo import GeoContext, AOI

from .scenecollection import SceneCollection
from .helpers import REQUEST_PARAMS


# map from v1 field names to v2 field names for sort
IMAGE_FIELD_MAP = {
    "cloud_fraction_0": "alt_cloud_fraction",
    "descartes_version": "processing_pipeline_id",
    "identifier": "provider_id",
    "key": "name",
    "processed": "created",
    "proj4": "projection",
    "sat_id": "satellite_id",
}





[docs]@deprecate(removed=["metadata_client"]) def get_product(product_id): """Retrieve information about a single product. Parameters ---------- product_id : str Product Identifier. Returns ------- DotDict A dictionary with metadata for a single product. Raises ------ NotFoundError Raised if a product id cannot be found. """ product = Product.get(product_id, request_params=REQUEST_PARAMS) if product is None: raise NotFoundError(f"Product {product_id} not found") return DotDict(product.v1_properties)
[docs]@deprecate(removed=["bands", "offset", "metadata_client"]) def search_products( limit=None, owner=None, text=None, ): """Search products that are available on the platform. An empty search with no parameters will pass back all available products. Parameters ---------- limit : int, optional Number of results to return. owner : str, optional Filter products by the owner's uuid. text : str, optional Filter products by string match. Returns ------- DotList(DotDict) List of dicts containing at most `limit` products. Empty if no matching products are found. """ search = Product.search(request_params=REQUEST_PARAMS) if owner: search = search.filter(properties.owners.in_([owner])) if text: search = search.find_text(text) if limit: search = search.limit(limit) return DotList(DotDict(p.v1_properties) for p in search)
[docs]@deprecate(removed=["metadata_client"]) def get_band(band_id): """Get information about a single band. Parameters ---------- band_id : str A band identifier. Returns ------- DotDict A dictionary of metadata for the band Raises ------ NotFoundError Raised if a band id cannot be found. """ band = Band.get(band_id, request_params=REQUEST_PARAMS) if band is None: raise NotFoundError(f"Band {band_id} not found") return DotDict(band.v1_properties)
[docs]@deprecate(removed=["bands", "offset", "metadata_client"]) def search_bands( products, limit=None, wavelength=None, resolution=None, tags=None, ): """Search for imagery data bands that you have access to. Parameters ---------- products : list(str) A list of product(s) to return bands for. May not be empty. limit : int, optional Number of results to return. wavelength : float, optional A wavelength in nm e.g 700 that the band sensor must measure. resolution : int, optional The resolution in meters per pixel e.g 30 of the data available in this band. tags : list(str), optional A list of tags to match. Any band which has any of these tags will be included. Returns ------- DotList(DotDict) List of dicts containing at most `limit` bands. Empty if there are no bands matching query (e.g. product id not available). """ if wavelength: search = SpectralBand.search(request_params=REQUEST_PARAMS) else: search = Band.search(request_params=REQUEST_PARAMS) if not products: raise ValueError("Products is a required parameter.") elif isinstance(products, str): products = [products] search = search.filter(properties.product_id.in_(products)) if wavelength: search = search.filter(properties.wavelength_nm_min <= wavelength).filter( properties.wavelength_nm_max >= wavelength ) if resolution: search = search.filter(properties.resolution == resolution) if tags: if isinstance(tags, str): tags = [tags] search = search.filter(properties.tags.in_(tags)) if limit: search = search.limit(limit) return DotList(DotDict(b.v1_properties) for b in search)
[docs]@deprecate(removed=["metadata_client"]) def get_derived_band(derived_band_id): """Get information about a single derived band. Parameters ---------- derived_band_id : str Derived band identifier. Returns ------- DotDict A dictionary with metadata for a single derived band. Raises ------ NotFoundError Raised if a band id cannot be found. """ band = DerivedBand.get(derived_band_id, request_params=REQUEST_PARAMS) if band is None: raise NotFoundError(f"DerivedBand {derived_band_id} not found") return DotDict(band.v1_properties)
[docs]@deprecate(removed=["offset", "metadata_client"]) def search_derived_bands( bands, require_bands=None, limit=None, ): """Search for predefined derived bands that you have access to. Parameters ---------- bands : list(str) Limit the derived bands to ones that can be computed using this list of spectral bands. e.g ["red", "nir", "swir1"] require_bands : bool Control whether searched bands *must* contain all the spectral bands passed in the bands param. Defaults to False. limit : int Number of results to return. Returns ------- DotList(DotDict) List of dicts containing at most `limit` bands. """ search = DerivedBand.search(request_params=REQUEST_PARAMS) if bands: if isinstance(bands, str): bands = [bands] if require_bands: for band in bands: search = search.filter(properties.bands == band) else: search = search.filter(properties.bands.in_(bands)) if limit: search = search.limit(limit) return DotList(DotDict(db.v1_properties) for db in search)