ST_LineCrossingDirection — 返回一个数字,指示两个 LineString 的交叉行为
integer ST_LineCrossingDirection(
geometry linestringA, geometry linestringB)
;
给定两个线串返回一个介于 -3 和 3 之间的整数,指示它们之间存在哪种交叉行为。 0表示没有交叉。 仅 LINESTRING
支持此功能。
交叉编号的含义如下:
0:线无交叉
-1:向左交叉线
1:向右交叉线
-2:表示一条线段多次交叉,并且最后一次交叉是从左侧
2:表示一条线段多次交叉,并且最后一次交叉是从右侧
-3:表示一条线段多次交叉,但最后一次交叉是从同一侧(首次)的左侧
3:表示一条线段多次交叉,但最后一次交叉是从同一侧(首次)的右侧
有效性:1.4
示例: 向左交叉线和向右交叉线
SELECT ST_LineCrossingDirection(lineA, lineB) As A_cross_B, ST_LineCrossingDirection(lineB, lineA) As B_cross_A FROM (SELECT ST_GeomFromText('LINESTRING(25 169,89 114,40 70,86 43)') As lineA, ST_GeomFromText('LINESTRING (20 140, 71 74, 161 53)') As lineB ) As foo; A_cross_B | B_cross_A -----------+----------- -1 | 1
示例: 一条线段多次交叉并且最后一次交叉从同一侧(首次)左侧和一条线段多次交叉并且最后一次交叉从同一侧(首次)右侧
SELECT ST_LineCrossingDirection(lineA, lineB) As A_cross_B, ST_LineCrossingDirection(lineB, lineA) As B_cross_A FROM (SELECT ST_GeomFromText('LINESTRING(25 169,89 114,40 70,86 43)') As lineA, ST_GeomFromText('LINESTRING(171 154,20 140,71 74,161 53)') As lineB ) As foo; A_cross_B | B_cross_A -----------+----------- 3 | -3
示例: 一条线段多次交叉并且最后一次交叉从左侧和一条线段多次交叉并且最后一次交叉从右侧
SELECT ST_LineCrossingDirection(lineA, lineB) As A_cross_B, ST_LineCrossingDirection(lineB, lineA) As B_cross_A FROM (SELECT ST_GeomFromText('LINESTRING(25 169,89 114,40 70,86 43)') As lineA, ST_GeomFromText('LINESTRING(5 90, 71 74, 20 140, 171 154)') As lineB ) As foo; A_cross_B | B_cross_A -----------+----------- -2 | 2
示例: 查找所有交叉的街道
SELECT s1.gid, s2.gid, ST_LineCrossingDirection(s1.geom, s2.geom) FROM streets s1 CROSS JOIN streets s2 ON (s1.gid != s2.gid AND s1.geom && s2.geom ) WHERE ST_LineCrossingDirection(s1.geom, s2.geom) > 0;