从头安装gdal库(Linux环境下的Python版)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

前言

大多数情况下都是可以直接pip wheel安装成功的但有些特殊情况下比如一个容器内需要从头配置gdal库。我“有幸”亲自体验了一把整个过程** “痛并快乐着” **。整个过程查了很多资料中间也遇到了很多问题部分在网上没有找到相关答案整个过程全网也没有一个类似的教程因此记录一下留给有需要的程序猿/程序媛。

整个思路就是风来将挡、水来土掩。首先安装GDAL不出意外的话会报错需要安装PROJ6。然后安装PROJ6不出意外的话就会报错需要安装sqlite3、pkg-config和libtiff-4。一般上述安装完成基本上没有太大问题但如果你是一个缺失各种环境和包的容器不出意外的话又会报错比如还需要安装liblzma、libjpeg和zlib。

要构建 GDAL 3 或更高版本至少需要一个 C++11 兼容编译器以及 PROJ 6 或更高版本。如果是在C++11编译器上构建老版本GDAL会提示namespace std错误。新版本需要安装PROJ依赖库PROJ又依赖sqlite3和libtiff4依次安装即可。具体情况可能不同不过可以从GDAL开始安装根据提示逐渐安装相应的包即可。
————————————————
原文链接https://blog.csdn.net/wokaowokaowokao12345/article/details/126644466

GDAL安装

下载地址http://download.osgeo.org/gdal/
选择合适的版本。我这里以3.3.3版本为例。

# 新建临时文件夹存储
mkdir /temp
cd temp
# 开始安装
wget http://download.osgeo.org/gdal/3.3.3/gdal-3.3.3.tar.gz  # 下载安装包
tar -xvzf gdal-3.3.3.tar.gz  # 解压
cd gdal-3.3.3
# 编译安装
./configure --with-python='/usr/bin/python3.7'  # 可通过which python3.7查看
make -j8  # -j为多线程执行可通过make --help查看
make install

# 构建python包
cd swig/python/
python setup.py build
python setup.py install


ldconfig  # 更新动态链接库
gdalinfo --version

这里使用./configure --with-python='/usr/bin/python3.7'配置时会出现如下所示的结果
注意查看SWIG bindings的状态如果是no就需要安装SWIG才能构建python包安装完后再次配置该状态会变为python。
安装完SWIG后再次编译之前最好清除之前编译的文件
make distclean
Github/gdal issues里面有相关问题的提问但没有解决方法不过给了一些优化的方法可以参考
https://github.com/OSGeo/gdal/issues/1199
在这里插入图片描述
在这里插入图片描述

报错

collect2: error: ld returned 1 exit status
GNUmakefile:90: recipe for target 'gdalinfo' failed
make[1]: *** [gdalinfo] Error 1
make[1]: Leaving directory '/temp/gdal-3.3.3/apps'
GNUmakefile:120: recipe for target 'apps-target' failed
make: *** [apps-target] Error 2

在这里插入图片描述
这个没有解决不过测试的Gdal已经可以正常使用。有巨佬知道的话欢迎留言指导

SWIG安装

下载地址https://sourceforge.net/projects/swig/files/swig/
https://sourceforge.net/projects/swig/files/swig/swig-4.1.0/swig-4.1.0.tar.gz
安装流程

cd temp
wget https://sourceforge.net/projects/swig/files/swig/swig-4.1.0/swig-4.1.0.tar.gz
tar -xvzf swig-4.1.0.tar.gz
# 编译安装
cd swig-4.1.0
./configure
make
make install

安装完成后使用swig -version测试安装是否成果。下图表示安装成功。
在这里插入图片描述

报错Cannot find pcre2-config script from PCRE2 (Perl Compatible Regular Expressions) library package. This dependency is needed for configure to complete,
在这里插入图片描述
这个下面已经给出了解决办法4种方法最简单的是./configure --without-pcre
安装不安装都行这个是prel语言包没有安装再次编译时会提示The SWIG test-suite and examples are configured for the following languages: perl5
安装方法如下
perl5安装

