将 GEOGRAPHY 点与 GEOGRAPHY LINESTRING 相结合

combine GEOGRAPHY Points to GEOGRAPHY LINESTRING

有没有像这样组合多个 GEOGRAPHY 点的简单方法:

CREATE TABLE #Points
(
    Id INT IDENTITY(1,1),
    Point GEOGRAPHY
)

到 Sql Server 2014 中的 GEOGRAPHY LINESTRING?

要结合两点你可以这样做:

@Point1.STUnion(@Point2).STConvexHull()

要创建线,点必须按顺序排列,因此像 ConvexHullAggregate 这样的东西不起作用。您可以尝试编写一个循环来按顺序组合它们,然后存储组合。

另一种方法是从点的 (x y) 坐标构造字符串(通过 point.StX() 和 point.StY()),连接它们并构建描述线串的 WKT 字符串,然后转换它到地理类型。

我有一个场景,我有一些要点:

积分

PathId Step PointGeo
1 1 0xE61...
1 2 0xE61...

我想把它们作为线串table(路径;一条路径有很多点)放入父级

路径

PathId PathAsLineStringGeo
1 NULL

我写了一个 SQL 写了一个 SQL:

  select
    concat(
      'update Paths set PathAsLineStringGeo = geography::STGeomFromText(''LINESTRING(',
      string_agg(cast(concat(PointGeo.Long, ' ', PointGeo.Lat) as varchar(max)), ' ') within group(order by Step),
      ')'', 4326) where PathId = ',
      PathId
    ) 

  from points
  group by pathid

您需要调整以使其与您的上下文相关的列采用 PascalCase

这生成了一堆我从结果window和运行中复制出来的更新语句:

UPDATE Paths SET PathAsLineStringGeo = geography::STGeomFromText('LINESTRING(-1.22 3.44, 5.66 7.88)', 4326) where PathId = 1
UPDATE Paths SET PathAsLineStringGeo = geography::STGeomFromText('LINESTRING(-7.89 10.11 -12.13 14.15 -16.17 18.19)', 4326) where PathId = 2

虽然没有什么可以阻止您将其设为典型的可更新连接。也许是这样的:

update x
set somelinestringgeocolumn = geography::STGeomFromText(y.calculatedlinestringWKT, 4326)
from 

  parent_table x
  inner join 
  (
    select
      pathId,
      CONCAT(
        'LINESTRING(',
        string_agg(cast(concat(point.Long, ' ', point.Lat) as varchar(max)), ', ') within group(order by Step),
        ')'
      ) as calculatedlinestringWKT
    from child_table_with_points
  ) y
  ON x.pathid = y.pathid