mirrorworld.mapillary: Mapillary

Mapillary has a documented public API and needs a free access token (MLY|... from https://www.mapillary.com/dashboard/developers): pass access_token= or set MAPILLARY_ACCESS_TOKEN. Imagery is CC BY-SA 4.0. Images can be 360° panoramas or ordinary photos (is_pano).

Finding images

find_panoramas(lat, lon, radius=25, limit=None, panos_only=False, session=None, access_token=None)[source]

Finds images within a radius around a point, closest first.

This reads the coverage tiles that overlap the circle, because Mapillary’s bounding-box search fails in densely covered areas.

Parameters:
  • lat (float) – Latitude of the center point.

  • lon (float) – Longitude of the center point.

  • radius (float) – (optional) Search radius in meters. Defaults to 25.

  • limit (int | None) – (optional) Maximum number of results. Defaults to no limit.

  • panos_only (bool) – (optional) Only return 360° panoramas. Defaults to False.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A list of MapillaryPanorama objects.

Return type:

List[MapillaryPanorama]

async find_panoramas_async(lat, lon, session, radius=25, limit=None, panos_only=False, access_token=None)[source]
Parameters:
Return type:

List[MapillaryPanorama]

find_panorama_by_id(panoid, session=None, access_token=None)[source]

Fetches the full metadata of an image, including camera parameters and image URLs.

Parameters:
  • panoid (int) – The image ID.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A MapillaryPanorama object, or None if no image with this ID exists.

Return type:

MapillaryPanorama | None

async find_panorama_by_id_async(panoid, session, access_token=None)[source]
Parameters:
  • panoid (int)

  • session (ClientSession)

  • access_token (str)

Return type:

MapillaryPanorama | None

get_coverage_tile(tile_x, tile_y, session=None, access_token=None)[source]

Fetches all images on a Mapillary coverage tile.

The tiles are XYZ tiles at zoom level 14 (about 1.5 km across at mid latitudes). The returned objects carry the location, capture time, heading, panorama flag, sequence and uploader; call find_panorama_by_id() for camera details and image URLs.

Parameters:
  • tile_x (int) – X coordinate of the tile.

  • tile_y (int) – Y coordinate of the tile.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A list of MapillaryPanorama objects (empty if there is no coverage).

Return type:

List[MapillaryPanorama]

async get_coverage_tile_async(tile_x, tile_y, session, access_token=None)[source]
Parameters:
  • tile_x (int)

  • tile_y (int)

  • session (ClientSession)

  • access_token (str)

Return type:

List[MapillaryPanorama]

get_coverage_tile_by_latlon(lat, lon, session=None, access_token=None)[source]

Same as get_coverage_tile(), but for fetching the tile on which a point is located.

Parameters:
  • lat (float) – Latitude of the point.

  • lon (float) – Longitude of the point.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A list of MapillaryPanorama objects.

Return type:

List[MapillaryPanorama]

async get_coverage_tile_by_latlon_async(lat, lon, session, access_token=None)[source]
Parameters:
  • lat (float)

  • lon (float)

  • session (ClientSession)

  • access_token (str)

Return type:

List[MapillaryPanorama]

get_sequence(sequence_id, session=None, access_token=None)[source]

Lists the IDs of the images in a sequence, in capture order.

Parameters:
  • sequence_id (str) – The sequence ID.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

The image IDs.

Return type:

List[int]

async get_sequence_async(sequence_id, session, access_token=None)[source]
Parameters:
  • sequence_id (str)

  • session (ClientSession)

  • access_token (str)

Return type:

List[int]

Usage sample:

from mirrorworld import mapillary

panos = mapillary.find_panoramas(47.6062, -122.3321, radius=50, panos_only=True)
pano = mapillary.find_panorama_by_id(panos[0].id)
print(pano.date, pano.camera_type, pano.permalink)
mapillary.download_panorama(pano, f"{pano.id}.jpg", size="original")

Async:

from mirrorworld import mapillary
from aiohttp import ClientSession

async with ClientSession() as session:
    panos = await mapillary.find_panoramas_async(47.6062, -122.3321, session, radius=50)
    await mapillary.download_panorama_async(panos[0], f"{panos[0].id}.jpg", session, size="2048")

Downloading images

get_panorama(pano, size='original', session=None, access_token=None)[source]

Downloads an image and returns it as PIL image.

Parameters:
  • pano (MapillaryPanorama | int) – The image, or its ID. Metadata without image URLs (e.g. from a coverage tile) is completed with find_panorama_by_id() first.

  • size (str) – (optional) 256, 1024, 2048 (width in pixels) or original. Defaults to original.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A PIL image.

Return type:

Image

async get_panorama_async(pano, session, size='original', access_token=None)[source]
Parameters:
Return type:

Image

download_panorama(pano, path, size='original', pil_args=None, session=None, access_token=None)[source]

Downloads an image to a file. If the chosen format is JPEG, Exif metadata (and XMP GPano metadata for 360° panoramas) is included.

Parameters:
  • pano (MapillaryPanorama | int) – The image, or its ID.

  • path (str) – Output path.

  • size (str) – (optional) 256, 1024, 2048 or original. Defaults to original.

  • pil_args (dict) – (optional) Additional arguments for PIL’s Image.save method, e.g. {"quality":100}. Defaults to {}.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Return type:

None

async download_panorama_async(pano, path, session, size='original', pil_args=None, access_token=None)[source]
Parameters:
Return type:

None

Map features, detections and point clouds

Objects triangulated from many images (utility poles, manholes, junction boxes, street lights, hydrants, traffic signs, …), per-image semantic segmentations and structure-from-motion point clouds.

find_map_features(lat, lon, radius=50, values=None, layer='point', session=None, access_token=None)[source]

Finds map features (triangulated objects such as utility poles or manholes) around a point, closest first.

Parameters:
  • lat (float) – Latitude of the center point.

  • lon (float) – Longitude of the center point.

  • radius (float) – (optional) Search radius in meters. Defaults to 50.

  • values (Iterable[str] | None) – (optional) Keep only these classes. An entry ending in -- matches as a prefix, e.g. ["object--support--utility-pole", "object--manhole", "regulatory--"]. Defaults to all.

  • layer (str) – (optional) point or traffic_sign. Defaults to point.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A list of MapFeature objects.

Return type:

List[MapFeature]

async find_map_features_async(lat, lon, session, radius=50, values=None, layer='point', access_token=None)[source]
Parameters:
Return type:

List[MapFeature]

find_map_feature_by_id(feature_id, session=None, access_token=None)[source]

Fetches a map feature with its facing direction and the IDs of the images it was detected in.

Parameters:
  • feature_id (int) – The map feature ID.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A MapFeature object, or None if no feature with this ID exists.

Return type:

MapFeature | None

async find_map_feature_by_id_async(feature_id, session, access_token=None)[source]
Parameters:
  • feature_id (int)

  • session (ClientSession)

  • access_token (str)

Return type:

MapFeature | None

get_map_features_tile(tile_x, tile_y, layer='point', session=None, access_token=None)[source]

Fetches all map features (triangulated objects) on a coverage tile at zoom level 14.

Parameters:
  • tile_x (int) – X coordinate of the tile.

  • tile_y (int) – Y coordinate of the tile.

  • layer (str) – (optional) point (poles, manholes, junction boxes, street lights, hydrants, …) or traffic_sign. Defaults to point.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A list of MapFeature objects.

Return type:

List[MapFeature]

async get_map_features_tile_async(tile_x, tile_y, session, layer='point', access_token=None)[source]
Parameters:
  • tile_x (int)

  • tile_y (int)

  • session (ClientSession)

  • layer (str)

  • access_token (str)

Return type:

List[MapFeature]

get_detections(pano, session=None, access_token=None)[source]

Fetches Mapillary’s semantic segmentation of an image: one polygon set per detected object or surface (road, sidewalk, curb, vegetation, sky, poles, manholes, …).

Parameters:
  • pano (MapillaryPanorama | int) – The image, or its ID.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A list of Detection objects with polygons in normalized image coordinates.

Return type:

List[Detection]

async get_detections_async(pano, session, access_token=None)[source]
Parameters:
Return type:

List[Detection]

get_point_cloud(pano, session=None, access_token=None)[source]

Downloads the structure-from-motion reconstruction that an image belongs to: a colored point cloud of the surroundings plus the poses of all images in the reconstruction.

Parameters:
  • pano (MapillaryPanorama | int) – The image, or its ID.

  • session (Session) – (optional) A requests session.

  • access_token (str) – (optional) Mapillary access token. Defaults to MAPILLARY_ACCESS_TOKEN.

Returns:

A PointCloud, or None if the image has no reconstruction.

Return type:

PointCloud | None

async get_point_cloud_async(pano, session, access_token=None)[source]
Parameters:
Return type:

PointCloud | None

Usage sample:

from mirrorworld import mapillary

poles = mapillary.find_map_features(47.68, -122.30, radius=150,
                                    values=["object--support--utility-pole", "object--junction-box"])
pole = mapillary.find_map_feature_by_id(poles[0].id)       # adds the images it was seen in
detections = mapillary.get_detections(pole.image_ids[0])   # polygons in 0..1 image coordinates
cloud = mapillary.get_point_cloud(pole.image_ids[0])       # cloud.xyz, cloud.rgb, cloud.to_wgs84()

Data classes

class MapillaryPanorama(id, lat, lon, date=None, heading=None, is_pano=None, sequence_id=None, creator_id=None, organization_id=None, quality_score=None, computed_lat=None, computed_lon=None, computed_heading=None, elevation=None, computed_elevation=None, camera_type=None, camera_parameters=None, make=None, model=None, width=None, height=None, creator_username=None, image_urls=None)[source]

Metadata of a Mapillary image.

Mapillary hosts both 360° panoramas and ordinary (perspective) photos; is_pano tells them apart. The class is named like the other providers’ for consistency.

ID, latitude and longitude are always present. Coverage tiles provide the fields up to quality_score; mirrorworld.mapillary.find_panorama_by_id() fills in the rest.

Parameters:
  • id (int)

  • lat (float)

  • lon (float)

  • date (datetime | None)

  • heading (float | None)

  • is_pano (bool | None)

  • sequence_id (str | None)

  • creator_id (int | None)

  • organization_id (int | None)

  • quality_score (float | None)

  • computed_lat (float | None)

  • computed_lon (float | None)

  • computed_heading (float | None)

  • elevation (float | None)

  • computed_elevation (float | None)

  • camera_type (str | None)

  • camera_parameters (list | None)

  • make (str | None)

  • model (str | None)

  • width (int | None)

  • height (int | None)

  • creator_username (str | None)

  • image_urls (Dict[str, str] | None)

id: int

The image ID.

lat: float

Latitude as recorded by the camera.

lon: float

Longitude as recorded by the camera.

date: datetime | None = None

Capture time (UTC).

heading: float | None = None

Heading in radians as recorded by the camera, where 0 is north and π/2 is east.

is_pano: bool | None = None

Whether this is a 360° panorama.

sequence_id: str | None = None

ID of the sequence this image belongs to.

creator_id: int | None = None

ID of the uploader.

organization_id: int | None = None

ID of the organization that uploaded the image, if any.

quality_score: float | None = None

Mapillary’s image quality score from 0 to 1.

computed_lat: float | None = None

Latitude after Mapillary’s structure-from-motion correction.

computed_lon: float | None = None

Longitude after Mapillary’s structure-from-motion correction.

computed_heading: float | None = None

Heading in radians after Mapillary’s correction.

elevation: float | None = None

Altitude in meters as recorded by the camera.

computed_elevation: float | None = None

Altitude in meters after Mapillary’s correction.

camera_type: str | None = None

Projection: perspective, fisheye, equirectangular or spherical.

camera_parameters: list | None = None

Intrinsics, e.g. focal length and radial distortion for perspective cameras.

make: str | None = None

Camera make.

model: str | None = None

Camera model.

width: int | None = None

Width of the original image in pixels.

height: int | None = None

Height of the original image in pixels.

creator_username: str | None = None

User name of the uploader.

image_urls: Dict[str, str] | None = None

Signed image URLs by size: 256, 1024, 2048 and original. They expire after a while.

A link to this image on mapillary.com.

class MapFeature(id, lat, lon, value, layer, first_seen=None, last_seen=None, aligned_direction=None, image_ids=None, distance=None)[source]

An object that Mapillary detected in several images and triangulated to a map position, e.g. a utility pole, manhole, junction box, street light, fire hydrant or traffic sign.

The full list of values is at https://www.mapillary.com/developer/api-documentation/points and https://www.mapillary.com/developer/api-documentation/traffic-signs.

Parameters:
id: int

The map feature ID.

lat: float

Latitude of the triangulated position.

lon: float

Longitude of the triangulated position.

value: str

Object class, e.g. object--support--utility-pole or regulatory--stop--g1.

layer: str

point or traffic_sign.

first_seen: datetime | None = None

Capture time of the earliest image showing the object.

last_seen: datetime | None = None

Capture time of the latest image showing the object.

aligned_direction: float | None = None

Facing direction in radians (traffic signs), from find_map_feature_by_id().

image_ids: List[int] | None = None

IDs of the images in which the object was detected, from find_map_feature_by_id().

distance: float | None = None

Distance from the search point in meters (only set by find_map_features()).

class Detection(id, value, polygons, created_at=None)[source]

A segmentation of one object in one image.

Parameters:
id: int

The detection ID.

value: str

Object or surface class, e.g. construction--flat--sidewalk or object--manhole.

polygons: List[List[List[Tuple[float, float]]]]

Polygons, each a list of rings, each a list of (x, y) points normalized to 0..1 of the image width and height with the origin at the top left. The first ring of a polygon is its outline; further rings are holes. For 360° panoramas the coordinates refer to the equirectangular image.

created_at: datetime | None = None

When Mapillary created the detection.

class PointCloud(xyz, rgb, reference, reconstruction=<factory>)[source]

Points of a Mapillary structure-from-motion reconstruction (OpenSfM), in a local east/north/up frame centered on reference.

Parameters:
xyz: ndarray

(N, 3) east, north, up coordinates in meters relative to reference.

rgb: ndarray

(N, 3) colors, 0..255.

reference: Tuple[float, float, float]

Latitude, longitude and altitude of the local frame’s origin.

reconstruction: dict

The full OpenSfM reconstruction (cameras, shots with poses, points).

to_wgs84()[source]

Converts the points to geographic coordinates.

Returns:

(N, 3) latitude, longitude and ellipsoidal height.

Return type:

ndarray

Miscellaneous

Creates a link to an image, or to a location, on mapillary.com.

Parameters:
  • id (int) – (optional) The image ID.

  • lat (float) – (optional) Latitude of the map center.

  • lon (float) – (optional) Longitude of the map center.

  • zoom (float) – (optional) Zoom level of the map. Defaults to 17.

Returns:

A Mapillary URL.

Return type:

str

get_access_token(access_token=None)[source]

Returns the access token to use for Mapillary requests: the one passed in, or else the MAPILLARY_ACCESS_TOKEN environment variable. Tokens (MLY|...) are free from https://www.mapillary.com/dashboard/developers.

Parameters:

access_token (str) – (optional) An explicit token.

Returns:

The token.

Return type:

str