首页 > Postgresql > linux 源码安装postgis 2.3.5

linux 源码安装postgis 2.3.5

1 postgis简介
百度百科这样说:
PostGIS是对象关系型数据库系统PostgreSQL的一个扩展,PostGIS提供如下空间信息服务功能:空间对象、空间索引、空间操作函数和空间操作符。同时,PostGIS遵循OpenGIS的规范。
PostGIS的版权被纳入到GNU的GPL中,也就是说任何人可以自由得到PostGIS的源码并对其做研究和改进。正是由于这一点,PostGIS得到了迅速的发展,越来越多的爱好者和研究机构参与到PostGIS的应用开发和完善当中。

2 我的环境
os:centos x86_64 linux7.4 1708
postgresql库:9.6.6

3 postgis版本选择
适配pg9.6.6的postgis最新的版本是2.3.5这个版本。
postgis源码下载网址:http://postgis.net/source/

4 postgis依赖package安装
a、postgis需要gcc、gcc-c++、make、libxml2、libxml2-devel,用yum安装即可。
b、Proj4,选4.9.3的版本
下载地址:http://trac.osgeo.org/proj/

#tar zxvf proj-4.9.3.tar.gz
#cd proj-4.9.3
#./configure --prefix=/usr/local/proj-4.9.3
#make
#make install

编辑文件/etc/ld.so.conf.d/proj-4.9.3.conf
#vi /etc/ld.so.conf.d/proj-4.9.3.conf
添加以下内容
/usr/local/proj-4.9.3/lib

保存并退出vi
执行以下命令,将proj的so库的路径添加到系统中
#ldconfig
c、安装geos
下载地址:http://trac.osgeo.org/geos/

#tar jxvf geos-3.5.1.tar.bz2
#cd geos-3.5.1
#./configure --prefix=/usr/local/geos-3.5.1
#make
#make install

编辑文件/etc/ld.so.conf.d/geos-3.5.1.conf
#vi /etc/ld.so.conf.d/geos-3.5.1.conf
添加以下内容
/usr/local/geos-3.5.1/lib
保存并退出vi
执行以下命令,将geos的so库的路径添加到系统中
#ldconfig
c、安装gdal
下载地址:http://www.gdal.org/

#tar zxvf gdal-2.1.4.tar.gz
#cd gdal-2.1.4
#./configure --prefix=/usr/local/gdal-2.1.4
#make
#make install

编辑文件/etc/ld.so.conf.d/gdal-2.1.4.conf
#vi /etc/ld.so.conf.d/gdal-2.1.4.conf
添加以下内容
/usr/local/gdal-2.1.4/lib
保存并退出vi
执行以下命令,将gdal的so库的路径添加到系统中
#ldconfig

5 安装postgis

#tar zxvf postgis-2.3.5.tar.gz
#cd postgis-2.3.5
./configure  --prefix=/usr/local/postgis-2.3.5 \
             --with-pgconfig=/usr/local/pgsql/bin/pg_config \
             --with-gdalconfig=/usr/local/gdal-2.1.4/bin/gdal-config \
             --with-geosconfig=/usr/local/geos-3.5.1/bin/geos-config \
             --with-xml2config=/usr/bin/xml2-config \
             --with-projdir=/usr/local/proj-4.9.3
             
#make
当屏幕出现“PostGIS was built successfully. Ready to install.”,说明之前的工作都ok,可以安心安装了。
#make install

做个目录链接,版本升级变更方便

#ln -sf /usr/local/postgis-2.3.5 /usr/local/postgis

目录属主改为postgres

#chown postgres:postgres /usr/local/postgis-2.3.5
#chown postgres:postgres /usr/local/pgsql9.6.6

修改postgres环境变量LD_LIBRARY_PATH

export LD_LIBRARY_PATH=/usr/local/pgsql/lib:/usr/local/postgis/lib:$LD_LIBRARY_PATH

至此postgis软件安装工作结束

6 后续配置工作
a、示例建个gisdb库,并加入postgis扩展

[postgres@pgaa1 ~]$createdb gisdb
[postgres@pgaa1 ~]$psql gisdb

gisdb=# CREATE EXTENSION postgis;
gisdb=# CREATE EXTENSION postgis_topology;

b、在gisdb数据库中输入\du,查看已安装的插件

[postgres@pgaa1 ~]$ psql gisdb
psql (9.6.6)
Type "help" for help.

gisdb=# 
gisdb=# \dx
                                         List of installed extensions
       Name       | Version |   Schema   |                             Description                             
