环境:

Win10

ArcMap10.4(用于数据处理)

postgresql9.4

postgis2.2.3

pgRouting2.3(postgresql插件)

说明:

继上一篇文章做了爆管分析找出周围所有影响阀门后(参见:https://www.cnblogs.com/giser-s/p/11662932.html),发现在业务上使用有局限性,因为通常爆管以后我并不需要关闭所有周围阀门,而是只要关闭上游阀门即可。

下面的方法,是在查找到周围所有阀门的基础上继续的,在周围阀门中找出与他相接的上游阀门

步骤:

 1、在PostGIS中,建立方向模型,模拟气体流向。原理就是新增length或者reverse_cost(关键字),与source/target方向一致的,则length为值reverse_cost为设一个大点的值9999999;如source/target方向不一致,则给reverse_cost赋值,length为设一个大点的值9999999。

这里在查询时,pgr_dijkstraCost会识别关键词cost和reverse_cost

双向查询时不用关注reverse_cost(注意cost字段一定要有,或可以如下例子将length命名cost的别名)

SELECT * FROM pgr_dijkstra( 'SELECT gid AS id, source, target, length as cost  FROM zy', 15139, 13670, directed := false);

单向查询时需要加上reverse_cost一起查(注意cost字段一定要有,或可以如下例子将length命名cost的别名)

SELECT * FROM pgr_dijkstra( 'SELECT gid AS id, source, target, length as cost,reverse_cost as reverse_cost  FROM zy', 15139, 13670, directed := true);

#正向结果:

#逆向结果:

2、这里查询到爆点周围所有影响阀门后,需要进行记录,这里采用拼接成字符串记录在数组中(这里略复杂,不同于上一博文直接返回输出了)

格式:[{v_uptap_gid||','||cast(v_uptap_geom as text)||','||up_temprow.source]}]

说明:[{阀门gid,转成字符串的geom,管段的头source}]

raise notice '%' , up_temprow.source;
--记录阀门所在管段source
IF (v_cost @> ARRAY[up_temprow.source::integer]) THEN
ELSE
SELECT array_append(v_cost,up_temprow.source) into v_cost;
SELECT array_append(v_cost,up_temprow.target) into v_cost;
END IF;
IF (res_tap_pipe is not null) THEN
select res_tap_pipe || ARRAY[v_uptap_gid||','||cast(v_uptap_geom as text)||','||up_temprow.source] into res_tap_pipe;
ELSE
select ARRAY[v_uptap_gid||','||cast(v_uptap_geom as text)||','||up_temprow.source] into res_tap_pipe;
END IF;

3、v_cost是周围所有阀门,利用pgRouting的pgr_dijkstraCost函数,逆向找出与爆点管段相接的阀门。

    --查找上游阀门
FOREACH m_cost IN ARRAY v_cost
LOOP
SELECT count(*) FROM pgr_dijkstraCost('select gid as id, source, target, length as cost, reverse_cost from zy',m_cost, ARRAY[v_startSource,v_startTarget], true) where agg_cost >= 9999999 into m_cost_value;
raise notice '%' , 'm_cost---'||cast(m_cost as text);
----如果没有消耗大于9999999的(阈值),则认为是上游阀门
IF(m_cost_value = 0) THEN
FOREACH m_tap_pipe IN ARRAY res_tap_pipe
LOOP
raise notice '%' , 'm_cost---'||cast(m_cost as text) ;
raise notice '%' , 'm_tap_pipe---'||cast(m_tap_pipe as text) ;
IF (split_part(m_tap_pipe, ',', 3)::integer = m_cost) THEN
--阀门id,阀门图形要素,阀门类型(上游/下游)
return query
select split_part(m_tap_pipe, ',', 1)::integer as res_uptap_gid,split_part(m_tap_pipe, ',', 2)::geometry as res_uptap_geom ,split_part(m_tap_pipe, ',', 3)::integer as res_source;
END IF;
END LOOP;
END IF;
END LOOP;

4、附上全部存储过程

 -- Function: test_getpoint8(character varying, double precision, double precision)

-- DROP FUNCTION test_getpoint8(character varying, double precision, double precision);

