RadGEEToolbox 1.6.10__py3-none-any.whl → 1.7.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2711 @@
1
+ import ee
2
+ import pandas as pd
3
+ import numpy as np
4
+
5
+
6
+ class GenericCollection:
7
+ """
8
+ Represents a user-defined RadGEEToolbox class collection of any ee.ImageCollection from Google Earth Engine (GEE).
9
+
10
+ This class enables simplified definition, filtering, masking, and processing of generic geospatial imagery.
11
+ It supports multiple spatial and temporal filters, and caching for efficient computation. It also includes utilities for cloud masking,
12
+ mosaicking, zonal statistics, and transect analysis.
13
+
14
+ Initialization can be done by providing filtering parameters or directly passing in a pre-filtered GEE collection.
15
+
16
+ Inspect the documentation or source code for details on the methods and properties available.
17
+
18
+ Args:
19
+ start_date (str): Start date in 'YYYY-MM-dd' format. Required unless `collection` is provided.
20
+ end_date (str): End date in 'YYYY-MM-dd' format. Required unless `collection` is provided.
21
+ boundary (ee.Geometry, optional): A geometry for filtering to images that intersect with the boundary shape. Overrides `tile_path` and `tile_row` if provided.
22
+ collection (ee.ImageCollection, optional): A pre-filtered Landsat ee.ImageCollection object to be converted to a GenericCollection object. Overrides all other filters.
23
+
24
+ Attributes:
25
+ collection (ee.ImageCollection): The filtered or user-supplied image collection converted to an ee.ImageCollection object.
26
+
27
+ Raises:
28
+ ValueError: Raised if required filter parameters are missing, or if both `collection` and other filters are provided.
29
+
30
+ Note:
31
+ See full usage examples in the documentation or notebooks:
32
+ https://github.com/radwinskis/RadGEEToolbox/tree/main/Example%20Notebooks
33
+
34
+ """
35
+
36
+ def __init__(
37
+ self,
38
+ collection=None,
39
+ start_date=None,
40
+ end_date=None,
41
+ boundary=None,
42
+ _dates_list=None
43
+ ):
44
+ if collection is None:
45
+ raise ValueError(
46
+ "The required `collection` argument has not been provided. Please specify an input ee.ImageCollection."
47
+ )
48
+
49
+ if isinstance(collection, GenericCollection):
50
+ base_collection = collection.collection
51
+ else:
52
+ # Otherwise, assume it's a valid ee.ImageCollection
53
+ base_collection = collection
54
+
55
+ if (start_date is not None and end_date is None) or \
56
+ (start_date is None and end_date is not None):
57
+ raise ValueError("Please provide both start_date and end_date, or provide neither for entire collection")
58
+
59
+ self.collection = base_collection
60
+ self.start_date = start_date
61
+ self.end_date = end_date
62
+ self.boundary = boundary
63
+
64
+ if self.start_date and self.end_date:
65
+ if self.boundary:
66
+ self.collection = self.get_boundary_and_date_filtered_collection()
67
+ else:
68
+ self.collection = self.get_filtered_collection()
69
+ elif self.boundary:
70
+ self.collection = self.get_boundary_filtered_collection()
71
+ else:
72
+ self.collection = self.get_generic_collection()
73
+
74
+ self._dates_list = _dates_list
75
+ self._dates = None
76
+ self._geometry_masked_collection = None
77
+ self._geometry_masked_out_collection = None
78
+ self._median = None
79
+ self._monthly_median = None
80
+ self._monthly_mean = None
81
+ self._monthly_sum = None
82
+ self._monthly_max = None
83
+ self._monthly_min = None
84
+ self._mean = None
85
+ self._max = None
86
+ self._min = None
87
+ self._MosaicByDate = None
88
+ self._PixelAreaSumCollection = None
89
+ self._daily_aggregate_collection = None
90
+
91
+ @staticmethod
92
+ def image_dater(image):
93
+ """
94
+ Adds date to image properties as 'Date_Filter'.
95
+
96
+ Args:
97
+ image (ee.Image): Input image
98
+
99
+ Returns:
100
+ ee.Image: Image with date in properties.
101
+ """
102
+ date = ee.Number(image.date().format("YYYY-MM-dd"))
103
+ return image.set({"Date_Filter": date})
104
+
105
+
106
+ @staticmethod
107
+ def anomaly_fn(image, geometry, band_name=None, anomaly_band_name=None, replace=True, scale=None):
108
+ """
109
+ Calculates the anomaly of a singleband image compared to the mean of the singleband image.
110
+
111
+ This function computes the anomaly for each band in the input image by
112
+ subtracting the mean value of that band from a provided image.
113
+ The anomaly is a measure of how much the pixel values deviate from the
114
+ average conditions represented by the mean of the image.
115
+
116
+ Args:
117
+ image (ee.Image): An ee.Image for which the anomaly is to be calculated.
118
+ It is assumed that this image is a singleband image.
119
+ geometry (ee.Geometry): The geometry for image reduction to define the mean value to be used for anomaly calculation.
120
+ band_name (str, optional): A string representing the band name to be used for the output anomaly image. If not provided, the band name of the first band of the input image will be used.
121
+ anomaly_band_name (str, optional): A string representing the band name to be used for the output anomaly image. If not provided, the band name of the first band of the input image will be used.
122
+ replace (bool, optional): A boolean indicating whether to replace the original band with the anomaly band in the output image. If True, the output image will contain only the anomaly band. If False, the output image will contain both the original band and the anomaly band. Default is True.
123
+
124
+ Returns:
125
+ ee.Image: An ee.Image where each band represents the anomaly (deviation from
126
+ the mean) for that band. The output image retains the same band name.
127
+ """
128
+ if band_name:
129
+ band_name = band_name
130
+ else:
131
+ band_name = ee.String(image.bandNames().get(0))
132
+
133
+ image_to_process = image.select([band_name])
134
+
135
+ image_scale = image_to_process.projection().nominalScale()
136
+
137
+ # If the user supplies a numeric scale (int/float), keep it; otherwise default to image projection scale.
138
+ scale_value = scale if scale is not None else image_scale # Can be Python number or ee.Number
139
+
140
+ # Compute mean over geometry at chosen scale (scale_value may be ee.Number).
141
+ mean_image = image_to_process.reduceRegion(
142
+ reducer=ee.Reducer.mean(),
143
+ geometry=geometry,
144
+ scale=scale_value,
145
+ maxPixels=1e13
146
+ ).toImage()
147
+
148
+ # Compute the anomaly by subtracting the mean image from the input image.
149
+ if scale is None:
150
+ anomaly_image = image_to_process.subtract(mean_image)
151
+ else:
152
+ anomaly_image = image_to_process.reproject(crs=image_to_process.projection(), scale=scale_value).subtract(mean_image)
153
+
154
+ if anomaly_band_name is None:
155
+ if band_name:
156
+ anomaly_image = anomaly_image.rename(band_name)
157
+ else:
158
+ # Preserve original properties from the input image.
159
+ anomaly_image = anomaly_image.rename(ee.String(image.bandNames().get(0)))
160
+ else:
161
+ anomaly_image = anomaly_image.rename(anomaly_band_name)
162
+ # return anomaly_image
163
+ if replace:
164
+ return anomaly_image.copyProperties(image).set('system:time_start', image.get('system:time_start'))
165
+ else:
166
+ return image.addBands(anomaly_image, overwrite=True).copyProperties(image)
167
+
168
+ @staticmethod
169
+ def mask_via_band_fn(image, band_to_mask, band_for_mask, threshold, mask_above=False, add_band_to_original_image=False):
170
+ """
171
+ Masks pixels of interest from a specified band of a target image, based on a specified reference band and threshold.
172
+ Designed for single image input which contains both the target and reference band.
173
+ Example use case is masking vegetation from image when targeting land pixels. Can specify whether to mask pixels above or below the threshold.
174
+
175
+ Args:
176
+ image (ee.Image): input ee.Image
177
+ band_to_mask (str): name of the band which will be masked (target image)
178
+ band_for_mask (str): name of the band to use for the mask (band you want to remove/mask from target image)
179
+ threshold (float): value where pixels less or more than threshold (depending on `mask_above` argument) will be masked
180
+ mask_above (bool): if True, masks pixels above the threshold; if False, masks pixels below the threshold
181
+
182
+ Returns:
183
+ ee.Image: masked ee.Image
184
+ """
185
+
186
+ band_to_mask_image = image.select(band_to_mask)
187
+ band_for_mask_image = image.select(band_for_mask)
188
+
189
+ mask = band_for_mask_image.lte(threshold) if mask_above else band_for_mask_image.gte(threshold)
190
+
191
+ if add_band_to_original_image:
192
+ return image.addBands(band_to_mask_image.updateMask(mask).rename(band_to_mask), overwrite=True)
193
+ else:
194
+ return ee.Image(band_to_mask_image.updateMask(mask).rename(band_to_mask).copyProperties(image))
195
+
196
+ @staticmethod
197
+ def mask_via_singleband_image_fn(image_to_mask, image_for_mask, threshold, band_name_to_mask=None, band_name_for_mask=None, mask_above=True):
198
+ """
199
+ Masks pixels of interest from a specified band of a target image, based on a specified reference band and threshold.
200
+ Designed for the case where the target and reference bands are in separate images.
201
+ Example use case is masking vegetation from image when targeting land pixels. Can specify whether to mask pixels above or below the threshold.
202
+
203
+ Args:
204
+ image_to_mask (ee.Image): image which will be masked (target image). If multiband, only the first band will be masked.
205
+ image_for_mask (ee.Image): image to use for the mask (image you want to remove/mask from target image). If multiband, only the first band will be used for the masked.
206
+ threshold (float): value where pixels less or more than threshold (depending on `mask_above` argument) will be masked
207
+ band_name_to_mask (str, optional): name of the band in image_to_mask to be masked. If None, the first band will be used.
208
+ band_name_for_mask (str, optional): name of the band in image_for_mask to be used for masking. If None, the first band will be used.
209
+ mask_above (bool): if True, masks pixels above the threshold; if False, masks pixels below the threshold.
210
+
211
+ Returns:
212
+ ee.Image: masked ee.Image
213
+ """
214
+ if band_name_to_mask is None:
215
+ band_to_mask = ee.String(image_to_mask.bandNames().get(0))
216
+ else:
217
+ band_to_mask = ee.String(band_name_to_mask)
218
+
219
+ if band_name_for_mask is None:
220
+ band_for_mask = ee.String(image_for_mask.bandNames().get(0))
221
+ else:
222
+ band_for_mask = ee.String(band_name_for_mask)
223
+
224
+ band_to_mask_image = image_to_mask.select(band_to_mask)
225
+ band_for_mask_image = image_for_mask.select(band_for_mask)
226
+ if mask_above:
227
+ mask = band_for_mask_image.gt(threshold)
228
+ else:
229
+ mask = band_for_mask_image.lt(threshold)
230
+ return band_to_mask_image.updateMask(mask).rename(band_to_mask).copyProperties(image_to_mask)
231
+
232
+ @staticmethod
233
+ def band_rename_fn(image, current_band_name, new_band_name):
234
+ """Renames a band in an ee.Image (single- or multi-band) in-place.
235
+
236
+ Replaces the band named `current_band_name` with `new_band_name` without
237
+ retaining the original band name. If the band does not exist, returns the
238
+ image unchanged.
239
+
240
+ Args:
241
+ image (ee.Image): The input image (can be multiband).
242
+ current_band_name (str): The existing band name to rename.
243
+ new_band_name (str): The desired new band name.
244
+
245
+ Returns:
246
+ ee.Image: The image with the band renamed (or unchanged if not found).
247
+ """
248
+ img = ee.Image(image)
249
+ current = ee.String(current_band_name)
250
+ new = ee.String(new_band_name)
251
+
252
+ band_names = img.bandNames()
253
+ has_band = band_names.contains(current)
254
+
255
+ def _rename():
256
+ # Build a new band-name list with the target name replaced.
257
+ new_names = band_names.map(
258
+ lambda b: ee.String(
259
+ ee.Algorithms.If(ee.String(b).equals(current), new, b)
260
+ )
261
+ )
262
+ # Rename the image using the updated band-name list.
263
+ return img.rename(ee.List(new_names))
264
+
265
+ out = ee.Image(ee.Algorithms.If(has_band, _rename(), img))
266
+ return out.copyProperties(img)
267
+
268
+ @staticmethod
269
+ def PixelAreaSum(
270
+ image, band_name, geometry, threshold=-1, scale=30, maxPixels=1e12
271
+ ):
272
+ """
273
+ Calculates the summation of area for pixels of interest (above a specific threshold) in a geometry
274
+ and store the value as image property (matching name of chosen band). If multiple band names are provided in a list,
275
+ the function will calculate area for each band in the list and store each as a separate property.
276
+
277
+ NOTE: The resulting value has units of square meters.
278
+
279
+ Args:
280
+ image (ee.Image): input ee.Image
281
+ band_name (string or list of strings): name of band(s) (string) for calculating area. If providing multiple band names, pass as a list of strings.
282
+ geometry (ee.Geometry): ee.Geometry object denoting area to clip to for area calculation
283
+ threshold (float): integer threshold to specify masking of pixels below threshold (defaults to -1). If providing multiple band names, the same threshold will be applied to all bands. Best practice in this case is to mask the bands prior to passing to this function and leave threshold at default of -1.
284
+ scale (int): integer scale of image resolution (meters) (defaults to 30)
285
+ maxPixels (int): integer denoting maximum number of pixels for calculations
286
+
287
+ Returns:
288
+ ee.Image: ee.Image with area calculation in square meters stored as property matching name of band
289
+ """
290
+ # Ensure band_name is a server-side ee.List for consistent processing. Wrap band_name in a list if it's a single string.
291
+ bands = ee.List(band_name) if isinstance(band_name, list) else ee.List([band_name])
292
+ # Create an image representing the area of each pixel in square meters
293
+ area_image = ee.Image.pixelArea()
294
+
295
+ # Function to iterate over each band and calculate area, storing the result as a property on the image
296
+ def calculate_and_set_area(band, img_accumulator):
297
+ # Explcitly cast inputs to expected types
298
+ img_accumulator = ee.Image(img_accumulator)
299
+ band = ee.String(band)
300
+
301
+ # Create a mask from the input image for the current band
302
+ mask = img_accumulator.select(band).gte(threshold)
303
+ # Combine the original image with the area image
304
+ final = img_accumulator.addBands(area_image)
305
+
306
+ # Calculation of area for a given band, utilizing other inputs
307
+ stats = (
308
+ final.select("area").updateMask(mask)
309
+ .rename(band) # renames 'area' to band name like 'ndwi'
310
+ .reduceRegion(
311
+ reducer=ee.Reducer.sum(),
312
+ geometry=geometry,
313
+ scale=scale,
314
+ maxPixels=maxPixels,
315
+ )
316
+ )
317
+ # Retrieving the area value from the stats dictionary with stats.get(band), as the band name is now the key
318
+ reduced_area = stats.get(band)
319
+ # Checking whether the calculated area is valid and replaces with 0 if not. This avoids breaking the loop for erroneous images.
320
+ area_value = ee.Algorithms.If(reduced_area, reduced_area, 0)
321
+
322
+ # Set the property on the image, named after the band
323
+ return img_accumulator.set(band, area_value)
324
+
325
+ # Call to iterate the calculate_and_set_area function over the list of bands, starting with the original image
326
+ final_image = ee.Image(bands.iterate(calculate_and_set_area, image))
327
+ return final_image #.set('system:time_start', image.get('system:time_start'))
328
+
329
+ def PixelAreaSumCollection(
330
+ self, band_name, geometry, threshold=-1, scale=30, maxPixels=1e12, output_type='ImageCollection', area_data_export_path=None
331
+ ):
332
+ """
333
+ Calculates the geodesic summation of area for pixels of interest (above a specific threshold)
334
+ within a geometry and stores the value as an image property (matching name of chosen band) for an entire
335
+ image collection. Optionally exports the area data to a CSV file.
336
+
337
+ NOTE: The resulting value has units of square meters.
338
+
339
+ Args:
340
+ band_name (string or list of strings): name of band(s) (string) for calculating area. If providing multiple band names, pass as a list of strings.
341
+ geometry (ee.Geometry): ee.Geometry object denoting area to clip to for area calculation.
342
+ threshold (float): integer threshold to specify masking of pixels below threshold (defaults to -1). If providing multiple band names, the same threshold will be applied to all bands. Best practice in this case is to mask the bands prior to passing to this function and leave threshold at default of -1.
343
+ scale (int): integer scale of image resolution (meters) (defaults to 30).
344
+ maxPixels (int): integer denoting maximum number of pixels for calculations.
345
+ output_type (str): 'ImageCollection' or 'ee.ImageCollection' to return an ee.ImageCollection, 'GenericCollection' to return a GenericCollection object, or 'DataFrame', 'Pandas', 'pd', 'dataframe', 'df' to return a pandas DataFrame (defaults to 'ImageCollection').
346
+ area_data_export_path (str, optional): If provided, the function will save the resulting area data to a CSV file at the specified path.
347
+
348
+ Returns:
349
+ ee.ImageCollection or GenericCollection: Image collection of images with area calculation (square meters) stored as property matching name of band. Type of output depends on output_type argument.
350
+ """
351
+ # If the area calculation has not been computed for this GenericCollection instance, the area will be calculated for the provided bands
352
+ if self._PixelAreaSumCollection is None:
353
+ collection = self.collection
354
+ # Area calculation for each image in the collection, using the PixelAreaSum function
355
+ AreaCollection = collection.map(
356
+ lambda image: GenericCollection.PixelAreaSum(
357
+ image,
358
+ band_name=band_name,
359
+ geometry=geometry,
360
+ threshold=threshold,
361
+ scale=scale,
362
+ maxPixels=maxPixels,
363
+ )
364
+ )
365
+ # Storing the result in the instance variable to avoid redundant calculations
366
+ self._PixelAreaSumCollection = AreaCollection
367
+
368
+ # If an export path is provided, the area data will be exported to a CSV file
369
+ if area_data_export_path:
370
+ GenericCollection(collection=self._PixelAreaSumCollection).ExportProperties(property_names=[band_name], file_path=area_data_export_path+'.csv')
371
+
372
+ # Returning the result in the desired format based on output_type argument or raising an error for invalid input
373
+ if output_type == 'ImageCollection' or output_type == 'ee.ImageCollection':
374
+ return self._PixelAreaSumCollection
375
+ elif output_type == 'GenericCollection':
376
+ return GenericCollection(collection=self._PixelAreaSumCollection)
377
+ elif output_type == 'DataFrame' or output_type == 'Pandas' or output_type == 'pd' or output_type == 'dataframe' or output_type == 'df':
378
+ return GenericCollection(collection=self._PixelAreaSumCollection).ExportProperties(property_names=[band_name])
379
+ else:
380
+ raise ValueError("Incorrect `output_type`. The `output_type` argument must be one of the following: 'ImageCollection', 'ee.ImageCollection', 'GenericCollection', 'DataFrame', 'Pandas', 'pd', 'dataframe', or 'df'.")
381
+
382
+ @staticmethod
383
+ def add_month_property_fn(image):
384
+ """
385
+ Adds a numeric 'month' property to the image based on its date.
386
+
387
+ Args:
388
+ image (ee.Image): Input image.
389
+
390
+ Returns:
391
+ ee.Image: Image with the 'month' property added.
392
+ """
393
+ return image.set('month', image.date().get('month'))
394
+
395
+ @property
396
+ def add_month_property(self):
397
+ """
398
+ Adds a numeric 'month' property to each image in the collection.
399
+
400
+ Returns:
401
+ GenericCollection: A GenericCollection image collection with the 'month' property added to each image.
402
+ """
403
+ col = self.collection.map(GenericCollection.add_month_property_fn)
404
+ return GenericCollection(collection=col)
405
+
406
+ def combine(self, other):
407
+ """
408
+ Combines the current GenericCollection with another GenericCollection, using the `combine` method.
409
+
410
+ Args:
411
+ other (GenericCollection): Another GenericCollection to combine with current collection.
412
+
413
+ Returns:
414
+ GenericCollection: A new GenericCollection containing images from both collections.
415
+ """
416
+ # Checking if 'other' is an instance of GenericCollection
417
+ if not isinstance(other, GenericCollection):
418
+ raise ValueError("The 'other' parameter must be an instance of GenericCollection.")
419
+
420
+ # Merging the collections using the .combine() method
421
+ merged_collection = self.collection.combine(other.collection)
422
+ return GenericCollection(collection=merged_collection)
423
+
424
+ def merge(self, collections=None, multiband_collection=None, date_key='Date_Filter'):
425
+ """
426
+ Merge many singleband GenericCollection products into the parent collection,
427
+ or merge a single multiband collection with parent collection,
428
+ pairing images by exact Date_Filter and returning one multiband image per date.
429
+
430
+ NOTE: if you want to merge two multiband collections, use the `combine` method instead.
431
+
432
+ Args:
433
+ collections (list): List of singleband collections to merge with parent collection, effectively adds one band per collection to each image in parent
434
+ multiband_collection (GenericCollection, optional): A multiband collection to merge with parent. Specifying a collection here will override `collections`.
435
+ date_key (str): image property key for exact pairing (default 'Date_Filter')
436
+
437
+ Returns:
438
+ GenericCollection: parent with extra single bands attached (one image per date)
439
+ """
440
+
441
+ if collections is None and multiband_collection is not None:
442
+ # Exact-date inner-join merge of two collections (adds ALL bands from 'other').
443
+ join = ee.Join.inner()
444
+ flt = ee.Filter.equals(leftField=date_key, rightField=date_key)
445
+ paired = join.apply(self.collection, multiband_collection.collection, flt)
446
+
447
+ def _pair_two(f):
448
+ f = ee.Feature(f)
449
+ a = ee.Image(f.get('primary'))
450
+ b = ee.Image(f.get('secondary'))
451
+ # Overwrite on name collision
452
+ merged = a.addBands(b, None, True)
453
+ # Keep parent props + date key
454
+ merged = merged.copyProperties(a, a.propertyNames())
455
+ merged = merged.set(date_key, a.get(date_key))
456
+ return ee.Image(merged)
457
+
458
+ return GenericCollection(collection=ee.ImageCollection(paired.map(_pair_two)))
459
+
460
+ # Preferred path: merge many singleband products into the parent
461
+ if not isinstance(collections, list) or len(collections) == 0:
462
+ raise ValueError("Provide a non-empty list of GenericCollection objects in `collections`.")
463
+
464
+ result = self.collection
465
+ for extra in collections:
466
+ if not isinstance(extra, GenericCollection):
467
+ raise ValueError("All items in `collections` must be GenericCollection objects.")
468
+
469
+ join = ee.Join.inner()
470
+ flt = ee.Filter.equals(leftField=date_key, rightField=date_key)
471
+ paired = join.apply(result, extra.collection, flt)
472
+
473
+ def _attach_one(f):
474
+ f = ee.Feature(f)
475
+ parent = ee.Image(f.get('primary'))
476
+ sb = ee.Image(f.get('secondary'))
477
+ # Assume singleband product; grab its first band name server-side
478
+ bname = ee.String(sb.bandNames().get(0))
479
+ # Add the single band; overwrite if the name already exists in parent
480
+ merged = parent.addBands(sb.select([bname]).rename([bname]), None, True)
481
+ # Preserve parent props + date key
482
+ merged = merged.copyProperties(parent, parent.propertyNames())
483
+ merged = merged.set(date_key, parent.get(date_key))
484
+ return ee.Image(merged)
485
+
486
+ result = ee.ImageCollection(paired.map(_attach_one))
487
+
488
+ return GenericCollection(collection=result)
489
+
490
+ @property
491
+ def dates_list(self):
492
+ """
493
+ Property attribute to retrieve list of dates as server-side (GEE) object.
494
+
495
+ Returns:
496
+ ee.List: Server-side ee.List of dates.
497
+ """
498
+ if self._dates_list is None:
499
+ dates = self.collection.aggregate_array("Date_Filter")
500
+ self._dates_list = dates
501
+ return self._dates_list
502
+
503
+ @property
504
+ def dates(self):
505
+ """
506
+ Property attribute to retrieve list of dates as readable and indexable client-side list object.
507
+
508
+ Returns:
509
+ list: list of date strings.
510
+ """
511
+ if self._dates_list is None:
512
+ dates = self.collection.aggregate_array("Date_Filter")
513
+ self._dates_list = dates
514
+ if self._dates is None:
515
+ dates = self._dates_list.getInfo()
516
+ self._dates = dates
517
+ return self._dates
518
+
519
+ def remove_duplicate_dates(self, sort_by='system:time_start', ascending=True):
520
+ """
521
+ Removes duplicate images that share the same date, keeping only the first one encountered.
522
+ Useful for handling duplicate acquisitions or overlapping path/rows.
523
+
524
+ Args:
525
+ sort_by (str): Property to sort by before filtering distinct dates. Defaults to 'system:time_start' which is a global property.
526
+ Take care to provide a property that exists in all images if using a custom property.
527
+ ascending (bool): Sort order. Defaults to True.
528
+
529
+ Returns:
530
+ GenericCollection: A new GenericCollection object with distinct dates.
531
+ """
532
+
533
+ # Sort the collection to ensure the "best" image comes first (e.g. least cloudy)
534
+ sorted_col = self.collection.sort(sort_by, ascending)
535
+
536
+ # distinct() retains the first image for each unique value of the specified property
537
+ distinct_col = sorted_col.distinct('Date_Filter')
538
+
539
+ return GenericCollection(collection=distinct_col)
540
+
541
+ def ExportProperties(self, property_names, file_path=None):
542
+ """
543
+ Fetches and returns specified properties from each image in the collection as a list, and returns a pandas DataFrame and optionally saves the results to a csv file.
544
+
545
+ Args:
546
+ property_names (list or str): A property name or list of property names to retrieve. The 'Date_Filter' property is always included to provide temporal context.
547
+ file_path (str, optional): If provided, the function will save the resulting DataFrame to a CSV file at this path. Defaults to None.
548
+
549
+ Returns:
550
+ pd.DataFrame: A pandas DataFrame containing the requested properties for each image, sorted chronologically by 'Date_Filter'.
551
+ """
552
+ # Ensure property_names is a list for consistent processing
553
+ if isinstance(property_names, str):
554
+ property_names = [property_names]
555
+
556
+ # Ensure properties are included without duplication, including 'Date_Filter'
557
+ all_properties_to_fetch = list(set(['Date_Filter'] + property_names))
558
+
559
+ # Defining the helper function to create features with specified properties
560
+ def create_feature_with_properties(image):
561
+ """A function to map over the collection and store the image properties as an ee.Feature.
562
+ Args:
563
+ image (ee.Image): An image from the collection.
564
+ Returns:
565
+ ee.Feature: A feature containing the specified properties from the image.
566
+ """
567
+ properties = image.toDictionary(all_properties_to_fetch)
568
+ return ee.Feature(None, properties)
569
+
570
+ # Map the feature creation function over the server-side collection.
571
+ # The result is an ee.FeatureCollection where each feature holds the properties of one image.
572
+ mapped_collection = self.collection.map(create_feature_with_properties)
573
+ # Explicitly cast to ee.FeatureCollection for clarity
574
+ feature_collection = ee.FeatureCollection(mapped_collection)
575
+
576
+ # Use the existing ee_to_df static method. This performs the single .getInfo() call
577
+ # and converts the structured result directly to a pandas DataFrame.
578
+ df = GenericCollection.ee_to_df(feature_collection, columns=all_properties_to_fetch)
579
+
580
+ # Sort by date for a clean, chronological output.
581
+ if 'Date_Filter' in df.columns:
582
+ df = df.sort_values(by='Date_Filter').reset_index(drop=True)
583
+
584
+ # Check condition for saving to CSV
585
+ if file_path:
586
+ # Check whether file_path ends with .csv, if not, append it
587
+ if not file_path.lower().endswith('.csv'):
588
+ file_path += '.csv'
589
+ # Save DataFrame to CSV
590
+ df.to_csv(file_path, index=True)
591
+ print(f"Properties saved to {file_path}")
592
+
593
+ return df
594
+
595
+ def get_generic_collection(self):
596
+ """
597
+ Filters image collection based on GenericCollection class arguments. Automatically calculated when using collection method, depending on provided class arguments (when tile info is provided).
598
+
599
+ Returns:
600
+ ee.ImageCollection: Filtered image collection - used for subsequent analyses or to acquire ee.ImageCollection from GenericCollection object
601
+ """
602
+ filtered_collection = (
603
+ self.collection
604
+ .map(GenericCollection.image_dater)
605
+ .sort("Date_Filter")
606
+ )
607
+ return filtered_collection
608
+
609
+ def get_filtered_collection(self):
610
+ """
611
+ Filters image collection based on GenericCollection class arguments. Automatically calculated when using collection method, depending on provided class arguments (when tile info is provided).
612
+
613
+ Returns:
614
+ ee.ImageCollection: Filtered image collection - used for subsequent analyses or to acquire ee.ImageCollection from GenericCollection object
615
+ """
616
+ filtered_collection = (
617
+ self.collection
618
+ .filterDate(ee.Date(self.start_date), ee.Date(self.end_date).advance(1, 'day'))
619
+ .map(GenericCollection.image_dater)
620
+ .sort("Date_Filter")
621
+ )
622
+ return filtered_collection
623
+
624
+ def get_boundary_filtered_collection(self):
625
+ """
626
+ Filters and masks image collection based on GenericCollection class arguments. Automatically calculated when using collection method, depending on provided class arguments (when boundary info is provided).
627
+
628
+ Returns:
629
+ ee.ImageCollection: Filtered image collection - used for subsequent analyses or to acquire ee.ImageCollection from GenericCollection object
630
+
631
+ """
632
+ filtered_collection = (
633
+ self.collection
634
+ .filterBounds(self.boundary)
635
+ .map(GenericCollection.image_dater)
636
+ .sort("Date_Filter")
637
+ )
638
+ return filtered_collection
639
+
640
+ def get_boundary_and_date_filtered_collection(self):
641
+ """
642
+ Filters and masks image collection based on GenericCollection class arguments. Automatically calculated when using collection method, depending on provided class arguments (when boundary info is provided).
643
+
644
+ Returns:
645
+ ee.ImageCollection: Filtered image collection - used for subsequent analyses or to acquire ee.ImageCollection from GenericCollection object
646
+
647
+ """
648
+ filtered_collection = (
649
+ self.collection
650
+ .filterDate(ee.Date(self.start_date), ee.Date(self.end_date).advance(1, 'day'))
651
+ .filterBounds(self.boundary)
652
+ .map(GenericCollection.image_dater)
653
+ .sort("Date_Filter")
654
+ )
655
+ return filtered_collection
656
+
657
+ @property
658
+ def median(self):
659
+ """
660
+ Property attribute function to calculate median image from image collection. Results are calculated once per class object then cached for future use.
661
+
662
+ Returns:
663
+ ee.Image: median image from entire collection.
664
+ """
665
+ if self._median is None:
666
+ col = self.collection.median()
667
+ self._median = col
668
+ return self._median
669
+
670
+ @property
671
+ def mean(self):
672
+ """
673
+ Property attribute function to calculate mean image from image collection. Results are calculated once per class object then cached for future use.
674
+
675
+ Returns:
676
+ ee.Image: mean image from entire collection.
677
+
678
+ """
679
+ if self._mean is None:
680
+ col = self.collection.mean()
681
+ self._mean = col
682
+ return self._mean
683
+
684
+ @property
685
+ def max(self):
686
+ """
687
+ Property attribute function to calculate max image from image collection. Results are calculated once per class object then cached for future use.
688
+
689
+ Returns:
690
+ ee.Image: max image from entire collection.
691
+ """
692
+ if self._max is None:
693
+ col = self.collection.max()
694
+ self._max = col
695
+ return self._max
696
+
697
+ @property
698
+ def min(self):
699
+ """
700
+ Property attribute function to calculate min image from image collection. Results are calculated once per class object then cached for future use.
701
+
702
+ Returns:
703
+ ee.Image: min image from entire collection.
704
+ """
705
+ if self._min is None:
706
+ col = self.collection.min()
707
+ self._min = col
708
+ return self._min
709
+
710
+ @property
711
+ def monthly_median_collection(self):
712
+ """Creates a monthly median composite from a GenericCollection image collection.
713
+
714
+ This function computes the median for each
715
+ month within the collection's date range, for each band in the collection. It automatically handles the full
716
+ temporal extent of the input collection.
717
+
718
+ The resulting images have a 'system:time_start' property set to the
719
+ first day of each month and an 'image_count' property indicating how
720
+ many images were used in the composite. Months with no images are
721
+ automatically excluded from the final collection.
722
+
723
+ NOTE: the day of month for the 'system:time_start' property is set to the earliest date of the first month observed and may not be the first day of the month.
724
+
725
+ Returns:
726
+ GenericCollection: A new GenericCollection object with monthly median composites.
727
+ """
728
+ if self._monthly_median is None:
729
+ collection = self.collection
730
+ target_proj = collection.first().projection()
731
+ # Get the start and end dates of the entire collection.
732
+ date_range = collection.reduceColumns(ee.Reducer.minMax(), ["system:time_start"])
733
+ original_start_date = ee.Date(date_range.get('min'))
734
+ end_date = ee.Date(date_range.get('max'))
735
+
736
+ start_year = original_start_date.get('year')
737
+ start_month = original_start_date.get('month')
738
+ start_date = ee.Date.fromYMD(start_year, start_month, 1)
739
+
740
+ # Calculate the total number of months in the date range.
741
+ # The .round() is important for ensuring we get an integer.
742
+ num_months = end_date.difference(start_date, 'month').round()
743
+
744
+ # Generate a list of starting dates for each month.
745
+ # This uses a sequence and advances the start date by 'i' months.
746
+ def get_month_start(i):
747
+ return start_date.advance(i, 'month')
748
+
749
+ month_starts = ee.List.sequence(0, num_months).map(get_month_start)
750
+
751
+ # Define a function to map over the list of month start dates.
752
+ def create_monthly_composite(date):
753
+ # Cast the input to an ee.Date object.
754
+ start_of_month = ee.Date(date)
755
+ # The end date is exclusive, so we advance by 1 month.
756
+ end_of_month = start_of_month.advance(1, 'month')
757
+
758
+ # Filter the original collection to get images for the current month.
759
+ monthly_subset = collection.filterDate(start_of_month, end_of_month)
760
+
761
+ # Count the number of images in the monthly subset.
762
+ image_count = monthly_subset.size()
763
+
764
+ # Compute the median. This is robust to outliers like clouds.
765
+ monthly_median = monthly_subset.median()
766
+
767
+ # Set essential properties on the resulting composite image.
768
+ # The timestamp is crucial for time-series analysis and charting.
769
+ # The image_count is useful metadata for quality assessment.
770
+ return monthly_median.set({
771
+ 'system:time_start': start_of_month.millis(),
772
+ 'month': start_of_month.get('month'),
773
+ 'year': start_of_month.get('year'),
774
+ 'Date_Filter': start_of_month.format('YYYY-MM-dd'),
775
+ 'image_count': image_count
776
+ }).reproject(target_proj)
777
+
778
+ # Map the composite function over the list of month start dates.
779
+ monthly_composites_list = month_starts.map(create_monthly_composite)
780
+
781
+ # Convert the list of images into an ee.ImageCollection.
782
+ monthly_collection = ee.ImageCollection.fromImages(monthly_composites_list)
783
+
784
+ # Filter out any composites that were created from zero images.
785
+ # This prevents empty/masked images from being in the final collection.
786
+ final_collection = GenericCollection(collection=monthly_collection.filter(ee.Filter.gt('image_count', 0)))
787
+ self._monthly_median = final_collection
788
+ else:
789
+ pass
790
+
791
+ return self._monthly_median
792
+
793
+ @property
794
+ def monthly_mean_collection(self):
795
+ """Creates a monthly mean composite from a GenericCollection image collection.
796
+
797
+ This function computes the mean for each
798
+ month within the collection's date range, for each band in the collection. It automatically handles the full
799
+ temporal extent of the input collection.
800
+
801
+ The resulting images have a 'system:time_start' property set to the
802
+ first day of each month and an 'image_count' property indicating how
803
+ many images were used in the composite. Months with no images are
804
+ automatically excluded from the final collection.
805
+
806
+ NOTE: the day of month for the 'system:time_start' property is set to the earliest date of the first month observed and may not be the first day of the month.
807
+
808
+ Returns:
809
+ GenericCollection: A new GenericCollection object with monthly mean composites.
810
+ """
811
+ if self._monthly_mean is None:
812
+ collection = self.collection
813
+ target_proj = collection.first().projection()
814
+ # Get the start and end dates of the entire collection.
815
+ date_range = collection.reduceColumns(ee.Reducer.minMax(), ["system:time_start"])
816
+ original_start_date = ee.Date(date_range.get('min'))
817
+ end_date = ee.Date(date_range.get('max'))
818
+
819
+ start_year = original_start_date.get('year')
820
+ start_month = original_start_date.get('month')
821
+ start_date = ee.Date.fromYMD(start_year, start_month, 1)
822
+
823
+ # Calculate the total number of months in the date range.
824
+ # The .round() is important for ensuring we get an integer.
825
+ num_months = end_date.difference(start_date, 'month').round()
826
+
827
+ # Generate a list of starting dates for each month.
828
+ # This uses a sequence and advances the start date by 'i' months.
829
+ def get_month_start(i):
830
+ return start_date.advance(i, 'month')
831
+
832
+ month_starts = ee.List.sequence(0, num_months).map(get_month_start)
833
+
834
+ # Define a function to map over the list of month start dates.
835
+ def create_monthly_composite(date):
836
+ # Cast the input to an ee.Date object.
837
+ start_of_month = ee.Date(date)
838
+ # The end date is exclusive, so we advance by 1 month.
839
+ end_of_month = start_of_month.advance(1, 'month')
840
+
841
+ # Filter the original collection to get images for the current month.
842
+ monthly_subset = collection.filterDate(start_of_month, end_of_month)
843
+
844
+ # Count the number of images in the monthly subset.
845
+ image_count = monthly_subset.size()
846
+
847
+ # Compute the mean. This is robust to outliers like clouds.
848
+ monthly_mean = monthly_subset.mean()
849
+
850
+ # Set essential properties on the resulting composite image.
851
+ # The timestamp is crucial for time-series analysis and charting.
852
+ # The image_count is useful metadata for quality assessment.
853
+ return monthly_mean.set({
854
+ 'system:time_start': start_of_month.millis(),
855
+ 'month': start_of_month.get('month'),
856
+ 'year': start_of_month.get('year'),
857
+ 'Date_Filter': start_of_month.format('YYYY-MM-dd'),
858
+ 'image_count': image_count
859
+ }).reproject(target_proj)
860
+
861
+ # Map the composite function over the list of month start dates.
862
+ monthly_composites_list = month_starts.map(create_monthly_composite)
863
+
864
+ # Convert the list of images into an ee.ImageCollection.
865
+ monthly_collection = ee.ImageCollection.fromImages(monthly_composites_list)
866
+
867
+ # Filter out any composites that were created from zero images.
868
+ # This prevents empty/masked images from being in the final collection.
869
+ final_collection = GenericCollection(collection=monthly_collection.filter(ee.Filter.gt('image_count', 0)))
870
+ self._monthly_mean = final_collection
871
+ else:
872
+ pass
873
+
874
+ return self._monthly_mean
875
+
876
+ @property
877
+ def monthly_sum_collection(self):
878
+ """Creates a monthly sum composite from a GenericCollection image collection.
879
+
880
+ This function computes the sum for each
881
+ month within the collection's date range, for each band in the collection. It automatically handles the full
882
+ temporal extent of the input collection.
883
+
884
+ The resulting images have a 'system:time_start' property set to the
885
+ first day of each month and an 'image_count' property indicating how
886
+ many images were used in the composite. Months with no images are
887
+ automatically excluded from the final collection.
888
+
889
+ NOTE: the day of month for the 'system:time_start' property is set to the earliest date of the first month observed and may not be the first day of the month.
890
+
891
+ Returns:
892
+ GenericCollection: A new GenericCollection object with monthly sum composites.
893
+ """
894
+ if self._monthly_sum is None:
895
+ collection = self.collection
896
+ target_proj = collection.first().projection()
897
+ # Get the start and end dates of the entire collection.
898
+ date_range = collection.reduceColumns(ee.Reducer.minMax(), ["system:time_start"])
899
+ original_start_date = ee.Date(date_range.get('min'))
900
+ end_date = ee.Date(date_range.get('max'))
901
+
902
+ start_year = original_start_date.get('year')
903
+ start_month = original_start_date.get('month')
904
+ start_date = ee.Date.fromYMD(start_year, start_month, 1)
905
+
906
+ # Calculate the total number of months in the date range.
907
+ # The .round() is important for ensuring we get an integer.
908
+ num_months = end_date.difference(start_date, 'month').round()
909
+
910
+ # Generate a list of starting dates for each month.
911
+ # This uses a sequence and advances the start date by 'i' months.
912
+ def get_month_start(i):
913
+ return start_date.advance(i, 'month')
914
+
915
+ month_starts = ee.List.sequence(0, num_months).map(get_month_start)
916
+
917
+ # Define a function to map over the list of month start dates.
918
+ def create_monthly_composite(date):
919
+ # Cast the input to an ee.Date object.
920
+ start_of_month = ee.Date(date)
921
+ # The end date is exclusive, so we advance by 1 month.
922
+ end_of_month = start_of_month.advance(1, 'month')
923
+
924
+ # Filter the original collection to get images for the current month.
925
+ monthly_subset = collection.filterDate(start_of_month, end_of_month)
926
+
927
+ # Count the number of images in the monthly subset.
928
+ image_count = monthly_subset.size()
929
+
930
+ # Compute the sum. This is robust to outliers like clouds.
931
+ monthly_sum = monthly_subset.sum()
932
+
933
+ # Set essential properties on the resulting composite image.
934
+ # The timestamp is crucial for time-series analysis and charting.
935
+ # The image_count is useful metadata for quality assessment.
936
+ return monthly_sum.set({
937
+ 'system:time_start': start_of_month.millis(),
938
+ 'month': start_of_month.get('month'),
939
+ 'year': start_of_month.get('year'),
940
+ 'Date_Filter': start_of_month.format('YYYY-MM-dd'),
941
+ 'image_count': image_count
942
+ }).reproject(target_proj)
943
+
944
+ # Map the composite function over the list of month start dates.
945
+ monthly_composites_list = month_starts.map(create_monthly_composite)
946
+
947
+ # Convert the list of images into an ee.ImageCollection.
948
+ monthly_collection = ee.ImageCollection.fromImages(monthly_composites_list)
949
+
950
+ # Filter out any composites that were created from zero images.
951
+ # This prevents empty/masked images from being in the final collection.
952
+ final_collection = GenericCollection(collection=monthly_collection.filter(ee.Filter.gt('image_count', 0)))
953
+ self._monthly_sum = final_collection
954
+ else:
955
+ pass
956
+
957
+ return self._monthly_sum
958
+
959
+ @property
960
+ def monthly_max_collection(self):
961
+ """Creates a monthly max composite from a GenericCollection image collection.
962
+
963
+ This function computes the max for each
964
+ month within the collection's date range, for each band in the collection. It automatically handles the full
965
+ temporal extent of the input collection.
966
+
967
+ The resulting images have a 'system:time_start' property set to the
968
+ first day of each month and an 'image_count' property indicating how
969
+ many images were used in the composite. Months with no images are
970
+ automatically excluded from the final collection.
971
+
972
+ NOTE: the day of month for the 'system:time_start' property is set to the earliest date of the first month observed and may not be the first day of the month.
973
+
974
+ Returns:
975
+ GenericCollection: A new GenericCollection object with monthly max composites.
976
+ """
977
+ if self._monthly_max is None:
978
+ collection = self.collection
979
+ target_proj = collection.first().projection()
980
+ # Get the start and end dates of the entire collection.
981
+ date_range = collection.reduceColumns(ee.Reducer.minMax(), ["system:time_start"])
982
+ original_start_date = ee.Date(date_range.get('min'))
983
+ end_date = ee.Date(date_range.get('max'))
984
+
985
+ start_year = original_start_date.get('year')
986
+ start_month = original_start_date.get('month')
987
+ start_date = ee.Date.fromYMD(start_year, start_month, 1)
988
+
989
+ # Calculate the total number of months in the date range.
990
+ # The .round() is important for ensuring we get an integer.
991
+ num_months = end_date.difference(start_date, 'month').round()
992
+
993
+ # Generate a list of starting dates for each month.
994
+ # This uses a sequence and advances the start date by 'i' months.
995
+ def get_month_start(i):
996
+ return start_date.advance(i, 'month')
997
+
998
+ month_starts = ee.List.sequence(0, num_months).map(get_month_start)
999
+
1000
+ # Define a function to map over the list of month start dates.
1001
+ def create_monthly_composite(date):
1002
+ # Cast the input to an ee.Date object.
1003
+ start_of_month = ee.Date(date)
1004
+ # The end date is exclusive, so we advance by 1 month.
1005
+ end_of_month = start_of_month.advance(1, 'month')
1006
+
1007
+ # Filter the original collection to get images for the current month.
1008
+ monthly_subset = collection.filterDate(start_of_month, end_of_month)
1009
+
1010
+ # Count the number of images in the monthly subset.
1011
+ image_count = monthly_subset.size()
1012
+
1013
+ # Compute the max. This is robust to outliers like clouds.
1014
+ monthly_max = monthly_subset.max()
1015
+
1016
+ # Set essential properties on the resulting composite image.
1017
+ # The timestamp is crucial for time-series analysis and charting.
1018
+ # The image_count is useful metadata for quality assessment.
1019
+ return monthly_max.set({
1020
+ 'system:time_start': start_of_month.millis(),
1021
+ 'month': start_of_month.get('month'),
1022
+ 'year': start_of_month.get('year'),
1023
+ 'Date_Filter': start_of_month.format('YYYY-MM-dd'),
1024
+ 'image_count': image_count
1025
+ }).reproject(target_proj)
1026
+
1027
+ # Map the composite function over the list of month start dates.
1028
+ monthly_composites_list = month_starts.map(create_monthly_composite)
1029
+
1030
+ # Convert the list of images into an ee.ImageCollection.
1031
+ monthly_collection = ee.ImageCollection.fromImages(monthly_composites_list)
1032
+
1033
+ # Filter out any composites that were created from zero images.
1034
+ # This prevents empty/masked images from being in the final collection.
1035
+ final_collection = GenericCollection(collection=monthly_collection.filter(ee.Filter.gt('image_count', 0)))
1036
+ self._monthly_max = final_collection
1037
+ else:
1038
+ pass
1039
+
1040
+ return self._monthly_max
1041
+
1042
+ @property
1043
+ def monthly_min_collection(self):
1044
+ """Creates a monthly min composite from a GenericCollection image collection.
1045
+
1046
+ This function computes the min for each
1047
+ month within the collection's date range, for each band in the collection. It automatically handles the full
1048
+ temporal extent of the input collection.
1049
+
1050
+ The resulting images have a 'system:time_start' property set to the
1051
+ first day of each month and an 'image_count' property indicating how
1052
+ many images were used in the composite. Months with no images are
1053
+ automatically excluded from the final collection.
1054
+
1055
+ NOTE: the day of month for the 'system:time_start' property is set to the earliest date of the first month observed and may not be the first day of the month.
1056
+
1057
+ Returns:
1058
+ GenericCollection: A new GenericCollection object with monthly min composites.
1059
+ """
1060
+ if self._monthly_min is None:
1061
+ collection = self.collection
1062
+ target_proj = collection.first().projection()
1063
+ # Get the start and end dates of the entire collection.
1064
+ date_range = collection.reduceColumns(ee.Reducer.minMax(), ["system:time_start"])
1065
+ original_start_date = ee.Date(date_range.get('min'))
1066
+ end_date = ee.Date(date_range.get('max'))
1067
+
1068
+ start_year = original_start_date.get('year')
1069
+ start_month = original_start_date.get('month')
1070
+ start_date = ee.Date.fromYMD(start_year, start_month, 1)
1071
+
1072
+ # Calculate the total number of months in the date range.
1073
+ # The .round() is important for ensuring we get an integer.
1074
+ num_months = end_date.difference(start_date, 'month').round()
1075
+
1076
+ # Generate a list of starting dates for each month.
1077
+ # This uses a sequence and advances the start date by 'i' months.
1078
+ def get_month_start(i):
1079
+ return start_date.advance(i, 'month')
1080
+
1081
+ month_starts = ee.List.sequence(0, num_months).map(get_month_start)
1082
+
1083
+ # Define a function to map over the list of month start dates.
1084
+ def create_monthly_composite(date):
1085
+ # Cast the input to an ee.Date object.
1086
+ start_of_month = ee.Date(date)
1087
+ # The end date is exclusive, so we advance by 1 month.
1088
+ end_of_month = start_of_month.advance(1, 'month')
1089
+
1090
+ # Filter the original collection to get images for the current month.
1091
+ monthly_subset = collection.filterDate(start_of_month, end_of_month)
1092
+
1093
+ # Count the number of images in the monthly subset.
1094
+ image_count = monthly_subset.size()
1095
+
1096
+ # Compute the min. This is robust to outliers like clouds.
1097
+ monthly_min = monthly_subset.min()
1098
+
1099
+ # Set essential properties on the resulting composite image.
1100
+ # The timestamp is crucial for time-series analysis and charting.
1101
+ # The image_count is useful metadata for quality assessment.
1102
+ return monthly_min.set({
1103
+ 'system:time_start': start_of_month.millis(),
1104
+ 'month': start_of_month.get('month'),
1105
+ 'year': start_of_month.get('year'),
1106
+ 'Date_Filter': start_of_month.format('YYYY-MM-dd'),
1107
+ 'image_count': image_count
1108
+ }).reproject(target_proj)
1109
+
1110
+ # Map the composite function over the list of month start dates.
1111
+ monthly_composites_list = month_starts.map(create_monthly_composite)
1112
+
1113
+ # Convert the list of images into an ee.ImageCollection.
1114
+ monthly_collection = ee.ImageCollection.fromImages(monthly_composites_list)
1115
+
1116
+ # Filter out any composites that were created from zero images.
1117
+ # This prevents empty/masked images from being in the final collection.
1118
+ final_collection = GenericCollection(collection=monthly_collection.filter(ee.Filter.gt('image_count', 0)))
1119
+ self._monthly_min = final_collection
1120
+ else:
1121
+ pass
1122
+
1123
+ return self._monthly_min
1124
+
1125
+ @property
1126
+ def daily_aggregate_collection_from_properties(self):
1127
+ """
1128
+ Property attribute to aggregate (sum) collection images that share the same date.
1129
+
1130
+ This is useful for collections with multiple images per day (e.g., 3-hour SMAP data)
1131
+ that need to be converted to a daily sum. It uses the 'Date_Filter' property
1132
+ to group images. The 'system:time_start' of the first image of the day
1133
+ is preserved. Server-side friendly.
1134
+
1135
+ NOTE: This function sums all bands.
1136
+
1137
+ Returns:
1138
+ GenericCollection: GenericCollection image collection with daily summed imagery.
1139
+ """
1140
+ if self._daily_aggregate_collection is None:
1141
+ input_collection = self.collection
1142
+
1143
+ # Function to sum images of the same date and accumulate them
1144
+ def sum_and_accumulate(date, list_accumulator):
1145
+ # Cast inputs to server-side objects
1146
+ date = ee.String(date)
1147
+ list_accumulator = ee.List(list_accumulator)
1148
+
1149
+ # Filter collection to only images from this date
1150
+ date_filter = ee.Filter.eq("Date_Filter", date)
1151
+ date_collection = input_collection.filter(date_filter)
1152
+
1153
+ # Get the first image of the day to use for its metadata
1154
+ first_image = ee.Image(date_collection.first())
1155
+
1156
+ # Reduce the daily collection by summing all images
1157
+ # This creates a single image where each pixel is the sum
1158
+ # of all pixels from that day.
1159
+ daily_sum = date_collection.sum()
1160
+
1161
+ # --- Property Management ---
1162
+ # Copy the 'system:time_start' from the first image of the
1163
+ # day to the new daily-summed image. This is critical.
1164
+ props_to_copy = ["system:time_start"]
1165
+ daily_sum = daily_sum.copyProperties(first_image, props_to_copy)
1166
+
1167
+ # Set the 'Date_Filter' property (since .sum() doesn't preserve it)
1168
+ daily_sum = daily_sum.set("Date_Filter", date)
1169
+
1170
+ # Also add a property to know how many images were summed
1171
+ image_count = date_collection.size()
1172
+ daily_sum = daily_sum.set('images_summed', image_count)
1173
+
1174
+ # Add the new daily image to our list
1175
+ return list_accumulator.add(daily_sum)
1176
+
1177
+ # Get a server-side list of all unique dates in the collection
1178
+ distinct_dates = input_collection.aggregate_array("Date_Filter").distinct()
1179
+
1180
+ # Initialize an empty list as the accumulator
1181
+ initial = ee.List([])
1182
+
1183
+ # Iterate over each date to create sums and accumulate them in a list
1184
+ summed_list = distinct_dates.iterate(sum_and_accumulate, initial)
1185
+
1186
+ # Convert the list of summed images to an ImageCollection
1187
+ new_col = ee.ImageCollection.fromImages(summed_list)
1188
+
1189
+ # Cache the result as a new GenericCollection
1190
+ self._daily_aggregate_collection = GenericCollection(collection=new_col)
1191
+
1192
+ return self._daily_aggregate_collection
1193
+
1194
+ def daily_aggregate_collection(self, method='algorithmic'):
1195
+ """
1196
+ Aggregates (sums) collection images that share the same date.
1197
+
1198
+ This is useful for collections with multiple images per day (e.g., 3-hour SMAP data)
1199
+ that need to be converted to a daily sum. It uses the 'Date_Filter' property
1200
+ to group images. The 'system:time_start' of the first image of the day
1201
+ is preserved. Server-side friendly.
1202
+
1203
+ Args:
1204
+ method (str, optional): The method for generating the list of unique dates.
1205
+ - 'algorithmic' (default): Generates dates from self.start_date and
1206
+ self.end_date. This is highly efficient and robust for large
1207
+ collections. Requires start/end dates to be set on the object.
1208
+ - 'aggregate': Scans the entire collection for unique 'Date_Filter'
1209
+ properties. This can cause a 'User memory limit exceeded' error
1210
+ on very large collections.
1211
+
1212
+ Returns:
1213
+ GenericCollection: A new GenericCollection image collection with daily summed imagery.
1214
+
1215
+ Raises:
1216
+ ValueError: If 'algorithmic' method is used but self.start_date or
1217
+ self.end_date are not set.
1218
+ """
1219
+ input_collection = self.collection
1220
+
1221
+ # --- Select the method for generating the date list ---
1222
+ if method == 'algorithmic':
1223
+ # Check that start/end dates are available on the object
1224
+ if not self.start_date or not self.end_date:
1225
+ raise ValueError(
1226
+ "The 'algorithmic' method requires start_date and end_date to be "
1227
+ "set on the GenericCollection object. Initialize the object "
1228
+ "with start_date and end_date, or use method='aggregate'."
1229
+ )
1230
+
1231
+ # 1. Get ee.Date objects from the instance properties
1232
+ start_date = ee.Date(self.start_date)
1233
+ end_date = ee.Date(self.end_date)
1234
+
1235
+ # 2. Calculate the total number of days in the range
1236
+ num_days = end_date.difference(start_date, 'day').round()
1237
+
1238
+ # 3. Create a server-side list of all day-starting numbers
1239
+ day_numbers = ee.List.sequence(0, num_days)
1240
+
1241
+ # 4. Map over the numbers to create a list of 'YYYY-MM-DD' date strings
1242
+ def get_date_string(n):
1243
+ return start_date.advance(n, 'day').format('YYYY-MM-dd')
1244
+
1245
+ distinct_dates = day_numbers.map(get_date_string)
1246
+
1247
+ elif method == 'aggregate':
1248
+ # This is the original, memory-intensive method.
1249
+ distinct_dates = input_collection.aggregate_array("Date_Filter").distinct()
1250
+
1251
+ else:
1252
+ raise ValueError(f"Unknown method '{method}'. Must be 'algorithmic' or 'aggregate'.")
1253
+ # --- End of date list generation ---
1254
+
1255
+ # Function to sum images of the same date and accumulate them
1256
+ def sum_and_accumulate(date, list_accumulator):
1257
+ # Cast inputs to server-side objects
1258
+ date = ee.String(date)
1259
+ list_accumulator = ee.List(list_accumulator)
1260
+
1261
+ # Filter collection to only images from this date
1262
+ date_filter = ee.Filter.eq("Date_Filter", date)
1263
+ date_collection = input_collection.filter(date_filter)
1264
+
1265
+ # Check if any images actually exist for this day
1266
+ image_count = date_collection.size()
1267
+
1268
+ # Define the summing function to be run *only* if images exist
1269
+ def if_images_exist():
1270
+ # Get the first image of the day to use for its metadata
1271
+ first_image = ee.Image(date_collection.first())
1272
+
1273
+ # Reduce the daily collection by summing all images
1274
+ daily_sum = date_collection.sum()
1275
+
1276
+ # Copy 'system:time_start' from the first image
1277
+ props_to_copy = ["system:time_start"]
1278
+ daily_sum = daily_sum.copyProperties(first_image, props_to_copy)
1279
+
1280
+ # Set the 'Date_Filter' property
1281
+ daily_sum = daily_sum.set("Date_Filter", date)
1282
+ daily_sum = daily_sum.set('images_summed', image_count)
1283
+
1284
+ # Add the new daily image to our list
1285
+ return list_accumulator.add(daily_sum)
1286
+
1287
+ # Use ee.Algorithms.If to run the sum *only* if image_count > 0
1288
+ # This avoids errors from calling .first() or .sum() on empty collections
1289
+ return ee.Algorithms.If(
1290
+ image_count.gt(0),
1291
+ if_images_exist(), # if True
1292
+ list_accumulator # if False (just return the list unchanged)
1293
+ )
1294
+
1295
+ # Initialize an empty list as the accumulator
1296
+ initial = ee.List([])
1297
+
1298
+ # Iterate over each date to create sums and accumulate them
1299
+ summed_list = distinct_dates.iterate(sum_and_accumulate, initial)
1300
+
1301
+ # Convert the list of summed images to an ImageCollection
1302
+ new_col = ee.ImageCollection.fromImages(summed_list)
1303
+
1304
+ # Return the new GenericCollection wrapper
1305
+ return GenericCollection(collection=new_col)
1306
+
1307
+ def daily_aggregate_collection_via_join(self, method='algorithmic'):
1308
+ """
1309
+ Aggregates (sums) collection images that share the same date based on a join approach.
1310
+
1311
+ Args:
1312
+ method (str): The method for which to aggregate the daily collection. Options are 'algorithmic' (default) and 'aggregate'.
1313
+ The algorithmic method is server-side friendly while the aggregate method makes client-side calls.
1314
+ Algorithmic is more efficient and chosen as the default.
1315
+
1316
+ Returns:
1317
+ Image Collection (GenericCollection): The daily aggregated image collection as a GenericCollection object type.
1318
+
1319
+ """
1320
+ input_collection = self.collection
1321
+
1322
+ if method == 'algorithmic':
1323
+ if not self.start_date or not self.end_date:
1324
+ raise ValueError(
1325
+ "The 'algorithmic' method requires start_date and end_date to be "
1326
+ "set on the GenericCollection object. Initialize the object "
1327
+ "with start_date and end_date, or use method='aggregate'."
1328
+ )
1329
+
1330
+ start_date = ee.Date(self.start_date)
1331
+ end_date = ee.Date(self.end_date)
1332
+ num_days = end_date.difference(start_date, 'day').round()
1333
+ day_numbers = ee.List.sequence(0, num_days)
1334
+
1335
+ def get_date_string(n):
1336
+ return start_date.advance(n, 'day').format('YYYY-MM-dd')
1337
+
1338
+ distinct_dates = day_numbers.map(get_date_string) # This is our server-side list
1339
+
1340
+ elif method == 'aggregate':
1341
+ distinct_dates = input_collection.aggregate_array("Date_Filter").distinct()
1342
+
1343
+ else:
1344
+ raise ValueError(f"Unknown method '{method}'. Must be 'algorithmic' or 'aggregate'.")
1345
+
1346
+ def create_date_feature(date_str):
1347
+ return ee.Feature(None, {'Date_Filter': ee.String(date_str)})
1348
+
1349
+ dummy_dates_fc = ee.FeatureCollection(distinct_dates.map(create_date_feature))
1350
+
1351
+ date_filter = ee.Filter.equals(leftField='Date_Filter', rightField='Date_Filter')
1352
+ join = ee.Join.saveAll(matchesKey='matches')
1353
+ joined_fc = join.apply(dummy_dates_fc, input_collection, date_filter)
1354
+
1355
+ def sum_daily_images(feature_with_matches):
1356
+ images_list = ee.List(feature_with_matches.get('matches'))
1357
+ image_count = images_list.size()
1358
+
1359
+ # Define a function to run *only* if the list is not empty
1360
+ def if_images_exist():
1361
+ image_collection_for_day = ee.ImageCollection.fromImages(images_list)
1362
+ first_image = ee.Image(image_collection_for_day.first())
1363
+ daily_sum = image_collection_for_day.sum()
1364
+ daily_sum = daily_sum.copyProperties(first_image, ["system:time_start"])
1365
+ daily_sum = daily_sum.set(
1366
+ 'Date_Filter', feature_with_matches.get('Date_Filter'),
1367
+ 'images_summed', image_count
1368
+ )
1369
+ return daily_sum
1370
+
1371
+ # Use ee.Algorithms.If. If count is 0, return a *null* image.
1372
+ return ee.Algorithms.If(
1373
+ image_count.gt(0),
1374
+ if_images_exist(), # if True
1375
+ None # if False (return None)
1376
+ )
1377
+
1378
+ # Map the robust function, and use dropNulls=True to filter out
1379
+ # any days that had no images (and returned None).
1380
+ image_collection = joined_fc.map(sum_daily_images, dropNulls=True)
1381
+
1382
+ # Explicitly cast to an ImageCollection to avoid client-side confusion
1383
+ final_collection = ee.ImageCollection(image_collection)
1384
+
1385
+ return GenericCollection(
1386
+ collection=final_collection,
1387
+ start_date=self.start_date, # Pass along other metadata
1388
+ end_date=self.end_date,
1389
+ boundary=self.boundary,
1390
+ _dates_list=distinct_dates # <-- This is the key
1391
+ )
1392
+
1393
+ def export_daily_sum_to_asset(
1394
+ self,
1395
+ asset_collection_path,
1396
+ region,
1397
+ scale,
1398
+ filename_prefix="",
1399
+ crs=None,
1400
+ max_pixels=int(1e13),
1401
+ description_prefix="export"
1402
+ ):
1403
+ """
1404
+ Exports a daily-summed (aggregated) collection to a GEE Asset Collection.
1405
+
1406
+ This function is designed to be called from a collection with
1407
+ sub-daily data (e.g., 3-hourly). It efficiently creates one
1408
+ small, independent export task for each day by summing *only*
1409
+ that day's images. This avoids the re-computing of an entire collection per image task performance pitfall.
1410
+
1411
+ It requires self.start_date and self.end_date to be set on the
1412
+ GenericCollection object.
1413
+
1414
+ Args:
1415
+ asset_collection_path (str): The path to the asset collection.
1416
+ region (ee.Geometry): The region to export.
1417
+ scale (int): The scale of the export.
1418
+ filename_prefix (str, optional): The filename prefix. Defaults to "", i.e. blank.
1419
+ crs (str, optional): The coordinate reference system. Defaults to None.
1420
+ max_pixels (int, optional): The maximum number of pixels. Defaults to int(1e13).
1421
+ description_prefix (str, optional): The description prefix. Defaults to "export".
1422
+
1423
+ Returns:
1424
+ None: (queues export tasks)
1425
+ """
1426
+ # This is the *original* 3-hourly (or sub-daily) collection
1427
+ original_collection = self.collection
1428
+
1429
+ # --- 1. Algorithmic Date Generation ---
1430
+ if not self.start_date or not self.end_date:
1431
+ raise ValueError(
1432
+ "export_daily_sum_to_asset requires start_date and end_date "
1433
+ "to be set on the GenericCollection object."
1434
+ )
1435
+
1436
+ start_date = ee.Date(self.start_date)
1437
+ end_date = ee.Date(self.end_date)
1438
+ num_days = end_date.difference(start_date, 'day').round()
1439
+ day_numbers = ee.List.sequence(0, num_days)
1440
+
1441
+ def get_date_string(n):
1442
+ # Use lowercase 'dd' for day of month!
1443
+ return start_date.advance(n, 'day').format('YYYY-MM-dd')
1444
+
1445
+ # Get a client-side list of all dates to loop over
1446
+ date_list = day_numbers.map(get_date_string).getInfo()
1447
+ # --- End of Date Generation ---
1448
+
1449
+ # --- 2. Create Asset Collection (if needed) ---
1450
+ try:
1451
+ ee.data.getAsset(asset_collection_path)
1452
+ except ee.EEException:
1453
+ print(f"Creating new asset collection: {asset_collection_path}")
1454
+ ee.data.createAsset({'type': 'ImageCollection'}, asset_collection_path)
1455
+
1456
+ print(f"Queuing {len(date_list)} small, daily-sum export tasks...")
1457
+
1458
+ # --- 3. Loop and Create Tiny Tasks ---
1459
+ for date_str in date_list:
1460
+
1461
+ # --- This is the simple, efficient recipe for *one* day ---
1462
+
1463
+ # 1. Filter the *original* collection for just this one day
1464
+ daily_images = original_collection.filter(
1465
+ ee.Filter.eq('Date_Filter', date_str)
1466
+ )
1467
+
1468
+ # 2. Get the first image for metadata
1469
+ first_image = daily_images.first()
1470
+
1471
+ # 3. Create the daily sum
1472
+ daily_sum = daily_images.sum()
1473
+
1474
+ # 4. Set properties
1475
+ daily_sum = ee.Image(daily_sum.copyProperties(first_image, ["system:time_start"]))
1476
+ daily_sum = daily_sum.set(
1477
+ 'Date_Filter', date_str,
1478
+ 'images_summed', daily_images.size()
1479
+ )
1480
+ # --- End of recipe ---
1481
+
1482
+ # Define asset ID and description
1483
+ asset_id = asset_collection_path + "/" + filename_prefix + date_str
1484
+ desc = description_prefix + "_" + filename_prefix + date_str
1485
+
1486
+ params = {
1487
+ 'image': daily_sum,
1488
+ 'description': desc,
1489
+ 'assetId': asset_id,
1490
+ 'region': region,
1491
+ 'scale': scale,
1492
+ 'maxPixels': max_pixels
1493
+ }
1494
+ if crs:
1495
+ params['crs'] = crs
1496
+
1497
+ # Start the server-side export task
1498
+ ee.batch.Export.image.toAsset(**params).start()
1499
+
1500
+ print("All", len(date_list), "export tasks queued to", asset_collection_path)
1501
+
1502
+ def smap_flux_to_mm(self):
1503
+ """
1504
+ Converts a daily-summed SMAP flux collection (kg/m²/s)
1505
+ to a daily total amount (mm/day).
1506
+
1507
+ This works by multiplying each image by 10800
1508
+ (3 hours * 60 min/hr * 60 sec/min).
1509
+
1510
+ Assumes 1 kg/m² = 1 mm of water.
1511
+
1512
+ Returns:
1513
+ GenericCollection: A new collection with values in mm/day.
1514
+ """
1515
+ # Define the conversion function
1516
+ def convert_to_mm(image):
1517
+ # Get the original band name(s)
1518
+ band_names = image.bandNames()
1519
+ # Multiply and rename the bands to indicate the new units
1520
+ new_band_names = band_names.map(lambda b: ee.String(b).cat('_mm'))
1521
+
1522
+ converted_image = image.multiply(10800).rename(new_band_names)
1523
+ return converted_image.copyProperties(image, image.propertyNames())
1524
+
1525
+ # Map the function over the entire collection
1526
+ converted_collection = self.collection.map(convert_to_mm)
1527
+
1528
+ # Return a new GenericCollection object
1529
+ return GenericCollection(
1530
+ collection=converted_collection,
1531
+ start_date=self.start_date,
1532
+ end_date=self.end_date,
1533
+ boundary=self.boundary,
1534
+ _dates_list=self._dates_list # Pass along the cached dates!
1535
+ )
1536
+
1537
+ def mask_to_polygon(self, polygon):
1538
+ """
1539
+ Function to mask GenericCollection image collection by a polygon (ee.Geometry), where pixels outside the polygon are masked out.
1540
+
1541
+ Args:
1542
+ polygon (ee.Geometry): ee.Geometry polygon or shape used to mask image collection.
1543
+
1544
+ Returns:
1545
+ GenericCollection: masked GenericCollection image collection
1546
+
1547
+ """
1548
+ if self._geometry_masked_collection is None:
1549
+ # Convert the polygon to a mask
1550
+ mask = ee.Image.constant(1).clip(polygon)
1551
+
1552
+ # Update the mask of each image in the collection
1553
+ masked_collection = self.collection.map(lambda img: img.updateMask(mask))
1554
+
1555
+ # Update the internal collection state
1556
+ self._geometry_masked_collection = GenericCollection(
1557
+ collection=masked_collection
1558
+ )
1559
+
1560
+ # Return the updated object
1561
+ return self._geometry_masked_collection
1562
+
1563
+ def mask_out_polygon(self, polygon):
1564
+ """
1565
+ Function to mask GenericCollection image collection by a polygon (ee.Geometry), where pixels inside the polygon are masked out.
1566
+
1567
+ Args:
1568
+ polygon (ee.Geometry): ee.Geometry polygon or shape used to mask image collection.
1569
+
1570
+ Returns:
1571
+ GenericCollection: masked GenericCollection image collection
1572
+
1573
+ """
1574
+ if self._geometry_masked_out_collection is None:
1575
+ # Convert the polygon to a mask
1576
+ full_mask = ee.Image.constant(1)
1577
+
1578
+ # Use paint to set pixels inside polygon as 0
1579
+ area = full_mask.paint(polygon, 0)
1580
+
1581
+ # Update the mask of each image in the collection
1582
+ masked_collection = self.collection.map(lambda img: img.updateMask(area))
1583
+
1584
+ # Update the internal collection state
1585
+ self._geometry_masked_out_collection = GenericCollection(
1586
+ collection=masked_collection
1587
+ )
1588
+
1589
+ # Return the updated object
1590
+ return self._geometry_masked_out_collection
1591
+
1592
+
1593
+ def binary_mask(self, threshold=None, band_name=None, classify_above_threshold=True, mask_zeros=False):
1594
+ """
1595
+ Function to create a binary mask (value of 1 for pixels above set threshold and value of 0 for all other pixels) of the GenericCollection image collection based on a specified band.
1596
+ If a singleband image is provided, the band name is automatically determined.
1597
+ If multiple bands are available, the user must specify the band name to use for masking.
1598
+
1599
+ Args:
1600
+ threshold (float, optional): The threshold value for creating the binary mask. Defaults to None.
1601
+ band_name (str, optional): The name of the band to use for masking. Defaults to None.
1602
+ classifiy_above_threshold (bool, optional): If True, pixels above the threshold are classified as 1. If False, pixels below the threshold are classified as 1. Defaults to True.
1603
+ mask_zeros (bool, optional): If True, pixels with a value of 0 after the binary mask are masked out in the output binary mask. Useful for classifications. Defaults to False.
1604
+
1605
+ Returns:
1606
+ GenericCollection: GenericCollection singleband image collection with binary masks applied.
1607
+ """
1608
+ if self.collection.size().eq(0).getInfo():
1609
+ raise ValueError("The collection is empty. Cannot create a binary mask.")
1610
+ if band_name is None:
1611
+ first_image = self.collection.first()
1612
+ band_names = first_image.bandNames()
1613
+ if band_names.size().getInfo() == 0:
1614
+ raise ValueError("No bands available in the collection.")
1615
+ if band_names.size().getInfo() > 1:
1616
+ raise ValueError("Multiple bands available, please specify a band name.")
1617
+ else:
1618
+ band_name = band_names.get(0).getInfo()
1619
+ if threshold is None:
1620
+ raise ValueError("Threshold must be specified for binary masking.")
1621
+
1622
+ if classify_above_threshold:
1623
+ if mask_zeros:
1624
+ col = self.collection.map(
1625
+ lambda image: image.select(band_name).gte(threshold).rename(band_name).updateMask(image.select(band_name).gt(0)).copyProperties(image).set('system:time_start', image.get('system:time_start'))
1626
+ )
1627
+ else:
1628
+ col = self.collection.map(
1629
+ lambda image: image.select(band_name).gte(threshold).rename(band_name).copyProperties(image).set('system:time_start', image.get('system:time_start'))
1630
+ )
1631
+ else:
1632
+ if mask_zeros:
1633
+ col = self.collection.map(
1634
+ lambda image: image.select(band_name).lte(threshold).rename(band_name).updateMask(image.select(band_name).gt(0)).copyProperties(image).set('system:time_start', image.get('system:time_start'))
1635
+ )
1636
+ else:
1637
+ col = self.collection.map(
1638
+ lambda image: image.select(band_name).lte(threshold).rename(band_name).copyProperties(image).set('system:time_start', image.get('system:time_start'))
1639
+ )
1640
+ return GenericCollection(collection=col)
1641
+
1642
+ def anomaly(self, geometry, band_name=None, anomaly_band_name=None, replace=True, scale=None):
1643
+ """
1644
+ Calculates the anomaly of each image in a collection compared to the mean of each image.
1645
+
1646
+ This function computes the anomaly for each band in the input image by
1647
+ subtracting the mean value of that band from a provided ImageCollection.
1648
+ The anomaly is a measure of how much the pixel values deviate from the
1649
+ average conditions represented by the collection.
1650
+
1651
+ Args:
1652
+ geometry (ee.Geometry): The geometry for image reduction to define the mean value to be used for anomaly calculation.
1653
+ band_name (str, optional): A string representing the band name to be used for the output anomaly image. If not provided, the band name of the first band of the input image will be used.
1654
+ anomaly_band_name (str, optional): A string representing the band name to be used for the output anomaly image. If not provided, the band name of the first band of the input image will be used.
1655
+ replace (bool, optional): A boolean indicating whether to replace the original band with the anomaly band. If True, the output image will only contain the anomaly band. If False, the output image will retain all original bands and add the anomaly band. Default is True.
1656
+ scale (int, optional): The scale (in meters) to use for the image reduction. If not provided, the nominal scale of the image will be used.
1657
+
1658
+ Returns:
1659
+ GenericCollection: A GenericCollection where each image represents the anomaly (deviation from
1660
+ the mean) for the chosen band. The output images retain the same band name.
1661
+ """
1662
+ if self.collection.size().eq(0).getInfo():
1663
+ raise ValueError("The collection is empty.")
1664
+ if band_name is None:
1665
+ first_image = self.collection.first()
1666
+ band_names = first_image.bandNames()
1667
+ if band_names.size().getInfo() == 0:
1668
+ raise ValueError("No bands available in the collection.")
1669
+ elif band_names.size().getInfo() > 1:
1670
+ band_name = band_names.get(0).getInfo()
1671
+ print("Multiple bands available, will be using the first band in the collection for anomaly calculation. Please specify a band name if you wish to use a different band.")
1672
+ else:
1673
+ band_name = band_names.get(0).getInfo()
1674
+
1675
+ col = self.collection.map(lambda image: GenericCollection.anomaly_fn(image, geometry=geometry, band_name=band_name, anomaly_band_name=anomaly_band_name, replace=replace, scale=scale))
1676
+ return GenericCollection(collection=col)
1677
+
1678
+ def mask_via_band(self, band_to_mask, band_for_mask, threshold=-1, mask_above=True, add_band_to_original_image=False):
1679
+ """
1680
+ Masks select pixels of a selected band from an image based on another specified band and threshold (optional).
1681
+ Example use case is masking vegetation from image when targeting land pixels. Can specify whether to mask pixels above or below the threshold.
1682
+
1683
+ Args:
1684
+ band_to_mask (str): name of the band which will be masked (target image)
1685
+ band_for_mask (str): name of the band to use for the mask (band you want to remove/mask from target image)
1686
+ threshold (float): value between -1 and 1 where pixels less than threshold will be masked; defaults to -1 assuming input band is already classified (masked to pixels of interest).
1687
+ mask_above (bool): if True, masks pixels above the threshold; if False, masks pixels below the threshold
1688
+
1689
+ Returns:
1690
+ GenericCollection: A new GenericCollection with the specified band masked to pixels excluding from `band_for_mask`.
1691
+ """
1692
+ if self.collection.size().eq(0).getInfo():
1693
+ raise ValueError("The collection is empty.")
1694
+
1695
+ col = self.collection.map(
1696
+ lambda image: GenericCollection.mask_via_band_fn(
1697
+ image,
1698
+ band_to_mask=band_to_mask,
1699
+ band_for_mask=band_for_mask,
1700
+ threshold=threshold,
1701
+ mask_above=mask_above,
1702
+ add_band_to_original_image=add_band_to_original_image
1703
+ )
1704
+ )
1705
+ return GenericCollection(collection=col)
1706
+
1707
+ def mask_via_singleband_image(self, image_collection_for_mask, band_name_to_mask, band_name_for_mask, threshold=-1, mask_above=False, add_band_to_original_image=False):
1708
+ """
1709
+ Masks select pixels of a selected band from an image collection based on another specified singleband image collection and threshold (optional).
1710
+ Example use case is masking vegetation from image when targeting land pixels. Can specify whether to mask pixels above or below the threshold.
1711
+ This function pairs images from the two collections based on an exact match of the 'Date_Filter' property.
1712
+
1713
+ Args:
1714
+ image_collection_for_mask (GenericCollection): GenericCollection image collection to use for masking (source of pixels that will be used to mask the parent image collection)
1715
+ band_name_to_mask (str): name of the band which will be masked (target image)
1716
+ band_name_for_mask (str): name of the band to use for the mask (band which contains pixels the user wants to remove/mask from target image)
1717
+ threshold (float): threshold value where pixels less (or more, depending on `mask_above`) than threshold will be masked; defaults to -1.
1718
+ mask_above (bool): if True, masks pixels above the threshold; if False, masks pixels below the threshold
1719
+ add_band_to_original_image (bool): if True, adds the band used for masking to the original image as an additional band; if False, only the masked band is retained in the output image.
1720
+
1721
+ Returns:
1722
+ GenericCollection: A new GenericCollection with the specified band masked to pixels excluding from `band_for_mask`.
1723
+ """
1724
+
1725
+ if self.collection.size().eq(0).getInfo():
1726
+ raise ValueError("The collection is empty.")
1727
+ if not isinstance(image_collection_for_mask, GenericCollection):
1728
+ raise ValueError("image_collection_for_mask must be a GenericCollection object.")
1729
+ size1 = self.collection.size().getInfo()
1730
+ size2 = image_collection_for_mask.collection.size().getInfo()
1731
+ if size1 != size2:
1732
+ raise ValueError(f"Warning: Collections have different sizes ({size1} vs {size2}). Please ensure both collections have the same number of images and matching dates.")
1733
+ if size1 == 0 or size2 == 0:
1734
+ raise ValueError("Warning: One of the input collections is empty.")
1735
+
1736
+ # Pair by exact Date_Filter property
1737
+ primary = self.collection.select([band_name_to_mask])
1738
+ secondary = image_collection_for_mask.collection.select([band_name_for_mask])
1739
+ join = ee.Join.inner()
1740
+ flt = ee.Filter.equals(leftField='Date_Filter', rightField='Date_Filter')
1741
+ paired = join.apply(primary, secondary, flt)
1742
+
1743
+ def _map_pair(f):
1744
+ f = ee.Feature(f) # <-- treat as Feature
1745
+ prim = ee.Image(f.get('primary')) # <-- get the primary Image
1746
+ sec = ee.Image(f.get('secondary')) # <-- get the secondary Image
1747
+
1748
+ merged = prim.addBands(sec.select([band_name_for_mask]))
1749
+
1750
+ out = GenericCollection.mask_via_band_fn(
1751
+ merged,
1752
+ band_to_mask=band_name_to_mask,
1753
+ band_for_mask=band_name_for_mask,
1754
+ threshold=threshold,
1755
+ mask_above=mask_above,
1756
+ add_band_to_original_image=add_band_to_original_image
1757
+ )
1758
+
1759
+ # guarantee single band + keep properties
1760
+ out = ee.Image(out).select([band_name_to_mask]).copyProperties(prim, prim.propertyNames())
1761
+ out = out.set('Date_Filter', prim.get('Date_Filter'))
1762
+ return ee.Image(out) # <-- return as Image
1763
+
1764
+ col = ee.ImageCollection(paired.map(_map_pair))
1765
+ return GenericCollection(collection=col)
1766
+
1767
+ def band_rename(self, current_band_name, new_band_name):
1768
+ """Renames a band in all images of the GenericCollection in-place.
1769
+
1770
+ Replaces the band named `current_band_name` with `new_band_name` without
1771
+ retaining the original band name. If the band does not exist in an image,
1772
+ that image is returned unchanged.
1773
+
1774
+ Args:
1775
+ current_band_name (str): The existing band name to rename.
1776
+ new_band_name (str): The desired new band name.
1777
+
1778
+ Returns:
1779
+ GenericCollection: The GenericCollection with the band renamed in all images.
1780
+ """
1781
+ # check if `current_band_name` exists in the first image
1782
+ first_image = self.collection.first()
1783
+ has_band = first_image.bandNames().contains(current_band_name).getInfo()
1784
+ if not has_band:
1785
+ raise ValueError(f"Band '{current_band_name}' does not exist in the collection.")
1786
+
1787
+ renamed_collection = self.collection.map(
1788
+ lambda img: self.band_rename_fn(img, current_band_name, new_band_name)
1789
+ )
1790
+ return GenericCollection(collection=renamed_collection)
1791
+
1792
+ def image_grab(self, img_selector):
1793
+ """
1794
+ Selects ("grabs") an image by index from the collection. Easy way to get latest image or browse imagery one-by-one.
1795
+
1796
+ Args:
1797
+ img_selector: index of image in the collection for which user seeks to select/"grab".
1798
+
1799
+ Returns:
1800
+ ee.Image: ee.Image of selected image
1801
+ """
1802
+ # Convert the collection to a list
1803
+ image_list = self.collection.toList(self.collection.size())
1804
+
1805
+ # Get the image at the specified index
1806
+ image = ee.Image(image_list.get(img_selector))
1807
+
1808
+ return image
1809
+
1810
+ def custom_image_grab(self, img_col, img_selector):
1811
+ """
1812
+ Function to select ("grab") image of a specific index from an ee.ImageCollection object.
1813
+
1814
+ Args:
1815
+ img_col: ee.ImageCollection with same dates as another GenericCollection image collection object.
1816
+ img_selector: index of image in list of dates for which user seeks to "select".
1817
+
1818
+ Returns:
1819
+ ee.Image: ee.Image of selected image
1820
+ """
1821
+ # Convert the collection to a list
1822
+ image_list = img_col.toList(img_col.size())
1823
+
1824
+ # Get the image at the specified index
1825
+ image = ee.Image(image_list.get(img_selector))
1826
+
1827
+ return image
1828
+
1829
+ def image_pick(self, img_date):
1830
+ """
1831
+ Selects ("grabs") image of a specific date in format of 'YYYY-MM-dd' - will not work correctly if collection is composed of multiple images of the same date.
1832
+
1833
+ Args:
1834
+ img_date: date (str) of image to select in format of 'YYYY-MM-dd'
1835
+
1836
+ Returns:
1837
+ ee.Image: ee.Image of selected image
1838
+ """
1839
+ new_col = self.collection.filter(ee.Filter.eq("Date_Filter", img_date))
1840
+ return new_col.first()
1841
+
1842
+ def CollectionStitch(self, img_col2):
1843
+ """
1844
+ Function to mosaic two GenericCollection objects which share image dates.
1845
+ Mosaics are only formed for dates where both image collections have images.
1846
+ Image properties are copied from the primary collection. Server-side friendly.
1847
+
1848
+ Args:
1849
+ img_col2: secondary GenericCollection image collection to be mosaiced with the primary image collection
1850
+
1851
+ Returns:
1852
+ GenericCollection: GenericCollection image collection
1853
+ """
1854
+ dates_list = (
1855
+ ee.List(self._dates_list).cat(ee.List(img_col2.dates_list)).distinct()
1856
+ )
1857
+ filtered_dates1 = self._dates_list
1858
+ filtered_dates2 = img_col2._dates_list
1859
+
1860
+ filtered_col2 = img_col2.collection.filter(
1861
+ ee.Filter.inList("Date_Filter", filtered_dates1)
1862
+ )
1863
+ filtered_col1 = self.collection.filter(
1864
+ ee.Filter.inList(
1865
+ "Date_Filter", filtered_col2.aggregate_array("Date_Filter")
1866
+ )
1867
+ )
1868
+
1869
+ # Create a function that will be mapped over filtered_col1
1870
+ def mosaic_images(img):
1871
+ # Get the date of the image
1872
+ date = img.get("Date_Filter")
1873
+
1874
+ # Get the corresponding image from filtered_col2
1875
+ img2 = filtered_col2.filter(ee.Filter.equals("Date_Filter", date)).first()
1876
+
1877
+ # Create a mosaic of the two images
1878
+ mosaic = ee.ImageCollection.fromImages([img, img2]).mosaic()
1879
+
1880
+ # Copy properties from the first image and set the 'Date_Filter' property
1881
+ mosaic = (
1882
+ mosaic.copyProperties(img)
1883
+ .set("Date_Filter", date)
1884
+ .set("system:time_start", img.get("system:time_start"))
1885
+ )
1886
+
1887
+ return mosaic
1888
+
1889
+ # Map the function over filtered_col1
1890
+ new_col = filtered_col1.map(mosaic_images)
1891
+
1892
+ # Return a GenericCollection instance
1893
+ return GenericCollection(collection=new_col)
1894
+
1895
+ @property
1896
+ def MosaicByDate(self):
1897
+ """
1898
+ Property attribute function to mosaic collection images that share the same date.
1899
+
1900
+ The property CLOUD_COVER for each image is used to calculate an overall mean,
1901
+ which replaces the CLOUD_COVER property for each mosaiced image.
1902
+ Server-side friendly.
1903
+
1904
+ NOTE: if images are removed from the collection from cloud filtering, you may have mosaics composed of only one image.
1905
+
1906
+ Returns:
1907
+ GenericCollection: GenericCollection image collection with mosaiced imagery and mean CLOUD_COVER as a property
1908
+ """
1909
+ if self._MosaicByDate is None:
1910
+ input_collection = self.collection
1911
+
1912
+ # Function to mosaic images of the same date and accumulate them
1913
+ def mosaic_and_accumulate(date, list_accumulator):
1914
+ # date = ee.Date(date)
1915
+ list_accumulator = ee.List(list_accumulator)
1916
+ date_filter = ee.Filter.eq("Date_Filter", date)
1917
+ date_collection = input_collection.filter(date_filter)
1918
+ # Convert the collection to a list
1919
+ image_list = date_collection.toList(date_collection.size())
1920
+
1921
+ # Get the image at the specified index
1922
+ first_image = ee.Image(image_list.get(0))
1923
+ # Create mosaic
1924
+ mosaic = date_collection.mosaic().set("Date_Filter", date)
1925
+
1926
+ props_of_interest = [
1927
+ "system:time_start"
1928
+ ]
1929
+
1930
+ # mosaic = mosaic.copyProperties(self.image_grab(0), props_of_interest).set({
1931
+ # 'CLOUD_COVER': cloud_percentage
1932
+ # })
1933
+ mosaic = mosaic.copyProperties(first_image, props_of_interest)
1934
+
1935
+ return list_accumulator.add(mosaic)
1936
+
1937
+ # Get distinct dates
1938
+ distinct_dates = input_collection.aggregate_array("Date_Filter").distinct()
1939
+
1940
+ # Initialize an empty list as the accumulator
1941
+ initial = ee.List([])
1942
+
1943
+ # Iterate over each date to create mosaics and accumulate them in a list
1944
+ mosaic_list = distinct_dates.iterate(mosaic_and_accumulate, initial)
1945
+
1946
+ new_col = ee.ImageCollection.fromImages(mosaic_list)
1947
+ col = GenericCollection(collection=new_col)
1948
+ self._MosaicByDate = col
1949
+
1950
+ # Convert the list of mosaics to an ImageCollection
1951
+ return self._MosaicByDate
1952
+
1953
+ @staticmethod
1954
+ def ee_to_df(
1955
+ ee_object, columns=None, remove_geom=True, sort_columns=False, **kwargs
1956
+ ):
1957
+ """
1958
+ Converts an ee.FeatureCollection to pandas dataframe. Adapted from the geemap package (https://geemap.org/common/#geemap.common.ee_to_df)
1959
+
1960
+ Args:
1961
+ ee_object (ee.FeatureCollection): ee.FeatureCollection.
1962
+ columns (list): List of column names. Defaults to None.
1963
+ remove_geom (bool): Whether to remove the geometry column. Defaults to True.
1964
+ sort_columns (bool): Whether to sort the column names. Defaults to False.
1965
+ kwargs: Additional arguments passed to ee.data.computeFeature.
1966
+
1967
+ Raises:
1968
+ TypeError: ee_object must be an ee.FeatureCollection
1969
+
1970
+ Returns:
1971
+ pd.DataFrame: pandas DataFrame
1972
+ """
1973
+ if isinstance(ee_object, ee.Feature):
1974
+ ee_object = ee.FeatureCollection([ee_object])
1975
+
1976
+ if not isinstance(ee_object, ee.FeatureCollection):
1977
+ raise TypeError("ee_object must be an ee.FeatureCollection")
1978
+
1979
+ try:
1980
+ property_names = ee_object.first().propertyNames().sort().getInfo()
1981
+ if remove_geom:
1982
+ data = ee_object.map(
1983
+ lambda f: ee.Feature(None, f.toDictionary(property_names))
1984
+ )
1985
+ else:
1986
+ data = ee_object
1987
+
1988
+ kwargs["expression"] = data
1989
+ kwargs["fileFormat"] = "PANDAS_DATAFRAME"
1990
+
1991
+ df = ee.data.computeFeatures(kwargs)
1992
+
1993
+ if isinstance(columns, list):
1994
+ df = df[columns]
1995
+
1996
+ if remove_geom and ("geo" in df.columns):
1997
+ df = df.drop(columns=["geo"], axis=1)
1998
+
1999
+ if sort_columns:
2000
+ df = df.reindex(sorted(df.columns), axis=1)
2001
+
2002
+ return df
2003
+ except Exception as e:
2004
+ raise Exception(e)
2005
+
2006
+ @staticmethod
2007
+ def extract_transect(
2008
+ image,
2009
+ line,
2010
+ reducer="mean",
2011
+ n_segments=100,
2012
+ dist_interval=None,
2013
+ scale=None,
2014
+ crs=None,
2015
+ crsTransform=None,
2016
+ tileScale=1.0,
2017
+ to_pandas=False,
2018
+ **kwargs,
2019
+ ):
2020
+ """
2021
+ Extracts transect from an image. Adapted from the geemap package (https://geemap.org/common/#geemap.common.extract_transect).
2022
+
2023
+ Args:
2024
+ image (ee.Image): The image to extract transect from.
2025
+ line (ee.Geometry.LineString): The LineString used to extract transect from an image.
2026
+ reducer (str, optional): The ee.Reducer to use, e.g., 'mean', 'median', 'min', 'max', 'stdDev'. Defaults to "mean".
2027
+ n_segments (int, optional): The number of segments that the LineString will be split into. Defaults to 100.
2028
+ dist_interval (float, optional): The distance interval used for splitting the LineString. If specified, the n_segments parameter will be ignored. Defaults to None.
2029
+ scale (float, optional): A nominal scale in meters of the projection to work in. Defaults to None.
2030
+ crs (ee.Projection, optional): The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. Defaults to None.
2031
+ crsTransform (list, optional): The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and will replace any transform already set on the projection. Defaults to None.
2032
+ tileScale (float, optional): A scaling factor used to reduce aggregation tile size; using a larger tileScale (e.g. 2 or 4) may enable computations that run out of memory with the default. Defaults to 1.
2033
+ to_pandas (bool, optional): Whether to convert the result to a pandas dataframe. Default to False.
2034
+
2035
+ Raises:
2036
+ TypeError: If the geometry type is not LineString.
2037
+ Exception: If the program fails to compute.
2038
+
2039
+ Returns:
2040
+ ee.FeatureCollection: The FeatureCollection containing the transect with distance and reducer values.
2041
+ """
2042
+ try:
2043
+ geom_type = line.type().getInfo()
2044
+ if geom_type != "LineString":
2045
+ raise TypeError("The geometry type must be LineString.")
2046
+
2047
+ reducer = eval("ee.Reducer." + reducer + "()")
2048
+ maxError = image.projection().nominalScale().divide(5)
2049
+
2050
+ length = line.length(maxError)
2051
+ if dist_interval is None:
2052
+ dist_interval = length.divide(n_segments)
2053
+
2054
+ distances = ee.List.sequence(0, length, dist_interval)
2055
+ lines = line.cutLines(distances, maxError).geometries()
2056
+
2057
+ def set_dist_attr(l):
2058
+ l = ee.List(l)
2059
+ geom = ee.Geometry(l.get(0))
2060
+ distance = ee.Number(l.get(1))
2061
+ geom = ee.Geometry.LineString(geom.coordinates())
2062
+ return ee.Feature(geom, {"distance": distance})
2063
+
2064
+ lines = lines.zip(distances).map(set_dist_attr)
2065
+ lines = ee.FeatureCollection(lines)
2066
+
2067
+ transect = image.reduceRegions(
2068
+ **{
2069
+ "collection": ee.FeatureCollection(lines),
2070
+ "reducer": reducer,
2071
+ "scale": scale,
2072
+ "crs": crs,
2073
+ "crsTransform": crsTransform,
2074
+ "tileScale": tileScale,
2075
+ }
2076
+ )
2077
+
2078
+ if to_pandas:
2079
+ return GenericCollection.ee_to_df(transect)
2080
+ return transect
2081
+
2082
+ except Exception as e:
2083
+ raise Exception(e)
2084
+
2085
+ @staticmethod
2086
+ def transect(
2087
+ image,
2088
+ lines,
2089
+ line_names,
2090
+ reducer="mean",
2091
+ n_segments=None,
2092
+ dist_interval=30,
2093
+ to_pandas=True,
2094
+ ):
2095
+ """
2096
+ Computes and stores the values along a transect for each line in a list of lines. Builds off of the extract_transect function from the geemap package
2097
+ where checks are ran to ensure that the reducer column is present in the transect data. If the reducer column is not present, a column of NaNs is created.
2098
+ An ee reducer is used to aggregate the values along the transect, depending on the number of segments or distance interval specified. Defaults to 'mean' reducer.
2099
+
2100
+ Args:
2101
+ image (ee.Image): ee.Image object to use for calculating transect values.
2102
+ lines (list): List of ee.Geometry.LineString objects.
2103
+ line_names (list of strings): List of line string names.
2104
+ reducer (str): The ee reducer to use. Defaults to 'mean'.
2105
+ n_segments (int): The number of segments that the LineString will be split into. Defaults to None.
2106
+ dist_interval (float): The distance interval in meters used for splitting the LineString. If specified, the n_segments parameter will be ignored. Defaults to 30.
2107
+ to_pandas (bool): Whether to convert the result to a pandas dataframe. Defaults to True.
2108
+
2109
+ Returns:
2110
+ pd.DataFrame or ee.FeatureCollection: organized list of values along the transect(s)
2111
+ """
2112
+ # Create empty dataframe
2113
+ transects_df = pd.DataFrame()
2114
+
2115
+ # Check if line is a list of lines or a single line - if single line, convert to list
2116
+ if isinstance(lines, list):
2117
+ pass
2118
+ else:
2119
+ lines = [lines]
2120
+
2121
+ for i, line in enumerate(lines):
2122
+ if n_segments is None:
2123
+ transect_data = GenericCollection.extract_transect(
2124
+ image=image,
2125
+ line=line,
2126
+ reducer=reducer,
2127
+ dist_interval=dist_interval,
2128
+ to_pandas=to_pandas,
2129
+ )
2130
+ if reducer in transect_data.columns:
2131
+ # Extract the 'mean' column and rename it
2132
+ mean_column = transect_data[["mean"]]
2133
+ else:
2134
+ # Handle the case where 'mean' column is not present
2135
+ print(
2136
+ f"{reducer} column not found in transect data for line {line_names[i]}"
2137
+ )
2138
+ # Create a column of NaNs with the same length as the longest column in transects_df
2139
+ max_length = max(transects_df.shape[0], transect_data.shape[0])
2140
+ mean_column = pd.Series([np.nan] * max_length)
2141
+ else:
2142
+ transect_data = GenericCollection.extract_transect(
2143
+ image=image,
2144
+ line=line,
2145
+ reducer=reducer,
2146
+ n_segments=n_segments,
2147
+ to_pandas=to_pandas,
2148
+ )
2149
+ if reducer in transect_data.columns:
2150
+ # Extract the 'mean' column and rename it
2151
+ mean_column = transect_data[["mean"]]
2152
+ else:
2153
+ # Handle the case where 'mean' column is not present
2154
+ print(
2155
+ f"{reducer} column not found in transect data for line {line_names[i]}"
2156
+ )
2157
+ # Create a column of NaNs with the same length as the longest column in transects_df
2158
+ max_length = max(transects_df.shape[0], transect_data.shape[0])
2159
+ mean_column = pd.Series([np.nan] * max_length)
2160
+
2161
+ transects_df = pd.concat([transects_df, mean_column], axis=1)
2162
+
2163
+ transects_df.columns = line_names
2164
+
2165
+ return transects_df
2166
+
2167
+ def transect_iterator(
2168
+ self,
2169
+ lines,
2170
+ line_names,
2171
+ reducer="mean",
2172
+ dist_interval=30,
2173
+ n_segments=None,
2174
+ scale=30,
2175
+ processing_mode='aggregated',
2176
+ save_folder_path=None,
2177
+ sampling_method='line',
2178
+ point_buffer_radius=15
2179
+ ):
2180
+ """
2181
+ Computes and returns pixel values along transects for each image in a collection.
2182
+
2183
+ This iterative function generates time-series data along one or more lines, and
2184
+ supports two different geometric sampling methods ('line' and 'buffered_point')
2185
+ for maximum flexibility and performance.
2186
+
2187
+ There are two processing modes available, aggregated and iterative:
2188
+ - 'aggregated' (default; suggested): Fast, server-side processing. Fetches all results
2189
+ in a single request. Highly recommended. Returns a dictionary of pandas DataFrames.
2190
+ - 'iterative': Slower, client-side loop that processes one image at a time.
2191
+ Kept for backward compatibility (effectively depreciated). Returns None and saves individual CSVs.
2192
+ This method is not recommended unless absolutely necessary, as it is less efficient and may be subject to client-side timeouts.
2193
+
2194
+ Args:
2195
+ lines (list): A list of one or more ee.Geometry.LineString objects that
2196
+ define the transects.
2197
+ line_names (list): A list of string names for each transect. The length
2198
+ of this list must match the length of the `lines` list.
2199
+ reducer (str, optional): The name of the ee.Reducer to apply at each
2200
+ transect point (e.g., 'mean', 'median', 'first'). Defaults to 'mean'.
2201
+ dist_interval (float, optional): The distance interval in meters for
2202
+ sampling points along each transect. Will be overridden if `n_segments` is provided.
2203
+ Defaults to 30. Recommended to increase this value when using the
2204
+ 'line' processing method, or else you may get blank rows.
2205
+ n_segments (int, optional): The number of equal-length segments to split
2206
+ each transect line into for sampling. This parameter overrides `dist_interval`.
2207
+ Defaults to None.
2208
+ scale (int, optional): The nominal scale in meters for the reduction,
2209
+ which should typically match the pixel resolution of the imagery.
2210
+ Defaults to 30.
2211
+ processing_mode (str, optional): The method for processing the collection.
2212
+ - 'aggregated' (default): Fast, server-side processing. Fetches all
2213
+ results in a single request. Highly recommended. Returns a dictionary
2214
+ of pandas DataFrames.
2215
+ - 'iterative': Slower, client-side loop that processes one image at a
2216
+ time. Kept for backward compatibility. Returns None and saves
2217
+ individual CSVs.
2218
+ save_folder_path (str, optional): If provided, the function will save the
2219
+ resulting transect data to CSV files. The behavior depends on the
2220
+ `processing_mode`:
2221
+ - In 'aggregated' mode, one CSV is saved for each transect,
2222
+ containing all dates. (e.g., 'MyTransect_transects.csv').
2223
+ - In 'iterative' mode, one CSV is saved for each date,
2224
+ containing all transects. (e.g., '2022-06-15_transects.csv').
2225
+ sampling_method (str, optional): The geometric method used for sampling.
2226
+ - 'line' (default): Reduces all pixels intersecting each small line
2227
+ segment. This can be unreliable and produce blank rows if
2228
+ `dist_interval` is too small relative to the `scale`.
2229
+ - 'buffered_point': Reduces all pixels within a buffer around the
2230
+ midpoint of each line segment. This method is more robust and
2231
+ reliably avoids blank rows, but may not reduce all pixels along a line segment.
2232
+ point_buffer_radius (int, optional): The radius in meters for the buffer
2233
+ when `sampling_method` is 'buffered_point'. Defaults to 15.
2234
+
2235
+ Returns:
2236
+ dict or None:
2237
+ - If `processing_mode` is 'aggregated', returns a dictionary where each
2238
+ key is a transect name and each value is a pandas DataFrame. In the
2239
+ DataFrame, the index is the distance along the transect and each
2240
+ column represents an image date. Optionally saves CSV files if
2241
+ `save_folder_path` is provided.
2242
+ - If `processing_mode` is 'iterative', returns None as it saves
2243
+ files directly.
2244
+
2245
+ Raises:
2246
+ ValueError: If `lines` and `line_names` have different lengths, or if
2247
+ an unknown reducer or processing mode is specified.
2248
+ """
2249
+ # Validating inputs
2250
+ if len(lines) != len(line_names):
2251
+ raise ValueError("'lines' and 'line_names' must have the same number of elements.")
2252
+ ### Current, server-side processing method ###
2253
+ if processing_mode == 'aggregated':
2254
+ # Validating reducer type
2255
+ try:
2256
+ ee_reducer = getattr(ee.Reducer, reducer)()
2257
+ except AttributeError:
2258
+ raise ValueError(f"Unknown reducer: '{reducer}'.")
2259
+ ### Function to extract transects for a single image
2260
+ def get_transects_for_image(image):
2261
+ image_date = image.get('Date_Filter')
2262
+ # Initialize an empty list to hold all transect FeatureCollections
2263
+ all_transects_for_image = ee.List([])
2264
+ # Looping through each line and processing
2265
+ for i, line in enumerate(lines):
2266
+ # Index line and name
2267
+ line_name = line_names[i]
2268
+ # Determine maxError based on image projection, used for geometry operations
2269
+ maxError = image.projection().nominalScale().divide(5)
2270
+ # Calculate effective distance interval
2271
+ length = line.length(maxError) # using maxError here ensures consistency with cutLines
2272
+ # Determine effective distance interval based on n_segments or dist_interval
2273
+ effective_dist_interval = ee.Algorithms.If(
2274
+ n_segments,
2275
+ length.divide(n_segments),
2276
+ dist_interval or 30 # Defaults to 30 if both are None
2277
+ )
2278
+ # Generate distances along the line(s) for segmentation
2279
+ distances = ee.List.sequence(0, length, effective_dist_interval)
2280
+ # Segmenting the line into smaller lines at the specified distances
2281
+ cut_lines_geoms = line.cutLines(distances, maxError).geometries()
2282
+ # Function to create features with distance attributes
2283
+ # Adjusted to ensure consistent return types
2284
+ def set_dist_attr(l):
2285
+ # l is a list: [geometry, distance]
2286
+ # Extracting geometry portion of line
2287
+ geom_segment = ee.Geometry(ee.List(l).get(0))
2288
+ # Extracting distance value for attribute
2289
+ distance = ee.Number(ee.List(l).get(1))
2290
+ ### Determine final geometry based on sampling method
2291
+ # If the sampling method is 'buffered_point',
2292
+ # create a buffered point feature at the centroid of each segment,
2293
+ # otherwise create a line feature
2294
+ final_feature = ee.Algorithms.If(
2295
+ ee.String(sampling_method).equals('buffered_point'),
2296
+ # True Case: Create the buffered point feature
2297
+ ee.Feature(
2298
+ geom_segment.centroid(maxError).buffer(point_buffer_radius),
2299
+ {'distance': distance}
2300
+ ),
2301
+ # False Case: Create the line segment feature
2302
+ ee.Feature(geom_segment, {'distance': distance})
2303
+ )
2304
+ # Return either the line segment feature or the buffered point feature
2305
+ return final_feature
2306
+ # Creating a FeatureCollection of the cut lines with distance attributes
2307
+ # Using map to apply the set_dist_attr function to each cut line geometry
2308
+ line_features = ee.FeatureCollection(cut_lines_geoms.zip(distances).map(set_dist_attr))
2309
+ # Reducing the image over the line features to get transect values
2310
+ transect_fc = image.reduceRegions(
2311
+ collection=line_features, reducer=ee_reducer, scale=scale
2312
+ )
2313
+ # Adding image date and line name properties to each feature
2314
+ def set_props(feature):
2315
+ return feature.set({'image_date': image_date, 'transect_name': line_name})
2316
+ # Append to the list of all transects for this image
2317
+ all_transects_for_image = all_transects_for_image.add(transect_fc.map(set_props))
2318
+ # Combine all transect FeatureCollections into a single FeatureCollection and flatten
2319
+ # Flatten is used to merge the list of FeatureCollections into one
2320
+ return ee.FeatureCollection(all_transects_for_image).flatten()
2321
+ # Map the function over the entire image collection and flatten the results
2322
+ results_fc = ee.FeatureCollection(self.collection.map(get_transects_for_image)).flatten()
2323
+ # Convert the results to a pandas DataFrame
2324
+ df = GenericCollection.ee_to_df(results_fc, remove_geom=True)
2325
+ # Check if the DataFrame is empty
2326
+ if df.empty:
2327
+ print("Warning: No transect data was generated.")
2328
+ return {}
2329
+ # Initialize dictionary to hold output DataFrames for each transect
2330
+ output_dfs = {}
2331
+ # Loop through each unique transect name and create a pivot table
2332
+ for name in sorted(df['transect_name'].unique()):
2333
+ transect_df = df[df['transect_name'] == name]
2334
+ pivot_df = transect_df.pivot(index='distance', columns='image_date', values=reducer)
2335
+ pivot_df.columns.name = 'Date'
2336
+ output_dfs[name] = pivot_df
2337
+ # Optionally save each transect DataFrame to CSV
2338
+ if save_folder_path:
2339
+ for transect_name, transect_df in output_dfs.items():
2340
+ safe_filename = "".join(x for x in transect_name if x.isalnum() or x in "._-")
2341
+ file_path = f"{save_folder_path}{safe_filename}_transects.csv"
2342
+ transect_df.to_csv(file_path)
2343
+ print(f"Saved transect data to {file_path}")
2344
+
2345
+ return output_dfs
2346
+
2347
+ ### old, depreciated iterative client-side processing method ###
2348
+ elif processing_mode == 'iterative':
2349
+ if not save_folder_path:
2350
+ raise ValueError("`save_folder_path` is required for 'iterative' processing mode.")
2351
+
2352
+ image_collection_dates = self.dates
2353
+ for i, date in enumerate(image_collection_dates):
2354
+ try:
2355
+ print(f"Processing image {i+1}/{len(image_collection_dates)}: {date}")
2356
+ image = self.image_grab(i)
2357
+ transects_df = GenericCollection.transect(
2358
+ image, lines, line_names, reducer, n_segments, dist_interval, to_pandas=True
2359
+ )
2360
+ transects_df.to_csv(f"{save_folder_path}{date}_transects.csv")
2361
+ print(f"{date}_transects saved to csv")
2362
+ except Exception as e:
2363
+ print(f"An error occurred while processing image {i+1}: {e}")
2364
+ else:
2365
+ raise ValueError("`processing_mode` must be 'iterative' or 'aggregated'.")
2366
+
2367
+ @staticmethod
2368
+ def extract_zonal_stats_from_buffer(
2369
+ image,
2370
+ coordinates,
2371
+ buffer_size=1,
2372
+ reducer_type="mean",
2373
+ scale=30,
2374
+ tileScale=1,
2375
+ coordinate_names=None,
2376
+ ):
2377
+ """
2378
+ Function to extract spatial statistics from an image for a list or single set of (long, lat) coordinates, providing individual statistics for each location.
2379
+ A radial buffer is applied around each coordinate to extract the statistics, which defaults to 1 meter.
2380
+ The function returns a pandas DataFrame with the statistics for each coordinate.
2381
+
2382
+ NOTE: Be sure the coordinates are provided as longitude, latitude (x, y) tuples!
2383
+
2384
+ Args:
2385
+ image (ee.Image): The image from which to extract statistics. Should be single-band.
2386
+ coordinates (list or tuple): A single (lon, lat) tuple or a list of (lon, lat) tuples.
2387
+ buffer_size (int, optional): The radial buffer size in meters. Defaults to 1.
2388
+ reducer_type (str, optional): The ee.Reducer to use ('mean', 'median', 'min', etc.). Defaults to 'mean'.
2389
+ scale (int, optional): The scale in meters for the reduction. Defaults to 30.
2390
+ tileScale (int, optional): The tile scale factor. Defaults to 1.
2391
+ coordinate_names (list, optional): A list of names for the coordinates.
2392
+
2393
+ Returns:
2394
+ pd.DataFrame: A pandas DataFrame with the image's 'Date_Filter' as the index and a
2395
+ column for each coordinate location.
2396
+ """
2397
+ if isinstance(coordinates, tuple) and len(coordinates) == 2:
2398
+ coordinates = [coordinates]
2399
+ elif not (
2400
+ isinstance(coordinates, list)
2401
+ and all(isinstance(coord, tuple) and len(coord) == 2 for coord in coordinates)
2402
+ ):
2403
+ raise ValueError(
2404
+ "Coordinates must be a list of tuples with two elements each (longitude, latitude)."
2405
+ )
2406
+
2407
+ if coordinate_names is not None:
2408
+ if not isinstance(coordinate_names, list) or not all(
2409
+ isinstance(name, str) for name in coordinate_names
2410
+ ):
2411
+ raise ValueError("coordinate_names must be a list of strings.")
2412
+ if len(coordinate_names) != len(coordinates):
2413
+ raise ValueError(
2414
+ "coordinate_names must have the same length as the coordinates list."
2415
+ )
2416
+ else:
2417
+ coordinate_names = [f"Location {i+1}" for i in range(len(coordinates))]
2418
+
2419
+ image_date = image.get('Date_Filter')
2420
+
2421
+ points = [
2422
+ ee.Feature(
2423
+ ee.Geometry.Point(coord).buffer(buffer_size),
2424
+ {"location_name": str(name)},
2425
+ )
2426
+ for coord, name in zip(coordinates, coordinate_names)
2427
+ ]
2428
+ features = ee.FeatureCollection(points)
2429
+
2430
+ try:
2431
+ reducer = getattr(ee.Reducer, reducer_type)()
2432
+ except AttributeError:
2433
+ raise ValueError(f"Unknown reducer_type: '{reducer_type}'.")
2434
+
2435
+ stats_fc = image.reduceRegions(
2436
+ collection=features,
2437
+ reducer=reducer,
2438
+ scale=scale,
2439
+ tileScale=tileScale,
2440
+ )
2441
+
2442
+ df = GenericCollection.ee_to_df(stats_fc, remove_geom=True)
2443
+
2444
+ if df.empty:
2445
+ print("Warning: No results returned. The points may not intersect the image.")
2446
+ empty_df = pd.DataFrame(columns=coordinate_names)
2447
+ empty_df.index.name = 'Date'
2448
+ return empty_df
2449
+
2450
+ if reducer_type not in df.columns:
2451
+ print(f"Warning: Reducer type '{reducer_type}' not found in results. Returning raw data.")
2452
+ return df
2453
+
2454
+ pivot_df = df.pivot(columns='location_name', values=reducer_type)
2455
+ pivot_df['Date'] = image_date.getInfo() # .getInfo() is needed here as it's a server object
2456
+ pivot_df = pivot_df.set_index('Date')
2457
+ return pivot_df
2458
+
2459
+ def iterate_zonal_stats(
2460
+ self,
2461
+ geometries,
2462
+ band=None,
2463
+ reducer_type="mean",
2464
+ scale=30,
2465
+ geometry_names=None,
2466
+ buffer_size=1,
2467
+ tileScale=1,
2468
+ dates=None,
2469
+ file_path=None
2470
+ ):
2471
+ """
2472
+ Iterates over a collection of images and extracts spatial statistics (defaults to mean) for a given list of geometries or coordinates. Individual statistics are calculated for each geometry or coordinate provided.
2473
+ When coordinates are provided, a radial buffer is applied around each coordinate to extract the statistics, where the size of the buffer is determined by the buffer_size argument (defaults to 1 meter).
2474
+ The function returns a pandas DataFrame with the statistics for each coordinate and date, or optionally exports the data to a table in .csv format.
2475
+
2476
+ Args:
2477
+ geometries (ee.Geometry, ee.Feature, ee.FeatureCollection, list, or tuple): Input geometries for which to extract statistics. Can be a single ee.Geometry, an ee.Feature, an ee.FeatureCollection, a list of (lon, lat) tuples, or a list of ee.Geometry objects. Be careful to NOT provide coordinates as (lat, lon)!
2478
+ band (str, optional): The name of the band to use for statistics. If None, the first band is used. Defaults to None.
2479
+ reducer_type (str, optional): The ee.Reducer to use, e.g., 'mean', 'median', 'max', 'sum'. Defaults to 'mean'. Any ee.Reducer method can be used.
2480
+ scale (int, optional): Pixel scale in meters for the reduction. Defaults to 30.
2481
+ geometry_names (list, optional): A list of string names for the geometries. If provided, must match the number of geometries. Defaults to None.
2482
+ buffer_size (int, optional): Radial buffer in meters around coordinates. Defaults to 1.
2483
+ tileScale (int, optional): A scaling factor to reduce aggregation tile size. Defaults to 1.
2484
+ dates (list, optional): A list of date strings ('YYYY-MM-DD') for filtering the collection, such that only images from these dates are included for zonal statistic retrieval. Defaults to None, which uses all dates in the collection.
2485
+ file_path (str, optional): File path to save the output CSV.
2486
+
2487
+ Returns:
2488
+ pd.DataFrame or None: A pandas DataFrame with dates as the index and coordinate names
2489
+ as columns. Returns None if using 'iterative' mode with file_path.
2490
+
2491
+ Raises:
2492
+ ValueError: If input parameters are invalid.
2493
+ TypeError: If geometries input type is unsupported.
2494
+ """
2495
+ # Create a local reference to the collection object to allow for modifications (like band selection) without altering the original instance
2496
+ img_collection_obj = self
2497
+
2498
+ # If a specific band is requested, select only that band
2499
+ if band:
2500
+ img_collection_obj = GenericCollection(collection=img_collection_obj.collection.select(band))
2501
+ else:
2502
+ # If no band is specified, default to using the first band of the first image in the collection
2503
+ first_image = img_collection_obj.image_grab(0)
2504
+ first_band = first_image.bandNames().get(0)
2505
+ img_collection_obj = GenericCollection(collection=img_collection_obj.collection.select([first_band]))
2506
+
2507
+ # If a list of dates is provided, filter the collection to include only images matching those dates
2508
+ if dates:
2509
+ img_collection_obj = GenericCollection(
2510
+ collection=self.collection.filter(ee.Filter.inList('Date_Filter', dates))
2511
+ )
2512
+
2513
+ # Initialize variables to hold the standardized feature collection and coordinates
2514
+ features = None
2515
+ validated_coordinates = []
2516
+
2517
+ # Define a helper function to ensure every feature has a standardized 'geo_name' property
2518
+ # This handles features that might have different existing name properties or none at all
2519
+ def set_standard_name(feature):
2520
+ has_geo_name = feature.get('geo_name')
2521
+ has_name = feature.get('name')
2522
+ has_index = feature.get('system:index')
2523
+ new_name = ee.Algorithms.If(
2524
+ has_geo_name, has_geo_name,
2525
+ ee.Algorithms.If(has_name, has_name,
2526
+ ee.Algorithms.If(has_index, has_index, 'unnamed_geometry')))
2527
+ return feature.set({'geo_name': new_name})
2528
+
2529
+ # Handle input: FeatureCollection or single Feature
2530
+ if isinstance(geometries, (ee.FeatureCollection, ee.Feature)):
2531
+ features = ee.FeatureCollection(geometries)
2532
+ if geometry_names:
2533
+ print("Warning: 'geometry_names' are ignored when the input is an ee.Feature or ee.FeatureCollection.")
2534
+
2535
+ # Handle input: Single ee.Geometry
2536
+ elif isinstance(geometries, ee.Geometry):
2537
+ name = geometry_names[0] if (geometry_names and geometry_names[0]) else 'unnamed_geometry'
2538
+ features = ee.FeatureCollection([ee.Feature(geometries).set('geo_name', name)])
2539
+
2540
+ # Handle input: List (could be coordinates or ee.Geometry objects)
2541
+ elif isinstance(geometries, list):
2542
+ if not geometries: # Handle empty list case
2543
+ raise ValueError("'geometries' list cannot be empty.")
2544
+
2545
+ # Case: List of tuples (coordinates)
2546
+ if all(isinstance(i, tuple) for i in geometries):
2547
+ validated_coordinates = geometries
2548
+ # Generate default names if none provided
2549
+ if geometry_names is None:
2550
+ geometry_names = [f"Location_{i+1}" for i in range(len(validated_coordinates))]
2551
+ elif len(geometry_names) != len(validated_coordinates):
2552
+ raise ValueError("geometry_names must have the same length as the coordinates list.")
2553
+ # Create features with buffers around the coordinates
2554
+ points = [
2555
+ ee.Feature(ee.Geometry.Point(coord).buffer(buffer_size), {'geo_name': str(name)})
2556
+ for coord, name in zip(validated_coordinates, geometry_names)
2557
+ ]
2558
+ features = ee.FeatureCollection(points)
2559
+
2560
+ # Case: List of ee.Geometry objects
2561
+ elif all(isinstance(i, ee.Geometry) for i in geometries):
2562
+ if geometry_names is None:
2563
+ geometry_names = [f"Geometry_{i+1}" for i in range(len(geometries))]
2564
+ elif len(geometry_names) != len(geometries):
2565
+ raise ValueError("geometry_names must have the same length as the geometries list.")
2566
+ geom_features = [
2567
+ ee.Feature(geom).set({'geo_name': str(name)})
2568
+ for geom, name in zip(geometries, geometry_names)
2569
+ ]
2570
+ features = ee.FeatureCollection(geom_features)
2571
+
2572
+ else:
2573
+ raise TypeError("Input list must be a list of (lon, lat) tuples OR a list of ee.Geometry objects.")
2574
+
2575
+ # Handle input: Single tuple (coordinate)
2576
+ elif isinstance(geometries, tuple) and len(geometries) == 2:
2577
+ name = geometry_names[0] if geometry_names else 'Location_1'
2578
+ features = ee.FeatureCollection([
2579
+ ee.Feature(ee.Geometry.Point(geometries).buffer(buffer_size), {'geo_name': name})
2580
+ ])
2581
+ else:
2582
+ raise TypeError("Unsupported type for 'geometries'.")
2583
+
2584
+ # Apply the naming standardization to the created FeatureCollection
2585
+ features = features.map(set_standard_name)
2586
+
2587
+ # Dynamically retrieve the Earth Engine reducer based on the string name provided
2588
+ try:
2589
+ reducer = getattr(ee.Reducer, reducer_type)()
2590
+ except AttributeError:
2591
+ raise ValueError(f"Unknown reducer_type: '{reducer_type}'.")
2592
+
2593
+ # Define the function to map over the image collection
2594
+ def calculate_stats_for_image(image):
2595
+ image_date = image.get('Date_Filter')
2596
+ # Calculate statistics for all geometries in 'features' for this specific image
2597
+ stats_fc = image.reduceRegions(
2598
+ collection=features, reducer=reducer, scale=scale, tileScale=tileScale
2599
+ )
2600
+
2601
+ # Helper to ensure the result has the reducer property, even if masked
2602
+ # If the property is missing (e.g., all pixels masked), set it to a sentinel value (-9999)
2603
+ def guarantee_reducer_property(f):
2604
+ has_property = f.propertyNames().contains(reducer_type)
2605
+ return ee.Algorithms.If(has_property, f, f.set(reducer_type, -9999))
2606
+
2607
+ # Apply the guarantee check
2608
+ fixed_stats_fc = stats_fc.map(guarantee_reducer_property)
2609
+
2610
+ # Attach the image date to every feature in the result so we know which image it came from
2611
+ return fixed_stats_fc.map(lambda f: f.set('image_date', image_date))
2612
+
2613
+ # Map the calculation over the image collection and flatten the resulting FeatureCollections into one
2614
+ results_fc = ee.FeatureCollection(img_collection_obj.collection.map(calculate_stats_for_image)).flatten()
2615
+
2616
+ # Convert the Earth Engine FeatureCollection to a pandas DataFrame (client-side operation)
2617
+ df = GenericCollection.ee_to_df(results_fc, remove_geom=True)
2618
+
2619
+ # Check for empty results or missing columns
2620
+ if df.empty:
2621
+ raise ValueError("No results found for the given parameters. Check if the geometries intersect with the images, if the dates filter is too restrictive, or if the provided bands are empty.")
2622
+ if reducer_type not in df.columns:
2623
+ print(f"Warning: Reducer '{reducer_type}' not found in results.")
2624
+
2625
+ # Filter out the sentinel values (-9999) which indicate failed reductions/masked pixels
2626
+ initial_rows = len(df)
2627
+ df.dropna(subset=[reducer_type], inplace=True)
2628
+ df = df[df[reducer_type] != -9999]
2629
+ dropped_rows = initial_rows - len(df)
2630
+ if dropped_rows > 0:
2631
+ print(f"Warning: Discarded {dropped_rows} results due to failed reductions (e.g., no valid pixels in geometry).")
2632
+
2633
+ # Pivot the DataFrame so that each row represents a date and each column represents a geometry location
2634
+ pivot_df = df.pivot(index='image_date', columns='geo_name', values=reducer_type)
2635
+ # Rename the column headers (geometry names) to include the reducer type
2636
+ pivot_df.columns = [f"{col}_{reducer_type}" for col in pivot_df.columns]
2637
+ # Rename the index axis to 'Date' so it is correctly labeled when moved to a column later
2638
+ pivot_df.index.name = 'Date'
2639
+ # Remove the name of the columns axis (which defaults to 'geo_name') so it doesn't appear as a confusing label in the final output
2640
+ pivot_df.columns.name = None
2641
+ # Reset the index to move the 'Date' index into a regular column and create a standard numerical index (0, 1, 2...)
2642
+ pivot_df = pivot_df.reset_index(drop=False)
2643
+
2644
+ # If a file path is provided, save the resulting DataFrame to CSV
2645
+ if file_path:
2646
+ # Check if file_path ends with .csv and remove it if so for consistency
2647
+ if file_path.endswith('.csv'):
2648
+ file_path = file_path[:-4]
2649
+ pivot_df.to_csv(f"{file_path}.csv")
2650
+ print(f"Zonal stats saved to {file_path}.csv")
2651
+ return
2652
+ return pivot_df
2653
+
2654
+ def export_to_asset_collection(
2655
+ self,
2656
+ asset_collection_path,
2657
+ region,
2658
+ scale,
2659
+ dates=None,
2660
+ filename_prefix="",
2661
+ crs=None,
2662
+ max_pixels=int(1e13),
2663
+ description_prefix="export"
2664
+ ):
2665
+ """
2666
+ Exports an image collection to a Google Earth Engine asset collection. The asset collection will be created if it does not already exist,
2667
+ and each image exported will be named according to the provided filename prefix and date.
2668
+
2669
+ Args:
2670
+ asset_collection_path (str): The path to the asset collection.
2671
+ region (ee.Geometry): The region to export.
2672
+ scale (int): The scale of the export.
2673
+ dates (list, optional): The dates to export. Defaults to None.
2674
+ filename_prefix (str, optional): The filename prefix. Defaults to "", i.e. blank.
2675
+ crs (str, optional): The coordinate reference system. Defaults to None, which will use the image's CRS.
2676
+ max_pixels (int, optional): The maximum number of pixels. Defaults to int(1e13).
2677
+ description_prefix (str, optional): The description prefix. Defaults to "export".
2678
+
2679
+ Returns:
2680
+ None: (queues export tasks)
2681
+ """
2682
+ ic = self.collection
2683
+ if dates is None:
2684
+ dates = self.dates
2685
+ try:
2686
+ ee.data.createAsset({'type': 'ImageCollection'}, asset_collection_path)
2687
+ except Exception:
2688
+ pass
2689
+
2690
+ for date_str in dates:
2691
+ img = ee.Image(ic.filter(ee.Filter.eq('Date_Filter', date_str)).first())
2692
+ asset_id = asset_collection_path + "/" + filename_prefix + date_str
2693
+ desc = description_prefix + "_" + filename_prefix + date_str
2694
+
2695
+ params = {
2696
+ 'image': img,
2697
+ 'description': desc,
2698
+ 'assetId': asset_id,
2699
+ 'region': region,
2700
+ 'scale': scale,
2701
+ 'maxPixels': max_pixels
2702
+ }
2703
+ if crs:
2704
+ params['crs'] = crs
2705
+
2706
+ ee.batch.Export.image.toAsset(**params).start()
2707
+
2708
+ print("Queued", len(dates), "export tasks to", asset_collection_path)
2709
+
2710
+
2711
+