Linux(CentOS)编译安装GDAL3.0.4+Postgresql 12.2+Postgis-3.0.1
有需要GDAL开发的同学,在linux服务器上,最好还是自己编译环境,初次编译是很痛苦,但是在此过程中,对插件之间的版本依赖关系,插件编译配置、问题定位,解决问题,shell脚本都能够全面的提升。最后还能得到一个最新版的环境,何乐而不为了!!!
一、环境准备
1、环境要求
Centos系统可以访问外网。
Centos系统版本:CentOS Linux release 7.3.1611 (Core)
需要安装的软件如下:
apache-ant-1.10.12-bin.tar.gz
cgal-4.14.tar.xz
cmake-3.20.6.tar.gz
gdal-3.0.4.tar.gz
geos-3.8.0.tar.bz2
gmp-6.1.2.tar.bz2
json-c-json-c-0.13.1-20180305.tar.gz
libxml2-2.9.10.tar.gz
mpfr-4.0.2.tar.gz
pcre-8.44.tar.gz
postgis-3.0.1.tar.gz
postgresql-12.2.tar.gz
proj-6.3.1.tar.gz
protobuf-c-1.3.3.tar.gz
protobuf-cpp-3.11.4.tar.gz
SFCGAL-1.3.7.tar.gz
sqlite-autoconf-3360000.tar.gz
swig-4.0.1.tar.gz
2、环境安装
先通过yum基础插件
yum -y install gcc gcc-c++ gcc-g77 make libtool flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel libxml2-devel gettext-devel pcre-devel
二、编译Postgresql-12.2
以下所有安装源文件放置均放在/usr/local/src/下
1、安装postgresql数据库
#用户组
groupadd postgres
#用户组添加用户
useradd -g postgres postgres
mkdir -p /usr/local/pgsql
mkdir /usr/local/pgsql/data
chown -R postgres:postgres /usr/local/pgsql
cd /usr/local/src/
wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz
tar -xzvf postgresql-12.2.tar.gz
cd postgresql-12.2
./configure --prefix=/usr/local/pgsql --without-readline
make && make install
./configure --prefix=/usr/local/pgsql --without-readline,效果:
make 效果:
make install 效果:
2、添加环境变量
vi打开配置文件后,按i插入,ESC退出编译,:wq保存,:q!不保存,以后每次保存后要刷新环境变量:source /etc/profile
vi /etc/profile
PG_HOME=/usr/local/pgsql
LD_LIBRARY_PATH=$PG_HOME/lib:$LD_LIBRARY_PATH
PATH=$PG_HOME/bin:$PATH
PKG_CONFIG_PATH=$PG_HOME/lib/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH LD_LIBRARY_PATH
source /etc/profile
查看Postgresql版本命令
pg_config --version
3、初始化数据库
#切换用户
su postgres
#初始化数据库
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
![image image](https://img-blog.csdnimg.cn/img_convert/a360d5685f7d76ed213ee099f60689ea.png)
4、设置远程可访问数据库
#切换root
exit
vi /usr/local/pgsql/data/postgresql.conf
listen_address = '*'
port = 5432
vi /usr/local/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 md5
host all all 192.168.1.0/24 md5
host all all ::1/128 ident
5、数据库服务
#切换用户
su postgres
#启动数据库服务器
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
#创建数据库
/usr/local/pgsql/bin/createdb gisdb
#停止数据库服务
/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data
#重新启动数据库
/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data
到这里,数据库已经安装成功了,使用navicat或dbeaver连接试试吧!
操作命令说明
pg_ctl stop -D /usr/local/pgsql/data/
pg_ctl start -D /usr/local/pgsql/data/
pg_ctl restart -D /usr/local/pgsql/data/
6、设置Postgresql自启动
/usr/lib/systemd/system 新建postgresql.service文件,复制以下内容到postgresql.service
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Port number for server to listen on
Environment=PGPORT=5432
# Location of database directory
Environment=PGDATA=/usr/local/pgsql/data
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
#ExecStartPre=/usr/local/pgsql/bin/postgresql-check-db-dir ${PGDATA}
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
su root
cd /usr/lib/systemd/system
chmod 754 postgresql.service
systemctl enable postgresql.service
7、其它一些操作命令说明
启动,停止,重启
systemctl start postgresql.service
systemctl stop postgresql.service
systemctl restart postgresql.service
修改数据库密码
su - postgres
psql -U postgres
ALTER USER postgres WITH PASSWORD '123456'
\q
三、编译GDAL依赖
CentOs7安装unzip
yum install -y unzip zip
1、安装 sqlite3
wget https://www.sqlite.org/2020/sqlite-autoconf-3360000.tar.gz
tar –zxvf sqlite-autoconf-3360000.tar.gz
cd sqlite-autoconf-3360000
bash ./configure --prefix=/usr/local/pgsql/plugin/sqlite3
make && make install
#软连接到/usr/bin目录
ln -s /usr/local/pgsql/plugin/sqlite3/bin/sqlite3 /usr/bin/sqlite3
echo "/usr/local/pgsql/plugin/sqlite3/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
#查看版本号
sqlite3 -version
bash ./configure --prefix=/usr/local/pgsql/plugin/sqlite3 命令结果:
make 命令结果:
make install 命令结果:
2、安装 json-c-0.13.1
wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
tar -xzvf json-c-0.13.1-20180305.tar.gz
cd json-c-json-c-0.13.1-20180305
bash ./configure --prefix=/usr/local/pgsql/plugin/json-c
make && make install
echo "/usr/local/pgsql/plugin/json-c/lib" > /etc/ld.so.conf.d/json-c-0.13.1.conf
ldconfig
bash ./configure --prefix=/usr/local/pgsql/plugin/json-c,命令效果:
make 命令效果:
make install 命令效果:
3、编译 protobuf-3.11.4,protobuf-c 1.3.3
编译protobuf-3.11.4
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protobuf-cpp-3.11.4.tar.gz
tar -xzvf protobuf-cpp-3.11.4.tar.gz
cd protobuf-3.11.4
bash ./configure
make && make install
bash ./configure 命令效果:
make 命令效果:
make install 命令效果:
编译protobuf-c 1.3.3
wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.3/protobuf-c-1.3.3.tar.gz
tar -xzvf protobuf-c-1.3.3.tar.gz
cd protobuf-c-1.3.3
bash ./configure
make && make install
bash ./configure 报错:
vi /etc/profile 添加配置
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH
source /etc/profile 配置生效。
.configure 命令正常效果:
make 命令效果:
make install 命令效果:
4、安装Proj 6.3.1
wget https://download.osgeo.org/proj/proj-6.3.1.tar.gz
tar -xf proj-6.3.1.tar.gz
cd proj-6.3.1
./configure --prefix=/usr/local/pgsql/plugin/proj
make && make install
echo "/usr/local/pgsql/plugin/proj/lib" > /etc/ld.so.conf.d/proj-6.3.1.conf
ldconfig
./configure --prefix=/usr/local/pgsql/plugin/proj 命令报错,提示sqlite3版本太低,必须大于等于3.11,当前版本为3.7.17
解决方法:升级sqlite版本。如果再出现以下提示,应该是在安装sqlite3时指定了安装目录,个别命令没有安装全面。
重新安装sqlite3
wget https://www.sqlite.org/2020/sqlite-autoconf-3360000.tar.gz
tar –zxvf sqlite-autoconf-3360000.tar.gz
cd sqlite-autoconf-3360000
./configure --prefix=/usr/local/pgsql/plugin/sqlite3
make && make install
#查看版本号
sqlite3 -version
进入 /usr/local/src/proj-6.3.1目录, ./configure --prefix=/usr/local/pgsql/plugin/proj 命令正常效果:
make 命令效果:
make install 命令效果:
5、安装GEOS 3.8.0
wget http://download.osgeo.org/geos/geos-3.8.0.tar.bz2
tar -jxf geos-3.8.0.tar.bz2
cd geos-3.8.0
./configure --prefix=/usr/local/pgsql/plugin/geos
make && make install
echo "/usr/local/pgsql/plugin/geos/lib" > /etc/ld.so.conf.d/geos-3.8.0.conf
ldconfig
在解压 *.tar.bz2文件时报错如下,解决方法:yum -y install bzip2
./configure --prefix=/usr/local/pgsql/plugin/geos 命令效果:
make 命令效果:
make install 命令效果:
6、安装SFCGAL 1.3.7
由于SFCGAL需要依赖Boost、CGAL、GMP、MPFR这四个软件,所以具体总共需要安装以下四个软件:
boost-devel.x86_64
gmp-devel.x86_64
mpfr-devel.x86_64
CGAL-4.14
6.1 Boost-1.5.3
yum install boost boost-devel
6.2 GMP-6.1.2
wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2
tar -jxvf gmp-6.1.2.tar.bz2
cd gmp-6.1.2
bash ./configure --enable-cxx
make && make install
./configure --enable-cxx 命令效果:
make 命令效果:
make install 命令效果:
6.3 MPFR-4.0.2
wget https://www.mpfr.org/mpfr-current/mpfr-4.0.2.tar.gz
tar -xzvf mpfr-4.0.2.tar.gz
cd mpfr-4.0.2
./configure
make && make install
make install 命令效果:
6.4 cgal-4.14
wget http://distfiles.macports.org/cgal/cgal-4.14.tar.xz
xz -d cgal-4.14.tar.xz
tar xvf cgal-4.14.tar
cd CGAL-4.14
mkdir build && cd build
cmake ..
make && make install
cmake .. 命令提示未找到,具体安装步骤参考:https://blog.csdn.net/llwy1428/article/details/95473542
如果使用yum安装cmake,默认版本是:cmake version 2.8.12.2,在执行cmake ..命令时报以下错误
- 安装新版本cmake
wget https://cmake.org/files/v3.17/cmake-3.17.4.tar.gz
tar -zxvf cmake-3.17.4.tar.gz
cd cmake-3.17.4
./configure --prefix=/usr/local/cmake
make
make install
#创建链接
ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
- 修改环境变量,查看版本
vim /etc/profile
export CMAKE_HOME=/usr/local/cmake
export PATH=$PATH:$CMAKE_HOME/bin
#使配置生效
source /etc/profile
#查看版本
cmake –version
接下来继续安装cgal-4.14,进入/usr/local/src/CGAL-4.14/build,执行cmake .. 命令正常效果:
make 命令效果:
make install 命令效果:
安装过程中可能会出现以下错误
解决方法: 修改/usr/include/boost/ cstdint.hpp 44行代码为:
#if defined(BOOST_HAS_STDINT_H) \
&& (!defined(__GLIBC__) \
|| defined(__GLIBC_HAVE_LONG_LONG) \
|| (defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 17)))))
6.5 SFCGAL 1.3.7
wget https://github.com/Oslandia/SFCGAL/archive/v1.3.7.tar.gz
tar -zxvf SFCGAL-1.3.7.tar.gz
cd SFCGAL-1.3.7
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal ..
make && make install
echo "/usr/local/sfcgal/lib64" > /etc/ld.so.conf.d/sfcgal-1.3.7.conf
ldconfig
make 命令效果:
make install 命令效果:
7、pcre-8.44安装
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
tar -xzvf pcre-8.44.tar.gz
cd pcre-8.44
./configure --enable-utf8 --prefix=/usr/local/pcre
make && make intall
echo "/usr/local/pcre/lib" > /etc/ld.so.conf.d/pcre-8.44.conf
ldconfig
./configure --enable-utf8 --prefix=/usr/local/pcre 命令效果:
make 命令效果:
make install 命令效果:
8、libxml2-2.9.10安装
wget http://xmlsoft.org/sources/libxml2-2.9.10.tar.gz
tar -xzvf libxml2-2.9.10.tar.gz
cd libxml2-2.9.10
./configure --prefix=/usr/local/libxml2
make && make intall
echo "/usr/local/libxml2/lib" > /etc/ld.so.conf.d/libxml2-2.9.10.conf
ldconfig
make命令,遇到错误提示“libxml.c:14:20: fatal error: Python.h: No such file or directory”
解决方法如下:
# 可以先查看一下含python-devel的包
yum search python | grep python-devel
# 64位安装python-devel.x86_64,32位安装python-devel.i686,我这里安装:
sudo yum install python-devel.x86_64
# 进入/usr/include/python2.7看一下现在有没有Python.h,版本不同目录名不同,我这里是2.7版本。
# 其实也可以看到很多.h文件,python需要库或头文件都在这个地方。
make 命令正常效果:
make install 命令效果:
9、编译gdal.jar进行 java开发,不需要开发,可以跳过
9.1、 ant 1.10.12
wget https://mirrors.tuna.tsinghua.edu.cn/apache//ant/binaries/apache-ant-1.10.12-bin.tar.gz
tar -zxvf apache-ant-1.10.12-bin.tar.gz
mv apache-ant-1.10.12 /usr/local/ant
9.2、swig 4.0.1
wget http://prdownloads.sourceforge.net/swig/swig-4.0.1.tar.gz
tar -zxvf swig-4.0.1.tar.gz
cd swig-4.0.1
bash ./configure --prefix=/usr/local/swig --without-pcre
make && make install
./configure --prefix=/usr/local/swig --without-pcre 命令效果:
make 命令效果:
make install 命令效果:
9.3、配置profile
vim /etc/profile
ANT_HOME=/usr/local/ant
SWIG_HOME=/usr/local/swig
PATH=$ANT_HOME/bin:$SWIG_HOME/bin:$PATH
export ANT_HOME SWIG_HOME
source /etc/profile
查看版本
ant –version
swig –version
四、安装GDAL 3.0.4
1、编译gdal
wget http://download.osgeo.org/gdal/3.0.4/gdal-3.0.4.tar.gz
tar -xf gdal-3.0.4.tar.gz
cd gdal-3.0.4
bash ./configure --prefix=/usr/local/pgsql/plugin/gdal --with-proj=/usr/local/pgsql/plugin/proj --with-geos=/usr/local/pgsql/plugin/geos/bin/geos-config --with-libjson-c=/usr/local/pgsql/plugin/json-c
make && make install
echo "/usr/local/pgsql/plugin/gdal/lib" > /etc/ld.so.conf.d/gdal-3.0.4.conf
ldconfig
make 命令效果:
make install 命令效果:
2、配置GDAL环境变量
通过vi /etc/profile去配置gdal/lib、gdal/bin、gdal/data吧,也可以把profile下载到本地改完再上传
GDAL_HOME=/usr/local/pgsql/plugin/gdal
GDAL_DATA=$GDAL_HOME/share/gdal
LD_LIBRARY_PATH=$GDAL_HOME/lib:/usr/local/lib64:$JRE_HOME/lib:$LD_LIBRARY_PATH
PATH=$GDAL_HOME/bin:$PATH
export PATH LD_LIBRARY_PATH GDAL_DATA
3、GDAL安装完成,看看版本号和支持的数据格式吧
gdalinfo –version
ogr2ogr --formats
- 如只编译GDAL的同学,下面的内容可以不用看了,安装PostGIS的同学,请继续
五、编译安装PostGIS
1、编译
wget https://download.osgeo.org/postgis/source/postgis-3.0.1.tar.gz
tar -xvzf postgis-3.0.1.tar.gz
cd postgis-3.0.1
./configure --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-geosconfig=/usr/local/pgsql/plugin/geos/bin/geos-config --with-projdir=/usr/local/pgsql/plugin/proj --with-gdalconfig=/usr/local/pgsql/plugin/gdal/bin/gdal-config --with-jsondir=/usr/local/pgsql/plugin/json-c --with-pcredir=/usr/local/pcre
make && make install
./configure 命令效果:
make 命令效果:
make install 命令效果:
如果编译安装PostGIS报错:
undefined reference to `sqlite3_column_origin_name',解决链接参考:https://www.cnblogs.com/xuanmanstein/p/13227545.html
具体做法是在源码文件夹下
cd /usr/local/src/sqlite-autoconf-3360000
vi ./sqlite3.c
添加
#define SQLITE_CORE 1
#define SQLITE_AMALGAMATION 1
#ifndef SQLITE_PRIVATE
# define SQLITE_PRIVATE static
#endif
![image image](https://img-blog.csdnimg.cn/img_convert/7cf1f611878c476dd54b07b3f79ec10c.png)
然后 再
./configure
make
make install
2、安装fuzzystrmatch扩展
cd /usr/local/src/postgresql-12.2/contrib/fuzzystrmatch
make && make install
make 命令效果:
make install 命令效果:
3、创建PostGiS数据库
- 为了PostGIS插件生效,停止启动服务
systemctl stop postgresql.service
systemctl start postgresql.service
- 创建数据库
#切换postgres用户
su postgres
#登录PG数据库
psql
# 创建数据库
create database postgis;
#切换到postgis库中
\c postgis
#显示一下扩展模块
\dx
4、创建Postgresql扩展
create extension postgis;
create extension postgis_topology;
create extension fuzzystrmatch;
create extension postgis_tiger_geocoder;
4.1、错误
create extension postgis; 报错如下:
could not load library "/usr/local/pgsql/lib/postgis-3.so": libprotobuf-c.so.1
vi /etc/ld.so.conf
#依赖库比较多时,就直接把依赖库lib加入系统共享库,如
/usr/local/lib
ldconfig
重新启动postgresql数据库。
4.2 扩展成功创建
六、发布gdal项目、读取TIF高程文件,根据经纬度获取海拔
1、必须正常执行以下步骤
三、编译GDAL依赖
四、编译GDAL 3.0.4
2、编译生成gdal.jar
2.1 配置java.opt
修改文件:/usr/local/src/gdal-3.0.4/swig/java/java.opt,修改JAVA_HOME的值为java的根目录
JAVA_HOME = /usr/local/jdk1.8.0_281
2.2 编译
cd /usr/local/src/gdal-3.0.4/swig/java
make && make install
3、JavaWeb应用的 gdal环境配置
3.1、整理文件,把生成的文件放置到gdal/java目录
mkdir -p /usr/local/pgsql/plugin/gdal/java
cp /usr/local/src/gdal-3.0.4/swig/java/gdal.jar /usr/local/pgsql/plugin/gdal/java
cp /usr/local/src/gdal-3.0.4/swig/java/.libs/*.so /usr/local/pgsql/plugin/gdal/java
3.2、在centos下, java.library.path/usr/local/jdk1.8.0_281/lib/amd64,放置libgdalalljni.so到此目录,javaWeb项目从这里加载libgdalalljni.so动态库
cp /usr/local/src/gdal-3.0.4/swig/java/.libs/*.so /usr/local/jdk1.8.0_281/lib/amd64
3.3、把/swig/java中生成的gdal.jar文件复制到jre/lib/ext,jvm启动加载
cp /usr/local/src/gdal-3.0.4/swig/java/gdal.jar /usr/local/jdk1.8.0_281/jre/lib/ext
vi /etc/profile
CLASS_PATH=$JAVA_HOME/jre/lib/ext/gdal.jar:$CLASS_PATH
source /etc/profile
3.4、在/usr/local/gdal-3.0.4/swig/java/目录下复制gdal.jar、libgdalalljni.so文件到项目resources/gdal/linux
4、启动JavaWeb看看吧
- 载入动态库libgdalalljni.so
http://192.168.1.82:8080/gdal/api/gdal/load
- 注册gdal驱动
http://192.168.1.82:8080/gdal/api/gdal/register
- shp转geojson
http://192.168.1.82:8080/gdal/api/gdal/test/shp
- 根据经纬度查询海拔
http://192.168.1.82:8080/gdal/api/gdal/selectAltitude?lon=101.971729&lat=38.246638
5、可能出现的问题
错误1: java.lang.UnsatisfiedLinkError: Native Library /usr/java/packages/lib/amd64/libgdalalljni.so already loaded in another classloader
jvm启动时已经加载过libgdalalljni.so,不能能再次加载了,在linux系统上无需代码加载libgdalalljni.so
- 错误2:libpq.so.5: cannot open shared object file: No such file or directory 原因是未设置pg库到系统动态库配置请设置ld.so.conf
echo '/usr/local/pgsql/lib' >> /etc/ld.so.conf
ldconfig
- 错误3:nested exception is java.lang.UnsatisfiedLinkError: org.gdal.ogr.ogrJNI.RegisterAll()V
gdal.jar未放置到/usr/local/jdk1.8.0_281/jre/lib/ext
vi /etc/profile
CLASS_PATH=$JAVA_HOME/jre/lib/ext/gdal.jar:$CLASS_PATH
source /etc/profile