Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- pyshp>=2.3
- pyproj>=3.3.1
- packaging>=21
- urllib3>=2.3
# The testing label has the proper version of freetype included
- conda-forge/label/testing::matplotlib-base>=3.6

Expand Down
6 changes: 4 additions & 2 deletions lib/cartopy/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import collections
from pathlib import Path
import string
from urllib.request import urlopen
import warnings

from urllib3 import request

from cartopy import config


Expand Down Expand Up @@ -227,6 +228,7 @@ def acquire_resource(self, target_path, format_dict):
response = self._urlopen(url)

target_path.write_bytes(response.read())
response.release_conn()

return target_path

Expand All @@ -238,7 +240,7 @@ def _urlopen(self, url):

"""
warnings.warn(f'Downloading: {url}', DownloadWarning)
return urlopen(url)
return request('GET', url, preload_content=False)

@staticmethod
def from_config(specification, config_dict=None):
Expand Down
13 changes: 7 additions & 6 deletions lib/cartopy/io/img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ def _image_url(self, tile):
pass

def get_image(self, tile):
from urllib.request import HTTPError, Request, URLError, urlopen
from urllib3 import request
from urllib3.exceptions import HTTPError

if self.cache_path is not None:
filename = "_".join([str(i) for i in tile]) + ".npy"
Expand All @@ -215,13 +216,13 @@ def get_image(self, tile):
else:
url = self._image_url(tile)
try:
request = Request(url, headers={"User-Agent": self.user_agent})
fh = urlopen(request)
im_data = io.BytesIO(fh.read())
fh.close()
r = request('GET', url, headers={"User-Agent": self.user_agent},
preload_content=False)
im_data = io.BytesIO(r.read())
r.release_conn()
img = Image.open(im_data)

except (HTTPError, URLError) as err:
except HTTPError as err:
print(err)
img = Image.fromarray(np.full((256, 256, 3), (250, 250, 250),
dtype=np.uint8))
Expand Down
4 changes: 2 additions & 2 deletions lib/cartopy/io/ogc_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import io
import math
from pathlib import Path
from urllib.parse import urlparse
import warnings
import weakref
from xml.etree import ElementTree

import numpy as np
from PIL import Image
import shapely.geometry as sgeom
from urllib3.util import parse_url

import cartopy

Expand Down Expand Up @@ -753,7 +753,7 @@ def __init__(self, service, features, getfeature_extra_kwargs=None):
# agroenvgeo.data.inra.fr from full address
# http://mapserver.gis.umn.edu/mapserver
# or https://agroenvgeo.data.inra.fr:443/geoserver/wfs
self.url = urlparse(service).hostname
self.url = parse_url(service).hostname
# WebFeatureService of owslib
service = WebFeatureService(service)
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/cartopy/io/shapereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import io
import itertools
from pathlib import Path
from urllib.error import HTTPError

import shapefile
import shapely.geometry as sgeom
from urllib3.exceptions import HTTPError

from cartopy import config
from cartopy.io import Downloader
Expand Down
7 changes: 4 additions & 3 deletions lib/cartopy/io/srtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,11 @@ def _create_srtm_mask(resolution, filename=None):
# dependencies of cartopy.
from bs4 import BeautifulSoup
if filename is None:
from urllib.request import urlopen
from urllib3 import request
url = SRTMDownloader._SRTM_BASE_URL.format(resolution=resolution)
with urlopen(url) as f:
html = f.read()
with request('GET', url, preload_content=False) as r:
html = r.read()
r.release_conn()
else:
html = Path(filename).read_text()

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies = [
"packaging>=21",
"pyshp>=2.3",
"pyproj>=3.3.1",
"urllib3>=2.3"
]
dynamic = ["version"]

Expand Down