openGauss資料庫PostGIS 安裝與使用

語言: CN / TW / HK

目錄

 

概述

1.PostGIS 安裝

1.1 GCC-7.3編譯器安裝

1.2PostGIS依賴庫安裝

1.3.安裝Postgis

2.使用Extension

2.1建立PostGIS Extension

2.2使用Extension

2.3刪除Extension


 

概述

PostGIS Extension是PostgreSQL的空間資料庫擴充套件,提供如下空間資訊服務功能:空間物件、空間索引、空間操作函式和空間操作符。PostGIS Extension完全遵循OpenGIS規範。openGauss提供PostGIS Extension(版本為PostGIS-2.4.2)需要單獨安裝外掛。

1.PostGIS 安裝

環境資訊:Centos 7.6 + openGauss 3.1.0 極簡版

環境需要聯網下載一些依賴包。

1.1 GCC-7.3編譯器安裝

PostGIS安裝依賴GCC-7.3編譯器GNU編譯器套件若資料庫例項中已經安裝GCC-7.3編譯器,可直接跳過本步驟。我本機環境上是4.8.5,需要升級下。

 

GCC-7.3編譯器推薦使用原始碼由低版本的gcc和g++編譯器進行升級安裝。若資料庫例項中沒有低版本gcc和g++編譯器,可以通過掛載作業系統映象等方法進行安裝,這裡不做贅述。

 

wget http://mirrors.ustc.edu.cn/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz 

[omm@openGhostxx gcc-7.3.0]$ tar -zxvf gcc-7.3.0.tar.gz

[omm@openGhostxx packages]$ cd gcc-7.3.0

[omm@openGhostxx gcc-7.3.0]$ ./contrib/download_prerequisites

這一步會下載一些依賴,需要確保聯網。

./configure --prefix=/usr/local/gcc-7.3.0/ --enable-checking=release --enable-languages=c,c++ --disable-multilib  --disable-libsanitizer --disable-libcilkrts

 

然後make,時間會比較長,非常長。

make   

make install

完成後會提示安裝目錄,後續會用到。

 

設定下環境變數,不然gcc -v還是舊版本。

在vim編輯器中開啟~/.bashrc文件。

vi ~/.bashrc

加入以下內容實際路徑根據情況替換為前面make install的路徑。

export CC=/usr/local/gcc-7.3.0/bin/gcc

export CXX=/usr/local/gcc-7.3.0/bin/g++

export LD_LIBRARY_PATH=/usr/local/gcc-7.3.0/lib/../lib64:$LD_LIBRARY_PATH

export PATH=/usr/local/gcc-7.3.0/bin:$PATH

然後source生效

source ~/.bashrc

最後檢視下

 

 

1.2PostGIS依賴庫安裝

  1. 下載postgis外掛包並解壓重新命名。
​
[omm@openGhostxx ~]$ cd $GAUSSHOME/

[omm@openGhostxx openGauss]$ wget http://opengauss.obs.cn-south-1.myhuaweicloud.com/dependency/postgis-xc-master-2020-09-17.tar.gz

[omm@openGhostxx openGauss]$ tar -zxvf postgis-xc-master-2020-09-17.tar.gz

[omm@openGhostxx openGauss]$ mv postgis-xc-master postgis-xc/

​

 

2.下載補丁檔案到$GAUSSHOME/postgis-xc/目錄,並打入補丁。

下載路徑:gpl_dependency/postgis/postgis_2.4.2-2.patch · openGauss/openGauss-third_party - Gitee.com

cd $GAUSSHOME/postgis-xc/

[omm@openGhostxx postgis-xc]$ patch -p1 < $GAUSSHOME/postgis_2.4.2-2.patch

patching file gdal-1.11.0/frmts/postgisraster/postgisraster.h

patching file gdal-1.11.0/ogr/ogrsf_frmts/pg/ogr_pg.h

patching file gdal-1.11.0/ogr/ogrsf_frmts/pg/ogrpgutility.h

patching file postgis-2.4.2/configure

patching file postgis-2.4.2/postgis--2.4.2.sql

patching file postgis-2.4.2/postgis_svn_revision.h

patching file postgis-2.4.2/raster/rt_pg/rtpostgis.c

patching file postgis-2.4.2/topology/Makefile.in

patching file postgis-2.4.2/topology/postgis_topology.c

patching file postgis-2.4.2/topology/sql/topogeometry/totopogeom.sql.in

patching file postgis-2.4.2/topology/sql/topogeometry/type.sql.in

patching file postgis-2.4.2/topology/topology.sql.in

3.從網站http://gitee.com/opengauss/openGauss-third_party/blob/master/gpl_dependency/postgis/extension_dependency.h下載postgis依賴標頭檔案到$GAUSSHOME/include/postgresql/server/。

 

4.編譯proj

PROJ框架在做投影,座標系轉換時,其操作風格類似於Linux Shell命令。

cd $GAUSSHOME/postgis-xc/proj-4.9.2

chmod +x ./configure

./configure --prefix=$GAUSSHOME/install/proj

make -sj

make install -sj

 

5.編譯Geos

 

GEOS的前身是JTSJTS提供了全功能的,強大的空間操作和空間判斷。

cd $GAUSSHOME/postgis-xc/geos-3.6.2

chmod +x ./configure

./configure --prefix=$GAUSSHOME/install/geos

make -sj

make install -sj

6.編譯libxml

libxml是一個用於解析xml檔案的庫在各個平臺下都能使用也支援多種語言

cd $GAUSSHOME/postgis-xc/libxml2-2.7.1

