如何阅读HGT文件在C ++中 [英] How to read HGT files in C++

查看:396
本文介绍了如何阅读HGT文件在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取存储在HGT文件中的高程数据。据我所知,他们可以理解为二进制文件。

I'm trying to read the elevation data stored in HGT files. As far as I know they can be read as binary files.

我发现这个线程:结果
我如何访问C ++?
结果

I found this thread:
How do I access .HGT SRTM files in C++?

根据这个职位,我的例子code是:

Based on that post, my sample code is:

#include <iostream>
#include <fstream>

int main(int argc, const char * argv[])
{

std::ifstream::pos_type size;
char * memblock;

std::ifstream file ("N45W066.hgt", std::ios::in|std::ios::binary|std::ios::ate);

if (file.is_open())
{
    size = 2;
    memblock = new char [size];

    file.seekg(0, std::ios::beg);
    file.read(memblock, size);

    int srtm_ver = 1201;
    int height[1201][1021];

    for (int i = 0; i<srtm_ver; ++i){
        for (int j = 0; j < srtm_ver; ++j) {

            height[i][j] = (memblock[0] << 8 | memblock[1]);
            std::cout<<height[i][j]<<" ";
        }
        std::cout<<std::endl;
    }
}


return 0;
}

第一次运行之后,它给了我一串零,没有别的:|
该文件HGT是好的,我已经有可以读取多种类型的地图文件的应用程序测试了它,它包含了我所需要的高程数据。

After the first run, it gives me a bunch of zeros, and nothing else :| The hgt file is good, i've tested it with an application that can read several type of map files, and it contains the elevation data what i need.

推荐答案

这将读取该文件,并正确填充阵列。在每次读2字节通常是不这样做的最有效方式,但它是简单的。另一种方法是读取整个文件,然后后来交换字节。

This will read the file and populate the array correctly. Reading 2 bytes at a time generally isn't the most efficient way to do it, but it is simple. The alternative would be to read the whole file and then swap the bytes afterward.

我搬到外面主力高度阵列,以避免在Visual Studio中的默认堆栈大小堆栈溢出问题。如果你的筹码够大,你可以将其移回,或动态在堆上分配内存。

I moved the height array outside main to avoid a stack overflow issue with the default stack size in Visual Studio. If your stack is large enough you could move it back, or dynamically allocate the memory on the heap.

#include <iostream>
#include <fstream>

const int SRTM_SIZE = 1201;
short height[SRTM_SIZE][SRTM_SIZE] = {0};

int main(int argc, const char * argv[])
{
    std::ifstream file("N45W066.hgt", std::ios::in|std::ios::binary);
    if(!file)
    {
        std::cout << "Error opening file!" << std::endl;
        return -1;
    }

    unsigned char buffer[2];
    for (int i = 0; i < SRTM_SIZE; ++i)
    {
        for (int j = 0; j < SRTM_SIZE; ++j) 
        {
            if(!file.read( reinterpret_cast<char*>(buffer), sizeof(buffer) ))
            {
                std::cout << "Error reading file!" << std::endl;
                return -1;
            }
            height[i][j] = (buffer[0] << 8) | buffer[1];
        }
    }

    //Read single value from file at row,col
    const int row = 500;
    const int col = 1000;
    size_t offset = sizeof(buffer) * ((row * SRTM_SIZE) + col);
    file.seekg(offset, std::ios::beg);
    file.read( reinterpret_cast<char*>(buffer), sizeof(buffer) );
    short single_value = (buffer[0] << 8) | buffer[1];
    std::cout << "values at " << row << "," << col << ":" << std::endl;
    std::cout << "  height array: " << height[row][col] << ", file: " << single_value << std::endl;

    return 0;
}

这篇关于如何阅读HGT文件在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