Gdal 2.1.0 示例工具的二次开发


GDAL是功能强大的一个开源库,它提供了无缝访问多源数据的可能性,同时提供了一些基本的算法对影像进行处理,如格式转换、投影变换、拉伸处理、影像裁剪、合并、镶嵌、融合等。

对于这些功能的使用,GDAL一直提供着许多的示例(可以查看官网对各个DEMO的说明,也可以搜索查找其各个应用示例,并不复杂),供大家使用。在平时的工作中,一些基本的操作,用这些工具来进行处理已经足够。

对于基于GDAL的二次开发,当然可以通过调用GDAL的基本服务以实现上述镶嵌、投影变换等相对高级的功能,但肯定没有直接应用这些示例来得方便。

事实上,每一个示例,都有一个对应的源码文件,通过对GDAL提供的各个基础功能的排列组合,形成一个个的示例应用。如提供格式转换以及影像裁剪功能的示例是gdal_translate.exe,想在自己的应用中使用影像格式转换或者裁剪功能,有两个办法。一是直接调用gdal_translate.exe进行命令行处理,二是把gdal_translate.cpp里的代码拿出来,把该代码封装成一个单独的函数来进行调用。

前一种方法在代码中应用时很方便,但前提是需要处理的影像文件不能在内存中,而只能是磁盘上的一个文件,这限制了该种方法的使用。而后者则要灵活得多,把gdal_translate.cpp里的代码复制出来,读懂它们,然后把需要的地方拿出来作为自己应用的一部分就可以了。只是改造源码是一个费力的过程,想想那些大量的传入参数就头疼。

幸好,GDAL 2.1.0来了,在它里面,几乎每一个示例都作了改变。以gdal_translate为例,它被拆成了gdal_translate_bin.cpp和gdal_translate_lib.cpp,其中前者主要对输入参数进行处理,而后者才是真正实现gdal_translate功能的地方。

在前期版本中的大量参数被GDALTranslateOptions所取代,即所有参数都被放入GDALTranslateOptions这个结构体中,调用者只要把这个结构体维护好就可以便利地调用gdal_translate的功能了,它的功能实现主要通过GDALTranslate函数,它被定义在gdal_utils.h中。

仔细查看这个文件,你会发现很多示例都被这么改造过了,也就是说,你可以很方便地在函数中使用示例的功能了。下面是列表,至于下面这些函数具体能实现哪些功能,相信简单搜索一下就能发现了

GDALInfo
GDALTranslate
GDALWarp
GDALVectorTranslate
GDALDEMProcessing
GDALNearblack
GDALGrid
GDALRasterize
GDALBuildVRT

/*! Options for GDALInfo(). Opaque type */
typedef struct GDALInfoOptions GDALInfoOptions;

typedef struct GDALInfoOptionsForBinary GDALInfoOptionsForBinary;

