OSM2PO导入后用于创建带有pgrouting的集水区的OSM道路网络的分割线

时间:2018-04-04 07:39:54

标签: postgresql openstreetmap postgis pgrouting

我想基于osm.pbf文件创建一个带有pgrouting的集水区。在使用OSM2PO导入pbf文件后,使用pgrouting计算集水区工作正常。但是为了得到更详细的结果,至少在2km之后将所有较长的道路分开会很不错。使用PostGIS分割线条没有问题,但这会破坏我的路由网络。是另一种添加更多顶点的方法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

实际上,您可以按照自己的意愿分割路线,然后通过pgr_createTopology和pgr_analyzeGraph重新创建路线图。 我已经在plpgsql中使用以下功能成功做到了这一点

CREATE OR REPLACE FUNCTION public.__create_roads_graph(
    tabname text)
    RETURNS text
    LANGUAGE 'plpgsql'
AS $BODY$

DECLARE
        counter integer;
        expr text;
        vert_tab character varying;
        r record;
BEGIN
    expr := 'select count(*) as cnt from %s';
    expr := format(expr, tabname);
    counter := 0;
    for r in execute expr
    loop
        counter = r.cnt;
    end loop;
    if counter = 0
    then
        return 'Cancel';
    end if;

    expr := 'update ' || tabname || ' set km = ST_Length(geom_way::geography)/1000';
    execute expr;
    expr := 'update ' || tabname || ' set kmh=(CASE WHEN class=1 THEN 90 WHEN class=2 THEN 60 WHEN class=3 THEN 30 WHEN class=4 THEN 10 ELSE 0 END)
                            WHERE kmh is null OR kmh < 1';
    execute expr;
    expr := 'update ' || tabname || ' set cost = km / kmh where cost is null or cost=0';
    execute expr;
    expr := 'update ' || tabname || ' set reverse_cost = km / kmh where reverse_cost is null or reverse_cost=0';
    execute expr;
    expr := 'update ' || tabname || ' set x1=ST_X(ST_StartPoint(geom_way)), 
                           y1=ST_Y(ST_StartPoint(geom_way)), 
                           x2=ST_X(ST_EndPoint(geom_way)), 
                           y2=ST_Y(ST_EndPoint(geom_way))';
    execute expr;
    expr := format('select * from pgr_createTopology( %L, %s, %L, %L, %L, %L, %L, %s)', 
                   tabname, 0.000001, 'geom_way', 'id', 'source', 'target', 'true', 'true');
    execute expr;

    vert_tab := tabname ||  '_vertices_pgr';
    perform __bvv_recreate_vertices_table(vert_tab);

    expr := format('insert into %s(id, cnt, the_geom) with RECURSIVE united as (SELECT source AS id, x1 AS x, y1 AS y
                       FROM %s UNION ALL select target AS id, x2 AS x, y2 AS y
                       FROM %s) select id, count(*) as cnt, 
                       ST_SetSrid(ST_MakePoint(avg(x), avg(y)), 4326) from united group by id order by id', 
                        vert_tab, tabname, tabname);



    /*expr := format('select * from pgr_analyzeGraph( %L, %s, %L, %L, %L, %L)', 
                   tabname, 0.000001, 'geom_way', 'id', 'source', 'target');*/
    execute expr;
    RETURN 'Ok';
END

您可以看到分割路段之前的路线外观 Nodes and route before splitting

分割并执行_create_roads_graph函数后,您可以在行中心看到其他节点,更改了节点的编号,并与以前的路线相同

After splitting