PgRouting是基于开源空间数据库PostGIS用于网络分析的扩展模块,最初它被称作pgDijkstra,因为它只是利用Dijkstra算法实现最短路径搜索,之后慢慢添加了其他的路径分析算法,如A算法,双向A算法,Dijkstra算法,双向Dijkstra算法,tsp货郎担算法等,然后被更名为pgRouting。该扩展库依托PostGIS自身的gist索引,丰富的坐标系与图形类型,强大的几何处理能力,如空间查询,空间处理,线性参考等优势,能保障在较大数据级别下的网络分析效果更快更好。
最新源码见:
1、环境搭建
需要先安装PostgreSQL9.6,再安装PostGIS,注意版本号PostGIS2.4后已经集成了pgRouting,无需单独安装
PostGIS 官网 http://www.postgis.org/
pgRouting官网http://pgrouting.org/documentation.html
2、启用pgRouting功能,连接到对应postgres数据库后执行下面的sql语句
CREATE EXTENSION postgis;
CREATE EXTENSION pgrouting;
3、路网数据处理
由于路网分析的特殊性,只支持LineString类型,不支持MultiLineString类型,如果路网数据为MultiLineString类型导入前需加工处理,可使用arcgis在交叉点处使用打断相交线来分割线,参见
http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#//01m8,最终可用的数据必须为路网不自相交,导入时选择简单的线类型,否则网络拓扑计算数据会错误
4、路网拓扑数据计算处理,执行成功后,执行成功后会生产一个**_vertices_pgr的表,里面包含路网相交点的空间数据
alter table road add column source int; alter table road add column target int; create index road_source_idx on road("source");
create index road_target_idx on road("target"); ALTER TABLE road ADD COLUMN length double precision; SELECT pgr_createTopology('road',0.00001, 'geom', 'gid'); update road set length =st_length(geom);
5、查询最短路径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 xmpark_road',
1, 10, false, false) as di
join xmpark_road pt
on di.id2 = pt.gid
6、参数化的查询sql语句,可以用下面的语句,在geoserver中使用发布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 xmpark_road',
%a%, %b%, false, false) as di
join xmpark_road pt
on di.id2 = pt.gid
7、在ol中使用上面发布的搜索图层使用起止交叉点来显示导航路径,源码如下
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
<link rel="stylesheet" href="ol/ol.css" type="text/css">
<script src="ol/ol-debug.js"></script>
<script src="ol/proj4.js"></script>
<script src="js/jquery-1.8.0.min.js"></script>
<meta charset="utf-8" />
</head>
<body>
<div id="map" style="width: 100%;height:800px;border:solid 1px red" class="map">
<div>
id1: <input id="id1" value="30" />
id2: <input id="id2" value="66" />
<input type="button" value="query" onclick="queryRoute()" />
</div>
</div>
</body>
</html> <script type="text/javascript">
var format = 'image/png';
var bounds = [501369.9375, 2494100.25,
502073, 2495256.25];
var mousePositionControl = new ol.control.MousePosition({
className: 'custom-mouse-position',
target: document.getElementById('location'),
coordinateFormat: ol.coordinate.createStringXY(5),
undefinedHTML: ' '
});
var tiled = new ol.layer.Tile({
visible: true,
source: new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/pg_ws/wms',
params: {
'FORMAT': format,
'VERSION': '1.1.1',
tiled: true,
STYLES: '',
LAYERS: 'pg_ws:xmpark_road',
}
})
}); var cross_tiled = new ol.layer.Tile({
//visible: true,
source: new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/pg_ws/wms',
params: {
'FORMAT': format,
'VERSION': '1.1.1',
tiled: true,
STYLES: '',
LAYERS: 'pg_ws:xmpark_road_vertices_pgr',
}
})
}); var projection = new ol.proj.Projection({
code: 'EPSG:3857',
units: 'm',
axisOrientation: 'neu',
global: true
});
var map = new ol.Map({
controls: ol.control.defaults({
attribution: false
}).extend([mousePositionControl]),
target: 'map',
layers: [
tiled, cross_tiled
],
view: new ol.View({
projection: projection
})
}); map.getView().fit(bounds, map.getSize()); var routeLayer = null; function queryRoute() {
if (routeLayer != null) {
map.removeLayer(routeLayer);
} var val1 = $("#id1").val();
var val2 = $("#id2").val();
routeLayer = new ol.layer.Tile({
visible: true,
source: new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/pg_ws/wms',
params: {
'FORMAT': format,
'VERSION': '1.1.1',
tiled: true,
STYLES: '',
LAYERS: 'pg_ws:xmpark_road_route',
'viewparams': 'a:' + val1 + ';b:' + val2
}
})
}); map.addLayer(routeLayer);
}
</script>
导航图层搜索
最终实现效果如下: