【问题标题】:Read all point and insert into array读取所有点并插入数组
【发布时间】:2020-06-21 09:00:45
【问题描述】:

我遇到了这个 gdal 代码来读取特定位置的海拔点。 这段代码只允许我阅读一点。海拔文件信息:

左上 X = 103.0000 左上 Y = 4.0000

右下 X = 104.0000 右下 Y = 3.0000

没有。列数 = 3601,行数 = 3601

如何读取所有高程点并将其存储在向量数组中?

   int _tmain(int argc, _TCHAR* argv[])
{
    const char* pszFilename = "C:\\Elevation.DT2";
    double adfGeoTransform[6];
    float buffEleveation[1];

    int row;
    int col;

    std::vector<float> elevationVecArr;

    GDALRasterBand *poBand;

    GDALDataset  *poDataset;
    GDALAllRegister();
    poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );

    printf( "Driver: %s/%s\n",
        poDataset->GetDriver()->GetDescription(),
        poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
    printf( "Size is %dx%dx%d\n",
        poDataset->GetRasterXSize(),poDataset->GetRasterYSize(),
        poDataset->GetRasterCount() );
    if( poDataset->GetProjectionRef()  != NULL )
        printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
    if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
    {
        printf( "Origin = (%.6f,%.6f)\n",
                adfGeoTransform[0], adfGeoTransform[3] );
        printf( "Pixel Size = (%.6f,%.6f)\n",
                adfGeoTransform[1], adfGeoTransform[5] );
    }

    poDataset->GetGeoTransform(adfGeoTransform);
    poBand = poDataset->GetRasterBand(1); 
    float X = (103.0000);
    float Y = (4.000);
    
    row = (int)((adfGeoTransform[3] - Y) / -adfGeoTransform[5]); // ULY - xcoord / -N-S CellSize;
    col = (int)((X - adfGeoTransform[0]) / adfGeoTransform[1]);  // xcoord - ULX / E-W CellSize

    poBand->RasterIO(GF_Read, col, row, 1, 1, buffEleveation, 1, 1, GDT_Float32, 0, 0); // read the cell value

    float elevationValue = buffEleveation[0];           

    elevationVecArr.push_back(buffEleveation[0]);

    printf("Elevation = %.f", elevationValue);

    getchar();

    return 0;
    
    
}

【问题讨论】:

    标签: c++ gis gdal


    【解决方案1】:

    根据https://gdal.org/doxygen/classGDALRasterBand.html#a75d4af97b3436a4e79d9759eedf89af4 的文档,您可以指定要读取的范围。

    constexpr int rows=3601;
    constexpr int cols=3601;
    std::vector<float> buffEleveation(rows*cols);
    
    // Since we want to read all the data we set:
    // nXOff/nYOff to 0
    // nXSize/nYSize to 3601
    // nBufXSize/nBufXSize to 3601.
    poBand->RasterIO(GF_Read, 0, 0, rows, cols, buffEleveation.data(), rows, cols, GDT_Float32, 0, 0); // read all elevation values.
    

    【讨论】:

    • 这就是我一直在寻找的。抱歉,对 c++ 和 gdal 不太熟悉。我需要所有的高程点来在我的栅格地图上进行验证。谢谢你的回答
    • bufffElevation 仅具有所有 3601*3601 的高程值。我怎么知道哪个数字行和列会产生那个高程点
    猜你喜欢
    • 2018-07-05
    • 2021-05-10
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    相关资源
    最近更新 更多