------------------+---------+------------+---------------------------------------------------------------------
 plpgsql          | 1.0     | pg_catalog | PL/pgSQL procedural language
 postgis          | 2.3.5   | public     | PostGIS geometry, geography, and raster spatial types and functions
 postgis_topology | 2.3.5   | topology   | PostGIS topology spatial types and functions
(3 rows)

可以看到已经安装了postgis和postgis_topology。

7 使用
a、创建空间数据表
首先建立一个常规的表格存储有关城市(cities)的信息。这个表格有两栏,一个是 ID 编号,一个是城市名:

gisdb=# CREATE TABLE cities (id int4, name varchar(50));

现在添加一个空间列用于存储城市的位置。习惯上这个列叫做 the_geom。它记录了数据为什么类型(点、线、面)、有几维(这里是二维)以及空间坐标系统。此处使用 EPSG:4326 坐标系统:

gisdb=# SELECT AddGeometryColumn ('cities', 'the_geom', 4326, 'POINT', 2);
                  addgeometrycolumn                  
-----------------------------------------------------
 public.cities.the_geom SRID:4326 TYPE:POINT DIMS:2 
(1 row)

完成后,查询 cities 表单应当显示这个新栏目。同时页面将显示当前表达没有记录(0 rows)。

gisdb=# select * from cities;
 id | name | the_geom 
----+------+----------
(0 rows)

为添加记录,需要使用 SQL 命令。对于空间列,使用 PostGIS 的 ST_GeomFromText可以将文本转化为坐标与参考系号的记录:

gisdb=# INSERT INTO cities (id, the_geom, name) VALUES (1,ST_GeomFromText('POINT(-0.1257 51.508)',4326),'London, England');
INSERT 0 1
gisdb=# INSERT INTO cities (id, the_geom, name) VALUES (2,ST_GeomFromText('POINT(-81.233 42.983)',4326),'London, Ontario');
INSERT 0 1
gisdb=# 
gisdb=# SELECT * FROM cities;
 id |      name       |                      the_geom                      
----+-----------------+----------------------------------------------------
  1 | London, England | 0101000020E6100000BBB88D06F016C0BF1B2FDD2406C14940
  2 | London, Ontario | 0101000020E6100000F4FDD478E94E54C0E7FBA9F1D27D4540
(2 rows)

b、简单查询
标准的 SQL 操作都可以用于 PostGIS 表:

gisdb=# SELECT * FROM cities;
 id |      name       |                      the_geom                      
----+-----------------+----------------------------------------------------
  1 | London, England | 0101000020E6100000BBB88D06F016C0BF1B2FDD2406C14940
  2 | London, Ontario | 0101000020E6100000F4FDD478E94E54C0E7FBA9F1D27D4540
(2 rows)

这里的坐标是无法阅读的 16 进制格式。要以 WKT 文本显示,使用 ST_AsText(the_geom) 或ST_AsEwkt(the_geom) 函数。也可以使用 ST_X(the_geom) 和 ST_Y(the_geom) 显示一个维度的坐标:

gisdb=# SELECT id, ST_AsText(the_geom), ST_AsEwkt(the_geom), ST_X(the_geom), ST_Y(the_geom) FROM cities;
 id |       st_astext       |            st_asewkt            |  st_x   |  st_y  
----+-----------------------+---------------------------------+---------+--------
  1 | POINT(-0.1257 51.508) | SRID=4326;POINT(-0.1257 51.508) | -0.1257 | 51.508
  2 | POINT(-81.233 42.983) | SRID=4326;POINT(-81.233 42.983) | -81.233 | 42.983
(2 rows)

c、空间查询
PostGIS 为 PostgreSQL 扩展了许多空间操作功能。以上已经涉及了转换空间坐标格式的 ST_GeomFromText 。多数空间操作以 ST(spatial type)开头,在 PostGIS 文档相应章节有罗列。这里实践一个问题:上面两个城市相互的距离是多少?查询语句怎么写?

gisdb=# SELECT p1.name,p2.name,ST_DistanceSphere(p1.the_geom,p2.the_geom) FROM cities AS p1, cities AS p2 WHERE p1.id > p2.id;
      name       |      name       | st_distancesphere 
-----------------+-----------------+-------------------
 London, Ontario | London, England |  5875787.03777356
(1 row)

输出显示了距离数据。注意 ‘WHERE’ 部分防止了输出城市到自身的距离(0)或者两个城市不同排列的距离数据(London, England 到 London, Ontario 和 London, Ontario 到 London, England 的距离是一样的)。

分类: Postgresql 标签: , , ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.