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

Add caching to arcgisimage #562

Merged
merged 2 commits into from
Oct 30, 2022
Merged
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
28 changes: 26 additions & 2 deletions packages/basemap/src/mpl_toolkits/basemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4217,7 +4217,7 @@ def warpimage(self,image="bluemarble",scale=None,**kwargs):

def arcgisimage(self,server='http://server.arcgisonline.com/ArcGIS',\
service='World_Imagery',xpixels=400,ypixels=None,\
dpi=96,verbose=False,**kwargs):
dpi=96,cachedir=None,verbose=False,**kwargs):
"""
Retrieve an image using the ArcGIS Server REST API and display it on
the map. In order to use this method, the Basemap instance must be
Expand All @@ -4243,6 +4243,7 @@ def arcgisimage(self,server='http://server.arcgisonline.com/ArcGIS',\
map projection region.
dpi The device resolution of the exported image (dots per
inch, default 96).
cachedir An optional directory to use as cache folder for the retrieved images.
verbose if True, print URL used to retrieve image (default
False).
============== ====================================================
Expand Down Expand Up @@ -4302,8 +4303,31 @@ def arcgisimage(self,server='http://server.arcgisonline.com/ArcGIS',\
(server,service,xmin,ymin,xmax,ymax,self.epsg,self.epsg,xpixels,ypixels,dpi)
# print URL?
if verbose: print(basemap_url)

if cachedir != None:
# Generate a filename for the cached file.
filename = "%s-bbox-%s-%s-%s-%s-bboxsr%s-imagesr%s-size-%s-%s-dpi%s.png" %\
(service,xmin,ymin,xmax,ymax,self.epsg,self.epsg,xpixels,ypixels,dpi)

# Check if the cache directory exists, if not create it.
if not os.path.exists(cachedir):
os.makedirs(cachedir)

# Check if the image is already in the cachedir folder.
cache_path = cachedir + filename

if os.path.isfile(cache_path):
print('Image already in cache')
img = Image.open(cache_path)
return basemap.imshow(img, ax=ax, origin='upper')
else:
# Retrieve and save image
img = Image.open(urlopen(basemap_url))
img.save(cache_path)
else:
img = Image.open(urlopen(basemap_url))

# return AxesImage instance.
img = Image.open(urlopen(basemap_url))
return self.imshow(img, ax=ax, origin='upper')

def wmsimage(self,server,\
Expand Down