配置完成后,使用命令行创建数据库,使数据库支持PostGIS和pgRouting的函数和基础表
CREATE EXTENSION postgis;
CREATE EXTENSION pgrouting;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION address_standardizer;
二、导入shp数据
我使用的shp数据是自己手工绘制的,并通过拓扑打断相交线//添加起点id
ALTER TABLE crossroad ADD COLUMN source integer;
//添加终点id
ALTER TABLE crossroad ADD COLUMN target integer;
//添加道路权重值
ALTER TABLE crossroad ADD COLUMN length double precision;
//为sampledata表创建拓扑布局,即为source和target字段赋值,生成crossroad_vertices_pgr
SELECT pgr_createTopology('crossroad',0.00001, 'geom', 'gid');
//为source和target字段创建索引
CREATE INDEX source_idx ON crossroad ("source");
CREATE INDEX target_idx ON crossroad ("target");
//为length赋值
update crossroad set length =st_length(geom);
//或者用已有的字段长度赋值,下面shape_length为shp中已有的长度属性
UPDATE crossroad SET length = shape_length;
//为crossroad表添加reverse_cost字段并用length的值赋值
ALTER TABLE crossroad ADD COLUMN reverse_cost double precision;
UPDATE crossroad SET reverse_cost =length;
使用pgr_dijkstra算法查询
SELECT seq, id1 AS node, id2 AS edge, cost FROM pgr_dijkstra('
SELECT gid AS id,
source::integer,
target::integer,
length::double precision AS cost
FROM crossroad,
1, 9, false, false);
新建图层,选择数据存储为postGIS的存储,再选择“配置新的SQL视图”,在SQl语句中添加以下语句,创建视图
SELECT seq, id1 AS node, id2 AS edge, cost,geom FROM pgr_dijkstra('
SELECT gid AS id,
source::integer,
target::integer,
length::double precision AS cost
FROM crossroad',
1, 9, false, false) as di
join crossroad pt
on di.id2 = pt.gid;
路网基础服务:
var roadLayer = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/test/wms',
params: {'LAYERS': 'test:cross', 'TILED': true},
serverType: 'geoserver'
})
})
map.addLayer(roadLayer);
路径规划服务:(带参数)
var routeLayer = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/test/wms',
params: {'LAYERS': 'test:cross_route', 'TILED': true,'viewparams':'a:'+val1+';b:'+val2},
serverType: 'geoserver'
})
})
map.addLayer(routeLayer);
最终结果: