RadGEEToolbox 1.7.1__py3-none-any.whl → 1.7.3__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,233 @@
1
+ import ee
2
+
3
+ class ExportToDrive:
4
+ """
5
+ A class to handle exporting Earth Engine images and image collections to Google Drive.
6
+
7
+ This class supports exporting both native Earth Engine objects (ee.Image, ee.ImageCollection)
8
+ and RadGEEToolbox objects (e.g., LandsatCollection, Sentinel2Collection).
9
+
10
+ It is designed to intelligently handle RadGEEToolbox collections by utilizing their cached
11
+ `.dates` property for naming files, ensuring readable filenames (e.g., 'MyExport_2023-06-01')
12
+ instead of long system IDs.
13
+
14
+ Args:
15
+ input_data (ee.Image, ee.ImageCollection, or RadGEEToolbox object): The data to export.
16
+ Can be a single image, a collection, or a RadGEEToolbox wrapper object.
17
+ description (str): A description of the export task. This serves as the task name in the
18
+ GEE code editor and the default prefix for filenames. Defaults to 'export'.
19
+ folder (str, optional): The name of the destination folder in Google Drive. Defaults to None (root).
20
+ fileNamePrefix (str, optional): The filename prefix. For collections, this is prepended
21
+ to the generated unique name. If None, it defaults to the `description`.
22
+ scale (int): The resolution in meters per pixel. Defaults to 30.
23
+ region (ee.Geometry, optional): The region/geometry to export. Defaults to None (uses image footprint).
24
+ crs (str, optional): The coordinate reference system (e.g., 'EPSG:4326'). Defaults to None (uses image CRS).
25
+ maxPixels (int, optional): The maximum number of pixels allowed in the export. Defaults to 1e13.
26
+ fileFormat (str, optional): The output file format (e.g., 'GeoTIFF', 'TFRecord'). Defaults to 'GeoTIFF'.
27
+ name_pattern (str, optional): A string pattern for naming files when exporting a collection.
28
+ Supported placeholders:
29
+ - {date}: The date of the image. Prioritizes the RadGEEToolbox cached '.dates' list,
30
+ then 'Date_Filter' property, then formatted 'system:time_start'.
31
+ - {id}: The system:index of the image.
32
+ Defaults to '{date}'. The final filename will generally be "{fileNamePrefix}_{name_pattern}".
33
+ date_pattern (str, optional): The date format string to use if falling back to 'system:time_start'
34
+ (e.g., 'YYYY-MM-dd'). Defaults to 'YYYY-MM-dd'.
35
+ **kwargs: Additional keyword arguments passed directly to ee.batch.Export.image.toDrive
36
+ (e.g., 'formatOptions', 'shardSize').
37
+
38
+ Raises:
39
+ ValueError: If the input data type is not supported or if required arguments (like scale) are missing/invalid.
40
+ """
41
+
42
+ def __init__(
43
+ self,
44
+ input_data,
45
+ description="export",
46
+ folder=None,
47
+ fileNamePrefix=None,
48
+ scale=30,
49
+ region=None,
50
+ crs=None,
51
+ maxPixels=1e13,
52
+ fileFormat="GeoTIFF",
53
+ name_pattern="{date}",
54
+ date_pattern="YYYY-MM-dd",
55
+ **kwargs
56
+ ):
57
+ # 1. Capture RadGEEToolbox Metadata BEFORE unwrapping
58
+ # We explicitly look for the cached .dates list which is standard in RadGEEToolbox collections
59
+ self.radgee_dates = getattr(input_data, "dates", None)
60
+
61
+ # 2. Input Processing & Conversion
62
+ self.ee_object = self._validate_and_convert_input(input_data)
63
+
64
+ # Determine strict type of the resulting EE object
65
+ self.is_collection = isinstance(self.ee_object, ee.ImageCollection)
66
+ self.is_image = isinstance(self.ee_object, ee.Image)
67
+
68
+ if not self.is_collection and not self.is_image:
69
+ raise ValueError(
70
+ "Processed input must be an ee.Image or ee.ImageCollection. "
71
+ f"Got type: {type(self.ee_object)}"
72
+ )
73
+
74
+ # 3. Argument Validation
75
+ if scale is None:
76
+ raise ValueError("The 'scale' argument is required for export.")
77
+
78
+ if description is None or not isinstance(description, str):
79
+ raise ValueError("The 'description' argument must be a valid string.")
80
+
81
+ # Check for extraneous collection arguments when exporting a single image
82
+ if self.is_image:
83
+ if name_pattern != "{date}" or date_pattern != "YYYY-MM-dd":
84
+ print(
85
+ "Info: Extra arguments 'name_pattern' and/or 'date_pattern' were provided "
86
+ "but will be ignored because the input is a single image."
87
+ )
88
+
89
+ # 4. Store Attributes
90
+ self.description = description
91
+ self.folder = folder
92
+ self.fileNamePrefix = fileNamePrefix if fileNamePrefix else description
93
+ self.scale = scale
94
+ self.region = region
95
+ self.crs = crs
96
+ self.maxPixels = maxPixels
97
+ self.fileFormat = fileFormat
98
+ self.name_pattern = name_pattern
99
+ self.date_pattern = date_pattern
100
+ self.extra_kwargs = kwargs
101
+
102
+ def _validate_and_convert_input(self, input_data):
103
+ """
104
+ Helper to unwrap RadGEEToolbox objects or verify EE objects.
105
+ """
106
+ # Check if it's a RadGEEToolbox object (duck typing checks for 'collection' attribute)
107
+ if hasattr(input_data, "collection") and isinstance(input_data.collection, ee.ImageCollection):
108
+ return input_data.collection
109
+
110
+ # Check if it is already a valid EE object
111
+ if isinstance(input_data, (ee.Image, ee.ImageCollection)):
112
+ return input_data
113
+
114
+ # Attempt to handle computed objects that are implicitly images
115
+ try:
116
+ return ee.Image(input_data)
117
+ except Exception:
118
+ pass
119
+
120
+ raise ValueError(
121
+ "Input data is not a recognized RadGEEToolbox object, ee.Image, or ee.ImageCollection."
122
+ )
123
+
124
+ def export(self):
125
+ """
126
+ Initiates the export process. Detects whether to run a single task
127
+ or iterate through a collection.
128
+ """
129
+ if self.is_image:
130
+ self._export_single_image()
131
+ elif self.is_collection:
132
+ self._export_collection()
133
+
134
+ def _export_single_image(self):
135
+ """Internal method to export a single image."""
136
+ print(f"Starting export task for single image: {self.description}")
137
+
138
+ # Construct parameters dict, filtering out None values
139
+ params = {
140
+ "image": self.ee_object,
141
+ "description": self.description,
142
+ "folder": self.folder,
143
+ "fileNamePrefix": self.fileNamePrefix,
144
+ "scale": self.scale,
145
+ "region": self.region,
146
+ "crs": self.crs,
147
+ "maxPixels": self.maxPixels,
148
+ "fileFormat": self.fileFormat,
149
+ }
150
+ # Merge basic params with any extra kwargs provided
151
+ params.update(self.extra_kwargs)
152
+ # Remove keys with None values to allow GEE defaults to take over
153
+ params = {k: v for k, v in params.items() if v is not None}
154
+
155
+ task = ee.batch.Export.image.toDrive(**params)
156
+ task.start()
157
+ print(f"Task ID: {task.id}")
158
+
159
+ def _export_collection(self):
160
+ """Internal method to iterate and export a collection."""
161
+ # Get the size of the collection locally
162
+ try:
163
+ count = self.ee_object.size().getInfo()
164
+ except Exception as e:
165
+ raise ValueError(f"Could not determine collection size. Error: {e}")
166
+
167
+ if count == 0:
168
+ print("The image collection is empty. No export tasks were started.")
169
+ return
170
+
171
+ print(f"Processing collection export... ({count} images found)")
172
+
173
+ # Convert collection to list for iteration
174
+ col_list = self.ee_object.toList(count)
175
+
176
+ for i in range(count):
177
+ img = ee.Image(col_list.get(i))
178
+
179
+ # Fetch metadata efficiently in one call
180
+ meta = {}
181
+ try:
182
+ meta = img.toDictionary(["Date_Filter", "system:time_start", "system:index"]).getInfo()
183
+ except Exception:
184
+ pass
185
+
186
+ # --- Resolve Date ---
187
+ # Priority 1: Use the cached client-side list from RadGEEToolbox if available
188
+ if self.radgee_dates and i < len(self.radgee_dates):
189
+ date_val = self.radgee_dates[i]
190
+ # Priority 2: Check for 'Date_Filter' property (RadGEEToolbox standard)
191
+ elif "Date_Filter" in meta:
192
+ date_val = meta["Date_Filter"]
193
+ # Priority 3: Fallback to 'system:time_start'
194
+ elif "system:time_start" in meta:
195
+ date_val = ee.Date(meta["system:time_start"]).format(self.date_pattern).getInfo()
196
+ else:
197
+ date_val = "no_date"
198
+
199
+ # --- Resolve ID ---
200
+ # Use metadata dictionary or fallback to generated index
201
+ sys_index = meta.get("system:index", f"img_{i}")
202
+
203
+ # --- Generate Filename ---
204
+ # Apply pattern. Default is "{date}".
205
+ name_str = self.name_pattern.format(id=sys_index, date=date_val)
206
+
207
+ # Combine with global prefix.
208
+ # If prefix is "MyExport" and date is "2023-01-01", result is "MyExport_2023-01-01"
209
+ final_filename = f"{self.fileNamePrefix}_{name_str}" if self.fileNamePrefix else name_str
210
+
211
+ # Ensure unique description for the task (must be unique per task)
212
+ # We use the date in the description to make it easier to track in the GEE Task Manager
213
+ final_desc = f"{self.description}_{date_val}_{i}"
214
+
215
+ # Construct parameters
216
+ params = {
217
+ "image": img,
218
+ "description": final_desc,
219
+ "folder": self.folder,
220
+ "fileNamePrefix": final_filename,
221
+ "scale": self.scale,
222
+ "region": self.region,
223
+ "crs": self.crs,
224
+ "maxPixels": self.maxPixels,
225
+ "fileFormat": self.fileFormat,
226
+ }
227
+ params.update(self.extra_kwargs)
228
+ params = {k: v for k, v in params.items() if v is not None}
229
+
230
+ task = ee.batch.Export.image.toDrive(**params)
231
+ task.start()
232
+
233
+ print(f"Successfully queued {count} export tasks.")