#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PIXEL_SIZE 5.0 // 像素大小
#define NO_DATA_VALUE -99999 // 无效像素值

int main()
{
    FILE* fp_dem;
    const char* filename = 'D:\\kjfx\\dem.tif';
    int width = 486, height = 327, i, j;
    double* elevation;
    double fill_volume = 0.0, cut_volume = 0.0;

    // 打开DEM.tif文件
    fp_dem = fopen(filename, 'rb');
    if (fp_dem == NULL) {
        printf('Error: cannot open file %s!\n', filename);
        return 1;
    }

    // 分配内存空间
    elevation = (double*)malloc(width * height * sizeof(double));
    if (elevation == NULL) {
        printf('Error: cannot allocate memory!\n');
        fclose(fp_dem);
        return 1;
    }

    // 读取DEM.tif数据
    int valid_pixel_count = 0;
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            double value;
            size_t n = fread(&value, sizeof(double), 1, fp_dem); // 将读取数据的类型改为double
            if (n != 1) {
                printf('Error: cannot read file!\n');
                free(elevation);
                fclose(fp_dem);
                return 1;
            }
            elevation[i * width + j] = value;
            if (value != NO_DATA_VALUE) {
                valid_pixel_count++;
            }
        }
    }

    // 判断是否存在有效像素值
    if (valid_pixel_count == 0) {
        printf('Error: no valid pixel value in file %s!\n', filename);
        free(elevation);
        fclose(fp_dem);
        return 1;
    }

    double epsilon = 0.1; 
    // 遍历像素值,计算填方和挖方的体积
    int valid_pixel_processed = 0;
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            double elev1 = elevation[i * width + j];
            if (elev1 == NO_DATA_VALUE) {
                continue;
            }
            double elev2 = NO_DATA_VALUE;
            if (i > 0) {
                elev2 = elevation[(i - 1) * width + j];
                if (elev2 != NO_DATA_VALUE) {
                    double diff = elev1 - elev2 + epsilon; // 加上偏移量
                    if (diff > 0) {
                        cut_volume += diff * PIXEL_SIZE * PIXEL_SIZE;
                    }
                    else {
                        fill_volume += (-diff) * PIXEL_SIZE * PIXEL_SIZE;
                    }
                }
            }
            if (i < height - 1) {
                elev2 = elevation[(i + 1) * width + j];
                if (elev2 != NO_DATA_VALUE) {
                    double diff = elev1 - elev2 + epsilon;
                    if (diff > 0) {
                        cut_volume += diff * PIXEL_SIZE * PIXEL_SIZE;
                    }
                    else {
                        fill_volume += (-diff) * PIXEL_SIZE * PIXEL_SIZE;
                    }
                }
            }
            if (j > 0) {
                elev2 = elevation[i * width + j - 1];
                if (elev2 != NO_DATA_VALUE) {
                    double diff = elev1 - elev2 + epsilon;
                    if (diff > 0) {
                        cut_volume += diff * PIXEL_SIZE * PIXEL_SIZE;
                    }
                    else {
                        fill_volume += (-diff) * PIXEL_SIZE * PIXEL_SIZE;
                    }
                }
            }
            if (j < width - 1) {
                elev2 = elevation[i * width + j + 1];
                if (elev2 != NO_DATA_VALUE) {
                    double diff = elev1 - elev2 + epsilon;
                    if (diff > 0) {
                        cut_volume += diff * PIXEL_SIZE * PIXEL_SIZE;
                    }
                    else {
                        fill_volume += (-diff) * PIXEL_SIZE * PIXEL_SIZE;
                    }
                }
            }
            valid_pixel_processed++;
        }
    }

    // 判断是否所有有效像素值都已处理
    if (valid_pixel_processed != valid_pixel_count) {
        printf('Warning: not all valid pixel values are processed!\n');
    }

    // 输出结果
    if (isnan(fill_volume)) {
        printf('Fill volume = N/A\n');
    }
    else {
        printf('Fill volume = %.2f m3\n', fill_volume);
    }
    if (isinf(cut_volume)) {
        printf('Cut volume = N/A\n');
    }
    else {
        printf('Cut volume = %.2f m3\n', cut_volume);
    }
    // 释放内存空间
    free(elevation);

    // 关闭文件
    fclose(fp_dem);

    return 0;
}

问题: 计算结果出现 'Fill volume = 13765225595709223400245858957667803696433712406117507661824000.00 m3' 解决方法: 将读取数据的类型由float改为double,即将fread(&value, sizeof(float), 1, fp_dem)改为fread(&value, sizeof(double), 1, fp_dem)。同时,也需要将NO_DATA_VALUE的值改为double类型。

C语言计算DEM填挖方体积