公交线路方向处理(Oracle行转列+排序)

1、根据公交站点、线路站点、线路等表关联计算出相关数据。

insert into biz_bus_station_direct
select ls.line_no line_no,
       bb.line_name line_name,
       t1.line_direct line_direct,
       s.station_id station_id,
       s.station_name station_name,
       t.point_num point_num_now,
       s.location_x x_now,
       s.location_y y_now,
       t1.point_num point_num_last,
       t1.point_x x_last,
       t1.point_y y_last,
       s.location_x - t1.point_x x_value,
       s.location_y - t1.point_y y_value,
       0 direction,
       0 rn,
       '' STATION_DIRECT,
       sysdate create_time
  from biz_bus_station s
  join biz_bus_line_station ls
    on ls.station_id = s.station_id
  left join biz_bus_line bb on ls.line_no=bb.line_no
  left join biz_bus_line_distance t
    on t.station_id = s.station_id
   and t.line_no = ls.line_no
  left join biz_bus_line_distance t1
    on t1.line_no = t.line_no
   and t1.point_num + 2 = t.point_num
   and t.line_direct = t1.line_direct;

2、根据线路和站点ID对行号进行赋值。

  for t2 in (select t1.line_no,t1.station_id,rank() over(partition by replace(replace(t1.station_name,'-1',''),'-2','') order by t1.line_no,t1.station_id) rn from biz_bus_station_direct t1) loop
    update biz_bus_station_direct t
    set t.rn=t2.rn
    where t.line_no=t2.line_no
    and t.station_id=t2.station_id;
  end loop;

3、根据行号,逐行取值,赋方向值
/*cosθ=向量a.向量b/|向量a|×|向量b|
=(x1x2+y1y2)/[√(x12+y12)*√(x22+y22)]
夹角小于180度,则cosθ大于0,表示当前计算的车辆与RN=1的车辆方向一致
*/

  for t2 in (select replace(replace(t1.station_name,'-1',''),'-2','') station_name,t1.x_value,t1.y_value,t1.rn from biz_bus_station_direct t1 where t1.rn=1) loop
    update biz_bus_station_direct t
    set t.direction=case when t.x_value!=0 and t.y_value!=0 and t2.x_value!=0 and t2.y_value!=0 then (case when (t2.x_value*t.x_value+t2.y_value*t.y_value)/(SQRT(t2.x_value*t2.x_value+t2.y_value*t2.y_value)*SQRT(t.x_value*t.x_value+t.y_value*t.y_value))>0 then 1 else -1 end) else 1 end
    where t.rn <=(select max(rn) from biz_bus_station_direct where replace(replace(station_name,'-1',''),'-2','')=t2.station_name)
    and replace(replace(t.station_name,'-1',''),'-2','')=t2.station_name;
  end loop;

4、根据站点给各个线路赋值途径站点

  for t2 in (
    select A.line_no,A.line_direct,max(KEY) Station_DIRECT from (
select t.line_no,t.line_direct,
WMSYS.WM_CONCAT(t.station_name) OVER(PARTITION BY t.line_no,t.line_direct ORDER BY t.line_direct,t.point_num) KEY,
row_number() over(PARTITION BY t.line_no,t.line_direct ORDER BY t.line_direct,t.point_num) rs
from (
select t1.line_no,t1.line_direct,t1.point_num,t2.station_name from BIZ_BUS_LINE_DISTANCE t1
join biz_bus_station t2 on t1.station_id=t2.station_id
order by t1.line_direct,t1.point_num) t
) A
group by A.line_no,A.line_direct
    ) loop
    update biz_bus_station_direct bbs
    set bbs.station_direct=t2.station_direct
    where bbs.line_no=t2.line_no and bbs.line_direct=t2.line_direct;
  end loop;

5、提交事务和异常处理。

  commit;

--异常处理
exception
  when others then
    rollback;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值