gee-toolbox 0.1.0__tar.gz
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,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: gee-toolbox
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Common functions and tasks for Google Earth Engine Assets
|
|
5
|
+
Home-page: https://github.com/silversky54/gee-toolbox
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Keywords: earthengine,gee,google-earth-engine
|
|
8
|
+
Author: Erick G
|
|
9
|
+
Author-email: erick.linares@gmail.com
|
|
10
|
+
Maintainer: Erick G
|
|
11
|
+
Maintainer-email: erick.linares@gmail.com
|
|
12
|
+
Requires-Python: >=3.12,<4.0
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Topic :: Utilities
|
|
21
|
+
Requires-Dist: earthengine-api (>=0.1.418,<0.2.0)
|
|
22
|
+
Requires-Dist: geetools (>=1.4.0,<2.0.0)
|
|
23
|
+
Project-URL: Repository, https://github.com/silversky54/gee-toolbox
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# Google Earth Engine custom Toolbox for common tasks
|
|
27
|
+
|
|
28
|
+
**gee-toolbox** is a python package that serves as a custom toolbox with a variety of functions to execute common tasks related to Google Earth Engine. It Extends the functionality of GEE python client library and some other packages to manage GEE assets or process Images and ImageCollections.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
This package can currently only be installed from google
|
|
33
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Google Earth Engine custom Toolbox for common tasks
|
|
2
|
+
|
|
3
|
+
**gee-toolbox** is a python package that serves as a custom toolbox with a variety of functions to execute common tasks related to Google Earth Engine. It Extends the functionality of GEE python client library and some other packages to manage GEE assets or process Images and ImageCollections.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
This package can currently only be installed from google
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import ee
|
|
2
|
+
from geetools.Asset import Asset
|
|
3
|
+
import logging
|
|
4
|
+
from typing import List, Dict, Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _request_del_confirmation() -> bool:
|
|
8
|
+
# request Y/N confirmation from user. No input will return False
|
|
9
|
+
confirmation_loop = True
|
|
10
|
+
while confirmation_loop:
|
|
11
|
+
delete_confirmation = input(
|
|
12
|
+
"Are you sure you want to delete this asset? (Y/N)"
|
|
13
|
+
).lower()
|
|
14
|
+
if delete_confirmation in ["y", "n", ""]:
|
|
15
|
+
confirmation_loop = False
|
|
16
|
+
|
|
17
|
+
return delete_confirmation == "y"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _make_del_warning(asset: str, objects_list: list) -> str:
|
|
21
|
+
# count items in asset_types that are equal to type 'IMAGE_COLLECTION'
|
|
22
|
+
image_collections = len(
|
|
23
|
+
[type for type in objects_list if type == "IMAGE_COLLECTION"]
|
|
24
|
+
)
|
|
25
|
+
images = len([type for type in objects_list if type == "IMAGE"])
|
|
26
|
+
tables = len([type for type in objects_list if type == "TABLE"])
|
|
27
|
+
folders = len([type for type in objects_list if type == "FOLDER"])
|
|
28
|
+
warn_text = (
|
|
29
|
+
"******************************************************\n"
|
|
30
|
+
"WARNING\n"
|
|
31
|
+
"******************************************************\n"
|
|
32
|
+
"You are about to delete the following assets:\n"
|
|
33
|
+
f"-Images: {images}\n"
|
|
34
|
+
f"-Image Collections: {image_collections}\n"
|
|
35
|
+
f"-Tables: {tables}\n"
|
|
36
|
+
f"-Folders: {folders}\n"
|
|
37
|
+
f"Target: {asset}\n"
|
|
38
|
+
)
|
|
39
|
+
return warn_text
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
ALLOWED_ASSET_TYPES = ["IMAGE", "TABLE", "FOLDER", "IMAGE_COLLECTION"]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _check_asset_types(asset_types: str | List[str]) -> List[str]:
|
|
46
|
+
# verify allowed asset types
|
|
47
|
+
if not asset_types:
|
|
48
|
+
asset_types = ALLOWED_ASSET_TYPES
|
|
49
|
+
if type(asset_types) == str:
|
|
50
|
+
asset_types = [asset_types]
|
|
51
|
+
|
|
52
|
+
# Convert all asset types to uppercase
|
|
53
|
+
asset_types = [asset_type.upper() for asset_type in asset_types]
|
|
54
|
+
|
|
55
|
+
# if asset types not in valid types, raise an error
|
|
56
|
+
if not all([asset in ALLOWED_ASSET_TYPES for asset in asset_types]):
|
|
57
|
+
raise ValueError(f"Invalid asset type(s). Must be one of {ALLOWED_ASSET_TYPES}")
|
|
58
|
+
|
|
59
|
+
return asset_types
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def get_asset_names(asset_list: list) -> List:
|
|
63
|
+
return [asset["name"] for asset in asset_list]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def get_asset_types(asset_list: list) -> List:
|
|
67
|
+
return [asset["type"] for asset in asset_list]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# Creating alternative function to list assets to include filtering by asset type and inclusion/exclusion of parent folder
|
|
71
|
+
def list_assets(
|
|
72
|
+
parent: str,
|
|
73
|
+
asset_types: str | list = [],
|
|
74
|
+
recursive: bool = False,
|
|
75
|
+
inclusive: bool = False,
|
|
76
|
+
expand_image_collections: bool = False,
|
|
77
|
+
image_collections_exclusively: bool = False,
|
|
78
|
+
) -> List:
|
|
79
|
+
"""
|
|
80
|
+
lists assets from an assets folder or Image Collection in GEE. User can specify what type of assets to list.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
parent: path to the parent folder of the assets
|
|
84
|
+
asset_type: asset types to list. ['IMAGE', 'TABLE', 'FOLDER', 'IMAGE_COLLECTION']. If empty, all asset types are listed
|
|
85
|
+
recursive: Recursively search for assets in sub-folders
|
|
86
|
+
inclusive: Include the parent folder in the list
|
|
87
|
+
|
|
88
|
+
reference: https://github.com/spatialthoughts/projects/blob/master/ee-python/list_all_assets.py
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
# TODO - IF parent is image collection, should expand_image_collections be forced to true?
|
|
92
|
+
# TODO - Add handler for when results are paginated
|
|
93
|
+
# TODO - IF list is empty should this fail??
|
|
94
|
+
|
|
95
|
+
asset_types = _check_asset_types(asset_types)
|
|
96
|
+
|
|
97
|
+
# If Asset is not a folder or image collection raise error
|
|
98
|
+
try:
|
|
99
|
+
_parent = Asset(parent)
|
|
100
|
+
parent_type = _parent.type
|
|
101
|
+
if parent_type not in ["FOLDER", "IMAGE_COLLECTION"]:
|
|
102
|
+
raise ValueError("Path provided is not a Folder or Image Collection")
|
|
103
|
+
except (ValueError, ee.EEException) as e:
|
|
104
|
+
logging.error(e)
|
|
105
|
+
raise e
|
|
106
|
+
|
|
107
|
+
# If parent is an image collection, expand_image_collections is forced to True
|
|
108
|
+
if parent_type == "IMAGE_COLLECTION":
|
|
109
|
+
expand_image_collections = True
|
|
110
|
+
|
|
111
|
+
# List assets in the parent folder
|
|
112
|
+
try:
|
|
113
|
+
child_assets = ee.data.listAssets({"parent": _parent.as_posix()})["assets"]
|
|
114
|
+
except ee.EEException as e:
|
|
115
|
+
logging.warning(e)
|
|
116
|
+
raise e
|
|
117
|
+
|
|
118
|
+
asset_list = []
|
|
119
|
+
|
|
120
|
+
# if Inclusive is True add container info to the list
|
|
121
|
+
if inclusive:
|
|
122
|
+
asset_list.append({"name": _parent.as_posix(), "type": parent_type})
|
|
123
|
+
|
|
124
|
+
# Iterate over child assets.
|
|
125
|
+
for child_asset in child_assets:
|
|
126
|
+
# if not image_collections_exclusively, include everything
|
|
127
|
+
if (
|
|
128
|
+
not image_collections_exclusively
|
|
129
|
+
or child_asset["type"] != "IMAGE"
|
|
130
|
+
or parent_type == "IMAGE_COLLECTION"
|
|
131
|
+
):
|
|
132
|
+
asset_list.append(
|
|
133
|
+
{"name": child_asset["name"], "type": child_asset["type"]}
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# Recursively call the function to get child assets
|
|
137
|
+
if recursive and child_asset["type"] == "FOLDER":
|
|
138
|
+
asset_list.extend(
|
|
139
|
+
list_assets(
|
|
140
|
+
child_asset["name"],
|
|
141
|
+
asset_types=[], # Bring all, filter at the end
|
|
142
|
+
recursive=True, # Implicit from IF
|
|
143
|
+
inclusive=False, # False because parents are included above
|
|
144
|
+
expand_image_collections=expand_image_collections,
|
|
145
|
+
image_collections_exclusively=image_collections_exclusively,
|
|
146
|
+
)
|
|
147
|
+
)
|
|
148
|
+
if expand_image_collections and child_asset["type"] == "IMAGE_COLLECTION":
|
|
149
|
+
# Recursively call the function to get child assets
|
|
150
|
+
asset_list.extend(
|
|
151
|
+
list_assets(
|
|
152
|
+
child_asset["name"],
|
|
153
|
+
asset_types=[], # Bring all, filter at the end
|
|
154
|
+
recursive=True, # Indiferente, image collections are not recursive
|
|
155
|
+
inclusive=False, # False because parents are included above
|
|
156
|
+
expand_image_collections=True, # Implicit from IF
|
|
157
|
+
image_collections_exclusively=False,
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
# Filter assets not in asset_types
|
|
162
|
+
asset_list = [asset for asset in asset_list if asset["type"] in asset_types]
|
|
163
|
+
|
|
164
|
+
return asset_list
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def prune(
|
|
168
|
+
asset: str,
|
|
169
|
+
asset_types: str | List[str] = [],
|
|
170
|
+
recursive: bool = False,
|
|
171
|
+
expand_image_collections: bool = False,
|
|
172
|
+
inclusive: bool = True,
|
|
173
|
+
silent: bool = False,
|
|
174
|
+
dry_run: bool = False,
|
|
175
|
+
) -> Dict[str, List[str]]:
|
|
176
|
+
"""
|
|
177
|
+
Deletes Google Earth Engine assets in google projects.
|
|
178
|
+
|
|
179
|
+
Prune function can delete multiple assets if the specified asset is a folder or image collection. Delete recursively
|
|
180
|
+
in sub-folders by setting recursive=True. Specific asset types can be targeted using the asset_types argument. If
|
|
181
|
+
asset_types is empty, all asset types will be considered.
|
|
182
|
+
Folders and Image Collections will be excluded if not included in asset_types even if inclusive=True.
|
|
183
|
+
Deleting Image Collections will automatically include it's images in which case expand_image_collections=True is required.
|
|
184
|
+
Images in Image Collections can be targeted exclusively by setting image_collections_exclusively=True, this will
|
|
185
|
+
omit images that are not in an Image Collection.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
asset (str): The path to the asset.
|
|
189
|
+
asset_types (str | List[str], optional): One or more asset types to delete. Defaults to []. Valid asset
|
|
190
|
+
types are 'IMAGE', 'TABLE', 'FOLDER', 'IMAGE_COLLECTION'. An empty list [] deletes all asset types.
|
|
191
|
+
recursive (bool, optional): Whether to include sub-folders recursively. Defaults to False.
|
|
192
|
+
Required if deleting folders.
|
|
193
|
+
expand_image_collections (bool, optional): Whether to include images in image collections. Defaults to False.
|
|
194
|
+
Required if deleting folders or image collections.
|
|
195
|
+
inclusive (bool, optional): Whether to include the top asset if asset is a folder or image collection.
|
|
196
|
+
Defaults to True. Ignored if Folder or Image Collection is not included in asset_types.
|
|
197
|
+
silent (bool, optional): Whether to skip the confirmation prompt. Defaults to False.
|
|
198
|
+
Use with caution, will delete all assets without requesting confirmation.
|
|
199
|
+
dry_run (bool, optional): List all assets to delete without deleting them. Defaults to False.
|
|
200
|
+
Returns:
|
|
201
|
+
Dict: A dictionary containing the results of the deletion operation. The dictionary has the following keys:
|
|
202
|
+
- "deleted": List of assets successfully deleted.
|
|
203
|
+
- "failed": list of assets that failed to delete.
|
|
204
|
+
- "skipped": List of assets that were skipped from deleting.
|
|
205
|
+
Raises:
|
|
206
|
+
ValueError: If asset_types includes 'FOLDER' but omits any other required asset types.
|
|
207
|
+
ValueError: If deleting a folder but recursive=False or expand_image_collections=False.
|
|
208
|
+
ValueError: If deleting an image collection but expand_image_collections=False.
|
|
209
|
+
"""
|
|
210
|
+
|
|
211
|
+
_asset = Asset(asset)
|
|
212
|
+
results = {"deleted": [], "failed": [], "skipped": []}
|
|
213
|
+
|
|
214
|
+
_asset.exists(raised=True)
|
|
215
|
+
asset_types = _check_asset_types(asset_types)
|
|
216
|
+
|
|
217
|
+
# if asset_types includes FOLDER, asset_types should also include all other asset types or raise error
|
|
218
|
+
if any([asset_type == "FOLDER" for asset_type in asset_types]):
|
|
219
|
+
if not all(
|
|
220
|
+
[allowed_type in asset_types for allowed_type in ALLOWED_ASSET_TYPES]
|
|
221
|
+
):
|
|
222
|
+
raise ValueError(
|
|
223
|
+
(
|
|
224
|
+
"asset_types includes 'FOLDER' but omits other required asset types. "
|
|
225
|
+
"Prefer asset_types=[] to delete folders and all their contents."
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
# IF deleting folders, recursive and expand_image_collections need to be True
|
|
230
|
+
if (
|
|
231
|
+
"FOLDER" in asset_types
|
|
232
|
+
and not _asset.is_image_collection()
|
|
233
|
+
and (not recursive or not expand_image_collections)
|
|
234
|
+
):
|
|
235
|
+
raise ValueError(
|
|
236
|
+
"Deleting a folder requires recursive=True and expand_image_collections=True"
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
# If Deleting image collections, expand_image_collections needs to be True
|
|
240
|
+
if "IMAGE_COLLECTION" in asset_types and not expand_image_collections:
|
|
241
|
+
raise ValueError(
|
|
242
|
+
"Deleting an image collection requires expand_image_collections=True"
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
# if deleting image collections and not images (explicitly)
|
|
246
|
+
# add 'IMAGE' to asset_types and set image_collection_exclusively=True
|
|
247
|
+
image_collections_exclusively = False
|
|
248
|
+
if "IMAGE_COLLECTION" in asset_types and not "IMAGE" in asset_types:
|
|
249
|
+
image_collections_exclusively = True
|
|
250
|
+
asset_types.append("IMAGE")
|
|
251
|
+
|
|
252
|
+
is_container = (
|
|
253
|
+
_asset.is_project() or _asset.is_folder() or _asset.is_image_collection()
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
# list objects (images, tables, folders, imageCollections, etc) in folder and sub folders
|
|
257
|
+
if is_container:
|
|
258
|
+
asset_list = list_assets(
|
|
259
|
+
parent=_asset.as_posix(),
|
|
260
|
+
asset_types=asset_types,
|
|
261
|
+
recursive=recursive,
|
|
262
|
+
inclusive=inclusive,
|
|
263
|
+
expand_image_collections=expand_image_collections,
|
|
264
|
+
image_collections_exclusively=image_collections_exclusively,
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
# Split and sort per level of hierarchy. Recursive deleting will fail If not deleted in reverse order
|
|
268
|
+
assets_ordered: dict = {}
|
|
269
|
+
for _target_asset in asset_list:
|
|
270
|
+
_target_asset = Asset(_target_asset["name"])
|
|
271
|
+
lvl = len(_target_asset.parts)
|
|
272
|
+
assets_ordered.setdefault(lvl, [])
|
|
273
|
+
assets_ordered[lvl].append(_target_asset)
|
|
274
|
+
assets_ordered = dict(sorted(assets_ordered.items(), reverse=True))
|
|
275
|
+
|
|
276
|
+
else:
|
|
277
|
+
asset_list = [{"name": _asset.as_posix(), "type": _asset.type}]
|
|
278
|
+
|
|
279
|
+
print(_make_del_warning(_asset.as_posix(), get_asset_types(asset_list)))
|
|
280
|
+
|
|
281
|
+
# End if Dry Run
|
|
282
|
+
if dry_run:
|
|
283
|
+
results["skipped"] = get_asset_names(asset_list)
|
|
284
|
+
print("Dry run, no items will be deleted")
|
|
285
|
+
return results
|
|
286
|
+
|
|
287
|
+
# Warn user and ask for confirmation
|
|
288
|
+
if silent:
|
|
289
|
+
delete_confirmation = True
|
|
290
|
+
else:
|
|
291
|
+
delete_confirmation = _request_del_confirmation()
|
|
292
|
+
|
|
293
|
+
# Proceed to delete
|
|
294
|
+
# Not using Asset.delete() method. Need to be able to delete specific asset types only
|
|
295
|
+
if delete_confirmation:
|
|
296
|
+
|
|
297
|
+
def _delete(asset: Asset) -> None:
|
|
298
|
+
try:
|
|
299
|
+
ee.data.deleteAsset(str(asset))
|
|
300
|
+
results["deleted"].append(str(asset))
|
|
301
|
+
except Exception as e:
|
|
302
|
+
results["failed"].append(str(asset))
|
|
303
|
+
|
|
304
|
+
print(f"Deleting {len(asset_list)} items from {_asset.as_posix()}")
|
|
305
|
+
|
|
306
|
+
# delete all items starting from the more nested ones
|
|
307
|
+
for lvl in assets_ordered:
|
|
308
|
+
[_delete(asset) for asset in assets_ordered[lvl]]
|
|
309
|
+
|
|
310
|
+
print(
|
|
311
|
+
f"Deleted {len(results['deleted'])} items, {len(results['failed'])} items failed to delete"
|
|
312
|
+
)
|
|
313
|
+
else:
|
|
314
|
+
results["skipped"] = get_asset_names(asset_list)
|
|
315
|
+
print(f"No items deleted from {_asset.as_posix()}")
|
|
316
|
+
return results
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
if __name__ == "__main__":
|
|
320
|
+
pass
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "gee-toolbox"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Common functions and tasks for Google Earth Engine Assets"
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
authors = ["Erick G <erick.linares@gmail.com>"]
|
|
7
|
+
maintainers = ["Erick G <erick.linares@gmail.com>"]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
repository = "https://github.com/silversky54/gee-toolbox"
|
|
10
|
+
keywords = ["earthengine", "gee", "google-earth-engine"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 3 - Alpha",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Topic :: Utilities",
|
|
15
|
+
"Topic :: Software Development :: Libraries",
|
|
16
|
+
"License :: OSI Approved :: Apache Software License",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Operating System :: OS Independent"
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[tool.poetry.dependencies]
|
|
22
|
+
python = "^3.12"
|
|
23
|
+
earthengine-api = "^0.1.418"
|
|
24
|
+
geetools = "^1.4.0"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
[tool.poetry.group.dev.dependencies]
|
|
28
|
+
jupyter = "^1.1.1"
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["poetry-core"]
|
|
32
|
+
build-backend = "poetry.core.masonry.api"
|