From 6ab76aad195c8ba287103dd93aeca606c9994f85 Mon Sep 17 00:00:00 2001 From: kmarkert Date: Sun, 10 Oct 2021 13:04:05 -0600 Subject: [PATCH] added filter and select methods to dataset class to make it easier to interface with collection opbjects --- hydrafloods/datasets.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/hydrafloods/datasets.py b/hydrafloods/datasets.py index 26b8216..03e0ae2 100644 --- a/hydrafloods/datasets.py +++ b/hydrafloods/datasets.py @@ -252,6 +252,35 @@ def apply(self, func, inplace=False, *args, **kwargs): return self.apply_func(func, inplace, *args, *kwargs) + def filter(self, filter, inplace=False): + """Wrapper method for applying a filter to a datset collection. + + args: + filter (ee.Filter): an `ee.Filter` object to apply to the dataset collection. + + returns: + Dataset | None: returns the dataset with the filtered collection. + """ + + filtered = self.collection.filter(filter) + + return self._inplace_wrapper(filtered, inplace) + + def select(self, *args, inplace=False): + """Wrapper method for selecting bands from dataset collection + + args: + *args: arbitrary arguments to pass to the `ee.ImageCollection.select()` method + inplace (bool, optional): define whether to return another dataset object or update inplace. default = False + + returns: + Dataset | None: returns the dataset with the collection with selected bands. + + """ + selected = self.collection.select(*args) + + return self._inplace_wrapper(selected, inplace) + def clip_to_region(self, inplace=False): """Clips all of the images to the geographic extent defined by region. Useful for setting geometries on unbounded imagery in collection (e.g. MODIS or VIIRS imagery) @@ -589,9 +618,7 @@ def __init__(self, *args, asset_id="COPERNICUS/S1_GRD", use_qa=True, **kwargs): *args, asset_id=asset_id, use_qa=use_qa, **kwargs ) - self.collection = self.collection.filter( - ee.Filter.eq("orbitProperties_pass", "ASCENDING"), - ) + self.filter(ee.Filter.eq("orbitProperties_pass", "ASCENDING"), inplace=True) return @@ -614,9 +641,7 @@ def __init__(self, *args, asset_id="COPERNICUS/S1_GRD", use_qa=True, **kwargs): *args, asset_id=asset_id, use_qa=use_qa, **kwargs ) - self.collection = self.collection.filter( - ee.Filter.eq("orbitProperties_pass", "DESCENDING"), - ) + self.filter(ee.Filter.eq("orbitProperties_pass", "DESCENDING"), inplace=True) return