Yukon for PostgreSQL

参考官方:安装与卸载 — Yukon 2.0-beta documentation

环境准备

Yukon  1.0.1 + PostgreSQL 13.5

Centos : 7.6

编译器: GCC 7.3.0

CMake: 3.21.1

python:v2.7.5

安装依赖

名称

Centos版本

openEuler

(20.03)版本

操作

备注

libtiff-devel

4.0.3

4.1.0

yum install libtiff-devel

PROJ 依赖

libcurl-devel

7.29.0

7.71.1

yum install libcurl-devel

PROJ 依赖

gmp-devel

6.0.0

6.2.0

yum install gmp-devel

CGAL 依赖

mpfr-devel

3.1.1

4.1.0

yum install mpfr-devel

CGAL 依赖

boost-devel

1.53.0

1.73.0

yum install boost-devel

CGAL/SFCGAL 依赖

libuuid-devel

2.23.2

2.35.2

yum install libuuid-devel

JSON-C

0.11

0.15

yum install json-c-devel

LIBXML2

2.9.1

2.9.10

yum install libxml2-devel

SQLite3

3.39

3.39

编译

PROJ 依赖

PROJ

8.1.1

8.1.1

编译

GEOS

3.10.4

3.10.4

编译

GDAL

3.3.2

3.3.2

编译

CGAL

4.13.2

4.13.2

编译

SFCGAL

1.3.8

1.3.8

编译

PROTOBUF-CPP

3.16.0

编译

PROTOBUF-C 依赖

PROTOBUF-C

1.4.0

编译

我把需要编译的依赖,全部放到了/usr/src目录下,这个随意

下文中,make -j4报错,使用make再试试,可能因为内存或者其他原因导致。

GCC 

        参考gcc升级,建议7.3.0

Python

yum install python*

        可能只需要Python python-devel,不过嫌麻烦,直接全部装了

cmak

[root@localhost src]# yum -y gcc gcc-c++ wget make automake openssl openssl-devel
[root@localhost src]# wget https://codeload.github.com/Kitware/CMake/zip/refs/tags/v3.21.1
[root@localhost src]# unzip v3.21.1
[root@localhost src]# cd CMake-3.21.1/
[root@localhost CMake-3.21.1]# ./configure --prefix=/usr/local/cmake-3.21.1
[root@localhost CMake-3.21.1]# make -j4
[root@localhost CMake-3.21.1]# make install
[root@localhost CMake-3.21.1]# vim /etc/profile
export CMAKE_HOME=/usr/local/cmake-3.21.1
export PATH=$CMAKE_HOME/bin:$PATH
[root@localhost CMake-3.21.1]# source /etc/profile

可能出现的错误:

Q1:/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found

解决办法:说明gcc的动态库还是旧版本的 

a) 查找编译gcc最新的动态链接库,find / -name "libstdc++.so*"

/usr/src/gcc-7.4.0/build/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.24

b) 拷贝到/usr/lib64下,移除旧版本的动态链接库

c) 删除原来软连接: rm -rf /lib64/libstdc++.so.6 

d) 软连接指向最新动态库(看自己的文件名):

cd /usr/lib64

ln -s libstdc++.so.6.0.24 libstdc++.so.6

然后再次执行第4步

SQLite3

  1. 下载源码

    wget https://www.sqlite.org/2022/sqlite-autoconf-3390400.tar.gz
  2. 解压软件包

    tar -xf sqlite-autoconf-3390400.tar.gz
  3. 修改源代码

    在后续编译 GDAL 时,会使用到 SQLite3 的一些特性,所以这里我们需要修改一下源代码,进入 sqlite3 目录,在 sqlite3.c 文件的第 25 行添加 #define SQLITE_ENABLE_COLUMN_METADATA 1,或者直接使用下面的命令修改

    sed -i '25 i #define SQLITE_ENABLE_COLUMN_METADATA 1' sqlite3.c
  4. 检查环境,生成编译脚本

    ./configure --prefix=/home/3rd_inst/sqlite3
  5. 编译,安装

    make -j4 && make install

    编译完成后就可以在 /home/3rd_inst/sqlite3 看到编译好的 SQLite3.