chmod +x ./configure

./configure --prefix=$GAUSSHOME/install/libxml2

make -sj

make install -sj

7.安裝JSON-C

 

JSON-C實現了一個引用計數物件模型,它允許您輕鬆地使用C語言來構建JSON物件,將它們輸出為JSON格式的字串,並將JSON格式字串解析回JSON物件的C語言表示形式。

 

cd $GAUSSHOME/postgis-xc/json-c-json-c-0.12.1-20160607

chmod +x ./configure

./configure --prefix=$GAUSSHOME/install/json

make -sj

make install -sj

1.3.安裝Postgis

cd $GAUSSHOME/postgis-xc/postgis-2.4.2

chmod +x ./configure

./configure --prefix=$GAUSSHOME/install/postgis2.4.2 --with-pgconfig=$GAUSSHOME/bin/pg_config --with-projdir=$GAUSSHOME/install/proj --with-geosconfig=$GAUSSHOME/install/geos/bin/geos-config --with-jsondir=$GAUSSHOME/install/json  --with-xml2config=$GAUSSHOME/install/libxml2/bin/xml2-config   --without-raster --without-topology CFLAGS='-O2 -fpermissive -DPGXC  -pthread -D_THREAD_SAFE -D__STDC_FORMAT_MACROS -DMEMORY_CONTEXT_CHECKING -w'  CC=g++

make -sj

make install -sj

 

如果編譯出現類似/home/carrot/data/openGauss-server/third_party/buildtools/gcc/res/lib64/libstdc++.la 找不到,可以自建目錄,將libstdc++.la拷貝進去,然後再make -sj(如果libstdc++.so出現類似問題,按同樣方法處理)。

 

 

omm使用者執行下面的語句,完成PostGIS相關動態連結庫在資料庫例項節點中的分發。因為我的openGauss是單機極簡版,所以使用cp 複製到節點路徑。如果是企業版需要使用tranfer.py,具體參考openGauss產品文件PostGIS安裝章節。

mv $GAUSSHOME/lib/postgresql/postgis-2.4.so $GAUSSHOME/install/postgis-2.4.so


cp $GAUSSHOME/install/postgis-2.4.so $GAUSSHOME/lib/postgresql/postgis-2.4.so

cp $GAUSSHOME/install/json/lib/libjson-c.so.2 $GAUSSHOME/lib/libjson-c.so.2

cp $GAUSSHOME/install/geos/lib/libgeos_c.so.1 $GAUSSHOME/lib/libgeos_c.so.1

cp $GAUSSHOME/install/proj/lib/libproj.so.9 $GAUSSHOME/lib/libproj.so.9

cp $GAUSSHOME/install/geos/lib/libgeos-3.6.2.so $GAUSSHOME/lib/libgeos-3.6.2.so

cp $GAUSSHOME/install/postgis2.4.2/lib/liblwgeom-2.4.so.0 $GAUSSHOME/lib/liblwgeom-2.4.so.0

cp $GAUSSHOME/postgis-xc/postgis-2.4.2/postgis--2.4.2.sql $GAUSSHOME/share/postgresql/extension/postgis--2.4.2.sql

cp $GAUSSHOME/postgis-xc/postgis-2.4.2/postgis.control $GAUSSHOME/share/postgresql/extension/postgis.control

最後重啟資料庫例項。

gs_ctl stop -D /opt/software/openGauss/data/single_node/

gs_ctl start -D /opt/software/openGauss/data/single_node/

 

2.使用Extension

下列SQL語句展示PostGIS的簡單使用,對於各函式的具體使用,請參考《PostGIS-2.4.2使用者手冊》。

2.1建立PostGIS Extension

可直接使用CREATE EXTENSION命令進行建立:

openGauss=# CREATE EXTENSION postgis;

2.2使用Extension

 

示例1:幾何表的建立。

CREATE TABLE cities ( id integer, city_name varchar(50) );

SELECT AddGeometryColumn('cities', 'position', 4326, 'POINT', 2);

 

示例2:幾何資料的插入。

INSERT INTO cities (id, position, city_name) VALUES (1,ST_GeomFromText('POINT(-9.5 23)',4326),'CityA');

INSERT INTO cities (id, position, city_name) VALUES (2,ST_GeomFromText('POINT(-10.6 40.3)',4326),'CityB');

INSERT INTO cities (id, position, city_name) VALUES (3,ST_GeomFromText('POINT(20.8 30.3)',4326), 'CityC');

示例3:計算三個城市間任意兩個城市距離。

SELECT p1.city_name,p2.city_name,ST_Distance(p1.position,p2.position) FROM cities AS p1, cities AS p2 WHERE p1.id > p2.id;v

 

 

2.3刪除Extension

在openGauss中刪除PostGIS Extension的方法如下所示:

DROP EXTENSION postgis CASCADE;

 說明: 如果Extension被其它物件依賴(如建立的幾何表),需要加入CASCADE(級聯)關鍵字,刪除所有依賴物件。

 

確認執行OK後,可執行下列命令刪除$GAUSSHOME/postgis安裝目錄。

rm -rf $GAUSSHOME/postgis-xc

 

最後:除了上面這種安裝外掛支援空間資料,還有基於openGauss的Yukon 資料庫擴充套件地理空間資料的儲存和管理能力, 提供專業的GIS功能,管理地理空間資料。Yukon 目前共提供 postgis、postgis_raster、postgis_sfcgal、yukon_geomodel、yukon_geogridcoder五個擴充套件,感興趣的同學可以瞭解。