wget https://www.cpan.org/src/5.0/perl-5.28.0.tar.gz
tar -xzf perl-5.28.0.tar.gz
cd perl-5.28.0
./Configure -des -Dprefix=$HOME/localperl  # 注意大写C
make 
make test
make install

proj 安装

官方下载地址https://proj.org/download.html
proj-8.1.1.tar.gz

cd temp
wget https://download.osgeo.org/proj/proj-8.1.1.tar.gz
tar -xvzf proj-8.1.1.tar.gz
# 编译安装
cd proj-8.1.1
./configure
make
make install

报错
在这里插入图片描述

sqlite安装

下载地址https://www.sqlite.org/download.html

wget https://www.sqlite.org/snapshot/sqlite-snapshot-202301131932.tar.gz
# 解压
tar -xvzf sqlite-snapshot-202301131932.tar.gz
编译安装
cd sqlite-snapshot-202301131932
./configure
make
make install

在这里插入图片描述
测试

sqlite3 student
.exit

弹出如图所示表示安装成功。
在这里插入图片描述

pkg-config 安装

下载地址https://lists.freedesktop.org/archives/pkg-config/2017-March/001084.html

wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
tar -xvzf pkg-config-0.29.2.tar.gz
# 编译安装
cd pkg-config-0.29.2
./configure 
make
make check
make install

make check 结果
编译完成结果
报错
在这里插入图片描述将./configure更改为下面的命令即可。

./configure --with-internal-glib

参考https://blog.csdn.net/wxh0000mm/article/details/122322486


其他报错

出现Package '****', required by 'libtiff-4', not found 基本上都是这个包缺失下载编译即可解决。

No package ‘libtiff-4’ found

在这里插入图片描述
解决办法安装libtiff-4包

下载地址https://www.linuxfromscratch.org/blfs/view/svn/general/libtiff.html

wget https://download.osgeo.org/libtiff/tiff-4.5.0.tar.gz
tar -zxvf tiff-4.5.0.tar.gz
cd tiff-4.5.0
./configure
make
make install

Package ‘liblzma’, required by ‘libtiff-4’, not found

liblzma缺失liblzma库是xz utils的子集所以直接安装xz就可以解决。

wget http://tukaani.org/xz/xz-5.2.2.tar.gz
tar -xzvf xz-5.2.2.tar.gz
cd xz-5.2.2
./configure
make
make install

Package ‘libjpeg’, required by ‘libtiff-4’, not found

在这里插入图片描述
下载地址http://www.ijg.org/
http://www.ijg.org/files/jpegsrc.v9e.tar.gz

wget http://www.ijg.org/files/jpegsrc.v9e.tar.gz
tar -xzvf xz-5.2.2.tar.gz
cd xz-5.2.2
./configure
make
make install

Package ‘zlib’, required by ‘libtiff-4’, not found

在这里插入图片描述
下载地址http://www.zlib.net/
http://www.zlib.net/zlib-1.2.13.tar.gz

wget http://www.zlib.net/zlib-1.2.13.tar.gz
tar -xzvf zlib-1.2.13
cd zlib-1.2.13
./configure
make
make install

checking for curl-config… not-found

checking for curl-config… not-found
configure: error: curl not found. If wanting to do a build without curl support (and thus without built-in networking capability), explictly disable it with --without-curl
在这里插入图片描述
这里看需要有需要的先安装curl然后重新安装PRO。对我来说不重要所以直接忽略。
./configure --without-curl

configure: error: PROJ 6 symbols not found

1、安装PROJ 6以上的版本。安装教程proj 安装
2、如果安装过上面的还报错可能是之前使用apt install libproj-dev使用apt remove libproj-dev卸载问题便解决。

参考链接

Ubuntu 安装 GDAL C++库
linux 安装GDAL (python)
gdal-3.1.2 linux(Ubuntu) 编译 附SQLite3 PROJ6编译

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: linuxpython