PROJ

  1. 下载源码

    wget http://download.osgeo.org/proj/proj-8.1.1.tar.gz
  2. 解压软件包

    tar -xf proj-8.1.1.tar.gz
  3. 导出环境变量

    Porj 编译需要使用 SQLite3 3.11 以上版本,因此这里我们使用我们刚刚编译的 SQLite3,首先将其 pkg-config 文件路径导出:

    export PKG_CONFIG_PATH=/home/3rd_inst/sqlite3/lib/pkgconfig:$PKG_CONFIG_PATH

    同时使用我们编译好的 sqlite3 程序:

    export PATH=/home/3rd_inst/sqlite3/bin/:$PATH
  4. 检查环境,生成编译脚本

    yum install curl-devel
    
    ./configure --prefix=/home/3rd_inst/proj
  5. 编译,安装

    make -j4 && make install

    编译完成后就可以在 /home/3rd_inst/proj 看到编译好的 Proj.

GEOS

  1. 下载源码

    wget https://download.osgeo.org/geos/geos-3.10.4.tar.bz2
  2. 解压软件包

    tar -xf geos-3.10.4.tar.bz2
  3. 检查环境,生成编译脚本

    cmake -DCMAKE_INSTALL_PREFIX=/home/3rd_inst/geos .
  4. 编译,安装

    make -j4 && make install

    编译完成后就可以在 /home/3rd_inst/geos 看到编译好的 GEOS.

GDAL

  1. 下载源码

    wget http://upload.osgeo.org/gdal/3.3.2/gdal-3.3.2.tar.gz
  2. 解压软件包

    tar -xf gdal-3.3.2.tar.gz
  3. 导出环境变量

    export LIBRARY_PATH=/home/3rd_inst/proj/lib/:$LIBRARY_PATH
    export CPATH=/home/3rd_inst/proj/include:$CPATH
  4. 检查环境,生成编译脚本

    ./configure --prefix=/home/3rd_inst/gdal
  5. 编译,安装

    make -j4 && make install

    编译完成后就可以在 /home/3rd_inst/gdal 看到编译好的 GDAL.

CGAL

下载源码

wget https://github.com/CGAL/cgal/archive/refs/tags/releases/CGAL-4.13.2.tar.gz
tar -xf CGAL-4.13.2.tar.gz
cmake -DCMAKE_INSTALL_PREFIX=/home/3rd_inst/cgal -DCMAKE_BUILD_TYPE=Release
make -j4 && make install

 SFCGA

wget https://github.com/Oslandia/SFCGAL/archive/refs/tags/v1.3.8.tar.gz
tar -xf SFCGAL-1.3.8.tar.gz
cmake -DCMAKE_INSTALL_PREFIX=/home/3rd_inst/sfcgal -DCMAKE_PREFIX_PATH=/home/3rd_inst/cgal/lib64/cmake/CGAL
make -j4 && make install

 JSON-C

wget https://github.com/json-c/json-c/archive/refs/tags/json-c-0.15-20200726.tar.gz
tar -xf json-c-0.15-20200726.tar.gz
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/3rd_inst/json-c
make -j4 && make install

LIBXML2

yum -y install python-devel libtool
wget https://github.com/GNOME/libxml2/archive/refs/tags/v2.9.12.tar.gz
tar -xf v2.9.12.tar.gz
sh autogen.sh --prefix=/home/3rd_inst/libxml2
make -j4 && make install

PROTOBUF-CPP

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.16.0/protobuf-cpp-3.16.0.tar.gz
tar -xf protobuf-cpp-3.16.0.tar.gz
./configure --prefix=/home/3rd_inst/protobuf
make -j4 && make install

protobuf-c

wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.4.0/protobuf-c-1.4.0.tar.gz
tar -xf protobuf-c-1.4.0.tar.gz
export PKG_CONFIG_PATH=/home/3rd_inst/protobuf/lib/pkgconfig:$PKG_CONFIG_PATH
./configure --prefix=/home/3rd_inst/protobuf-c
make -j4 && make install

到此为止,所有的三方依赖库已经编译完成,这里我们新建一个 enable 文件,用于导出我们在后续会用到的环境变量

#!/bin/bash

PREFIX=/home/3rd_inst

