C/C++ programming tips using GDAL library - Common Portability Library (CPL)

C/C++ programming tips using GDAL library - Common Portability Library (CPL)

Introduction to CPL

GDAL stands for Geospatial Data Abstraction Library (Geospatial Data Abstraction Library). It is a powerful geographic raster spatial data conversion library that supports numerous raster and vector geographic spatial data formats. It has good cross-platform performance and provides C/C++, python APIs in other languages ​​are easy to use.
CPL (Common Portability Library), that is, the general portability library, is one of the components of GDAL, which is used to enhance the portability of GDAL. Users only need to call the functions without changing the code for different platforms, such as creating folders, obtaining the current program running path, and so on.

Summary of common functions

Most of the functions of CPL can be used after including gdal_priv.hand in the code gdal_utils.h:

CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");/// 默认文件名字符串的编码是UTF-8不支持中文路径,设置此行代码后支持中文路径
char strDir[256];
CPLGetExecPath(strDir, 256);// 获取当前exe全路径
const char* path = CPLGetPath(strDir);// 从输入字符串提取(文件夹)路径,xxx\yyy.tif提取为xxx
string strInputImg;
cin>>strInputImg;
const char* pExtIn = CPLGetExtension(strInputImg.c_str());// 获取文件扩展名,不带'.'.
char strOutDir[256];
strcpy(strOutDir, "E:/new_dir/new_subdir");
VSIMkdirRecursive(strOutDir, 0755);// 递归地创建文件夹,即如果父文件夹不存在,亦创建之。第二个参数是文件夹访问权限,默认给0755即可。
VSIStatBufL stat;
if (VSIStatExL(strInputImg, &stat, VSI_STAT_EXISTS_FLAG)==-1)//VSIStatExL函数用于获取文件的状态信息,包括修改时间、文件大小等,获取失败时也即文件不存在,返回-1
{
    
    
    return;//如果文件不存在
    CPLprintf("File %s not exist.", strInputImg.c_str());// CPLprintf用法完全同printf
}
string strOutputImg;
cin>>strOutputImg;
CPLCopyFile(strOutputImg, strInputImg);//拷贝文件 注意第一个参数是新文件路径,第二个参数是原始文件路径

cpl_For more interfaces, you can also view multiple header files beginning with , such as cpl_conv.h, cpl_port.h, cpl_vsi.hetc. under the include path of the GDAL library .
For more usage, please refer to the Chinese documentation .

PS: The function of GDAL is too powerful, and many things need to be learned from the documentation , try it yourself , and ask Google .

Guess you like

Origin blog.csdn.net/qq_42679415/article/details/132650615