PostGIS入门

    PostGIS是对象关系型数据库PostgreSQL的一个插件,PostGIS提供如下空间信息服务:空间对象、空间索引、空间操作函数和空间操作符。同时,PostGIS遵循OpenGIS的规范。

1.安装

    PostGIS官方网站下载地址: https://winnie.postgis.net/download/windows/pg10/buildbot/ ,下载后安装。

     在数据库Testpg中使用PostGIS,执行如下步骤:

Testpg=# CREATE EXTENSION postgis;
Testpg=# CREATE EXTENSION postgis_topology;

      查看安装是否成功

Testpg=# \dx
                                               已安装扩展列表
       名称       | 版本  |  架构模式  |                                描述
------------------+-------+------------+---------------------------------------------------------------------
 plpgsql          | 1.0   | pg_catalog | PL/pgSQL procedural language
 postgis          | 2.5.1 | public     | PostGIS geometry, geography, and raster spatial types and functions
 postgis_topology | 2.5.1 | topology   | PostGIS topology spatial types and functions
(3 行记录)

2.使用

    创建表cities,包含编号和名称。

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

    增加存储空间位置的列,它记录了数据类型(点),维度(二维),及空间坐标系统(EPSG:4326)。

Testpg=# SELECT AddGeometryColumn ('cities', 'the_geom', 4326, 'POINT', 2);

    添加数据,其中,ST_GeomFromText可以将文本转化为坐标与参考系号。

Testpg=# INSERT INTO cities (id, the_geom, name) VALUES (1,ST_GeomFromText('POINT(-0.1257 51.508)',4326),'London, England');
Testpg=# INSERT INTO cities (id, the_geom, name) VALUES (2,ST_GeomFromText('POINT(-81.233 42.983)',4326),'London, Ontario');
Testpg=# INSERT INTO cities (id, the_geom, name) VALUES (3,ST_GeomFromText('POINT(27.91162491 -33.01529)',4326),'East London,SA');

    查询数据。

Testpg=# select * from cities;
 id |      name       |                      the_geom
----+-----------------+----------------------------------------------------
  1 | London, England | 0101000020E6100000BBB88D06F016C0BF1B2FDD2406C14940
  2 | London, Ontario | 0101000020E6100000F4FDD478E94E54C0E7FBA9F1D27D4540
  3 | East London,SA  | 0101000020E610000040AB064060E93B4059FAD005F58140C0
(3 行记录)

    由于列the_geom以16进制表示,不方便阅读。可以使用 ST_AsText(the_geom) 或ST_AsEwkt(the_geom) 函数显示坐标。也可以使用 ST_X(the_geom) 和 ST_Y(the_geom) 显示一个维度的坐标。

Testpg=# 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
  3 | POINT(27.91162491 -33.01529) | SRID=4326;POINT(27.91162491 -33.01529) | 27.91162491 | -33.01529
(3 行记录)

      计算城市间的距离。其中,‘WHERE’ 部分防止输出城市到自身的距离(0)或者两个城市不同排列的距离数据。

Testpg=# 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
 East London,SA  | London, England |  9789680.59961472
 East London,SA  | London, Ontario |  13892208.6782928
(3 行记录)

猜你喜欢

转载自blog.csdn.net/liyazhen2011/article/details/88843714
今日推荐