export LIBRARY_PATH=${PREFIX}/sqlite3/lib:${PREFIX}/proj/lib:${PREFIX}/gdal/lib:${PREFIX}/protobuf/lib:${PREFIX}/protobuf-c/lib:${PREFIX}/libxml2/lib:${PREFIX}/geos/lib64:${PREFIX}/cgal/lib64:${PREFIX}/sfcgal/lib64:${PREFIX}/json-c/lib64:$LIBRARY_PATH

export LD_LIBRARY_PATH=${PREFIX}/sqlite3/lib:${PREFIX}/proj/lib:${PREFIX}/gdal/lib:${PREFIX}/protobuf/lib:${PREFIX}/protobuf-c/lib:${PREFIX}/libxml2/lib:${PREFIX}/geos/lib64:${PREFIX}/cgal/lib64:${PREFIX}/sfcgal/lib64:${PREFIX}/json-c/lib64:$LD_LIBRARY_PATH

export PKG_CONFIG_PATH=${PREFIX}/sqlite3/lib/pkgconfig:${PREFIX}/proj/lib/pkgconfig:${PREFIX}/geos/lib64/pkgconfig:${PREFIX}/gdal/lib/pkgconfig:${PREFIX}/sfcgal/lib64/pkgconfig:${PREFIX}/protobuf/lib/pkgconfig:${PREFIX}/protobuf-c/lib/pkgconfig:${PREFIX}/json-c/lib64/pkgconfig:${PREFIX}/libxml2/lib/pkgconfig:$PKG_CONFIG_PATH

export PATH=${PREFIX}/sqlite3/bin:${PREFIX}/proj/bin:${PREFIX}/geos/bin:${PREFIX}/gdal/bin:${PREFIX}/sfcgal/bin:${PREFIX}/protobuf/bin:${PREFIX}/protobuf-c/bin:${PREFIX}/libxml2/bin:$PATH

还有一个 ugc 的文件夹是 yukon_geomodel 的依赖文件,你可以从安装包中找到这个文件。然后将路径加入到``LD_LIBRARY_PATH`` 中。

============出现分歧===========

禹贡官方文档说编写一个enable即可,可是我并没有成功。于是我按照PostGIS的编译方法,才可以编译成功。(当然可能知识储备不足,导致某些地方配置少了也有可能)

[root@sougou lib]# vi /etc/ld.so.conf
#追加内容
/home/3rd_inst/proj/lib
/home/3rd_inst/gdal/lib
/home/3rd_inst/geos/lib64
/home/3rd_inst/json-c/lib64
/home/3rd_inst/libxml2/lib
/home/3rd_inst/protobuf/lib
/home/3rd_inst/protobuf-c/lib
/home/3rd_inst/sfcgal/lib64
#yukon_geomodel-1.0.so会使用libYkGeometry3D.so和libYkBase.so
/usr/src/yukon4pgsql/geomodel/libUGC/Builds/MakeProject/Bin64_Linux/


#匹配给使用的lib
[root@sougou lib]# ldconfig -v

Yukon for PostgreSQL 编译

1、下载源码

git clone https://gitee.com/isupermap/yukon4pgsql.git

2、生成 configure

sh autogen.sh

3、检查环境,生成编译脚本

由于自定义了路径,所以需要添加--with,我发现只添加这几行即可,并非全部的。

后面的我就注释了。

./configure --with-pgconfig=/usr/local/pg135/bin/pg_config
--with-geosconfig=/home/3rd_inst/geos/bin/geos-config 
--with-projdir=/home/3rd_inst/proj
--with-protobufdir=/home/3rd_inst/protobuf-c
--without-topology

