@@ -4516,6 +4516,7 @@ def download_files(
4516
4516
unzip = True ,
4517
4517
overwrite = False ,
4518
4518
subfolder = False ,
4519
+ multi_part = False ,
4519
4520
):
4520
4521
"""Download files from URLs, including Google Drive shared URL.
4521
4522
@@ -4534,6 +4535,14 @@ def download_files(
4534
4535
unzip (bool, optional): Unzip the file. Defaults to True.
4535
4536
overwrite (bool, optional): Overwrite the file if it already exists. Defaults to False.
4536
4537
subfolder (bool, optional): Create a subfolder with the same name as the file. Defaults to False.
4538
+ multi_part (bool, optional): If the file is a multi-part file. Defaults to False.
4539
+
4540
+ Examples:
4541
+
4542
+ files = ["sam_hq_vit_tiny.zip", "sam_hq_vit_tiny.z01", "sam_hq_vit_tiny.z02", "sam_hq_vit_tiny.z03"]
4543
+ base_url = "https://github.com/opengeos/datasets/releases/download/models/"
4544
+ urls = [base_url + f for f in files]
4545
+ leafmap.download_files(urls, out_dir="models", multi_part=True)
4537
4546
"""
4538
4547
4539
4548
if out_dir is None :
@@ -4542,12 +4551,17 @@ def download_files(
4542
4551
if filenames is None :
4543
4552
filenames = [None ] * len (urls )
4544
4553
4554
+ filepaths = []
4545
4555
for url , output in zip (urls , filenames ):
4546
4556
if output is None :
4547
4557
filename = os .path .join (out_dir , os .path .basename (url ))
4548
4558
else :
4549
4559
filename = os .path .join (out_dir , output )
4550
4560
4561
+ filepaths .append (filename )
4562
+ if multi_part :
4563
+ unzip = False
4564
+
4551
4565
download_file (
4552
4566
url ,
4553
4567
filename ,
@@ -4564,6 +4578,14 @@ def download_files(
4564
4578
subfolder ,
4565
4579
)
4566
4580
4581
+ if multi_part :
4582
+ archive = os .path .splitext (filename )[0 ] + ".zip"
4583
+ out_dir = os .path .dirname (filename )
4584
+ extract_archive (archive , out_dir )
4585
+
4586
+ for file in filepaths :
4587
+ os .remove (file )
4588
+
4567
4589
4568
4590
def download_folder (
4569
4591
url = None ,
@@ -13251,3 +13273,50 @@ def convert_coordinates(x, y, source_crs, target_crs="epsg:4326"):
13251
13273
13252
13274
# Return the converted coordinates
13253
13275
return lon , lat
13276
+
13277
+
13278
+ def extract_archive (archive , outdir = None , ** kwargs ):
13279
+ """
13280
+ Extracts a multipart archive.
13281
+
13282
+ This function uses the patoolib library to extract a multipart archive.
13283
+ If the patoolib library is not installed, it attempts to install it.
13284
+ If the archive does not end with ".zip", it appends ".zip" to the archive name.
13285
+ If the extraction fails (for example, if the files already exist), it skips the extraction.
13286
+
13287
+ Args:
13288
+ archive (str): The path to the archive file.
13289
+ outdir (str): The directory where the archive should be extracted.
13290
+ **kwargs: Arbitrary keyword arguments for the patoolib.extract_archive function.
13291
+
13292
+ Returns:
13293
+ None
13294
+
13295
+ Raises:
13296
+ Exception: An exception is raised if the extraction fails for reasons other than the files already existing.
13297
+
13298
+ Example:
13299
+
13300
+ files = ["sam_hq_vit_tiny.zip", "sam_hq_vit_tiny.z01", "sam_hq_vit_tiny.z02", "sam_hq_vit_tiny.z03"]
13301
+ base_url = "https://github.com/opengeos/datasets/releases/download/models/"
13302
+ urls = [base_url + f for f in files]
13303
+ leafmap.download_files(urls, out_dir="models", multi_part=True)
13304
+
13305
+ """
13306
+ try :
13307
+ import patoolib
13308
+ except ImportError :
13309
+ install_package ("patool" )
13310
+ import patoolib
13311
+
13312
+ if not archive .endswith (".zip" ):
13313
+ archive = archive + ".zip"
13314
+
13315
+ if outdir is None :
13316
+ outdir = os .path .dirname (archive )
13317
+
13318
+ try :
13319
+ patoolib .extract_archive (archive , outdir = outdir , ** kwargs )
13320
+ except Exception as e :
13321
+ print ("The unzipped files might already exist. Skipping extraction." )
13322
+ return
0 commit comments