CREATE OR REPLACE FUNCTION test_getpoint8(
IN tbl character varying,
IN startx double precision,
IN starty double precision)
RETURNS TABLE(v_gid integer, v_res geometry, v_type integer) AS
$BODY$ declare
v_startLine geometry;--离起点最近的线
v_startTarget integer;--距离起点最近线的终点
v_startSource integer;
v_statpoint geometry;--在v_startLine上距离起点最近的点
v_endpoint geometry;--在v_endLine上距离终点最近的点
v_up_source integer;--游标,记录是否有记录
v_up_idx integer;--记录遍历到多少层级
v_uptap_gid integer;--上游阀门gid
v_uptap_geom geometry;--上游阀门要素
v_all_where integer[];--记录所有查询过的管段
v_up_where integer[];--where条件,将遍历到阀门的管段gid排除
v_down_where integer[];--where条件,将遍历到阀门的管段gid排除
up_temprow record ;
--v_cost record;--记录阀门管段source(用于计算消耗,判断方向)
m_cost integer;
m_cost_value integer;
temprow record;
v_cost integer[];
res_source integer;
res_tap_pipe text[];
m_tap_pipe text;
idx_tap_pipe integer; --遍历结果游标
m_up_cost integer;--上游阀门
v_up_cost integer[];--上游阀门集合
res_main_pipe integer[];--总阀门集合
m_main_pipe integer;--总阀门
begin
--查询离起点最近的线
--3857坐标系
--找起点15米范围内的最近线
execute 'select geom, source, target, ST_StartPoint(geom) as startpoint,ST_EndPoint(geom) as endpoint from ' ||tbl||
' where ST_DWithin(geom,ST_Geometryfromtext(''point('|| startx ||' ' || starty ||')'',3857),15)
order by ST_Distance(geom,ST_GeometryFromText(''point('|| startx ||' '|| starty ||')'',3857)) limit 1'
into v_startLine, v_startSource ,v_startTarget, v_statpoint ,v_endpoint;
raise notice '%' , 'v_startSource---'||cast(v_startSource as text);
IF(v_startLine is not null) THEN
--查找上游阀门
v_up_idx = 0;
v_up_source = 1;
--寻找上游阀门
SELECT array_append(v_up_where, v_startSource) into v_up_where;
--如果没有下级节点需要遍历
WHILE array_length(v_up_where,1) > 0
LOOP
--游标归零
v_up_source = 0;
--记录层级
--v_up_idx = v_up_idx + 1;
--获取当前层级节点
FOR up_temprow IN
select zy1.gid,zy1.source,zy1.target from zy zy1 where source = any(v_up_where) or target = any(v_up_where)
--select zy1.gid,zy1.source,zy1.target from zy zy1 where target = any(v_up_where)--找上游
LOOP
--清空需要查的点
IF(v_up_source = 0) THEN
v_up_where = null;
END IF;
--清空初始执行节点
--v_startSource = 0;
--标志执行有数据
v_up_source = 1;
--查询管网上的点
select t.gid,t.geom from fm t where t.gid in (
select a.gid from fm a,(select c.* from zy c where c.gid = up_temprow.gid) b where ST_intersects(a.geom,b.geom)
) into v_uptap_gid, v_uptap_geom;
raise notice '%' , 'UP---'||up_temprow.gid;
--如果没查找到阀门,则继续往下查
IF(v_uptap_gid is null) then
--source去重,判断如果数组中已有,则不添加
IF (v_up_where @> ARRAY[up_temprow.source::integer] OR v_all_where @> ARRAY[up_temprow.source::integer]) THEN
ELSE
SELECT array_append(v_up_where,up_temprow.source) into v_up_where;
SELECT array_append(v_all_where,up_temprow.source) into v_all_where;
END IF;
--target去重,判断如果数组中已有,则不添加
IF (v_up_where @> ARRAY[up_temprow.target::integer] OR v_all_where @> ARRAY[up_temprow.target::integer]) THEN
ELSE
SELECT array_append(v_up_where,up_temprow.target) into v_up_where;
SELECT array_append(v_all_where,up_temprow.target) into v_all_where;
END IF;
ELSE
raise notice '%' , up_temprow.source;
--记录阀门所在管段source
IF (v_cost @> ARRAY[up_temprow.source::integer]) THEN
ELSE
SELECT array_append(v_cost,up_temprow.source) into v_cost;
SELECT array_append(v_cost,up_temprow.target) into v_cost;
END IF;
IF (res_tap_pipe is not null) THEN
select res_tap_pipe || ARRAY[v_uptap_gid||','||cast(v_uptap_geom as text)||','||up_temprow.source] into res_tap_pipe;
ELSE
select ARRAY[v_uptap_gid||','||cast(v_uptap_geom as text)||','||up_temprow.source] into res_tap_pipe;
END IF;
END IF;
END LOOP;
END LOOP;
--raise notice '%' , v_cost;
raise notice '%' , 'res_tap_pipe---'||cast(res_tap_pipe as text);
--return query select * from v_cost;
raise notice '%' , 'v_cost---'||cast(v_cost as text); --查找上游阀门
FOREACH m_cost IN ARRAY v_cost
LOOP
SELECT count(*) FROM pgr_dijkstraCost('select gid as id, source, target, length as cost, reverse_cost from zy',m_cost, ARRAY[v_startSource,v_startTarget], true) where agg_cost >= 9999999 into m_cost_value;
raise notice '%' , 'm_cost---'||cast(m_cost as text);
----如果没有消耗大于9999999的(阈值),则认为是上游阀门
--IF(m_cost_value = 0) THEN
-- --判断上游阀门间是否有上下游关系
-- SELECT array_append(v_up_cost,m_cost) into v_up_cost;
--END IF;
IF(m_cost_value = 0) THEN
FOREACH m_tap_pipe IN ARRAY res_tap_pipe
LOOP
raise notice '%' , 'm_cost---'||cast(m_cost as text) ;
raise notice '%' , 'm_tap_pipe---'||cast(m_tap_pipe as text) ;
IF (split_part(m_tap_pipe, ',', 3)::integer = m_cost) THEN
--阀门id,阀门图形要素,阀门类型(上游/下游)
return query
select split_part(m_tap_pipe, ',', 1)::integer as res_uptap_gid,split_part(m_tap_pipe, ',', 2)::geometry as res_uptap_geom ,split_part(m_tap_pipe, ',', 3)::integer as res_source;
END IF;
END LOOP;
END IF;
END LOOP; raise notice '%' , '上游阀门---'||cast(v_up_cost as text);
END IF;
end; $BODY$
LANGUAGE plpgsql VOLATILE STRICT
COST 100
ROWS 1000;
ALTER FUNCTION test_getpoint8(character varying, double precision, double precision)
OWNER TO postgres;

