| Crates.io | rgb2slope |
| lib.rs | rgb2slope |
| version | 0.1.0 |
| created_at | 2025-08-19 10:46:31.78436+00 |
| updated_at | 2025-08-19 10:46:31.78436+00 |
| description | A Rust library for converting Mapbox RGB tiles to slope maps. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1801697 |
| size | 332,253 |
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 :
The slope tile should show the areas where the slope angle is above a certain threashold. The pixels can be different colors:
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] },
];
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];
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];
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.
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.