forked from gee-community/geeViz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetManagerLib.py
729 lines (606 loc) · 25.5 KB
/
assetManagerLib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
"""
Helpful functions for managing GEE assets
geeViz.assetManagerLib includes functions for copying, deleting, uploading, changing permissions, and more.
"""
"""
Copyright 2024 Leah Campbell and Ian Housman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Pieces of code were taken from: https://github.com/tracek/gee_asset_manager
"""
# --------------------------------------------------------------------------
# ASSETMANAGERLIB.PY
# --------------------------------------------------------------------------
#%%
import geeViz.geeView
import sys, ee, os, shutil, subprocess, datetime, calendar, json, glob
import time, logging, pdb
taskLimit = 10
#%%
#############################################################################################
# Functions to update ACL
#############################################################################################
# Function for updating ACL for a given GEE asset path
def updateACL(assetName, writers=[], all_users_can_read=True, readers=[]):
print("Updating permissions for: ", assetName)
try:
ee.data.setAssetAcl(
assetName,
json.dumps(
{
"writers": writers,
"all_users_can_read": all_users_can_read,
"readers": readers,
}
),
)
except Exception as E:
print("Could not update permissions for: ", assetName)
print(E)
# --------------------------------------------------------------------------------------------
# Function for updating all assets under a given folder level in GEE
def batchUpdateAcl(folder, writers=[], all_users_can_read=True, readers=[]):
# Update ACL for folders first, then find all images within them.
assets = ee.data.listAssets({"parent": folder})["assets"]
folders = [a for a in assets if a["type"] == "FOLDER"]
for subFolder in folders:
print("Updating acl for:", subFolder)
try:
updateACL(subFolder["id"], writers, all_users_can_read, readers)
except:
print("Could not update", subFolder)
allImages = walkFolders(folder)
for image in allImages:
print("Updating acl for:", image)
try:
updateACL(image, writers, all_users_can_read, readers)
except:
print("Could not update", image)
#############################################################################################
# Functions for copying, deleting, etc. assets
#############################################################################################
# outTypes = 'imageCollection' or 'tables'
def batchCopy(fromFolder, toFolder, outType="imageCollection"):
if outType == "imageCollection":
create_image_collection(toFolder)
elif outType == "tables":
verify_path(toFolder)
if toFolder[-1] == "/":
toFolder = toFolder[:-1]
if outType == "imageCollection":
images = walkFolders(fromFolder)
elif outType == "tables":
images = walkFoldersTables(fromFolder)
# print( images)
for image in images:
out = toFolder + "/" + base(image)
if not ee_asset_exists(out):
print("Copying: ", image)
try:
ee.data.copyAsset(image, out)
except Exception as E:
print("Could not copy: ", image)
print(E)
else:
print(out, " already exists")
def copyByName(fromFolder, toFolder, nameIdentifier, outType="imageCollection"):
if outType == "imageCollection":
create_image_collection(toFolder)
elif outType == "tables":
verify_path(toFolder)
if toFolder[-1] == "/":
toFolder = toFolder[:-1]
if outType == "imageCollection":
images = walkFolders(fromFolder)
elif outType == "tables":
images = walkFoldersTables(fromFolder)
# print( images)
for image in images:
if nameIdentifier in image:
out = toFolder + "/" + base(image)
print(out)
try:
ee.data.copyAsset(image, out)
except:
print(out, "Error: May already exist")
# ---------------------------------------------------------------------------------------------
def moveImages(images, toFolder, delete_original=False):
create_image_collection(toFolder)
for image in images:
print("Copying", base(image))
out = toFolder + "/" + base(image)
if not ee_asset_exists(out):
try:
ee.data.copyAsset(image, out)
if delete_original:
ee.data.deleteAsset(image)
except Exception as E:
print("Error copying:", image)
print(E)
# ---------------------------------------------------------------------------------------------
# types = 'imageCollection' or 'tables'
def batchDelete(Collection, type="imageCollection"):
if type == "imageCollection":
images = walkFolders(Collection)
elif type == "tables":
images = walkFoldersTables(Collection)
for image in images:
print("Deleting: " + Collection + "/" + base(image))
try:
ee.data.deleteAsset(image)
except Exception as E:
print("Could not delete: ", image)
print(E)
def deleteByName(Collection, nameIdentifier, type="imageCollection"):
if type == "imageCollection":
images = walkFolders(Collection)
elif type == "tables":
images = walkFoldersTables(Collection)
for image in images:
if nameIdentifier in image:
print("Deleting: " + Collection + "/" + base(image))
try:
ee.data.deleteAsset(image)
except Exception as E:
print("Could not delete: ", image)
print(E)
#############################################################################################
# Asset Info Queries
#############################################################################################
# Adapted from Samapriya Roy's assetsize.py (https://github.com/samapriya/Planet-GEE-Pipeline-CLI)
suffixes = ["B", "KB", "MB", "GB", "TB", "PB"]
def humansize(nbytes):
i = 0
while nbytes >= 1024 and i < len(suffixes) - 1:
nbytes /= 1024.0
i += 1
f = ("%.2f" % nbytes).rstrip("0").rstrip(".")
return "%s %s" % (f, suffixes[i])
l = []
def assetsize(asset):
header = ee.data.getInfo(asset)["type"]
if header == "IMAGE_COLLECTION":
collc = ee.ImageCollection(asset)
size = collc.aggregate_array("system:asset_size")
print("")
print(str(asset) + " ===> " + str(humansize(sum(size.getInfo()))))
print("Total number of items in collection: " + str(collc.size().getInfo()))
elif header == "IMAGE":
collc = ee.Image(asset)
print("")
print(
str(asset)
+ " ===> "
+ str(humansize(collc.get("system:asset_size").getInfo()))
)
elif header == "TABLE":
collc = ee.FeatureCollection(asset)
print("")
print(
str(asset)
+ " ===> "
+ str(humansize(collc.get("system:asset_size").getInfo()))
)
elif header == "FOLDER":
b = subprocess.check_output("earthengine du " + asset + " -s", shell=True)
num = subprocess.check_output("earthengine ls " + asset, shell=True)
size = humansize(float(b.strip().split(" ")[0]))
print("")
print(str(asset) + " ===> " + str(size))
print("Total number of items in folder: " + str(len(num.split("\n")) - 1))
#############################################################################################
# Functions to Upload to Google Cloud Storage and Google Earth Engine Repository
#############################################################################################
# Wrapper function to upload to google cloudStorage
# Bucket must already exist
def upload_to_gcs(image_dir, gs_bucket, image_extension=".tif", copy_or_sync="copy"):
if copy_or_sync == "copy":
call_str = (
'gsutil.cmd -m cp "' + image_dir + "*" + image_extension + '" ' + gs_bucket
)
else:
call_str = 'gsutil.cmd -m rsync "' + image_dir + '" ' + gs_bucket
print(call_str)
call = subprocess.Popen(call_str)
call.wait()
# ---------------------------------------------------------------------------------------------
# Wrapper function for uploading GEE assets
def upload_to_gee(
image_dir,
gs_bucket,
asset_dir,
image_extension=".tif",
resample_method="MEAN",
band_names=[],
property_list=[],
):
# First upload to GCS
# upload_to_gcs(image_dir,gs_bucket,image_extension,'copy')
# Make sure collection exists
create_image_collection(asset_dir)
# Get the names that need to be transferred form GCS to EE
tifs = glob.glob(os.path.join(image_dir, image_extension))
# Set up the asset dir, files, and band names object
asset_dir = check_end(asset_dir)
gs_files = map(lambda i: gs_bucket + os.path.basename(i), tifs)
band_names = map(lambda i: {"id": i}, band_names)
# Iterate through each file to upload
i = 0
for gs_file in gs_files:
# Check to ensure the task limit (set globally) is not exceeded
# Exporting can slow down if too many tasks are submitted
limitTasks(taskLimit)
# Export asset if it does not exist
asset_name = asset_dir + base(gs_file)
if not ee_asset_exists(asset_name):
print("Importing asset:", asset_name)
properties = property_list[i]
# Set up sources
sl = {"primaryPath": gs_file, "additionalPaths": []}
# Set up request
request = {
"id": asset_name,
"tilesets": [{"sources": [sl]}],
"bands": band_names,
"properties": properties,
"pyramidingPolicy": resample_method,
}
print(request)
# Get a task id
taskid = ee.data.newTaskId(1)[0]
print(taskid)
# Submit task
message = ee.data.startIngestion(taskid, request)
print("Task message:", message)
else:
print(asset_name, "already exists")
i += 1
# Keep track of tasks once they are all submitted
task_count = countTasks(False)
while task_count >= 1:
running, ready = countTasks(True)
print(running, "tasks running at:", now())
print(ready, "tasks ready at:", now())
task_count = running + ready
time.sleep(5)
#############################################################################################
# Helper Functions
##############################################################################################
# Function to ensure trailing / is in path
def check_end(in_path, add="/"):
if in_path[-len(add) :] != add:
out = in_path + add
else:
out = in_path
return out
##############################################################################################
# Returns all files containing an extension or any of a list of extensions
# Can give a single extension or a list of extensions
# use glob.glob
# def glob(Dir, extension):
# Dir = check_end(Dir)
# if type(extension) != list:
# if extension.find('*') == -1:
# return map(lambda i : Dir + i, filter(lambda i: os.path.splitext(i)[1] == extension, os.listdir(Dir)))
# else:
# return map(lambda i : Dir + i, os.listdir(Dir))
# else:
# out_list = []
# for ext in extension:
# tl = map(lambda i : Dir + i, filter(lambda i: os.path.splitext(i)[1] == ext, os.listdir(Dir)))
# for l in tl:
# out_list.append(l)
# return out_list
##############################################################################################
# Function to get filename without extension
def base(in_path):
return os.path.basename(os.path.splitext(in_path)[0])
##############################################################################################
#################################################################
# Function to walk down folders and get all images
def walkFolders(folder, images=[]):
assets = ee.data.getList({"id": folder})
folders = [str(i["id"]) for i in assets if i["type"] == "Folder"]
imagesT = [str(i["id"]) for i in assets if i["type"] == "Image"]
print(imagesT)
for i in imagesT:
if i not in images:
images.append(i)
iteration = 2
while len(folders) > 0:
print("Starting iteration", iteration)
for folder in folders:
print(folder)
assets = ee.data.getList({"id": folder})
folders = [str(i["id"]) for i in assets if i["type"] == "Folder"]
imagesT = [str(i["id"]) for i in assets if i["type"] == "Image"]
for i in imagesT:
if i not in images:
images.append(i)
iteration += 1
return images
#################################################################
# Function to walk down folders and get all tables
def walkFoldersTables(folder, tables=[]):
assets = ee.data.getList({"id": folder})
folders = [str(i["id"]) for i in assets if i["type"] == "Folder"]
tablesT = [str(i["id"]) for i in assets if i["type"] == "Table"]
print(tablesT)
for i in tablesT:
if i not in tables:
tables.append(i)
iteration = 2
while len(folders) > 0:
print("Starting iteration", iteration)
for folder in folders:
print(folder)
assets = ee.data.getList({"id": folder})
folders = [str(i["id"]) for i in assets if i["type"] == "Folder"]
tablesT = [str(i["id"]) for i in assets if i["type"] == "Table"]
for i in tablesT:
if i not in tables:
tables.append(i)
iteration += 1
return tables
#################################################################
##############################################################################################
# Make sure the directory exists
def check_dir(in_path):
if os.path.exists(in_path) == False:
print("Making dir:", in_path)
os.makedirs(in_path)
##############################################################################################
def year_month_day_to_seconds(year_month_day):
ymd = year_month_day
return calendar.timegm(datetime.datetime(ymd[0], ymd[1], ymd[2]).timetuple())
#######################################################################################
def limitTasks(taskLimit):
taskCount = countTasks()
while taskCount > taskLimit:
running, ready = countTasks(True)
print(running, "tasks running at:", now())
print(ready, "tasks ready at:", now())
taskCount = running + ready
time.sleep(10)
#######################################################################################
def countTasks(break_running_ready=False):
tasks = ee.data.getTaskList()
running_tasks = len(filter(lambda i: i["state"] == "RUNNING", tasks))
ready_tasks = len(filter(lambda i: i["state"] == "READY", tasks))
if not break_running_ready:
return running_tasks + ready_tasks
else:
return running_tasks, ready_tasks
#######################################################################################
# Adapted from: https://github.com/tracek/gee_asset_manager/blob/master/geebam/helper_functions.py
# Author: Lukasz Tracewski
def ee_asset_exists(path):
return True if ee.data.getInfo(path) else False
# Adapted from: https://github.com/tracek/gee_asset_manager/blob/master/geebam/helper_functions.py
# Author: Lukasz Tracewski
def create_image_collection(full_path_to_collection):
if ee_asset_exists(full_path_to_collection):
print("Collection " + full_path_to_collection + " already exists")
else:
try:
ee.data.createAsset(
{"type": ee.data.ASSET_TYPE_IMAGE_COLL}, full_path_to_collection
)
print("New collection " + full_path_to_collection + " created")
except Exception as E:
print("Could not create: ", full_path_to_collection)
print(E)
# More general function to create asset collecctions or folders
# asset_type can be one of ee.data.ASSET_TYPE_FOLDER or ee.data.ASSET_TYPE_IMAGE_COLL
# If nested folders that do not already exist are provided, they will be created unless recursive = False
def create_asset(asset_path, asset_type=ee.data.ASSET_TYPE_FOLDER, recursive=True):
# Find the root and all nested folders
project_root = f'{asset_path.split("assets")[0]}assets'
sub_directories = f'{asset_path.split("assets/")[1]}'.split("/")
# If there is more than 1 level of folder, try to make them
if len(sub_directories) > 1 and recursive:
print("Found the following sub directories: ", sub_directories)
print("Will attempt to create them if they do not exist")
path_temp = project_root
for sub_directory in sub_directories[:-1]:
path_temp = f"{path_temp}/{sub_directory}"
create_asset(
path_temp, asset_type=ee.data.ASSET_TYPE_FOLDER, recursive=False
)
if ee_asset_exists(asset_path):
print("Asset " + asset_path + " already exists")
else:
try:
print(asset_path)
ee.data.createAsset({"type": asset_type}, asset_path)
print("New asset " + asset_path + " created")
except Exception as E:
print("Could not create: ", asset_path)
print(E)
def verify_path(path):
response = ee.data.getInfo(path)
if not response:
logging.error(
"%s is not a valid destination. Make sure full path is provided e.g. users/user/nameofcollection "
"or projects/myproject/myfolder/newcollection and that you have write access there.",
path,
)
sys.exit(1)
##############################################################################################
# Find whether image is a leap year
def is_leap_year(year):
year = int(year)
if year % 4 == 0:
if year % 100 == 0 and year % 400 != 0:
return False
else:
return True
else:
return False
##############################################################################################
# Function to find current readable date/time
def now(Format="%b %d %Y %H:%M:%S %a"):
## import datetime
today = datetime.datetime.today()
s = today.strftime(Format)
d = datetime.datetime.strptime(s, Format)
return d.strftime(Format)
##############################################################################################
# Convert julian to calendar
def julian_to_calendar(julian_date, year):
julian_date, year = int(julian_date), int(year)
is_leap = is_leap_year(year)
if is_leap:
leap, length = True, [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
leap, length = False, [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
ranges = []
start = 1
for month in length:
stop = start + month
ranges.append(range(start, stop))
start = start + month
month_no = 1
for Range in ranges:
if julian_date in Range:
mn = month_no
day_no = 1
for day in Range:
if day == julian_date:
dn = day_no
day_no += 1
month_no += 1
if len(str(mn)) == 1:
lmn = "0" + str(mn)
else:
lmn = str(mn)
if len(str(dn)) == 1:
ldn = "0" + str(dn)
else:
ldn = str(dn)
return [year, mn, dn]
##############################################################################################
# Functions to set date
def getDate(year, month, day):
return datetime.datetime(year, month, day).isoformat() + "Z"
def setDate(assetPath, year, month, day):
ee.data.updateAsset(
assetPath, {"start_time": getDate(year, month, day)}, ["start_time"]
)
#########################################################################
# Function to ingest tif from Google Cloud Storage as an asset
# Follows guidance from: https://developers.google.com/earth-engine/guides/image_manifest
# Must have bandNames specified for pyramiding policy and no data values to be set
# Band names must be one name for each band in images that will be uploaded
# pyramidingPolicy and noDataValues can be one for each band name or only one (that is then repeated for each band)
# The system:time_start and system:time_end properties are handled automatically assuming the proper format or ee Date object is provided
def ingestImageFromGCS(
gcsURIs,
assetPath,
overwrite=False,
bandNames=None,
properties=None,
pyramidingPolicy=None,
noDataValues=None,
):
if overwrite or not ee_asset_exists(assetPath):
taskID = ee.data.newTaskId(1)[0]
# Make sure collection or folder exists
create_image_collection(os.path.dirname(assetPath))
# Handle if single image path is provided - changes to a list
if str(type(gcsURIs)).find("'str'") > -1:
gcsURIs = [gcsURIs]
# Repeat the pyramiding policy and no data value if only one is provided
if (
bandNames != None
and pyramidingPolicy != None
and str(type(pyramidingPolicy)).find("'str'") > -1
):
pyramidingPolicy = [pyramidingPolicy] * len(bandNames)
if (
bandNames != None
and noDataValues != None
and str(type(noDataValues)).find("'list'") == -1
):
noDataValues = [noDataValues] * len(bandNames)
# Set up the manifest
params = {"name": assetPath, "tilesets": [{"sources": [{"uris": gcsURIs}]}]}
# Set up the band names, pyramiding policy, and no data values
if bandNames != None:
bnDict = []
for i, bn in enumerate(bandNames):
bnDictT = {"id": bn, "tileset_band_index": i}
if pyramidingPolicy != None:
bnDictT["pyramiding_policy"] = pyramidingPolicy[i]
if noDataValues != None:
bnDictT["missing_data"] = {"values": [noDataValues[i]]}
bnDict.append(bnDictT)
params["bands"] = bnDict
# Handle the date inconsistency in the GEE API
def fixDate(propIn, propOut):
if propIn in properties.keys():
d = properties[propIn]
if str(type(d)).find("ee.ee_date.Date") > -1:
d = (
d.format("YYYY-MM-dd")
.cat("T")
.cat(d.format("HH:mm:SS"))
.cat("Z")
.getInfo()
)
params[propOut] = d
properties.pop(propIn)
if properties != None:
fixDate("system:time_start", "start_time")
fixDate("system:time_end", "end_time")
params["properties"] = properties
print("Ingestion manifest:", params)
ee.data.startIngestion(taskID, params, overwrite)
print("Starting ingestion task:", assetPath)
else:
print(assetPath, "already exists")
#########################################################################
# Function to wrap the entire uploading an image to GEE image asset workflow
# First uploads to an existing GCS bucket, and then ingests to GEE
# Can handle multiple multi-band image tiles or a single image
# Must have bandNames specified for pyramiding policy and no data values to be set
# Band names must be one name for each band in images that will be uploaded
# pyramidingPolicy and noDataValues can be one for each band name or only one (that is then repeated for each band)
# The system:time_start and system:time_end properties are handled automatically assuming the proper format or ee Date object is provided
def uploadToGEEImageAsset(
localTif,
gcsBucket,
assetPath,
overwrite=False,
bandNames=None,
properties=None,
pyramidingPolicy=None,
noDataValues=None,
parallel_threshold = '150M',
gsutil_path="C:/Program Files (x86)/Google/Cloud SDK/google-cloud-sdk/bin/gsutil.cmd",
):
# List all local files with specified name or wildcard name and make GCS paths for each file
localTifs = glob.glob(localTif)
gcsURIs = [gcsBucket + "/" + os.path.basename(tif) for tif in localTifs]
# Upload files to GCS (will not overwrite)
uploadCommand = '"{}" -o "GSUtil:parallel_composite_upload_threshold="{}" " -m cp -n -r {} {}'.format(gsutil_path,parallel_threshold,localTif,gcsBucket)
call = subprocess.Popen(uploadCommand)
call.wait()
# Ingest to GEE
ingestImageFromGCS(
gcsURIs,
assetPath,
overwrite=overwrite,
bandNames=bandNames,
properties=properties,
pyramidingPolicy=pyramidingPolicy,
noDataValues=noDataValues,
)
#########################################################################