结尾:

此文算是半成品文章,代码很乱也没有进行优化,很多没有用到的变量没有删掉,这里暂且当作思路的记录。

算法上还有很多需要改进,这里只是在找到所有周边阀门的基础上继续往下写的,其实可以不用分两块,直接上来就开始找上游阀门(算法留待后面继续优化)

-- Function: test_getpoint8(character varying, double precision, double precision)
-- DROP FUNCTION test_getpoint8(character varying, double precision, double precision);
CREATE OR REPLACE FUNCTION test_getpoint8(    IN tbl character varying,    IN startx double precision,    IN starty double precision)  RETURNS TABLE(v_gid integer, v_res geometry, v_type integer) AS$BODY$  
declare      v_startLine geometry;--离起点最近的线     v_startTarget integer;--距离起点最近线的终点     v_startSource integer;     v_statpoint geometry;--在v_startLine上距离起点最近的点      v_endpoint geometry;--在v_endLine上距离终点最近的点      v_up_source integer;--游标,记录是否有记录    v_up_idx integer;--记录遍历到多少层级    v_uptap_gid integer;--上游阀门gid    v_uptap_geom geometry;--上游阀门要素    v_all_where integer[];--记录所有查询过的管段    v_up_where integer[];--where条件,将遍历到阀门的管段gid排除    v_down_where integer[];--where条件,将遍历到阀门的管段gid排除    up_temprow record ;    --v_cost record;--记录阀门管段source(用于计算消耗,判断方向)    m_cost integer;    m_cost_value integer;    temprow record;    v_cost integer[];    res_source integer;    res_tap_pipe text[];    m_tap_pipe text;    idx_tap_pipe integer; --遍历结果游标    m_up_cost integer;--上游阀门    v_up_cost integer[];--上游阀门集合    res_main_pipe integer[];--总阀门集合    m_main_pipe integer;--总阀门begin     --查询离起点最近的线     --3857坐标系    --找起点15米范围内的最近线     execute 'select geom, source, target, ST_StartPoint(geom) as startpoint,ST_EndPoint(geom) as endpoint from ' ||tbl||                             ' where ST_DWithin(geom,ST_Geometryfromtext(''point('|| startx ||' ' || starty ||')'',3857),15)                            order by ST_Distance(geom,ST_GeometryFromText(''point('|| startx ||' '|| starty ||')'',3857))  limit 1'                             into v_startLine, v_startSource ,v_startTarget, v_statpoint ,v_endpoint;     raise notice '%' , 'v_startSource---'||cast(v_startSource as text);    IF(v_startLine is not null) THEN    --查找上游阀门    v_up_idx = 0;    v_up_source = 1;    --寻找上游阀门    SELECT array_append(v_up_where, v_startSource) into v_up_where;    --如果没有下级节点需要遍历    WHILE array_length(v_up_where,1) > 0     LOOP      --游标归零      v_up_source = 0;       --记录层级      --v_up_idx = v_up_idx + 1;      --获取当前层级节点            FOR up_temprow IN         select zy1.gid,zy1.source,zy1.target from zy zy1 where source = any(v_up_where) or target = any(v_up_where)         --select zy1.gid,zy1.source,zy1.target from zy zy1 where target = any(v_up_where)--找上游      LOOP        --清空需要查的点        IF(v_up_source = 0) THEN          v_up_where = null;        END IF;        --清空初始执行节点        --v_startSource = 0;        --标志执行有数据        v_up_source = 1;        --查询管网上的点        select t.gid,t.geom from fm t where t.gid  in (          select a.gid from fm a,(select c.* from zy c where c.gid = up_temprow.gid) b where ST_intersects(a.geom,b.geom)         ) into v_uptap_gid, v_uptap_geom;          raise notice '%' , 'UP---'||up_temprow.gid;        --如果没查找到阀门,则继续往下查        IF(v_uptap_gid is null) then          --source去重,判断如果数组中已有,则不添加          IF (v_up_where @> ARRAY[up_temprow.source::integer] OR v_all_where @> ARRAY[up_temprow.source::integer]) THEN          ELSE            SELECT array_append(v_up_where,up_temprow.source) into v_up_where;            SELECT array_append(v_all_where,up_temprow.source) into v_all_where;          END IF;          --target去重,判断如果数组中已有,则不添加          IF (v_up_where @> ARRAY[up_temprow.target::integer] OR v_all_where @> ARRAY[up_temprow.target::integer]) THEN          ELSE            SELECT array_append(v_up_where,up_temprow.target) into v_up_where;            SELECT array_append(v_all_where,up_temprow.target) into v_all_where;          END IF;        ELSE          raise notice '%' , up_temprow.source;          --记录阀门所在管段source          IF (v_cost @> ARRAY[up_temprow.source::integer]) THEN          ELSE            SELECT array_append(v_cost,up_temprow.source) into v_cost;            SELECT array_append(v_cost,up_temprow.target) into v_cost;          END IF;          IF (res_tap_pipe is not null) THEN            select res_tap_pipe || ARRAY[v_uptap_gid||','||cast(v_uptap_geom as text)||','||up_temprow.source] into res_tap_pipe;          ELSE            select ARRAY[v_uptap_gid||','||cast(v_uptap_geom as text)||','||up_temprow.source] into res_tap_pipe;          END IF;        END IF;      END LOOP;    END LOOP;    --raise notice '%' , v_cost;    raise notice '%' , 'res_tap_pipe---'||cast(res_tap_pipe as text);    --return query select * from v_cost;    raise notice '%' , 'v_cost---'||cast(v_cost as text);
    --查找上游阀门    FOREACH m_cost IN ARRAY v_cost     LOOP      SELECT count(*) FROM pgr_dijkstraCost('select gid as id, source, target, length as cost, reverse_cost from zy',m_cost, ARRAY[v_startSource,v_startTarget], true) where agg_cost >= 9999999 into m_cost_value;      raise notice '%' , 'm_cost---'||cast(m_cost as text);      ----如果没有消耗大于9999999的(阈值),则认为是上游阀门      --IF(m_cost_value = 0) THEN      --  --判断上游阀门间是否有上下游关系      --  SELECT array_append(v_up_cost,m_cost) into v_up_cost;      --END IF;      IF(m_cost_value = 0) THEN                  FOREACH m_tap_pipe IN ARRAY res_tap_pipe         LOOP          raise notice '%' , 'm_cost---'||cast(m_cost as text)  ;          raise notice '%' , 'm_tap_pipe---'||cast(m_tap_pipe as text)  ;          IF (split_part(m_tap_pipe, ',', 3)::integer = m_cost) THEN             --阀门id,阀门图形要素,阀门类型(上游/下游)            return query            select split_part(m_tap_pipe, ',', 1)::integer as res_uptap_gid,split_part(m_tap_pipe, ',', 2)::geometry  as res_uptap_geom ,split_part(m_tap_pipe, ',', 3)::integer as res_source;          END IF;        END LOOP;      END IF;    END LOOP;
     raise notice '%' , '上游阀门---'||cast(v_up_cost as text);     END IF;end;  
