CentOS 7源码安装PostGIS(包含SFCGAL,PgRouting)

本文认为已安装PostgreSQL,安装步骤如 Centos7安装PostgreSQL,最好按照前文先把pg安装好,否则,在postgis,pgrouting安装时,指定pg的安装目录,直接抄路径应该不对,读者要指向自己的安装位置等。
注意,作者将Pg直接安装到了postgres用户下如下图:

Postgresql数据库安装全目录

本文将PostGIS及其外围用于三维的SFCGAL,用于网络分析的PgRouting安装都顺带上,安装步骤最好按照作者的说明安装,以免产生不可预测问题,毕竟坑我都趟过了。。。

一 安装gcc

[root@VM_0_4_centos ~]#  yum install gcc
[root@VM_0_4_centos ~]#  yum install gcc-c++   

二 编译GEOS

[root@VM_0_4_centos ~]# cd /opt
[root@VM_0_4_centos opt]# wget https://download.osgeo.org/geos/geos-3.7.0.tar.bz2
[root@VM_0_4_centos opt]# tar -jxvf geos-3.7.0.tar.bz2     
[root@VM_0_4_centos opt]# cd geos-3.7.0    
[root@VM_0_4_centos geos-3.7.0]# ./configure --prefix=/usr/local/geos-3.7.0
[root@VM_0_4_centos geos-3.7.0]# make
[root@VM_0_4_centos geos-3.7.0]# make install    

如果有以下问题:

  • 问题1:
g++: command not found

于是直接yum先装上再说:

yum install gcc-c++
  • 问题2:
error:#error "Can not compile without isnan function or macro"
报错截图.png

解决方法:
在geos解压的文件中,找到/include/config.h,编辑该文件,取消#undef HAVE_ISNAN的注释,保存退出,然后重新编译geos即可。


config.h文件目录位置

取消对HAVE_ISNAN的注释

三 编译Proj

[root@VM_0_4_centos opt]#  wget http://download.osgeo.org/proj/proj-5.2.0.tar.gz    
[root@VM_0_4_centos opt]# tar -zxvf proj-5.2.0.tar.gz    
[root@VM_0_4_centos opt]# cd proj-5.2.0    
[root@VM_0_4_centos proj-5.2.0]# ./configure  --prefix=/usr/local/proj-5.2.0
[root@VM_0_4_centos proj-5.2.0]# make    
[root@VM_0_4_centos proj-5.2.0]# make install   

四 编译GDAL

[root@VM_0_4_centos opt]#  wget https://download.osgeo.org/gdal/2.3.2/gdal-2.3.2.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf gdal-2.3.2.tar.gz 
[root@VM_0_4_centos opt]# cd gdal-2.3.2    
#编译时间比较久
[root@VM_0_4_centos gdal-2.3.2]# ./configure  --prefix=/usr/local/gdal-2.3.2 --with-pg=/home/postgres/bin/pg_config    
[root@VM_0_4_centos gdal-2.3.2]# make    
[root@VM_0_4_centos gdal-2.3.2]# make install   

五 编译json-c

[root@VM_0_4_centos opt]#  wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf json-c-0.13.1-20180305.tar.gz    
[root@VM_0_4_centos opt]# cd json-c-0.13.1-20180305    
[root@VM_0_4_centos json-c-0.13.1-20180305]# ./configure  --prefix=/usr/local/json-c-0.13.1
[root@VM_0_4_centos json-c-0.13.1-20180305]# make    
[root@VM_0_4_centos json-c-0.13.1-20180305]# make install   

六 编译libxml2

[root@VM_0_4_centos opt]#  wget https://github.com/GNOME/libxml2/archive/v2.9.7.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf libxml2-sources-2.9.7.tar.gz    
[root@VM_0_4_centos opt]# cd libxml2-2.9.7   
[root@VM_0_4_centos libxml2-2.9.7]# ./configure  --prefix=/usr/local/libxml2-2.9.7
[root@VM_0_4_centos libxml2-2.9.7]# make    
[root@VM_0_4_centos libxml2-2.9.7]# make install   

七 安装protobuf(可选安装,支持MVT等格式)

protobuf-c依赖protobuf,所以先安装protobuf。

