首页 > 代码库 > 整理一下postgresql的扩展功能postgis和pgrouting的使用

整理一下postgresql的扩展功能postgis和pgrouting的使用

postgis的安装使用postgresql的bin目录下的stackbuiler

postgis:

1.建表语句:

create table NODES (ID SERIAL not null,geometry geography(POINTZ, 4326) null); 字段geometry表示的是三维空间的点,二维的点将POINTZ改为POINT即可
create table EDGES (ID SERIAL not null,geometry geography(LINESTRINGZ, 4326) null);  字段geometry表示的是三维空间的线,同理,二维的将LINESTRINGZ改为LINESTRING即可

2.插入语句:

insert into nodes(geometry) values(ST_GeographyFromText(SRID=4326; POINT(-110 30 40)));
insert into edges(geometry) values(ST_GeographyFromText(SRID=4326; LINESTRING(-110 30 40,11 22 33)));

3.修改字段类型:

alter table public.nodes alter column geometry set data type geography(PointZ,4326);

4.查询语句:

select ST_AsText(geometry) from nodes;
select ST_AsText(geometry) from edges;

有关pgrouting:

1.Windows下的安装:  

  在官网下载和本机PostgreSQL对应版本的PGRouting,我这里的版本的PostgreSQL 9.2,这个版本可以使用的PGRouting对应版本是2.0。

  下载PGRouing之后,可以看到里面有3个文件夹(bin、lib、share)和5个文件,以后可能会有变动,将这三个文件夹拷贝到PostgreSQL的安装目录下,和同名文件夹合并。

2.让数据库支持pgrouting:

  CREATE EXTENSION pgrouting;

3.pgrouting需要在edges中加入一些基本的字段

  columns ‘source‘, ‘target‘ must be of type int4, ‘cost‘ must be of type float8(也可以使用其他的字段名,使用内置函数查询的时候注意点就好了)

4.查询两个node之间的最短路径:

  

select * from pgr_dijkstra(select id as id,source::integer,target::integer,length::double precision as cost from edges,30,60,false,false);

 

整理一下postgresql的扩展功能postgis和pgrouting的使用