#--with-xml2config=/home/3rd_inst/libxml2/bin/xml2-config 
#--with-gdalconfig=/home/3rd_inst/gdal/bin/gdal-config
#--with-jsondir=/home/3rd_inst/json-c
#--with-sfcgal=/home/3rd_inst/sfcgal/bin/sfcgal-config




 -------------- Dependencies --------------
  GEOS config:          /home/3rd_inst/geos/bin/geos-config
  GEOS version:         3.10.4
  GDAL config:          /home/3rd_inst/gdal/bin/gdal-config
  GDAL version:         3.3.2
  SFCGAL config:        /home/3rd_inst/sfcgal/bin/sfcgal-config
  SFCGAL version:       1.3.8
  PostgreSQL config:    /usr/local/pg135/bin/pg_config
  PostgreSQL version:   PostgreSQL 13.5
  PROJ4 version:        81
  Libxml2 config:       /home/3rd_inst/libxml2/bin/xml2-config
  Libxml2 version:      2.9.12
  JSON-C support:       yes
  protobuf support:     yes
  protobuf-c version:   1004000
  PCRE support:         Version 1
  Perl:                 /usr/bin/perl

 --------------- Extensions ---------------
  PostGIS Raster:                     enabled
  PostGIS Topology:                   disabled
  SFCGAL support:                     enabled
  Address Standardizer support:       enabled

 -------- Documentation Generation --------
  xsltproc:             /usr/bin/xsltproc
  xsl style sheets:
  dblatex:
  convert:
  mathml2.dtd:          http://www.w3.org/Math/DTD/mathml2/mathml2.dtd

configure: WARNING:
configure: WARNING:  | You are building using --with-projdir. This option isn't standard and    |
configure: WARNING:  | might be incompatible with future releases of PROJ.                      |
configure: WARNING:  | You can instead adjust the PKG_CONFIG_PATH environment variable if you   |
configure: WARNING:  | installed software in a non-standard prefix.                             |
configure: WARNING:  | Alternatively, you may set the environment variables PROJ_CFLAGS and     |
configure: WARNING:  | PROJ_LIBS to avoid the need to call pkg-config.

4、编译,安装

make -j4 && make install

5、验证

--- 创建 postgis 扩展
CREATE EXTENSION postgis;
--- 创建 postgis_raster 扩展
CREATE EXTENSION postgis_raster;
--- 创建 yukon_geomodel 扩展
CREATE EXTENSION yukon_geomodel;
--- 创建 yukon_geogridcoder 扩展
CREATE EXTENSION yukon_geogridcoder;
-- 如果在编译时支持了 SFCGAL 则可以创建 SFCGAL 扩展
CREATE EXTENSION postgis_sfcgal;


postgres=# select yukon_version();
                                yukon_version
-----------------------------------------------------------------------------
 1.0.1(Community Edition) Compiled at:2023-06-29 21:27:34 Commit ID:981845d
(1 row)

postgres=# select * from postgis_full_version();                                                                             postgis_full_version
------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
 POSTGIS="3.2.2 981845d" [EXTENSION] PGSQL="130" GEOS="3.10.4-CAPI-1.16.2" SFCGAL="1.3.8" PROJ="8.1.1" GDAL="GDAL 3.3.2,
 released 2021/09/01" LIBXML="2.9.12" LIBJSON="0.15" LIBPROTOBUF="1.4.0" WAGYU="0.5.0 (Internal)" RASTER
(1 row)

到此,就可以在 PostgreSQL 中使用 Yukon 了。

创建用户

在安装 openGauss/PostgreSQL 数据库时,会自动创建一个初始用户,初始用户具有系统的最高权限,能够执行所有的操作。这里我们通过命令行登录到数据库,并创建一个新的系统管理员用户。

# 登录到数据库
--PostgreSQL
psql -d postgres

# 创建新用户 yukontest
--PostgreSQL
CREATE USER yukontest WITH SUPERUSER password 'Bigdata@123' ;

创建表空间

通过使用表空间,管理员可以控制一个数据库安装的磁盘布局。这样有以下优点:

  • 如果初始化数据库所在的分区或者卷空间已满,又不能逻辑上扩展更多空间,可以在不同的分区上创建和使用表空间

  • 表空间允许管理员根据数据库对象的使用模式安排数据位置,从而提高性能

    • 一个频繁使用的索引可以放在性能稳定且运算速度较快的磁盘上,比如一种固态设备

    • 一个存储归档的数据,很少使用的或者对性能要求不高的表可以存储在一个运算速度较慢的磁盘上

  • 管理员通过表空间可以设置占用的磁盘空间。用以在和其他数据共用分区的时候,防止表空间占用相同分区上的其他空间

这里我们使用刚才创建的 yukontest 用户来创建一个表空间

# 创建表空间 yukonspace
CREATE TABLESPACE yukonspace LOCATION '/home/omm/data';

注意:目录 /home/omm/data 必须已经存在且具有可访问权限。

创建数据库

