GDAL
#!/usr/bin/env python3
1、Windows
Python
## conda : https://docs.conda.io/en/latest/miniconda.html
conda install gdal
2、Centos 7.9
2.1、安装依赖
yum update -y
yum install gcc-c++ gcc libpng libtiff dnf tcl -y
yum makecache
sudo dnf makecache
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
tar -zxvf SQLite-f047920c.tar.gz
cd SQLite-f047920c
./configure
make
make install
如果报错,找不到 tclsh
yum install dnf -y
yum makecache
yum -y install tcl
dnf makecache
dnf -y install tcl
然后修改 sqlite3.c 文件,添加一个宏, 否则后面 gdal 编译不通过
#define SQLITE_CORE 1
#define SQLITE_AMALGAMATION 1
#ifndef SQLITE_PRIVATE
# define SQLITE_PRIVATE static
#endif
#define SQLITE_ENABLE_COLUMN_METADATA 1 // 添加的宏,25 行
再编译一次 sqlite
./configure
make
make install
配置环境变量
vi /etc/profile
##### 添加内容 ####
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
########
source /etc/profile ## 使配置文件生效
2.3、安装 proj
tar -zxvf proj-6.3.2.tar.gz
cd proj-6.3.2
./configure
make
make install
2.4、安装 gdal
wget -c http://download.osgeo.org/gdal/3.4.1/gdal-3.4.1.tar.gz
tar -zxvf gdal-3.4.1.tar.gz
cd gdal-3.4.1
./configure
make
make install
3、proj_lib 与 postgis 冲突
import os
os.environ['PROJ_LIB'] = r'F:\ProgramData\Miniconda3\Library\share\proj'
4、vs2019 win10
5 C++ 读写数据
#include "iostream"
#include "gdal_priv.h"
#include "cpl_conv.h"
#include "gdalwarper.h"
#include "stdlib.h"
#include "ogrsf_frmts.h"
using namespace std;
GDALDataset* createRaster(const char* filename, int nRow, int nCol, GDALDataType type){
GDALDriver* driver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDataset* ds = driver->Create(
filename,
nRow,
nCol,
1,//波段数
type,
NULL);
return ds; //传回一个空的栅格数据集,但设置了它的一些基本参数
}
//读数据并输出保存
int tiffread(const char* file_path_name, const char* dem_des_file){
// 支持中文路径
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
//注册所有的驱动
GDALAllRegister();
//GDAL数据集
GDALDataset* poDataset;
poDataset = (GDALDataset*)GDALOpen(file_path_name, GA_ReadOnly);
if (poDataset == NULL){
cout << "fail in open files!!!" << endl;
return 0;
}
//获取图像的尺寸
int nImgSizeX = poDataset->GetRasterXSize();
int nImgSizeY = poDataset->GetRasterYSize();
cout << nImgSizeX << " * " << nImgSizeY << endl;
//获取坐标变换系数
double trans[6];
CPLErr aaa = poDataset->GetGeoTransform(trans);
//获取图像波段
GDALRasterBand* poBand;
int bandcount;
// 获取波段数:DEM为1个波段
bandcount = poDataset->GetRasterCount();
cout << "波段数:" << bandcount << endl;
// DEM数据为单波段数据 该代码只适用于单通道数据读取和写
//pafScanline为 int 型数组指针 用于存储dem数据
short* pafScanline = new short[nImgSizeX * nImgSizeY];
// 获取对应波段
poBand = poDataset->GetRasterBand(bandcount);
poBand->RasterIO(GF_Read, 0, 0, nImgSizeX, nImgSizeY, pafScanline,
nImgSizeX, nImgSizeY, GDALDataType(poBand->GetRasterDataType()), 0, 0);
GDALDataType type = poBand->GetRasterDataType();
//创建空的栅格数据集
GDALDataset* ods = createRaster(dem_des_file, nImgSizeX, nImgSizeY, type);
//设置投影
ods->SetProjection(poDataset->GetProjectionRef());
//设置空间转换的六参数
ods->SetGeoTransform(trans);
GDALRasterBand* oBand = ods->GetRasterBand(1);
oBand->RasterIO(
GF_Write,
0, 0,
nImgSizeX,
nImgSizeY,
//写入数据
pafScanline,
nImgSizeX,
nImgSizeY,
type,
0,
0
);
GDALClose(poDataset);
GDALClose(ods);
}
int main()
{
const char* dem_src_file = "D:\\app_3d\\3d\\dem\\horizontal\\test.tif";
const char* dem_des_file = "D:\\app_3d\\3d\\dem\\horizontal\\testtest.tif";
tiffread(dem_src_file,dem_des_file);
return 0;
}