Error with 'thumb_2048_url'

HI guys, I’m using python and I’m trying to, given an image id, download the corresponding photo, but I get this error:
KeyError: ‘thumb_2048_url’
{‘error’: {‘message’: ‘Tried accessing nonexisting field (thumb_2048_url) on node type (ChunkObject)’, ‘type’: ‘MLYApiException’, ‘code’: 100, ‘fbtrace_id’: ‘A2gwrQlLdZjcvK60-q86dlv’} }
as if it wasn’t possible.

this is the code to get the file geojson given the coordinates:

import requests
import codes
import math
import json
import os
from vt2geojson.tools import vt_bytes_to_geojson

def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return (xtile, ytile)

outputFolder = r"C:\example"

z = 14
ll_lat = 40.82386
ll_lon = 14.19281
ur_lat = 40.85177
ur_lon = 14.26812
llx,lly = deg2num (ll_lat, ll_lon, z)
urx,ury = deg2num (ur_lat, ur_lon, z)

type=“mly_map_feature_traffic_sign”

output = {“type”:“FeatureCollection”,“features”:}
for x in range(min(llx,urx),max(llx,urx),1):
for y in range(min(lly,ury),max(lly,ury),1):
print(type,x,y)
url = f"https://tiles.mapillary.com/maps/vtp/{type}/2/{z}/{x}/{y}?access_token={codes.API_KEY}"
r = requests.get(url)
assert r.status_code == 200, r.content
vt_content = r.content
features = vt_bytes_to_geojson(vt_content, x, y, z)
for f in features[“features”]:
output[‘features’].append(f)
with open(outputFolder + os.path.sep + type + “" + str(z) + "” + str(llx) + “" + str(urx) + "” + str(lly) + “_” + str(ury) + “.geojson”, “w”) as fx:
json.dump(output, fx)

this is the code to get the photo:

import geojson
import requests
import codes

outputFolder = r"C:\example"
id_list =
with open(r"C:\example\mly_map_feature_traffic_sign_14_8837_8841_6153_6151.geojson", “r”) as file:
temp = geojson.load(file)
for feature in temp[‘features’]:
id_list.append(feature[‘properties’][‘id’])

app_access_token = codes.API_KEY
image_id = id_list[0]
header = {‘Authorization’: ‘OAuth {}’.format(app_access_token)}
url = f"https://graph.mapillary.com/{image_id}?fields=thumb_2048_url"
response = requests.get(url, headers=header)
data = response.json()
print(data)
image_url = data[‘thumb_2048_url’]

with open(‘{}/{}.jpg’.format(outputFolder, image_id), ‘wb’) as handler:
image_data = requests.get(image_url, stream=True).content
handler.write(image_data)

@chrisbeddow for thoughts

Update:
I found out why it gives me that error, essentially the traffic signs are map features and as such they don’t have fields with the URLs of the images, so the question is only one, how can I get the images of the traffic signs via their IDs?

Update:
I managed to solve the problem, simply using the map feature id and through the detection I went back to the image id and then to the thumb field

this is the code for those who should need it

code to get the photo updated:

outputFolder = r"C:\example"
mapF_id_list =
images_id_list =
count = 0
with open(‘C:/example/mapillary-traffic-signs.json’, “r”) as file:
temp = geojson.load(file)
for feature in temp[‘features’]:
count += 1
mapF_id_list.append(feature[‘properties’][‘id’])
print(mapF_id_list)
print(count)

app_access_token = codes.API_KEY
mapFeature_id = mapF_id_list[0]
header = {‘Authorization’: ‘OAuth {}’.format(app_access_token)}
url = ‘https://graph.mapillary.com/{}/detections?fields=image’.format(mapFeature_id)
response = requests.get(url, headers=header)
data = response.json()
print(data)
for elem in data[‘data’]:
images_id_list.append(elem[‘image’][‘id’])
print(images_id_list)

app_access_token = codes.API_KEY
image_id = images_id_list[0]
header = {‘Authorization’: ‘OAuth {}’.format(app_access_token)}
url = ‘https://graph.mapillary.com/{}?fields=thumb_2048_url’.format(image_id)
response = requests.get(url, headers=header)
data = response.json()
print(data)
image_url = data[‘thumb_2048_url’]

with open(‘{}/{}.jpg’.format(outputFolder, image_id), ‘wb’) as handler:
image_data = requests.get(image_url, stream=True).content
handler.write(image_data)

2 Likes

Nice, thank you for sharing!

1 Like