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

Cartopy crashes on Google Colab notebook: URLError: <urlopen error [Errno -3] Temporary failure in name resolution> #1869

Closed
HaynesStephens opened this issue Sep 15, 2021 · 13 comments

Comments

@HaynesStephens
Copy link

Description

Trying to run Cartopy on this Colab notebook: https://colab.research.google.com/drive/12AbSjq5gJd5mhcZDplH-WFl9vtUFc-wi#scrollTo=gsPHPY_xxmeL

However, any attempt to download coastlines or features leads to a URLError. Is this simply because the website, https://naciscdn.org/, is down? Or perhaps there's some discrepancy between Python versions? I'm not sure what the cause is here, but is there any effort to fix this issue?

Code to reproduce

!apt-get install -qq libgdal-dev libproj-dev
#!pip install --no-binary shapely shapely
!pip install --no-binary shapely shapely --force
!pip install cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

%matplotlib inline
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plt.show()

Traceback

/usr/local/lib/python3.7/dist-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naciscdn.org/naturalearth/110m/physical/ne_110m_coastline.zip
  warnings.warn('Downloading: {}'.format(url), DownloadWarning)
---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
/usr/lib/python3.7/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1349                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1350                           encode_chunked=req.has_header('Transfer-encoding'))
   1351             except OSError as err: # timeout error

35 frames
/usr/lib/python3.7/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1280         """Send a complete request to the server."""
-> 1281         self._send_request(method, url, body, headers, encode_chunked)
   1282 

/usr/lib/python3.7/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1326             body = _encode(body, 'body')
-> 1327         self.endheaders(body, encode_chunked=encode_chunked)
   1328 

/usr/lib/python3.7/http/client.py in endheaders(self, message_body, encode_chunked)
   1275             raise CannotSendHeader()
-> 1276         self._send_output(message_body, encode_chunked=encode_chunked)
   1277 

/usr/lib/python3.7/http/client.py in _send_output(self, message_body, encode_chunked)
   1035         del self._buffer[:]
-> 1036         self.send(msg)
   1037 

/usr/lib/python3.7/http/client.py in send(self, data)
    975             if self.auto_open:
--> 976                 self.connect()
    977             else:

/usr/lib/python3.7/http/client.py in connect(self)
   1442 
-> 1443             super().connect()
   1444 

/usr/lib/python3.7/http/client.py in connect(self)
    947         self.sock = self._create_connection(
--> 948             (self.host,self.port), self.timeout, self.source_address)
    949         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

/usr/lib/python3.7/socket.py in create_connection(address, timeout, source_address)
    706     err = None
--> 707     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    708         af, socktype, proto, canonname, sa = res

/usr/lib/python3.7/socket.py in getaddrinfo(host, port, family, type, proto, flags)
    751     addrlist = []
--> 752     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    753         af, socktype, proto, canonname, sa = res

gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    332                 pass
    333             else:
--> 334                 return printer(obj)
    335             # Finally look for special method names
    336             method = get_real_method(obj, self.print_method)

/usr/local/lib/python3.7/dist-packages/IPython/core/pylabtools.py in <lambda>(fig)
    239 
    240     if 'png' in formats:
--> 241         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    242     if 'retina' in formats or 'png2x' in formats:
    243         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/usr/local/lib/python3.7/dist-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    123 
    124     bytes_io = BytesIO()
--> 125     fig.canvas.print_figure(bytes_io, **kw)
    126     data = bytes_io.getvalue()
    127     if fmt == 'svg':

/usr/local/lib/python3.7/dist-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
   2098                            else suppress())
   2099                     with ctx:
