GDAL

  1. #!/usr/bin/env python3

1、Windows

Python

  1. ## conda : https://docs.conda.io/en/latest/miniconda.html
  2. conda install gdal

2、Centos 7.9

2.1、安装依赖

  1. yum update -y
  2. yum install gcc-c++ gcc libpng libtiff dnf tcl -y
  3. yum makecache
  4. sudo dnf makecache
  5. sudo dnf -y install tcl

2.2、安装sqlite 3.11.1

wget -c https://www.sqlite.org/cgi/src/tarball/f047920c/SQLite-f047920c.tar.gz

  1. tar -zxvf SQLite-f047920c.tar.gz
  2. cd SQLite-f047920c
  3. ./configure
  4. make
  5. make install

如果报错,找不到 tclsh

  1. yum install dnf -y
  2. yum makecache
  3. yum -y install tcl
  4. dnf makecache
  5. dnf -y install tcl

然后修改 sqlite3.c 文件,添加一个宏, 否则后面 gdal 编译不通过
  1. #define SQLITE_CORE 1
  2. #define SQLITE_AMALGAMATION 1
  3. #ifndef SQLITE_PRIVATE
  4. # define SQLITE_PRIVATE static
  5. #endif
  6. #define SQLITE_ENABLE_COLUMN_METADATA 1 // 添加的宏,25 行

再编译一次 sqlite
  1. ./configure
  2. make
  3. make install

配置环境变量
  1. vi /etc/profile
  2. ##### 添加内容 ####
  3. export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
  4. ########
  5. source /etc/profile ## 使配置文件生效

2.3、安装 proj

wget -c https://download.osgeo.org/proj/proj-6.3.2.tar.gz

  1. tar -zxvf proj-6.3.2.tar.gz
  2. cd proj-6.3.2
  3. ./configure
  4. make
  5. make install

2.4、安装 gdal

wget -c http://download.osgeo.org/gdal/3.4.1/gdal-3.4.1.tar.gz

  1. tar -zxvf gdal-3.4.1.tar.gz
  2. cd gdal-3.4.1
  3. ./configure
  4. make
  5. make install

3、proj_lib 与 postgis 冲突

  1. import os
  2. os.environ['PROJ_LIB'] = r'F:\ProgramData\Miniconda3\Library\share\proj'

4、vs2019 win10

image-20220428160822829.pngimage-20220428160839310.pngimage-20220428160915917.png

5 C++ 读写数据

  1. #include "iostream"
  2. #include "gdal_priv.h"
  3. #include "cpl_conv.h"
  4. #include "gdalwarper.h"
  5. #include "stdlib.h"
  6. #include "ogrsf_frmts.h"
  7. using namespace std;
  8. GDALDataset* createRaster(const char* filename, int nRow, int nCol, GDALDataType type){
  9. GDALDriver* driver = GetGDALDriverManager()->GetDriverByName("GTiff");
  10. GDALDataset* ds = driver->Create(
  11. filename,
  12. nRow,
  13. nCol,
  14. 1,//波段数
  15. type,
  16. NULL);
  17. return ds; //传回一个空的栅格数据集,但设置了它的一些基本参数
  18. }
  19. //读数据并输出保存
  20. int tiffread(const char* file_path_name, const char* dem_des_file){
  21. // 支持中文路径
  22. CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
  23. //注册所有的驱动
  24. GDALAllRegister();
  25. //GDAL数据集
  26. GDALDataset* poDataset;
  27. poDataset = (GDALDataset*)GDALOpen(file_path_name, GA_ReadOnly);
  28. if (poDataset == NULL){
  29. cout << "fail in open files!!!" << endl;
  30. return 0;
  31. }
  32. //获取图像的尺寸
  33. int nImgSizeX = poDataset->GetRasterXSize();
  34. int nImgSizeY = poDataset->GetRasterYSize();
  35. cout << nImgSizeX << " * " << nImgSizeY << endl;
  36. //获取坐标变换系数
  37. double trans[6];
  38. CPLErr aaa = poDataset->GetGeoTransform(trans);
  39. //获取图像波段
  40. GDALRasterBand* poBand;
  41. int bandcount;
  42. // 获取波段数:DEM为1个波段
  43. bandcount = poDataset->GetRasterCount();
  44. cout << "波段数:" << bandcount << endl;
  45. // DEM数据为单波段数据 该代码只适用于单通道数据读取和写
  46. //pafScanline为 int 型数组指针 用于存储dem数据
  47. short* pafScanline = new short[nImgSizeX * nImgSizeY];
  48. // 获取对应波段
  49. poBand = poDataset->GetRasterBand(bandcount);
  50. poBand->RasterIO(GF_Read, 0, 0, nImgSizeX, nImgSizeY, pafScanline,
  51. nImgSizeX, nImgSizeY, GDALDataType(poBand->GetRasterDataType()), 0, 0);
  52. GDALDataType type = poBand->GetRasterDataType();
  53. //创建空的栅格数据集
  54. GDALDataset* ods = createRaster(dem_des_file, nImgSizeX, nImgSizeY, type);
  55. //设置投影
  56. ods->SetProjection(poDataset->GetProjectionRef());
  57. //设置空间转换的六参数
  58. ods->SetGeoTransform(trans);
  59. GDALRasterBand* oBand = ods->GetRasterBand(1);
  60. oBand->RasterIO(
  61. GF_Write,
  62. 0, 0,
  63. nImgSizeX,
  64. nImgSizeY,
  65. //写入数据
  66. pafScanline,
  67. nImgSizeX,
  68. nImgSizeY,
  69. type,
  70. 0,
  71. 0
  72. );
  73. GDALClose(poDataset);
  74. GDALClose(ods);
  75. }
  76. int main()
  77. {
  78. const char* dem_src_file = "D:\\app_3d\\3d\\dem\\horizontal\\test.tif";
  79. const char* dem_des_file = "D:\\app_3d\\3d\\dem\\horizontal\\testtest.tif";
  80. tiffread(dem_src_file,dem_des_file);
  81. return 0;
  82. }