Retrieve image from certain address via API

Hi all
I have this python code the retrieve an image of a specific address via API. Can someone help me to find out why i get an empty response? Thank you so much!

import requests

Set up API endpoint and parameters

endpoint = “https://graph.mapillary.com/images
params = {
“access_token”: “Client Secret”,
“query”: “{ "
’ search(n:1, query:”{ADDRESS}") { ’
" edges { "
" node { "
" id "
" thumb_url "
" } "
" } "
" } "
“}”,
}

Replace {ADDRESS} with the address you want to search for

address = “1600 Pennsylvania Ave NW, Washington, DC 20500”
params[“query”] = params[“query”].replace(“{ADDRESS}”, address)

Make a request to the Mapillary API

response = requests.get(endpoint, params=params)

Retrieve the URL of the image

if response.status_code == 200 and len(response.json()[“data”][“search”][“edges”]) > 0:
image_url = response.json()[“data”][“search”][“edges”][0][“node”][“thumb_url”]
print(image_url)
else:
print(“Unable to retrieve image”)

1 Like

also, this code didn’t work;

import requests

Set up API endpoint and parameters

endpoint = ‘https://a.mapillary.com/v3/images
params = {
‘access_token’: ‘Client secret’,
‘closeto’: ‘LATITUDE,LONGITUDE’,
‘radius’: ‘50’,
‘per_page’: ‘1’,
‘sort_by’: ‘captured_at’,
‘client_timestamp’: ‘YOUR_TIMESTAMP’
}

Convert address to latitude and longitude

You would need to use a geocoding service to do this

latitude = 37.7749
longitude = -122.4194

Update the ‘closeto’ parameter with the latitude and longitude values

params[‘closeto’] = f’{latitude},{longitude}’

Make a request to the Mapillary API

response = requests.get(endpoint, params=params)

Retrieve the URL of the image

if response.status_code == 200 and len(response.json()[‘features’]) > 0:
image_url = response.json()[‘features’][0][‘properties’][‘thumbnail’]
print(image_url)
else:
print(‘Unable to retrieve image’)

can anyone help?

It seems like there are a few issues in your code. I’ve made some corrections and improvements. Please check the modified code below:
import requests

Set up API endpoint and parameters

endpoint = “https ://graph mapillary com/images”
params = {
“access_token”: “Your_Client_Secret”,
“query”: ‘{ search(n:1, query:"’ + ‘{ADDRESS}’ + ‘") { edges { node { id thumb_url } } } }’,
}

Replace {ADDRESS} with the address you want to search for

address = “1600 Pennsylvania Ave NW, Washington, DC 20500”
params[“query”] = params[“query”].replace(“{ADDRESS}”, address)

Make a request to the Mapillary API https://tech-stack.com/blog/what-is-an-api/
response = requests.get(endpoint, params=params)

Retrieve the URL of the image

if response.status_code == 200 and len(response.json().get(“data”, {}).get(“search”, {}).get(“edges”, )) > 0:
image_url = response.json()[“data”][“search”][“edges”][0][“node”][“thumb_url”]
print(image_url)
else:
print(“Error:”, response.text)

Here are the changes I made:

Corrected the endpoint by removing the extra space and the ‘5’ at the end.
Adjusted the query string by properly formatting it and using single quotes for the query parameter.
Used the .get() method to safely access nested dictionary keys to avoid potential KeyError.
Added a print statement to display the error response in case the request is not successful.
Make sure to replace “Your_Client_Secret” with your actual Mapillary API client secret. Additionally, check the error message printed if the response is not successful to get more information about the issue.

1 Like