$BODY$  LANGUAGE plpgsql VOLATILE STRICT  COST 100  ROWS 1000;ALTER FUNCTION test_getpoint8(character varying, double precision, double precision)  OWNER TO postgres;

PostGIS 爆管分析之找出上游阀门的更多相关文章

  1. PostGIS 爆管分析之找出上游阀门(优化版)

    说明 前面描述过利用postgis查找上游阀门的原理,以及代码,其实当初写完就发现又很大的优化空间,但一直没有时间去做. 最近遇到一个情况,处理60w+条管网数据时,效率太慢了,于是腾时间优化了一版. ...

  2. PostGIS 爆管分析之找出总阀门

    这个算法算是被摒弃了,但是很多自己思考过后留下的成果,虽然不用了,留着做记录. 算法目的是为了发生爆管后找到总阀门,这里分了几个步骤: 1.找到爆管点所在管段 2.通过遍历找到爆管点所有影响的阀门 3 ...

  3. PostGIS 爆管分析之根据爆点找出所有影响阀门

    环境: Win10 ArcMap10.4(用于数据处理) postgresql9.4 postgis2.2.3 pgRouting2.3(postgresql插件) 说明: 做爆管分析的第一步,需要先 ...

  4. Python练习六十:网页分析,找出里面的正文与链接

    网页分析,找出里面的正文与链接 代码如下: from urllib import request from bs4 import BeautifulSoup request = request.url ...

  5. 《BI那点儿事》Microsoft 决策树算法——找出三国武将特性分布,献给广大的三国爱好者们

    根据游戏<三国志11>武将数据,利用决策树分析,找出三国武将特性分布.其中变量包括统率.武力.智力.政治.魅力.身分.变量说明:统率:武将带兵出征时的部队防御力.统帅越高受到普通攻击与兵法 ...

  6. 如何找出你性能最差的SQL Server查询

    我经常会被反复问到这样的问题:”我有一个性能很差的SQL Server.我如何找出最差性能的查询?“.因此在今天的文章里会给你一些让你很容易找到问题答案的信息向导. 问SQL Server! SQL ...

  7. 记录一下通过分析Tomcat内部jar包找出request.getReader()所用的字符编码在哪里设置和起效的完整分析流程

    前言: 之前写Java服务端处理POST请求时遇到了请求体转换成字符流所用编码来源的疑惑,在doPost方法里通过request.getReader()获取的BufferedReader对象内部的 R ...

  8. Bugku-CTF分析篇-weblogic(黑客攻击了Weblogic应用,请分析攻击过程,找出Weblogic的主机名。)

    weblogic 黑客攻击了Weblogic应用,请分析攻击过程,找出Weblogic的主机名. flag格式:flag{} Tip:主机名为十六进制.  

  9. c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)

    c#封装DBHelper类   public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...

