Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BadZipFile: File is not a zip file #853

Open
GKG1312 opened this issue Mar 16, 2023 · 1 comment
Open

BadZipFile: File is not a zip file #853

GKG1312 opened this issue Mar 16, 2023 · 1 comment

Comments

@GKG1312
Copy link

GKG1312 commented Mar 16, 2023

Hi,
I was following Earth lab's Intro to earth data science textbook and in Section 2, chapter-5 while running the codes for vector files, I am getting BadZipFile errors. This problem seems to be with all files from the Natural Earth webpage. I am able to download the zip file separately but using get_data function is giving an error.
Here is the code I am running:

# Import packages
import os
os.environ['USE_PYGEOS'] = '0'
import matplotlib.pyplot as plt
import geopandas as gpd
import earthpy as et

link = 'https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_boundary_lines_land.zip'
# Download the data and set working directory
et.data.get_data(url=link)

# Set working directory - earthpy creates earth-analytics for you in your home dir
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

I am getting following error:

Downloading from https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_boundary_lines_land.zip

BadZipFile Traceback (most recent call last)
Cell In[17], line 3
1 link = 'https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_boundary_lines_land.zip'
2 # Download the data and set working directory
----> 3 et.data.get_data(url=link)
5 # Set working directory - earthpy creates earth-analytics for you in your home dir
6 os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

File ~/miniconda3/envs/earth-analytics-python/lib/python3.8/site-packages/earthpy/io.py:247, in Data.get_data(self, key, url, replace, verbose)
240 if kind not in ALLOWED_FILE_TYPES:
241 raise ValueError(
242 "kind must be one of {}, got {}".format(
243 ALLOWED_FILE_TYPES, kind
244 )
245 )
--> 247 this_path = self._download(
248 url=url,
249 path=os.path.join(this_root, name),
250 kind=kind,
251 replace=replace,
252 verbose=verbose,
253 )
254 data_paths.append(this_path)
255 if len(data_paths) == 1:

File ~/miniconda3/envs/earth-analytics-python/lib/python3.8/site-packages/earthpy/io.py:300, in Data._download(self, url, path, kind, replace, verbose)
298 f.write(r.content)
299 else:
--> 300 self._download_and_extract(path, r, kind, verbose)
301 return path

File ~/miniconda3/envs/earth-analytics-python/lib/python3.8/site-packages/earthpy/io.py:328, in Data._download_and_extract(self, path, r, kind, verbose)
326 file_like_object = io.BytesIO(r.content)
327 if kind == "zip":
--> 328 archive = zipfile.ZipFile(file_like_object)
329 if kind == "tar":
330 archive = tarfile.open(fileobj=file_like_object)

File ~/miniconda3/envs/earth-analytics-python/lib/python3.8/zipfile.py:1269, in ZipFile.init(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps)
1267 try:
1268 if mode == 'r':
-> 1269 self._RealGetContents()
1270 elif mode in ('w', 'x'):
1271 # set the modified flag so central directory gets written
1272 # even if no files are added to the archive
1273 self._didModify = True

File ~/miniconda3/envs/earth-analytics-python/lib/python3.8/zipfile.py:1336, in ZipFile._RealGetContents(self)
1334 raise BadZipFile("File is not a zip file")
1335 if not endrec:
-> 1336 raise BadZipFile("File is not a zip file")
1337 if self.debug > 1:
1338 print(endrec)

BadZipFile: File is not a zip file

Hope this will be helpful for you in solving the issue.
Thank you
Girish

@eculler
Copy link
Contributor

eculler commented Jun 9, 2023

Hi Girish - thanks for the message.

It looks like naturalearthdata changed their download so that we can't use the same url anymore when downloading in Python (as opposed to the browser which still works fine). We're working on getting in touch with them so that we can get direct download lines. In the meantime, you can download from naturalearthdata in the browser and move the data into your earthpy-downloads folder under the path the code is looking for.

We'll update here when we have a permanent solution!

-Elsa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants