urllib.error.HTTPError: HTTP Error 429: Too Many R

2019-07-30 13:23发布

Until last week, I have been able to use the python smopy package https://pypi.org/project/smopy/ to display map tiles obtained from openstreetmap https://www.openstreetmap.org/#map=4/-28.15/133.29 in a python program. But since last week, for some strange reason, smopy has been failing, giving the following error:

Traceback (most recent call last):
  File "/home/pi/Python Learning/smopyTest.py", line 7, in <module>
    m = smopy.Map((-36.5,-150.0), z=0)
  File "/home/pi/Python Learning/smopy.py", line 291, in __init__
    self.fetch()
  File "/home/pi/Python Learning/smopy.py", line 323, in fetch
    self.img = fetch_map(self.box_tile, self.z)
  File "/home/pi/Python Learning/smopy.py", line 64, in fetch_map
    img.paste(fetch_tile(x, y, z), (px, py))
  File "/home/pi/Python Learning/smopy.py", line 44, in fetch_tile
    png = BytesIO(urlopen(url).read())
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 429: Too Many Requests

I notice on stack Overflow that many people have got the same HTTP error when trying to access other websites. The answers suggest to wait for some time before making multiple requests, etc. When I checked the code of smopy.py, it already has a protection to limit the number of tiles to 16 to ensure tile access policy is not violated.

Even when I changed the zoom level of the map requested to z=0, which should simply download only one map tile, I get the same error. So, it puzzles me why I get the too many requests error.

The python application I am trying to develop will display a number of fixed and mobile sensors on the map. So, once the map loads up, it remains mostly unchanged. Only occasionally, if one or more sensors move out of the map area, the application will have to reload the map to try to keep all sensors in view.

I found that the following website shows some alternative tile servers: https://wiki.openstreetmap.org/wiki/Tile_servers.

I experimented by modifying the smopy.py module by replacing the line

TILE_SERVER = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"

by either

TILE_SERVER = "http://c.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg"

or

TILE_SERVER = "https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png."

In both these cases, the map displayed as expected.

So, this suggests to me that it is a restriction specifically related to accessing the tiles from openstreetmap.org rather than something particularly wrong with smopy.

For my application, the watercolor maps of stamen are not suitable and the wikimedia server says that it is an experimental one; so I am not sure whether I can rely on that.

Therefore, can someone please show me what the correct way to access openstreetmap to display a map in a python program with smopy or another similar package?

I show below the minimal code I use to display a map.I am running this code on Ubuntu 18.04. I get identical error whether I run it in IDLE or Thonny.

import matplotlib.pyplot as plt
import smopy

plt.ion()
fig, ax = plt.subplots(figsize=(8,8))
m = smopy.Map((-36.5,-150.0), z=0)
m.show_mpl(ax = ax)

plt.draw()

0条回答
登录 后发表回答