#rgb #tile #mapbox #area #slope

app rgb2slope

A Rust library for converting Mapbox RGB tiles to slope maps

1 unstable release

Uses new Rust 2024

0.1.0 Aug 19, 2025

#512 in Images

MIT license

240KB
159 lines

I want to create a RUST cli application that takes as an input a Mapbox like RGB DEM tile, and that output 3 different terrain tiles :

  • A slope tile
  • An aspect tile
  • A flat areas tile

Slope tile

The slope tile should show the areas where the slope angle is above a certain threashold. The pixels can be different colors:

  • transparent if the slope is below 30 degrees
  • yellow if the slope is between 30 and 35 degrees
  • orange if the slope is between 35 and 40 degrees
  • red if the slope is between 40 and 45 degrees
  • pink if the slope is between 45 and 90 degrees

Here is the corresponding config written in Typescript:

export const SLOPE_ANGLE_INTERVALS: {
  start: number;
  end: number;
  color: Color;
}[] = [
  { start: 0, end: 30, color: [0, 0, 0, 0] },
  { start: 30, end: 35, color: [241, 231, 11, 255] },
  { start: 35, end: 40, color: [248, 111, 33, 255] },
  { start: 40, end: 45, color: [227, 3, 91, 255] },
  { start: 45, end: 90, color: [203, 135, 186, 255] },
];

Slope direction (aspect map)

Pixels where the slope is below 10 degrees should be transparent. Then pixel should be in different colors depending on the 8 possible directions of the slope. Bellow is the corresponding confi in Typescript:

export const NORTH_COLOR: Color = [66, 135, 245, 255];
export const NORTHEAST_COLOR: Color = [70, 204, 163, 255];
export const EAST_COLOR: Color = [123, 237, 159, 255];
export const SOUTHEAST_COLOR: Color = [249, 202, 36, 255];
export const SOUTH_COLOR: Color = [245, 127, 23, 255];
export const SOUTHWEST_COLOR: Color = [234, 32, 39, 255];
export const WEST_COLOR: Color = [156, 39, 176, 255];
export const NORTHWEST_COLOR: Color = [96, 125, 139, 255];

Flat areas

Pixels should be red if the slope is bellow 5 degrees. Below is the exact color to use as a RGBA typescript array:

export const FLAT_AREAS_COLOR = [234, 32, 39, 255];

Other considerations

The formula to decode elevation data in metters from RGB values of a Mapbox RGB DEM tile is:

height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)

The input Mapbox RGB DEM tile already has a 1 pixel buffer on the edges, to be able to compute the slope angle and direction values for the whole image.

The implementation should be as efficient as possible. Doing the least amount of loops over pixel matrixes.

You can write all the code in the main.rs file.

Algorithme

  • Read the Mapbox RGB DEM image provided as a path argument, and decode the pixels to an elevation matrix.
  • Create 3 ImageBuffer::<Rgba<u8> using the image crate for the 3 tiles to output. The width and height should be 2 pixels less than the input Mapbox image to remove the buffer.
  • Loop over the elevation matrix and compute the slope angle and direction for each pixels. Write the corresponding pixels to the image buffers in the same loop iteration.
  • Write the ImageBuffers to png files, using paths also prodided as cli arguments.

Dependencies

~8MB
~164K SLoC