#include <stdio.h> #include <stdlib.h> #include <tiffio.h>

#define ROWS 327 #define COLS 486 #define PIXEL_SIZE 5

int main(int argc, char* argv[]) { TIFF *tif; float *data; uint32 width, height; uint16 bits_per_sample, samples_per_pixel; tsize_t strip_size; unsigned long long total_fill_volume = 0, total_cut_volume = 0; int i, j;

if (argc < 2) {
    fprintf(stderr, "Usage: %s <input file>

", argv[0]); exit(1); }

tif = TIFFOpen(argv[1], "r");
if (!tif) {
    fprintf(stderr, "Error opening TIFF file

"); exit(1); }

TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);

if (width != COLS || height != ROWS || bits_per_sample != 32 || samples_per_pixel != 1) {
    fprintf(stderr, "Invalid TIFF format

"); exit(1); }

strip_size = TIFFStripSize(tif);
data = (float*) _TIFFmalloc(strip_size);
if (!data) {
    fprintf(stderr, "Error allocating memory

"); exit(1); }

for (i = 0; i < height; i++) {
    TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, i, 0), data, strip_size);
    for (j = 0; j < width; j++) {
        float elevation = data[j];
        if (elevation > 0) {
            total_fill_volume += (PIXEL_SIZE * PIXEL_SIZE * elevation);
        } else {
            total_cut_volume += (PIXEL_SIZE * PIXEL_SIZE * (-elevation));
        }
    }
}

printf("Total fill volume: %llu cubic meters

", total_fill_volume); printf("Total cut volume: %llu cubic meters ", total_cut_volume);

_TIFFfree(data);
TIFFClose(tif);

return 0;

}

这段代码是用来读取DEM(数字高程模型)数据的,它使用了TIFF库来读取TIFF格式的DEM文件。TIFF是一种常用的图像文件格式,可以存储多种类型的图像数据,包括DEM数据。

具体来说,这段代码读取了一个指定的TIFF文件,并检查其格式是否符合要求(宽度、高度、位数和像素数),然后逐行读取文件中的数据,并计算总填方和总挖方体积。填方和挖方分别表示地面高于和低于某一基准面的部分的体积。

在页面中读取DEM数据的方法因网站而异,但通常可以使用JavaScript或其他客户端脚本来读取和处理DEM数据。例如,可以使用JavaScript的XMLHttpRequest对象来异步加载DEM数据文件,并将其解析为JavaScript对象或数组,然后使用这些数据来绘制地形图或进行其他处理。

C语言读取DEM数据并计算填挖方体积

原文地址: https://www.cveoy.top/t/topic/fVd6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录