GDALInfoOptions CPL_DLL *GDALInfoOptionsNew(char** papszArgv, GDALInfoOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALInfoOptionsFree( GDALInfoOptions *psOptions );

char CPL_DLL *GDALInfo( GDALDatasetH hDataset, const GDALInfoOptions *psOptions );


/*! Options for GDALTranslate(). Opaque type */
typedef struct GDALTranslateOptions GDALTranslateOptions;

typedef struct GDALTranslateOptionsForBinary GDALTranslateOptionsForBinary;

GDALTranslateOptions CPL_DLL *GDALTranslateOptionsNew(char** papszArgv,
                                                      GDALTranslateOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALTranslateOptionsFree( GDALTranslateOptions *psOptions );

void CPL_DLL GDALTranslateOptionsSetProgress( GDALTranslateOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );

GDALDatasetH CPL_DLL GDALTranslate(const char *pszDestFilename,
                                   GDALDatasetH hSrcDataset,
                                   const GDALTranslateOptions *psOptions,
                                   int *pbUsageError);

/*! Options for GDALWarp(). Opaque type */
typedef struct GDALWarpAppOptions GDALWarpAppOptions;

typedef struct GDALWarpAppOptionsForBinary GDALWarpAppOptionsForBinary;

GDALWarpAppOptions CPL_DLL *GDALWarpAppOptionsNew(char** papszArgv,
                                                      GDALWarpAppOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALWarpAppOptionsFree( GDALWarpAppOptions *psOptions );

void CPL_DLL GDALWarpAppOptionsSetProgress( GDALWarpAppOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );
void CPL_DLL GDALWarpAppOptionsSetWarpOption( GDALWarpAppOptions *psOptions,
                                              const char* pszKey,
                                              const char* pszValue );

GDALDatasetH CPL_DLL GDALWarp( const char *pszDest, GDALDatasetH hDstDS,
                               int nSrcCount, GDALDatasetH *pahSrcDS,
                               const GDALWarpAppOptions *psOptions, int *pbUsageError );

/*! Options for GDALVectorTranslate(). Opaque type */
typedef struct GDALVectorTranslateOptions GDALVectorTranslateOptions;

typedef struct GDALVectorTranslateOptionsForBinary GDALVectorTranslateOptionsForBinary;

GDALVectorTranslateOptions CPL_DLL *GDALVectorTranslateOptionsNew(char** papszArgv,
                                                      GDALVectorTranslateOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALVectorTranslateOptionsFree( GDALVectorTranslateOptions *psOptions );

void CPL_DLL GDALVectorTranslateOptionsSetProgress( GDALVectorTranslateOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );

GDALDatasetH CPL_DLL GDALVectorTranslate( const char *pszDest, GDALDatasetH hDstDS, int nSrcCount,
                               GDALDatasetH *pahSrcDS,
                               const GDALVectorTranslateOptions *psOptions, int *pbUsageError );


/*! Options for GDALDEMProcessing(). Opaque type */
typedef struct GDALDEMProcessingOptions GDALDEMProcessingOptions;

typedef struct GDALDEMProcessingOptionsForBinary GDALDEMProcessingOptionsForBinary;

GDALDEMProcessingOptions CPL_DLL *GDALDEMProcessingOptionsNew(char** papszArgv,
                                                      GDALDEMProcessingOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALDEMProcessingOptionsFree( GDALDEMProcessingOptions *psOptions );

void CPL_DLL GDALDEMProcessingOptionsSetProgress( GDALDEMProcessingOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );

GDALDatasetH CPL_DLL GDALDEMProcessing(const char *pszDestFilename,
                                       GDALDatasetH hSrcDataset,
                                       const char* pszProcessing,
                                       const char* pszColorFilename,
                                       const GDALDEMProcessingOptions *psOptions,
                                       int *pbUsageError);

/*! Options for GDALNearblack(). Opaque type */
typedef struct GDALNearblackOptions GDALNearblackOptions;

typedef struct GDALNearblackOptionsForBinary GDALNearblackOptionsForBinary;

GDALNearblackOptions CPL_DLL *GDALNearblackOptionsNew(char** papszArgv,
                                                      GDALNearblackOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALNearblackOptionsFree( GDALNearblackOptions *psOptions );

void CPL_DLL GDALNearblackOptionsSetProgress( GDALNearblackOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );

GDALDatasetH CPL_DLL GDALNearblack( const char *pszDest, GDALDatasetH hDstDS,
                                    GDALDatasetH hSrcDS,
                                    const GDALNearblackOptions *psOptions, int *pbUsageError );

/*! Options for GDALGrid(). Opaque type */
typedef struct GDALGridOptions GDALGridOptions;

typedef struct GDALGridOptionsForBinary GDALGridOptionsForBinary;

GDALGridOptions CPL_DLL *GDALGridOptionsNew(char** papszArgv,
                                                      GDALGridOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALGridOptionsFree( GDALGridOptions *psOptions );

void CPL_DLL GDALGridOptionsSetProgress( GDALGridOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );

GDALDatasetH CPL_DLL GDALGrid( const char *pszDest,
                               GDALDatasetH hSrcDS,
                               const GDALGridOptions *psOptions, int *pbUsageError );

/*! Options for GDALRasterize(). Opaque type */
typedef struct GDALRasterizeOptions GDALRasterizeOptions;

typedef struct GDALRasterizeOptionsForBinary GDALRasterizeOptionsForBinary;

GDALRasterizeOptions CPL_DLL *GDALRasterizeOptionsNew(char** papszArgv,
                                                      GDALRasterizeOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALRasterizeOptionsFree( GDALRasterizeOptions *psOptions );

void CPL_DLL GDALRasterizeOptionsSetProgress( GDALRasterizeOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );

GDALDatasetH CPL_DLL GDALRasterize( const char *pszDest, GDALDatasetH hDstDS,
                                    GDALDatasetH hSrcDS,
                                    const GDALRasterizeOptions *psOptions, int *pbUsageError );

/*! Options for GDALBuildVRT(). Opaque type */
typedef struct GDALBuildVRTOptions GDALBuildVRTOptions;

typedef struct GDALBuildVRTOptionsForBinary GDALBuildVRTOptionsForBinary;

GDALBuildVRTOptions CPL_DLL *GDALBuildVRTOptionsNew(char** papszArgv,
                                                      GDALBuildVRTOptionsForBinary* psOptionsForBinary);

void CPL_DLL GDALBuildVRTOptionsFree( GDALBuildVRTOptions *psOptions );

void CPL_DLL GDALBuildVRTOptionsSetProgress( GDALBuildVRTOptions *psOptions,
                                              GDALProgressFunc pfnProgress,
                                              void *pProgressData );

GDALDatasetH CPL_DLL GDALBuildVRT( const char *pszDest,
                                   int nSrcCount, GDALDatasetH *pahSrcDS, const char* const* papszSrcDSNames,
                                   const GDALBuildVRTOptions *psOptions, int *pbUsageError );

转载自:https://blog.csdn.net/zhanghaoping/article/details/51627104

You may also like...