postgresql服务器编程——计算最近点及线段分割

drop function if exists processstationPoint(); create function processstationPoint() returns text as $$ declare v_stationId integer; v_stationGeom geometry; v_startPoint geometry; v_stationSource integer; v_stationTarget integer; v_stationLine geometry; v_stationLineId integer; v_stationSrcpulloutable boolean; v_stationDstPulloutable boolean; v_searchNode geometry; v_searchNodeId integer; v_searchLine geometry; v_searchLineMerged geometry; v_searchLineId integer; v_searchLineOneway varchar; v_searchLinesrc integer; v_searchLineDst integer; v_searchLinesrcIn integer; v_searchLinesrcOut integer; v_searchLineDstIn integer; v_searchLineDstOut integer; v_srcsegline geometry; v_dstsegline geometry; v_curs cursor for select gid,geom from station order by gid asc; begin v_stationSrcpulloutable := false; v_stationDstPulloutable := false; v_searchNodeId := -1; drop table if exists stationNear; CREATE TABLE stationNear ( stationId integer,geom geometry(Point),CONSTRAINT "stationNear_pkey" PRIMARY KEY (stationid) ); open v_curs; loop fetch v_curs into v_stationId,v_stationGeom; --raise notice 'fetch v_curs into %,%',v_stationId,v_stationGeom; if not found then exit;end if; /* v_searchNode := null; select id,the_geom into v_searchNodeId,v_searchNode from changsharoad_connected_vertices_pgr where ST_distance(the_geom,v_stationGeom) < 0.000001; if (v_searchNode != null) then update station set isNode = true where gid = v_stationId; update station set nodeId = v_searchNodeId where gid = v_stationId; end if;*/ v_searchLine := null; select gid,geom,oneway,source,target into v_searchLineId,v_searchLine,v_searchLineOneway,v_searchLinesrc,v_searchLineDst from changsharoad_connected where ST_DWithin(geom::geography,v_stationGeom::geography,1000,true) order by ST_distance(geom,v_stationGeom) limit 1; if (v_searchLine is null) then --raise notice '%,v_searchLine is null',v_stationId; else v_startPoint := ST_ClosestPoint(v_searchLine,v_stationGeom); INSERT INTO stationNear(stationid,geom) VALUES(v_stationId,v_startPoint) ON conflict(stationid) DO update set stationid = v_stationId,geom = v_startPoint; raise notice 'ST_ClosestPoint is on v_searchLine: %',ST_distanceSphere(v_searchLine,v_stationGeom) < 0.1; select ein,eout into v_searchLinesrcIn,v_searchLinesrcOut from changsharoad_connected_vertices_pgr where id = v_searchLinesrc; select ein,eout into v_searchLineDstIn,v_searchLineDstOut from changsharoad_connected_vertices_pgr where id = v_searchLineDst; if (v_searchLineOneway != 'T' and v_searchLineDstOut > 0) then v_stationDstPulloutable := true; --raise notice '%,v_stationPulloutable is true',v_stationId; else --raise notice '%,v_stationPulloutable is false',v_stationId; end if; if (v_searchLineOneway != 'F' and v_searchLinesrcOut > 0) then v_stationSrcpulloutable := true; --raise notice '%,v_stationId; end if; v_searchLineMerged := ST_LineMerge(v_searchLine); declare v_lineFraction float; v_isNode boolean; v_nodeId integer; begin v_nodeId := -1; v_lineFraction := ST_LineLocatePoint(v_searchLineMerged,v_startPoint); if (v_lineFraction < 0.0001) then v_srcsegline = null; v_isNode := true; v_nodeId := v_searchLinesrc; v_lineFraction := 0; --raise notice '坐标跟起点相同'; else v_srcsegline := st_reverse(st_linesubstring(v_searchLineMerged,v_lineFraction)); end if; if ((1.0 - v_lineFraction) < 0.0001) then v_dstsegline = null; v_isNode := true; v_nodeId := v_searchLineDst; v_lineFraction := 1; --raise notice '坐标跟终点相同'; else v_dstsegline := st_linesubstring(v_searchLineMerged,v_lineFraction,1); end if; update station set lineId = v_searchLineId,startPoint = v_startPoint,isnode = v_isNode,nodeid = v_nodeId,source = v_searchLinesrc,lineFraction = v_lineFraction,target = v_searchLineDst,srcpulloutable = v_stationSrcpulloutable,dstPulloutable = v_stationDstPulloutable,srcsegline = v_srcsegline,dstsegline = v_dstsegline where gid = v_stationId; --raise notice 'line geometry: %,st_astext(v_srcsegline),st_astext(v_dstsegline); --raise notice '%,update station.... ok!',v_stationId; end; end if; end loop; close v_curs; return 'ok'; end; $$ language plpgsql;

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...