[root@VM_0_4_centos opt]#  wget https://github.com/protocolbuffers/protobuf/archive/v2.6.1.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf protobuf-2.6.1.tar.gz    
[root@VM_0_4_centos opt]# cd protobuf-2.6.1  
[root@VM_0_4_centos protobuf-2.6.1]# ./configure  --prefix=/usr/local/protobuf-2.6.1
[root@VM_0_4_centos protobuf-2.6.1]# make    
[root@VM_0_4_centos protobuf-2.6.1]# make install   
#配置环境变量,增加下protobuf-2.6.1/bin
[root@VM_0_4_centos protobuf-2.6.1]# vi /etc/profile
export CMAKE_HOME=/usr/local/cmake-3.12.4
export PROTOBUF_HOME=/usr/local/protobuf-2.6.1
export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH
#保存退出
[root@VM_0_4_centos protobuf-2.6.1]# source /etc/profile
#验证protobuf执行程序
[root@VM_0_4_centos protobuf-2.6.1]# protoc --version
libprotoc 2.6.1
#安装成功

八 安装protobuf-c(可选安装,支持MVT等格式)

[root@VM_0_4_centos opt]#  wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.1/protobuf-c-1.3.1.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf protobuf-c-1.3.1.tar.gz    
[root@VM_0_4_centos opt]# cd protobuf-c-1.3.1  
#导入protobuf的pkgconfig,否则"--No package 'protobuf' found"
[root@VM_0_4_centos protobuf-c-1.3.1]# export PKG_CONFIG_PATH=/usr/local/protobuf-2.6.1/lib/pkgconfig
[root@VM_0_4_centos protobuf-c-1.3.1]# ./configure  --prefix=/usr/local/protobuf-c-1.3.1
[root@VM_0_4_centos protobuf-c-1.3.1]# make    
[root@VM_0_4_centos protobuf-c-1.3.1]# make install   

#配置环境变量,增加下protobuf-c-1.3.1/bin
[root@VM_0_4_centos protobuf-2.6.1]# vi /etc/profile
export CMAKE_HOME=/usr/local/cmake-3.12.4
export PROTOBUF_HOME=/usr/local/protobuf-2.6.1
export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:/usr/local/protobuf-c-1.3.1/bin:$PATH
#保存退出
[root@VM_0_4_centos protobuf-2.6.1]# source /etc/profile

九 安装cgal(可选)

sfcgal,pgrouting都依赖boost,cgal,作者实际掉坑了很多次这个东西,boost,cgal安装编译都成功,就是无法编译sfcgal,或者编译好了pgrouting却无法使用,经过一个月的不停尝试,找出问题就是boost,cgal这两个坑爹东西的问题,只要记住一点,只安装到默认路径,不要指定路径,否则坑死活不了。

#boost
[root@VM_0_4_centos opt]#  yum install boost-devel
# 安装指定版本cgal-4.13
[root@VM_0_4_centos opt]# wget https://github.com/CGAL/cgal/archive/releases/CGAL-4.13.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf CGAL-4.13.tar.gz   
[root@VM_0_4_centos opt]# cd CGAL-4.13  
[root@VM_0_4_centos CGAL-4.13]#mkdir build & cd build 
#cmake不要指定安装路径
[root@VM_0_4_centos build]# cmake ..
[root@VM_0_4_centos build]# make    
[root@VM_0_4_centos build]# make install   

九 安装sfcgal(可选安装,支持三维数据分析)

[root@VM_0_4_centos opt]#  wget https://github.com/Oslandia/SFCGAL/archive/v1.3.6.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf v1.3.6.tar.gz   
[root@VM_0_4_centos opt]# cd SFCGAL-1.3.6  
[root@VM_0_4_centos SFCGAL-1.3.6]#mkdir build & cd build 
[root@VM_0_4_centos build]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal-1.3.6 ..
[root@VM_0_4_centos build]# make    
[root@VM_0_4_centos build]# make install   

十 安装PostGIS

10.1 配置ld.so.conf

