如何计算SQL中两点沿线的距离
How to calculate distance of two points along line in TSQL
我在 MSSQL 数据库中添加了代表 map.As、
中的一条路线的行
update route
set locationArray=geography::STGeomFromText('LINESTRING(7.068419558716859 80.140939950943,7.070868430303301 80.14106869697571,7.072976578922316 80.14136910438538,7.074339417352194 80.14203429222107,7.07631978477513 80.14278531074524,7.078491790888004 80.14435172080994,7.078300143701023 80.14649748802185,7.078449202631107 80.14877200126648,7.076617903866453 80.15068173408508,7.074978246481405 80.15248417854309,7.074105179783337 80.15490889549255,7.0723803358444135 80.15643239021301)', 4326)
where routeID=45;
现在我选择了直线上的两个点,然后我想计算这两条直线之间的距离(不是直接距离或位移),类似于图像。
我已经尝试了 STLength() 函数和 STDistance() 函数。但它们都提供直接距离。有人可以帮我用 TSQL 计算沿线的距离吗?
Returns the total length of the elements in a geometry instance.
STLength (geography Data Type)
Returns the total length of the elements in a geography instance or
the geography instances within a GeometryCollection.
我计算了Excel中每个段SQRT(X*X + Y*Y)
的简单长度之和,并与SQL服务器进行了比较。对于 geometry
类型,Excel 给出了与 STLength
相同的结果。 STLength
不 计算从起点到终点的距离,它计算每个段的长度,然后按预期将它们相加。
DECLARE @G1 geometry = geometry::STGeomFromText ('LINESTRING(7.068419558716859 80.140939950943,7.070868430303301 80.14106869697571,7.072976578922316 80.14136910438538,7.074339417352194 80.14203429222107,7.07631978477513 80.14278531074524,7.078491790888004 80.14435172080994,7.078300143701023 80.14649748802185,7.078449202631107 80.14877200126648,7.076617903866453 80.15068173408508,7.074978246481405 80.15248417854309,7.074105179783337 80.15490889549255,7.0723803358444135 80.15643239021301)', 4326);
DECLARE @G2 geography = geography::STGeomFromText('LINESTRING(7.068419558716859 80.140939950943,7.070868430303301 80.14106869697571,7.072976578922316 80.14136910438538,7.074339417352194 80.14203429222107,7.07631978477513 80.14278531074524,7.078491790888004 80.14435172080994,7.078300143701023 80.14649748802185,7.078449202631107 80.14877200126648,7.076617903866453 80.15068173408508,7.074978246481405 80.15248417854309,7.074105179783337 80.15490889549255,7.0723803358444135 80.15643239021301)', 4326);
SELECT @G1.STLength() AS LGeometry, @G2.STLength() AS LGeography;
结果
LGeometry LGeography
0.0252888043080671 1809.85271737151
所以,STLength
工作正常,请检查您的代码。检查您使用的类型是否正确(geometry
vs geography
)。
我在 MSSQL 数据库中添加了代表 map.As、
中的一条路线的行update route
set locationArray=geography::STGeomFromText('LINESTRING(7.068419558716859 80.140939950943,7.070868430303301 80.14106869697571,7.072976578922316 80.14136910438538,7.074339417352194 80.14203429222107,7.07631978477513 80.14278531074524,7.078491790888004 80.14435172080994,7.078300143701023 80.14649748802185,7.078449202631107 80.14877200126648,7.076617903866453 80.15068173408508,7.074978246481405 80.15248417854309,7.074105179783337 80.15490889549255,7.0723803358444135 80.15643239021301)', 4326)
where routeID=45;
现在我选择了直线上的两个点,然后我想计算这两条直线之间的距离(不是直接距离或位移),类似于图像。
我已经尝试了 STLength() 函数和 STDistance() 函数。但它们都提供直接距离。有人可以帮我用 TSQL 计算沿线的距离吗?
Returns the total length of the elements in a geometry instance.
STLength (geography Data Type)
Returns the total length of the elements in a geography instance or the geography instances within a GeometryCollection.
我计算了Excel中每个段SQRT(X*X + Y*Y)
的简单长度之和,并与SQL服务器进行了比较。对于 geometry
类型,Excel 给出了与 STLength
相同的结果。 STLength
不 计算从起点到终点的距离,它计算每个段的长度,然后按预期将它们相加。
DECLARE @G1 geometry = geometry::STGeomFromText ('LINESTRING(7.068419558716859 80.140939950943,7.070868430303301 80.14106869697571,7.072976578922316 80.14136910438538,7.074339417352194 80.14203429222107,7.07631978477513 80.14278531074524,7.078491790888004 80.14435172080994,7.078300143701023 80.14649748802185,7.078449202631107 80.14877200126648,7.076617903866453 80.15068173408508,7.074978246481405 80.15248417854309,7.074105179783337 80.15490889549255,7.0723803358444135 80.15643239021301)', 4326);
DECLARE @G2 geography = geography::STGeomFromText('LINESTRING(7.068419558716859 80.140939950943,7.070868430303301 80.14106869697571,7.072976578922316 80.14136910438538,7.074339417352194 80.14203429222107,7.07631978477513 80.14278531074524,7.078491790888004 80.14435172080994,7.078300143701023 80.14649748802185,7.078449202631107 80.14877200126648,7.076617903866453 80.15068173408508,7.074978246481405 80.15248417854309,7.074105179783337 80.15490889549255,7.0723803358444135 80.15643239021301)', 4326);
SELECT @G1.STLength() AS LGeometry, @G2.STLength() AS LGeography;
结果
LGeometry LGeography
0.0252888043080671 1809.85271737151
所以,STLength
工作正常,请检查您的代码。检查您使用的类型是否正确(geometry
vs geography
)。