SQL Server是否支持通过路由测量距离?

时间:2017-01-17 22:43:48

标签: sql-server tsql gis spatial

我一直在尝试使用SQL Server空间函数,并且看到STDistance方法允许您确定两个几何之间的距离。是否有任何功能允许您通过道路测量两点之间的距离,例如谷歌地图?

1 个答案:

答案 0 :(得分:1)

是的,你可以......预先警告,因为它需要一些工作(当然,在ArcGIS或postGIS等GIS中你“免费”)。

基本上想一想,当你测量沿着一系列道路的两点之间的距离时(而不是直接的欧几里德距离),你实际上是在穿越网络graph

所以你需要

  1. 在道路上加载(图形边缘)
  2. 在交叉点(图形节点)中加载
  3. 在A点和Z点加载
  4. 然后在A和Z之间使用沿边缘但通过节点的最佳路由算法。
  5. 这里有一篇深度帖子解释它:Roads in SQL Server 2008但你需要从这里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);