[root@VM_0_4_centos opt]# vim /etc/ld.so.conf
#编辑内容如下
include ld.so.conf.d/*.conf
/home/postgres/lib
/usr/local/proj-5.2.0/lib
/usr/local/gdal-2.3.2/lib
/usr/local/geos-3.7.0/lib
/usr/local//sfcgal-1.3.6/lib64
/usr/local/json-c-0.13.1/lib
/usr/local/libxml2-2.9.7/lib
/usr/local/protobuf-2.6.1/lib
/usr/local/protobuf-c-1.3.1/lib
#编辑完成后wq!保存退出
#保存配置,重启生效
[root@VM_0_4_centos opt]# ldconfig -v 

10.2 编译安装PostGIS

[root@VM_0_4_centos opt]# wget http://download.osgeo.org/postgis/source/postgis-2.5.0.tar.gz    
[root@VM_0_4_centos opt]# tar -zxvf postgis-2.5.0.tar.gz    
[root@VM_0_4_centos opt]# cd postgis-2.5.0 
#根据安装不同的要求,选择任意一个configure
#基本安装,不带protobuf,sfcgal
[root@VM_0_4_centos postgis-2.5.0]# ./configure --prefix=/home/postgres --with-gdalconfig=/usr/local/gdal-2.3.2/bin/gdal-config --with-pgconfig=/home/postgres/bin/pg_config --with-geosconfig=/usr/local/geos-3.7.0/bin/geos-config --with-projdir=/usr/local/proj-5.2.0 --with-xml2config=/usr/local/libxml2-2.9.7/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1
#带protobuf安装,支持mvt
[root@VM_0_4_centos postgis-2.5.0]# ./configure --prefix=/home/postgres --with-gdalconfig=/usr/local/gdal-2.3.2/bin/gdal-config --with-pgconfig=/home/postgres/bin/pg_config --with-geosconfig=/usr/local/geos-3.7.0/bin/geos-config --with-projdir=/usr/local/proj-5.2.0 --with-xml2config=/usr/local/libxml2-2.9.7/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.1
# 带protobuf,sfcgal安装
[root@VM_0_4_centos postgis-2.5.0]# ./configure --prefix=/home/postgres --with-gdalconfig=/usr/local/gdal-2.3.2/bin/gdal-config --with-pgconfig=/home/postgres/bin/pg_config --with-geosconfig=/usr/local/geos-3.7.0/bin/geos-config --with-projdir=/usr/local/proj-5.2.0 --with-xml2config=/usr/local/libxml2-2.9.7/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.1 --with-sfcgal=/usr/local/sfcgal-1.3.6/bin/sfcgal-config

[root@VM_0_4_centos postgis-2.5.0]#  make
[root@VM_0_4_centos postgis-2.5.0]# make install

10.3 创建postgis扩展

[root@VM_0_4_centos opt]# su - postgres
[postgres@ssss~]$ psql
psql (11.0)
Type "help" for help.

postgres=# create database gistest;
CREATE DATABASE
postgres=# \c gistest
You are now connected to database "gistest" as user "postgres".
gistest=# create extension postgis;
CREATE EXTENSION
#如果安装了sfcgal,创建扩展测试下
gistest=# create extension postgis_sfcgal;
CREATE EXTENSION

十一 安装PgRouting(网络分析)

依赖boost,cgal,如果没有安装,依照前文,安装下boost,cgal

[root@VM_0_4_centos opt]# wget https://github.com/pgRouting/pgrouting/releases/download/v2.6.1/pgrouting-2.6.1.tar.gz
[root@VM_0_4_centos opt]# tar -zxvf pgrouting-2.6.1.tar.gz   
[root@VM_0_4_centos opt]# cd pgrouting-2.6.1  
[root@VM_0_4_centos pgrouting-2.6.1]#mkdir build & cd build 
#引入postgres的环境变量
[root@VM_0_4_centos build]#  source /home/postgres/.bashrc
[root@VM_0_4_centos build]# cmake ..
[root@VM_0_4_centos build]# make    
[root@VM_0_4_centos build]# make install   

如果不引入pg的环境变量,找不到pg库安装位置的话,cmake会报错如下:

-- PostgreSQL not found.
CMake Error at CMakeLists.txt:294 (message):
   Please check your PostgreSQL installation.

验证安装:

[root@VM_0_4_centos opt]# su - postgres
[postgres@ssss~]$ psql -d gistest
psql (11.0)
Type "help" for help.


gistest=# create extension pgrouting;
CREATE EXTENSION

十二 安装的问题

最容易产生问题的是sfcgal,pgrouting安装,笔者大概稍微整理下,方便遇到同样问题的朋友能搜索链接到此。

  • libpgrouting-2.6.so: undefined symbol
test=# create extension pgrouting;
ERROR: could not load library "/home/postgres/lib/libpgrouting-2.6.so": /home/postgres/lib/libpgrouting-2.6.so: undefined symbol: ZN4CGAL18postcondition_failEPKcS1_iS1

问题原因:boost,cgal,pgrouting也许都安装正确了,就是不能用,神奇不神奇?作者的原因就是安装了一堆东西,gcc,boost安装了两个版本之类的,我先卸载自己安装的gcc-5.4.0,boost-1.65.0,全部用yum install重新安装了gcc,boost见前文。另外一个坑就是cgal安装指定了路径,重新安装到默认的include,lib下,不要自己指定路径了!!!

  • sfcgal编译问题
    注意看github上sfcgal对应的cgal版本,有版本对应关系。还有一点,严格按照作者的做法,安装正确版本的 cgal到默认的安装目录,避免此问题。
  • postgis with sfcgal make error
[root@VM_0_4_centos postgis-2.5.0]# ./configure --prefix=/home/postgres --with-gdalconfig=/usr/local/gdal-2.3.2/bin/gdal-config --with-pgconfig=/home/postgres/bin/pg_config --with-geosconfig=/usr/local/geos-3.7.0/bin/geos-config --with-projdir=/usr/local/proj-5.2.0 --with-xml2config=/usr/local/libxml2-2.9.7/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.1 --with-sfcgal=/usr/local/sfcgal-1.3.6/bin/sfcgal-config
` -------------- Dependencies --------------
GEOS config: /usr/local/geos-3.7.0/bin/geos-config
GEOS version: 3.7.0
GDAL config: /usr/local/gdal-2.3.2/bin/gdal-config
GDAL version: 2.3.2
SFCGAL config: /usr/local/sfcgal/bin/sfcgal-config
SFCGAL version: 1.3.5
PostgreSQL config: /home/postgres/bin/pg_config
PostgreSQL version: PostgreSQL 11.0
PROJ4 version: 52
Libxml2 config: /usr/local/libxml2-2.9.7/bin/xml2-config
Libxml2 version: 2.9.7
JSON-C support: yes
protobuf-c support: yes
PCRE support: yes
Perl: /usr/bin/perl

--------------- Extensions ---------------
PostGIS Raster: enabled
PostGIS Topology: enabled
SFCGAL support: enabled
Address Standardizer support: enabled
[root@VM_0_4_centos postgis-2.5.0]# make 
.....
/usr/local//sfcgal/lib64/libSFCGAL.so.1: undefined reference to boost::serialization::void_upcast(boost::serialization::extended_type_info const&, boost::serialization::extended_type_info const&, void const*)' 
.....

配置验证都正确,就是make报错,请注意万恶的boost又出现了,哭不哭。。。
解决方案,全部用yum install重新安装了gcc,boost见前文。另外一个坑就是cgal安装指定了路径,重新安装到默认的include,lib下,不要自己指定路径了!!!

一个多月有空就编译,什么都正确,就是不好使,一度怀疑人生。。。万恶的boost,cgal问题!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,566评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,287评论 25 707
  • 《鸟哥的Linux私房菜》笔记 提到Linux,就不得不提GNU和GPL授权所产生的自由软件(free softw...
    Zhang21阅读 12,289评论 0 15
  • 箴言:所以到头来,是我记忆太好。不过如果可以选择,我愿意用自己这点所谓的能力去换取感情的完满。 2017年7月21...
    人间石头阅读 324评论 0 1
  • 01 毕达哥拉斯说:不能约束自己的人不能称他为自由的人 生活中,我们经常可以看到:插队买票;随地吐痰;顺手牵羊等等...
    四维娘娘阅读 2,174评论 0 0