【问题标题】:Find Segments lying inside another using Geography points SQL Server使用地理点 SQL Server 查找位于另一个内部的段
【发布时间】:2020-08-04 15:56:43
【问题描述】:

让我解释一下附图Yellow line termed as Construction Planning identified by Planning Id and all other colors termed as Construction Segment identified by SegmentId。我正在尝试使用地理点的STDistance 函数通过传递PlanningID=25801 来查找SegmentId=12689SegmentId=12687。我的程序应该返回值1268912687,因为这两个值完全位于PlanningID=25801 中。但是,它应该排除值SegmentId=12688,因为该段不完全位于或该段的大部分不在施工规划内。 我在 SQL Server 中创建了以下脚本

declare    @Planningid INT
declare    @agencyID int
set @Planningid=25801
set @agencyID=79
DECLARE @finalresult int
DECLARE @result TABLE (id INT, starting sys.geography, ending sys.geography,
                          chachedroute sys.geography)
DECLARE @tablevar TABLE(id INT)
insert into @result select id,
starting,Ending,CachedRoute from Route R where AgencyId=@agencyID and LayerId=1 
                  and CachedRoute.STDistance((select CachedRoute from route 
                                               where id=@Planningid))<0.5
select * from @result
/* the above part of the code is working fine as it should */

/* the next part of the logic is not working and it is excluding all the Segment Id when it 
                                should not and should only exclude SegmentId=12688 */
insert into @tablevar
select id from @result where 
            chachedroute.STDistance((select starting from route where id=@Planningid))<700 
            and 
            chachedroute.STDistance((select ending from route where id=@Planningid))<700
intersect
select id from @result

select * from @tablevar

我猜这个问题是因为代码第二部分中的and 逻辑,它计算值是否在 700 米之内。这段代码最初是根据每个PlanningId 应该只有一个SegmentID 的概念创建的,这就是我通过匹配段的开始和结束来编码它的原因。但现在的修改是单个PlanningId 可以有多个SegmentID,我猜这就是问题所在。 任何有关如何修改代码的帮助将不胜感激。 表Route 包含以下属性:

  1. ID(整数)
  2. 起点(地理点)
  3. 结局(地理点)
  4. 缓存路线(地理点)
  5. LayerId (int)
  6. AgencyId(int)

LayerId=1指的是Construction SegmentLayerId=4指的是Construction Planning

【问题讨论】:

    标签: sql-server sqlgeography


    【解决方案1】:

    我已将上述函数更改为以下使其工作:

    CREATE FUNCTION [dbo].[GetSegmentId]
    (   
        @Planningid INT,
        @agencyID int
    )
    
    RETURNS @tablevar TABLE 
    (
        id INT
    )
    
    AS
    
    BEGIN
            declare    @CachedRoute geography
            DECLARE @finalresult int
            DECLARE @result TABLE (id INT, starting sys.geography, ending sys.geography,chachedroute sys.geography)
            
    
            set @CachedRoute = (select CachedRoute from route where id = @Planningid)
    
            insert into @result select id,
            starting,Ending,CachedRoute from Route R where AgencyId=@agencyID and LayerId=1 and CachedRoute.STDistance(@CachedRoute)<0.5 
    
            insert into @tablevar
            select id from @result where @CachedRoute.STDistance(starting)<700 and @CachedRoute.STDistance(ending)<700
            intersect
            select id from @result
    
    
        RETURN;
    END;
    

    【讨论】:

      猜你喜欢
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多