GdalTest.exe中0x000007FEDF2C9120(gdal202.dll)的未处理异常:0xC0000005:访问冲突读取位置0x0000000000000058

时间:2018-11-14 03:30:27

标签: visual-c++ console-application gdal unhandled-exception

我尝试使用CreateCopy(),当我运行程序时似乎抛出了异常

  

GdalTest.exe中0x000007FEDF2C9120(gdal202.dll)的未处理异常:0xC0000005:访问冲突读取位置0x0000000000000058。

enter image description here

它将在控制台上显示以下输出:

enter image description here

我尝试在发生异常时进行调试,在调用堆栈上,它将转到新选项卡,然后显示未加载任何符号

enter image description here

我希望有人可以向我解释哪些文件无法加载,如果控制台可以查看栅格地图,它将引发错误或异常? 下面是我的代码,如果有人想要复制和引用它,只是我在“使用CreateCopy()”部分遇到问题。

感谢您和最诚挚的问候,

// GdalTest.cpp : Defines the entry point for the console application.
//



#include "cpl_conv.h" // for CPLMalloc()
#include "stdafx.h"
#include "gdal.h"
#include <stdlib.h>
#include <stdio.h>
#include "iostream"
#include "cpl_string.h"
#include "gdal_priv.h"




class GDALMajorObject;
class GDALDataset;
class GDALRasterBand;
class GDALDriver;
class GDALRasterAttributeTable;
class GDALProxyDataset;
class GDALProxyRasterBand;
class GDALAsyncReader;




int main(int argc,char* argv)
{

    ///*/*/*/*/*/*/*/*/*/*Getting Dataset Information*/*/*/*/*/*/*/*/*/*/

    GDALDataset *poDataset;


    //GDALDatasetH *poDatasetH;
    const char* Raster = new char[256];
    double  adfGeoTransform[6];


    GDALAllRegister();  

    Raster = "C:\\Users\\User\\Desktop\\DSI REFERENCE\\Map Data\\Raster\\4257.tif";

    poDataset = (GDALDataset *) GDALOpen(Raster, GA_ReadOnly )CPL_WARN_UNUSED_RESULT;
    //poDataset = (GDALDataset *) GDALOpen(Raster, GA_ReadOnly );

    if( poDataset == NULL )
    {
        printf("Not found");
    }         
    else
    {
        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] );

    }

    }

    ///*/*/*/*/*/*/*/*/*/*  Fetching a Raster Band*/*/*/*/*/*/*/*/*/*/

    GDALRasterBand  *poBand;
    int   nBlockXSize, nBlockYSize;
    int   bGotMin, bGotMax;
    double  adfMinMax[2];

    poBand = poDataset->GetRasterBand( 1 );
    poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );

    printf( "Block=%dx%d Type=%s, ColorInterp=%s\n", nBlockXSize, nBlockYSize,GDALGetDataTypeName(poBand->GetRasterDataType()), 
    GDALGetColorInterpretationName( poBand->GetColorInterpretation()) );

    adfMinMax[0] = poBand->GetMinimum( &bGotMin );
    adfMinMax[1] = poBand->GetMaximum( &bGotMax );

    if( ! (bGotMin && bGotMax) )
    GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);

    printf( "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );

    if( poBand->GetOverviewCount() > 0 )
    printf( "Band has %d overviews.\n", poBand->GetOverviewCount() );

    if( poBand->GetColorTable() != NULL )
    printf( "Band has a color table with %d entries.\n",
             poBand->GetColorTable()->GetColorEntryCount() );

    // make it 256 bytes for a string length of 255 plus null (\0) terminator

    ///*/*/*/*/*/*/*/*/*/*/*Reading Raster Data*/*/*/*/*/*/*/*/*/*/*/

    float *pafScanline;
    int   nXSize = poBand->GetXSize();
    pafScanline = (float *) CPLMalloc(sizeof(float)*nXSize);
    poBand->RasterIO( GF_Read, 0, 0, nXSize, 1,pafScanline, nXSize, 1, GDT_Float32,0, 0 );

    ///*/*/*/*/*/*/*/*/*/*/*Techniques for Creating Files (determine whether it supports Create() and/or CreateCopy().) */*/*/*/*/*/*/*/*/*/*/

    const char *pszFormat = "GTiff";
    GDALDriver *poDriver;
    char **papszMetadata;
    poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);

    if( poDriver == NULL )
        exit( 1 );

    papszMetadata = poDriver->GetMetadata();
    if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )
        printf( "Driver %s supports Create() method.\n", pszFormat );

    if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )
        printf( "Driver %s supports CreateCopy() method.\n", pszFormat );

    ///*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*Using CreateCopy()*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    const char*pszDstFilename= new char[256];

    GDALDataset *poSrcDS =(GDALDataset *) GDALOpen( pszDstFilename, GA_ReadOnly );
    GDALDataset *poDstDS;
    poDstDS = poDriver->CreateCopy( pszDstFilename, poSrcDS, FALSE,NULL, NULL, NULL );
    /* Once we're done, close properly the dataset */
    if( poDstDS != NULL )
    GDALClose( (GDALDatasetH) poDstDS );
    GDALClose( (GDALDatasetH) poSrcDS );

    std::cin.get();

    //delete[] Raster;// release the memory VERY IMPORTANT


}

1 个答案:

答案 0 :(得分:2)

您尚未将变量pszDstFilename设置为有意义的值。您分配了它,但它只包含随机垃圾。您需要将有效的现有数据集放入其中,以便在调用GDALOpen()时发生任何有用的事情。 documentation指出:

  

注册驱动程序后,应用程序应调用独立的GDALOpen()函数打开数据集,传递数据集的名称和所需的访问权限(GA_ReadOnly或GA_Update)。 >