将一行的两个端点连接到点表中的两个点

时间:2013-12-28 14:04:47

标签: sql sql-server join geospatial

我有一个线段表(没有多线)和我需要使用几何体连接(最终进入视图)的点表:LineID,FromPtID,ToPtID。我遇到的问题是我想要使用点几何两次 - 一次用于线的每一端 - “FromGeoPt”和“ToGeoPt”

    SELECT TOP 100
(SELECT top 1
    [CadData].Survey.SurveyPoint.Geometry as FromGeoPt
    FROM
    [CadData].dbo.CleanLines ,
    [CadData].Survey.SurveyPoint
    WHERE 
    SurveyPoint.Geometry.STEquals(CleanLines.Geom.STStartPoint())=1
    ),
(SELECT TOP 1
    [CadData].Survey.SurveyPoint.Geometry as ToGeoPt
    FROM
    [CadData].dbo.CleanLines ,
    [CadData].Survey.SurveyPoint
    WHERE 
    SurveyPoint.Geometry.STEquals(CleanLines.Geom.STEndPoint())=1
    ),
[CadData].dbo.CleanLines.FeatId as LINEid,
CleanLines.Geom.STEndPoint() as ENDPT,
CleanLines.Geom.STStartPoint() as STPT,
[CadData].Survey.SurveyPoint.ObjectId as SURVEYPOINTOBJECTid,
[CadData].Survey.SurveyPoint.Name as SurveyPointName,
[CadData].Survey.SurveyPoint.[Description],
[CadData].Survey.SurveyPoint.Geometry

FROM
[CadData].dbo.CleanLines ,
[CadData].Survey.SurveyPoint

WHERE       
(
CleanLines.Geom.STEndPoint().STEquals(TOGEOPT) = 1
and
CleanLines.Geom.STStartPoint().STEquals(FROMGEOPT) = 1
)

我对这个选择的主要问题是我需要在WHERE子句中使用@variable而不是TOGEOPT和FROMGEOPT,但我不知道如何。我尝试将上面的子查询移动如下,但它只给了我100次相同的行

    Declare @FromGeoPt as GEOMETRY
    Declare @ToGeoPt as GEOMETRY

SET @FromGeoPt = 
    (SELECT TOP 1
        [CadData].Survey.SurveyPoint.Geometry as FROMGEOPT
    FROM
    [CadData].dbo.CleanLines ,
    [CadData].Survey.SurveyPoint

    WHERE 
    SurveyPoint.Geometry.STEquals(CleanLines.Geom.STStartPoint())=1
    )


SET @ToGeoPt = 
    (SELECT TOP 1
        [CadData].Survey.SurveyPoint.Geometry as TOGEOPT
    FROM
    [CadData].dbo.CleanLines ,
    [CadData].Survey.SurveyPoint

    WHERE 
    SurveyPoint.Geometry.STEquals(CleanLines.Geom.STEndPoint())=1
    )

SELECT TOP 100
    @ToGeoPt AS TOGEO,
    @FromGeoPt AS FROMGEO,
    [CadData].dbo.CleanLines.FeatId as LINEid,
    CleanLines.Geom.STEndPoint() as ENDPT,
    CleanLines.Geom.STStartPoint() as STPT,
    [CadData].Survey.SurveyPoint.ObjectId as SURVEYPOINTOBJECTid,
    [CadData].Survey.SurveyPoint.Name as SurveyPointName,
    [CadData].Survey.SurveyPoint.[Description],
    [CadData].Survey.SurveyPoint.Geometry

    FROM
    [CadData].dbo.CleanLines ,
    [CadData].Survey.SurveyPoint

    WHERE
    (
    CleanLines.Geom.STEndPoint().STEquals(@ToGeoPt) = 1
    and
    CleanLines.Geom.STStartPoint().STEquals(@FromGeoPt) = 1
    )

(我显然有一些拓扑问题,因为如果在找到与每行的起点/终点匹配的点时不使用“TOP 1”,则会出现错误。)

我玩过JOIN,但有同样的基本问题:使用两个点加入一行。以下仅使用起点。

  Select top 100
  CadData.dbo.CleanLines.FeatId as LINEid
  , CadData.Survey.SurveyPoint.ObjectId as SurveyPointID
  , CadData.Survey.SurveyPoint.Name
  , CadData.Survey.SurveyPoint.[Description]
  from
  CadData.dbo.CleanLines inner join CadData.Survey.SurveyPoint on 
    CleanLines.Geom.STStartPoint().STEquals(SurveyPoint.Geometry) = 1
  order by LINEid asc

1 个答案:

答案 0 :(得分:1)

如果您想在同一输出行上使用起始值和结束值,则可以JOIN两次到点数表:

  FROM CadData.dbo.CleanLines 
  JOIN CadData.Survey.SurveyPoint start_point
    ON CleanLines.Geom.STStartPoint().STEquals(start_point.Geometry) = 1
  JOIN CadData.Survey.SurveyPoint end_point 
    ON CleanLines.Geom.STEndPoint().STEquals(end_point.Geometry) = 1
  order by LINEid asc

如果您希望在不同的输出行上使用起始值和结束值,请展开JOIN条件:

  FROM CadData.dbo.CleanLines 
  JOIN CadData.Survey.SurveyPoint 
    ON CleanLines.Geom.STStartPoint().STEquals(SurveyPoint .Geometry) = 1
      OR CleanLines.Geom.STEndPoint().STEquals(SurveyPoint .Geometry) = 1
  order by LINEid asc

可能必须在上述两个方面调整JOIN条件,但这个想法是合理的。