SQL服务器支持路由测距吗?

Does SQL Server support measuring distance via routes?

我一直在试验 SQL 服务器空间函数,发现 STDistance 方法允许您确定两个几何图形之间的距离。是否有任何功能可以让您通过道路测量两点之间的距离,例如 Google 地图?

是的,你可以...预先警告,因为它需要一些工作(这当然是 "free" 在 GIS 中,如 ArcGIS 或 postGIS)。

基本上,当您测量沿一系列道路的两点之间的距离(与直接欧氏距离相反)时,您实际上是在穿越网络 graph

所以你需要

  1. 加载您的道路(图形边缘)
  2. 加载交叉点(图形节点)
  3. 在 A 点和 Z 点加载
  4. 然后在 A 和 Z 之间使用最优路由算法,沿边但通过节点。

这里有一个深入的post解释:Roads in SQL Server 2008 but you need to hook in the routing algorithm from here Djkstra in SQL

从 Alastaira 的网站(上图)生成道路网络的代码是重复的,以防它消失。

DECLARE @Roads TABLE (
 RoadId int,
 RoadName varchar(32)
);

INSERT INTO @Roads VALUES
(1, 'Britannia Road'),
(2, 'Belsize Road'),
(3, 'Vincent Road'),
(4, 'Plumstead Road');

DECLARE @RoadSegments TABLE (
 SegmentId int,
 RoadId int,
 SegmentGeometry geography
);

INSERT INTO @RoadSegments VALUES
(1, 1, 'LINESTRING(1.313772 52.636871, 1.315038 52.635229)'),
(2, 1, 'LINESTRING(1.315038 52.635229,1.316052 52.63399,1.316401 52.633518)'),
(3, 1, 'LINESTRING(1.316401 52.633518,1.316497 52.632869,1.316642 52.632542)'),
(4, 2, 'LINESTRING(1.317538 52.632697,1.317307 52.633448,1.317098 52.633749)'),
(5, 3, 'LINESTRING(1.31734 52.633818,1.315982 52.635498,1.315038 52.635229)'),
(6, 4, 'LINESTRING(1.314546 52.633479,1.31529 52.633298,1.315902 52.633363,1.316401 52.633518)'),
(7, 4, 'LINESTRING(1.316401 52.633518,1.317097 52.633749)'),
(8, 4, 'LINESTRING(1.317098 52.633749,1.31734 52.633818)'),
(9, 4, 'LINESTRING(1.31734 52.633818,1.318332 52.634119)');

DECLARE @RoadIntersections TABLE (
 IntersectionId varchar(32),
 IntersectionLocation geography
);

INSERT INTO @RoadIntersections VALUES
('A', 'POINT(1.315038 52.635229)'),
('B', 'POINT(1.316401 52.633518)'),
('C', 'POINT(1.317097 52.633749)'),
('D', 'POINT(1.31734 52.633818)');

DECLARE @RoadIntersection_Segments TABLE (
 IntersectionId varchar(32),
 SegmentId int
);

INSERT INTO @RoadIntersection_Segments VALUES
('A',1),
('A',2),
('A',5),
('B',2),
('B',6),
('B',3),
('B',7),
('C',7),
('C',4),
('C',8),
('D',5),
('D',8),
('D',9);