失语者 2018-01-14
本文介绍在RedHat7环境下安装使用PostGIS的流程。
这个比较简单,直接使用yum安装即可。
$ sudo yum install -y postgresql-server postgresql-devel libxml2 libxml2-devel
顺便安装postgresql-devel、libxml2-devel,后边编译安装PostGIS会用到。
postgresql.x86_64 9.2.13-1.1 postgresql-devel.x86_64 9.2.13-1.1 postgresql-libs.x86_64 9.2.13-1.1 postgresql-server.x86_64 9.2.13-1.1 libxml2 2.9.1-6 libxml2-devel.x86_64 2.9.1-6
然后切换到postgres账户。
$ sudo su postgres postgres $
确认PostgreSQL数据目录。
postgres $ cat /var/lib/pgsql/.bash_profile [ -f /etc/profile ] && source /etc/profile PGDATA=/var/lib/pgsql/data export PGDATA
执行初始化操作。
postgres $ initdb
目录/var/lib/pgsql/data下存储了PostgreSQL的所有数据文件和配置。
使用pg_ctl启动PostgreSQL。
postgres $ pg_ctl start
使用psql客户端连接。
postgres $ psql
psql (9.2.13)
输入 "help" 来获取帮助信息.
postgres=# \l
                                         资料库列表
       名称       |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        
------------------+----------+----------+-------------+-------------+-----------------------
 postgres         | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0        | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
                  |          |          |             |             | postgres=CTc/postgres
 template1        | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
                  |          |          |             |             | postgres=CTc/postgres准备gdal、proj、geos和postgis的源码包,postgis版本注意和postgresql保持兼容。
兼容信息可以查看: http://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS
$ wget http://download.osgeo.org/gdal/2.2.3/gdal-2.2.3.tar.gz $ wget http://download.osgeo.org/proj/proj-4.8.0.tar.gz $ wget http://download.osgeo.org/geos/geos-3.3.3.tar.bz2 $ wget http://download.osgeo.org/postgis/source/postgis-2.2.6.tar.gz
依次解压、编译、安装以上软件包。
$ tar xf gdal-2.2.3.tar.gz && cd gdal-2.2.3 && ./configure --prefix=/usr/local/gdal && make && sudo make install $ tar xf proj-4.8.0.tar.gz && cd proj-4.8.0 && ./configure --prefix=/usr/local/proj && make && sudo make install $ tar xf geos-3.3.3.tar.bz2 && cd geos-3.3.3 && ./configure --prefix=/usr/local/geos && make && sudo make install $ tar xf postgis-2.2.6.tar.gz && cd postgis-2.2.6 && ./configure -prefix=/usr/local/postgis --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config && make && sudo make install
将gdal、proj、geos的lib目录添加到ldconfig。
$ sudo cat /etc/ld.so.conf include ld.so.conf.d/*.conf /usr/local/gdal/lib/ /usr/local/proj/lib/ /usr/local/geos/lib/ $ sudo ldconfig
# 创建无空间特性数据库
postgres $ createdb template_postgis
# 创建相关空间数据库相关的函数,类型,操作符等
postgres $ psql -f /usr/share/pgsql/contrib/postgis-2.2/postgis.sql -d template_postgis
postgres $ psql -f /usr/share/pgsql/contrib/postgis-2.2/rtpostgis.sql -d template_postgis
# 验证空间数据库版本
postgres $ psql template_postgis
psql (9.2.13)
输入 "help" 来获取帮助信息。
template_postgis=# select postgis_full_version(); 
                                                          postgis_full_version                                                            
---------------------------------------------------------------------------------------------------------------------------------------------
 POSTGIS="2.2.6 r16006" GEOS="3.3.3-CAPI-1.7.4" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 2.2.3, released 2017/11/20" LIBXML="2.9.1"RASTER
(1 行记录)
template_postgis=# \d
                    关联列表
架构模式  |        名称        |  型别  |  拥有者  
----------+-------------------+--------+----------
public  | geography_columns | 视观表 | postgres
public  | geometry_columns  | 视观表 | postgres
public  | raster_columns    | 视观表 | postgres
public  | raster_overviews  | 视观表 | postgres
public  | spatial_ref_sys  | 资料表 | postgres
(5 行记录)
postgres $ createdb -T template_postgis new_database
测试点(0, 0)是否在指定的多边形内。
new_database=# select ST_Within(ST_GeomFromText('POINT(0 0)', 4326), ST_GeomFromText('POLYGON((1 1, 1 -1, -1 -1, -1 1, 1 1))', 4326)) ;
 st_within 
-----------
 t
(1 行记录)详细语法规则可以参考PostGis使用手册:http://www.postgres.cn/docs/PostGis-2.2.0dev_Manual.pdf