Mapbox Terrain-RGB v1 API

The URL we are constructing uses the Raster Tiles API endpoint, a powerful feature of Mapbox that allows developers to request map tiles in raster format. Each tile is a small image, typically in PNG format, that represents a portion of the map at a specific zoom level. When combined, these tiles form a complete map.

For elevation data, we use the Mapbox Terrain-RGB v1 dataset. This dataset encodes elevation values directly into the red, green, and blue (RGB) channels of a PNG image. Each pixel in the image represents a specific geographic location, and the color value of the pixel encodes the elevation at that point. By decoding the RGB values, we can extract high-precision elevation data for any location on Earth.

The Mapbox Terrain-RGB v1 tiles are designed to provide seamless global coverage, making them ideal for applications that require detailed topographic data. This dataset, combined with the Raster Tiles API, allows for efficient and scalable retrieval of elevation data that can be used in various mapping and geospatial analysis applications.

By understanding how these components work together, users can effectively leverage Mapbox's powerful tools to obtain accurate and detailed elevation data for any area of interest.

1. Converting latitude and longitude to tile coordinates

To retrieve the correct map tile from Mapbox, we first need to convert geographic coordinates (latitude and longitude) into tile coordinates (x and y). This is achieved using the following C++ functions:


int lonToTileX(double lon, int zoom)
{
    return floor(((lon + 180.0) / 360.0) * pow(2, zoom));
}

int latToTileY(double lat, int zoom)
{
    double latRad = lat * M_PI / 180.0;
    return floor((1.0 - log(tan(latRad) + 1.0 / cos(latRad)) / M_PI) / 2.0 * (pow(2, zoom)));
}

The lonToTileX and latToTileY functions take the longitude and latitude respectively and convert them to x and y tile coordinates at the specified zoom level. These functions use trigonometric and logarithmic transformations to accurately map geographic coordinates to the correct tile on the Mapbox grid.



References