随机推荐

  1. Could not initialize class com.fasterxml.jackson.databind.SerializationConfig

    问题 Spring web 与 Spring eureka集成后出现错误: Caused by: java.lang.NoClassDefFoundError: Could not initializ ...

  2. jquery 往上滚动的时显示,上下滚动执行的js

    $(document).on("mousewheel DOMMouseScroll", function (e) { var delta = (e.originalEvent.wh ...

  3. 微信小程序文件压缩上传

    试用场景:上传图片过大,需进行压缩处理. 涉及微信API API 说明 文档 chooseImage 选择图片 https://developers.weixin.qq.com/miniprogram ...

  4. PassWord控件

    <StackPanel Margin="> <Label>Text:</Label> <TextBox /> <Label>Pas ...

  5. solr学习篇(二) solr 分词器篇

    关于solr7.4搭建与配置可以参考 solr7.4 安装配置篇  在这里我们探讨一下分词的配置 目录 关于分词 配置分词 验证成功 1.关于分词 1.分词是指将一个中文词语拆成若干个词,提供搜索引擎 ...

  6. ORM之单表操作

    ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...

  7. Python安装pyinstaller方法,以及将项目生成可执行程序的步骤

    pyinstaller安装方法 前提:确保计算机安装了Python语言环境,并且正确配置了环境变量. 方法一:联网在线自动安装 选择一 Windows OS下进入cmd(命令行窗口) 输入:pip i ...

  8. MIT线性代数:1.方程组的几何解析

  9. python机器学习——感知器

    最近在看机器学习相关的书籍,顺便把每天阅读的部分写出来和大家分享,共同学习探讨一起进步!作为机器学习的第一篇博客,我准备从感知器开始,之后会慢慢更新其他内容. 在实现感知器算法前,我们需要先了解一下神 ...

  10. Android9.0 SystemUI 网络信号栏定制修改

    前情提要 Android 8.1平台SystemUI 导航栏加载流程解析 9.0 改动点简要说明 1.新增 StatusBarMobileView 替代 SignalClusterView,用以控制信 ...