Skip to content

Commit

Permalink
Improved download function for tar file unzip (opengeos#505)
Browse files Browse the repository at this point in the history
* Improve download function for tar file unzip

* Update notebook
  • Loading branch information
giswqs authored Aug 9, 2023
1 parent ee97108 commit 9569c45
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/notebooks/32_local_tile.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"metadata": {},
"outputs": [],
"source": [
"m.add_raster(dem, palette='viridis', layer_name=\"DEM\")"
"m.add_raster(dem, cmap='viridis', layer_name=\"DEM\")"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/32_local_tile.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"metadata": {},
"outputs": [],
"source": [
"m.add_raster(dem, palette='viridis', layer_name=\"DEM\")"
"m.add_raster(dem, cmap='viridis', layer_name=\"DEM\")"
]
},
{
Expand Down
42 changes: 30 additions & 12 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4386,19 +4386,37 @@ def download_file(
url, output, quiet, proxy, speed, use_cookies, verify, id, fuzzy, resume
)

if unzip and output.endswith(".zip"):
with zipfile.ZipFile(output, "r") as zip_ref:
if not quiet:
print("Extracting files...")
if subfolder:
basename = os.path.splitext(os.path.basename(output))[0]

output = os.path.join(out_dir, basename)
if not os.path.exists(output):
os.makedirs(output)
zip_ref.extractall(output)
if unzip:
if output.endswith(".zip"):
with zipfile.ZipFile(output, "r") as zip_ref:
if not quiet:
print("Extracting files...")
if subfolder:
basename = os.path.splitext(os.path.basename(output))[0]

output = os.path.join(out_dir, basename)
if not os.path.exists(output):
os.makedirs(output)
zip_ref.extractall(output)
else:
zip_ref.extractall(os.path.dirname(output))
elif output.endswith(".tar.gz") or output.endswith(".tar"):
if output.endswith(".tar.gz"):
mode = "r:gz"
else:
zip_ref.extractall(os.path.dirname(output))
mode = "r"

with tarfile.open(output, mode) as tar_ref:
if not quiet:
print("Extracting files...")
if subfolder:
basename = os.path.splitext(os.path.basename(output))[0]
output = os.path.join(out_dir, basename)
if not os.path.exists(output):
os.makedirs(output)
tar_ref.extractall(output)
else:
tar_ref.extractall(os.path.dirname(output))

return os.path.abspath(output)

Expand Down

0 comments on commit 9569c45

Please sign in to comment.