Mostafa Didar
Mostafa Didar

Reputation: 3

How to replace a Google Maps Static API URL with a Mapbox static Images API URL?

I was looking through a piece of code that used Google Maps static API request to download day-time imagery. The problem is I do not have a billing account to use the Google Maps static API for free (since I do not have access to a credit card at the moment).

I was thinking of using MapBox static Images API which lets users make the initial API calls for free. I am not being able to convert the Google maps API URL code to the MapBox API URL code. Any sort of help or guidance could be very useful.

Here is the piece of code for making calls using the Google Maps Static API:

class ImageryDownloader:
def __init__(self, access_token):
    self.access_token = access_token
    self.url = 'https://maps.googleapis.com/maps/api/staticmap?center={},{}&zoom={}&size=400x400&maptype=satellite&key={}'

def download(self, lat, long, zoom):
    res = requests.get(self.url.format(lat, long, zoom, self.access_token))
    # server needs to make image available, takes a few seconds
    if res.status_code == 403:
        return 'RETRY'
    assert res.status_code < 400, print(f'Error - failed to download {lat}, {long}, {zoom}')
    image = plt.imread(BytesIO(res.content))
    return image

And then to make the API calls

lat = 38.441332
lon = -105.234751

access = open(ACCESS_TOKEN_DIR, 'r').readlines()[0]
url = "https://maps.googleapis.com/maps/api/staticmap?center={},{}&zoom=14&size=400x400&maptype=satellite&key={}".format(lat, lon, access)
res = requests.get(url)
plt.imshow(plt.imread(BytesIO(res.content)))

Link to the MapBox API documentation: MapBox Static Images API

Upvotes: 0

Views: 1186

Answers (1)

riastrad
riastrad

Reputation: 1774

Your question is fairly broad, so I can't share a very specific answer. But it's worth noting that you can use the Static Images playground on Mapbox's documentation website: https://docs.mapbox.com/playground/static/. This tool provides a GUI for structuring Static Images API requests.


⚠️ disclaimer: I currently work for Mapbox ⚠️

Upvotes: 1

Related Questions