-> 2100                         self.figure.draw(renderer)
   2101                     bbox_artists = kwargs.pop("bbox_extra_artists", None)
   2102                     bbox_inches = self.figure.get_tightbbox(renderer,

/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     36                 renderer.start_filter()
     37 
---> 38             return draw(artist, renderer, *args, **kwargs)
     39         finally:
     40             if artist.get_agg_filter() is not None:

/usr/local/lib/python3.7/dist-packages/matplotlib/figure.py in draw(self, renderer)
   1734             self.patch.draw(renderer)
   1735             mimage._draw_list_compositing_images(
-> 1736                 renderer, self, artists, self.suppressComposite)
   1737 
   1738             renderer.close_group('figure')

/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    135     if not_composite or not has_images:
    136         for a in artists:
--> 137             a.draw(renderer)
    138     else:
    139         # Composite any adjacent images together

/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     36                 renderer.start_filter()
     37 
---> 38             return draw(artist, renderer, *args, **kwargs)
     39         finally:
     40             if artist.get_agg_filter() is not None:

/usr/local/lib/python3.7/dist-packages/cartopy/mpl/geoaxes.py in draw(self, renderer, **kwargs)
    515         self._done_img_factory = True
    516 
--> 517         return matplotlib.axes.Axes.draw(self, renderer=renderer, **kwargs)
    518 
    519     def _update_title_position(self, renderer):

/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     36                 renderer.start_filter()
     37 
---> 38             return draw(artist, renderer, *args, **kwargs)
     39         finally:
     40             if artist.get_agg_filter() is not None:

/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
   2628             renderer.stop_rasterizing()
   2629 
-> 2630         mimage._draw_list_compositing_images(renderer, self, artists)
   2631 
   2632         renderer.close_group('axes')

/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    135     if not_composite or not has_images:
    136         for a in artists:
--> 137             a.draw(renderer)
    138     else:
    139         # Composite any adjacent images together

/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     36                 renderer.start_filter()
     37 
---> 38             return draw(artist, renderer, *args, **kwargs)
     39         finally:
     40             if artist.get_agg_filter() is not None:

/usr/local/lib/python3.7/dist-packages/cartopy/mpl/feature_artist.py in draw(self, renderer, *args, **kwargs)
    151         except ValueError:
    152             warnings.warn('Unable to determine extent. Defaulting to global.')
--> 153         geoms = self._feature.intersecting_geometries(extent)
    154 
    155         # Combine all the keyword args in priority order.

/usr/local/lib/python3.7/dist-packages/cartopy/feature/__init__.py in intersecting_geometries(self, extent)
    295         """
    296         self.scaler.scale_from_extent(extent)
--> 297         return super().intersecting_geometries(extent)
    298 
    299     def with_scale(self, new_scale):

/usr/local/lib/python3.7/dist-packages/cartopy/feature/__init__.py in intersecting_geometries(self, extent)
    104             extent_geom = sgeom.box(extent[0], extent[2],
    105                                     extent[1], extent[3])
--> 106             return (geom for geom in self.geometries() if
    107                     geom is not None and extent_geom.intersects(geom))
    108         else:

/usr/local/lib/python3.7/dist-packages/cartopy/feature/__init__.py in geometries(self)
    279             path = shapereader.natural_earth(resolution=self.scale,
    280                                              category=self.category,
--> 281                                              name=self.name)
    282             geometries = tuple(shapereader.Reader(path).geometries())
    283             _NATURAL_EARTH_GEOM_CACHE[key] = geometries

/usr/local/lib/python3.7/dist-packages/cartopy/io/shapereader.py in natural_earth(resolution, category, name)
    280     format_dict = {'config': config, 'category': category,
    281                    'name': name, 'resolution': resolution}
--> 282     return ne_downloader.path(format_dict)
    283 
    284 

/usr/local/lib/python3.7/dist-packages/cartopy/io/__init__.py in path(self, format_dict)
    201         else:
    202             # we need to download the file
--> 203             result_path = self.acquire_resource(target_path, format_dict)
    204 
    205         return result_path

/usr/local/lib/python3.7/dist-packages/cartopy/io/shapereader.py in acquire_resource(self, target_path, format_dict)
    335         url = self.url(format_dict)
    336 
--> 337         shapefile_online = self._urlopen(url)
    338 
    339         zfh = ZipFile(io.BytesIO(shapefile_online.read()), 'r')

/usr/local/lib/python3.7/dist-packages/cartopy/io/__init__.py in _urlopen(self, url)
    240         """
    241         warnings.warn('Downloading: {}'.format(url), DownloadWarning)
--> 242         return urlopen(url)
    243 
    244     @staticmethod

/usr/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

/usr/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    523             req = meth(req)
    524 
--> 525         response = self._open(req, data)
    526 
    527         # post-process response

/usr/lib/python3.7/urllib/request.py in _open(self, req, data)
    541         protocol = req.type
    542         result = self._call_chain(self.handle_open, protocol, protocol +
--> 543                                   '_open', req)
    544         if result:
    545             return result

/usr/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

/usr/lib/python3.7/urllib/request.py in https_open(self, req)
   1391         def https_open(self, req):
   1392             return self.do_open(http.client.HTTPSConnection, req,
-> 1393                 context=self._context, check_hostname=self._check_hostname)
   1394 
   1395         https_request = AbstractHTTPHandler.do_request_

/usr/lib/python3.7/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1350                           encode_chunked=req.has_header('Transfer-encoding'))
   1351             except OSError as err: # timeout error
-> 1352                 raise URLError(err)
   1353             r = h.getresponse()
   1354         except:

URLError: <urlopen error [Errno -3] Temporary failure in name resolution>
Full environment definition

Operating system

Google Colab on Google Chrome Browser

Cartopy version

Not sure. 0.19.0?

conda list

pip list

@kiefersmith
Copy link

kiefersmith commented Sep 16, 2021

Hi, I'm also facing this issue. My code is much the same as the above. Seems like https://naciscdn.org/naturalearth/110m/physical/ne_110m_coastline.zip can't be resolved at the present.

EDIT: On Ubuntu 20.04