# 创建数据库 yukontutorial ,并指定数据库编码为 'UTF8' 表空间为上面创建好的 yukonspace
CREATE DATABASE yukontutorial ENCODING='UTF8' TABLESPACE=yukonspace;

写入数据

新建表格 global_points,并写入点数据:

-- 创建带geometry列的表
CREATE TABLE global_points (id SERIAL PRIMARY KEY,name VARCHAR(64),location geometry(POINT,3857));

-- 写入10个点对象
INSERT INTO global_points (name, location) VALUES ('Town1', ST_GeomFromEWKT('SRID=3857;POINT (12057531.405152922 4096788.3009192282)') );
INSERT INTO global_points (name, location) VALUES ('Town2', ST_GeomFromEWKT('SRID=3857;POINT (12057879.323089447 4096794.06898925)') );
INSERT INTO global_points (name, location) VALUES ('Town3', ST_GeomFromEWKT('SRID=3857;POINT (12057241.150712628 4096835.404659481)') );
INSERT INTO global_points (name, location) VALUES ('Town4', ST_GeomFromEWKT('SRID=3857;POINT (12057396.37461059 4096885.372392862)') );
INSERT INTO global_points (name, location) VALUES ('Town5', ST_GeomFromEWKT('SRID=3857;POINT (12056940.710538926 4096886.9123589676)') );
INSERT INTO global_points (name, location) VALUES ('Town6', ST_GeomFromEWKT('SRID=3857;POINT (12058128.24460281 4096938.23117457)') );
INSERT INTO global_points (name, location) VALUES ('Town7', ST_GeomFromEWKT('SRID=3857;POINT (12058378.134595742 4097081.4360874156)') );
INSERT INTO global_points (name, location) VALUES ('Town8', ST_GeomFromEWKT('SRID=3857;POINT (12058636.607321415 4097235.1257181372)') );
INSERT INTO global_points (name, location) VALUES ('Town9', ST_GeomFromEWKT('SRID=3857;POINT (12058917.488660585 4097395.7746241256)') );
INSERT INTO global_points (name, location) VALUES ('Town10', ST_GeomFromEWKT('SRID=3857;POINT (12059180.102471316 4097548.7255362184)') );

空间查询

查询距离指定点500米范围内的对象: 

SELECT * FROM global_points WHERE ST_DWithin(location, ST_GeomFromEWKT('SRID=3857;POINT (12058378 4097081)'), 500);

 id | name  |                      location
----+-------+----------------------------------------------------
  6 | Town6 | 0101000020110F000046C9D307C2FF6641D920971DD5414F41
  7 | Town7 | 0101000020110F0000BB9B4E44E1FF664162B6D1B71C424F41
  8 | Town8 | 0101000020110F0000522D6F93010067412C88179069424F41
(3 rows)

geometry 子类型,总共有15种

POINT,点
LINESTRING,线、折线,由点串构成
POLYGON,面,由n个部分组成,每个部分是首位相连的线串
POLYHEDRALSURFACE,多面体表面,由n个部分组成,每个部分都是一个Polygon
MULTIPOINT,多点,由n个Point子对象组成
MULTILINESTRING,多线,由n个LineString子对象组成
MULTIPOLYGON,多面,由n个Polygon子对象组成
CIRCULARSTRING,线,由圆弧连接而成;用点串描述,三个点确定一段圆弧,前一个圆弧的最后一个点与后一个圆弧的第一个点共用;特别地,如果圆弧的第一个点与第三个点重合,则第一、二个点作为直径,以此来表达圆形
COMPOUNDCURVE,复合线,
MULTICURVE,多(曲)线,
CURVEPOLYGON,复合面,由n个部分组成,每个部分是首尾相连的CircularString或LineString或CompoundCurve;与Polygon类似,都表达一个闭合的区域,区别在于是否由CircularString对象参与构造
MULTISURFACE,多面,由n个子对象组成,每个子对象可以是Polygon或CurvePolygon类型;与MultiPolygon类似,都表达多面对象,区别在于是否由CircularString对象参与构造
GEOMETRYCOLLECTION,复合对象,由任意子类型构成
TRIANGLE,三角形,由首尾相连的4个点组成
TIN,不规则三角网,由n个Triangle组成

你可能感兴趣的:(PostGIS,postgresql,数据库)