matplotlib / basemap

Plot on map projections (with coastlines and political boundaries) using matplotlib
MIT License
774 stars 392 forks source link

Fix arcgisimage for cylindrical coordinates, remove imread #505

Closed guziy closed 3 years ago

guziy commented 3 years ago

Change summary

Objective

This PR makes the following code not fail and show a map:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

m = Basemap(projection='cyl',llcrnrlon=-90,llcrnrlat=30,urcrnrlon=-60,urcrnrlat=60, epsg="4326")
m.arcgisimage(verbose=True)
plt.show()

Current behavior

The code above produces the exception below with current basemap:

---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-1-18c43d1bdc96> in <module>
      8 m = Basemap(projection='cyl',llcrnrlon=-90,llcrnrlat=30,urcrnrlon=-60,urcrnrlat=60, epsg="4326")
      9 
---> 10 m.arcgisimage(verbose=True)
     11 

~/Python/anaconda3/envs/py373/lib/python3.7/site-packages/mpl_toolkits/basemap/__init__.py in arcgisimage(self, server, service, xpixels, ypixels, dpi, verbose, **kwargs)
   4271         if verbose: print(basemap_url)
   4272         # return AxesImage instance.
-> 4273         return self.imshow(imread(urlopen(basemap_url)),ax=ax,
   4274                            origin='upper')
   4275 

~/Python/anaconda3/envs/py373/lib/python3.7/site-packages/matplotlib/image.py in imread(fname, format)
   1474             with urllib.request.urlopen(fname) as response:
   1475                 return imread(response, format=ext)
-> 1476     with img_open(fname) as image:
   1477         return (_pil_png_to_float_array(image)
   1478                 if isinstance(image, PIL.PngImagePlugin.PngImageFile) else

~/Python/anaconda3/envs/py373/lib/python3.7/site-packages/PIL/ImageFile.py in __init__(self, fp, filename)
    115         try:
    116             try:
--> 117                 self._open()
    118             except (
    119                 IndexError,  # end of data

~/Python/anaconda3/envs/py373/lib/python3.7/site-packages/PIL/PngImagePlugin.py in _open(self)
    651             # get next chunk
    652 
--> 653             cid, pos, length = self.png.read()
    654 
    655             try:

~/Python/anaconda3/envs/py373/lib/python3.7/site-packages/PIL/PngImagePlugin.py in read(self)
    126             s = self.fp.read(8)
    127             cid = s[4:]
--> 128             pos = self.fp.tell()
    129             length = i32(s)
    130 

UnsupportedOperation: seek
molinav commented 3 years ago

Thanks @guziy!