EDIT2: @HaynesStephens nvkelso/natural-earth-vector#581
There is an outage for the content server.

@akrherz
Copy link
Contributor

akrherz commented Sep 16, 2021

see #1833, #1849.

@albavilanova
Copy link

Hey, I am having the same poblem. It seems like they have been redirecting the pages...

@greglucas
Copy link
Contributor

Closing as a duplicate. There are multiple solutions given in the linked issues.

We are planning a release soon that will fix this as well.

@HaynesStephens
Copy link
Author

see #1833, #1849.

Hi, I tried the !conda install -c conda-forge -y cartopy=0.19.0 from #1833.
I had to install Miniconda onto the Colab notebook and then run the !conda install command.

Now I have a new error as I am trying to import cartopy:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-18-a4ba129e3112> in <module>()
----> 1 import cartopy

1 frames
/usr/local/lib/python3.6/site-packages/cartopy/__init__.py in <module>()
    102 # Commonly used sub-modules. Imported here to provide end-user
    103 # convenience.
--> 104 import cartopy.crs
    105 import cartopy.feature  # noqa: F401  (flake8 = unused import)

/usr/local/lib/python3.6/site-packages/cartopy/crs.py in <module>()
     20 from shapely.prepared import prep
     21 
---> 22 from cartopy._crs import (CRS, Geodetic, Globe, PROJ4_VERSION,
     23                           WGS84_SEMIMAJOR_AXIS, WGS84_SEMIMINOR_AXIS)
     24 from cartopy._crs import Geocentric  # noqa: F401 (flake8 = unused import)

ModuleNotFoundError: No module named 'cartopy._crs'

@dopplershift
Copy link
Contributor

@HaynesStephens Try restarting the notebook, assuming the conda install worked without error?

@rcomer
Copy link
Member

rcomer commented Sep 16, 2021

You can avoid conda if you download the shapefiles with the script. This should work with any Cartopy version.

!wget https://raw.githubusercontent.com/SciTools/cartopy/master/tools/cartopy_feature_download.py
!python cartopy_feature_download.py physical

@HaynesStephens
Copy link
Author

HaynesStephens commented Sep 16, 2021

@dopplershift I tried restarting the notebook, conda install worked as executed (in the linked tutorial above), but the same ModuleNotFoundError persists.

@rcomer I gave that a try as well, installing cartopy using the following code from a tutorial

!pip install cartopy
!pip uninstall -y shapely    # cartopy and shapely aren't friends (early 2020)
!pip install shapely --no-binary shapely

import cartopy

!wget https://raw.githubusercontent.com/SciTools/cartopy/master/tools/cartopy_feature_download.py
!python cartopy_feature_download.py physical

The last line of code seems to run forever. I've let it go on for several minutes now and it still hasn't completed execution. Has this ever been an issue on your end in Colab Notebooks before?

@HaynesStephens
Copy link
Author

Update @rcomer : the !wget and !python commands completed after about 9 minutes. However, now I'm not sure how to grab those features that were downloaded and use them in plotting functions.

i.e. replicating this code using the downloaded features

import cartopy
import cartopy.crs as ccrs                   # for projections
import cartopy.feature as cfeature           # for features

from matplotlib import pyplot as plt
fig2 = plt.figure(constrained_layout=False, figsize=(0.9, 0.58))
spec = fig2.add_gridspec(ncols=1, nrows=1, wspace=0.0, hspace=0.0)

ax = fig2.add_subplot(spec[0, 0], projection=ccrs.PlateCarree())
ax.add_feature(cfeature.NaturalEarthFeature(
    'cultural', 'admin_1_states_provinces_lines', '110m',
    edgecolor='#414141', linewidth=0.5, facecolor='none'))
ax.add_feature(cfeature.NaturalEarthFeature(
    'physical', 'lakes', '110m',
    edgecolor='#414141', linewidth=0.5, facecolor='none'))
ax.add_feature(cfeature.BORDERS, edgecolor='#414141', linewidth=0.5)
ax.add_feature(cfeature.COASTLINE, edgecolor='#414141', linewidth=0.5)
ax.coastlines()

@rcomer
Copy link
Member

rcomer commented Sep 16, 2021

@HaynesStephens glad the download made it in the end! The plotting should just work now, as the download script places the files where Cartopy looks by default. Normally Cartopy would only download the files the first time you use them, and save them in the same place.

@rcomer
Copy link
Member

rcomer commented Sep 16, 2021

Although it looks like you will also need to do

!python cartopy_feature_download.py cultural

for your example.

@HaynesStephens
Copy link
Author

Woah all works now, thank you!

@albavilanova
Copy link

Thanks all, it works for me too :)

smithara added a commit to Swarm-DISC/Swarm_quicklooks that referenced this issue Sep 20, 2021
Required to fix old download URLs (SciTools/cartopy#1869)
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

7 participants