Small Python 3 Wrapper for Mapillary API V3

New to the community.

Wrote a Small Python 3 for most of the functionalities for Mapillary API V3. There is no uploading or gpx format support yet. Thought it might be easier to write data collecting tools with this if there was a wrapper. Let me know what to improve on, what needs to be fixed or what’s just bad besides my grammar.

1 Like

This is really great! In my work for Mapillary as a solutions engineer, I use some similar code in a Jupyter notebook. I also have worked on implementing pagination, here’s an example that lets me get all sequences from one username:

import json, requests

output = {"type":"FeatureCollection","features":[]}

client_id = 'xyz123' # insert your client ID here
usernames = 'chrisbeddow' #example username
url = ('https://a.mapillary.com/v3/sequences?client_id={}&per_page=1000&usernames={}').format(client_id,usernames)
r = requests.get(url)
data = r.json()
data_length = len(data['features'])
for f in data['features']:
    output['features'].append(f)
while data_length == 1000:
    link = r.links['next']['url']
    r = requests.get(link)
    data = r.json()
    for f in data['features']:
        output['features'].append(f)
    print(len(output['features']))
    data_length = len(data['features'])
    
with open('data.geojson', 'w') as outfile:
    json.dump(output, outfile)

Feel free to implement that to your code library!

In your example, you have coordinates for both closeto and lookat, which I am not sure the average user will understand for a simple example. Your query will find images close to the input point, but looking at another point–maybe just use closeto for a simple example, and make a different example for lookat.

This can be really useful for both images and sequences. I think when trying to get all sequences in a bbox, Mapillary will return sequences which cross through the bbox, but includes the geometry that extends beyond it, also. In this case, it could be nice to use a spatial library to perform a clip function, but to do it in pure python, you could actually loop through every point in each sequence line returned, and if the point itself is not inside the given bbox, then manually delete that line from the response before saving it. This way you get sequence geometry only in the bbox, not just intersecting the bbox but extending outside of it. This is very useful when you’re trying to measure the lengths of sequences in a bbox, for example.

Do you have a specific project in mind for this? Overall this is really great and I hope to see it become adopted for other peoples’ projects.

2 Likes

Thank you for that example. I will definitely put it in the code sample and hopefully implement similar functions to that as well for.

The variables “closeto” and “lookat” were just from the API V3 documentation in Mapillary so I decided to keep the variable names consistent incase someone wanted to know why these parameters were there and where I got them from. I think I will just make better documentation about the variable names for now or a wiki.

For the sequences that return beyond the geometry, I think I get what you mean, but might need to ask questions when it comes down to it. I’ll try do it in pure python to limit dependencies for now, but if it takes to long, clip it is.

I didn’t have any specific projects in mind when I created this. I just wanted an easy way to test algorithms like lane line detection with sample images. Though, when trying to stitch a video together I noticed that the frames in a sequence are to far apart and make a segmented video. When trying to get the dataset of images I thought “might as well make a python wrapper” since I didn’t find any easy way or a Github repo that did that’s programmable. The Mapillary cli is a great tool, but hard to develop with I guess? I kind of want to make visualization graphs of some kind, but not sure what to do. I will figure it out along the way.

Thank you for the feedback.