【问题标题】:PostGIS returns record as datatype. This is unexpectedPostGIS 将记录作为数据类型返回。这是出乎意料的
【发布时间】:2021-10-22 12:48:06
【问题描述】:

我有这个问题

WITH buffered AS (
    SELECT 
        ST_Buffer(geom , 10, 'endcap=round join=round') AS geom,
        id
    FROM line),
hexagons AS (
    SELECT 
        ST_HexagonGrid(10, buffered.geom) AS hex,
        buffered.id
    FROM buffered
) SELECT * FROM hexagons;

这给出了hex 列中的datatype record。这是出乎意料的。我希望geometrydatatype。这是为什么呢?

【问题讨论】:

    标签: postgresql postgis


    【解决方案1】:

    根据documentation,函数ST_HexagonGrid返回一个setof record。然而,这些记录包含一个名为geomgeometry 属性,因此为了访问此recordgeometry,您必须用括号() 包装变量并用点. 调用该属性,例如

    SELECT (hex).geom FROM hexagons;
    

    或者只是使用*(在本例中为ijgeom)访问获取所有属性:

    SELECT (hex).* FROM hexagons;
    

    演示(PostGIS 3.1):

    WITH j (hex) AS (
     SELECT 
      ST_HexagonGrid(
       10,ST_Buffer('LINESTRING(-105.55 41.11,-115.48 37.16,-109.29 29.38,-98.34 27.13)',1))    
    )
    SELECT ST_AsText((hex).geom,2) FROM j;
    
                                           st_astext                                        
    ----------------------------------------------------------------------------------------
     POLYGON((-130 34.64,-125 25.98,-115 25.98,-110 34.64,-115 43.3,-125 43.3,-130 34.64))
     POLYGON((-115 25.98,-110 17.32,-100 17.32,-95 25.98,-100 34.64,-110 34.64,-115 25.98))
     POLYGON((-115 43.3,-110 34.64,-100 34.64,-95 43.3,-100 51.96,-110 51.96,-115 43.3))
     POLYGON((-100 34.64,-95 25.98,-85 25.98,-80 34.64,-85 43.3,-95 43.3,-100 34.64))
    

    由于ST_HexagonGrid 返回setof record,您可以使用LATERAL 访问记录属性,如here 所述,或者只调用FROM 子句中的函数:

    SELECT i,j,ST_AsText(geom,2) FROM 
      ST_HexagonGrid(
       10,ST_Buffer('LINESTRING(-105.55 41.11,-115.48 37.16,-109.29 29.38,-98.34 27.13)',1));
    
     i  | j |                                       st_astext                                        
    ----+---+----------------------------------------------------------------------------------------
     -8 | 2 | POLYGON((-130 34.64,-125 25.98,-115 25.98,-110 34.64,-115 43.3,-125 43.3,-130 34.64))
     -7 | 1 | POLYGON((-115 25.98,-110 17.32,-100 17.32,-95 25.98,-100 34.64,-110 34.64,-115 25.98))
     -7 | 2 | POLYGON((-115 43.3,-110 34.64,-100 34.64,-95 43.3,-100 51.96,-110 51.96,-115 43.3))
     -6 | 2 | POLYGON((-100 34.64,-95 25.98,-85 25.98,-80 34.64,-85 43.3,-95 43.3,-100 34.64))
    

    延伸阅读:How to divide world into cells (grid)

    【讨论】:

    • 顺便问一下:你知道ij是什么吗?
    • @Stophface 据我了解,它们代表行和列索引,它们共同为每个单元格创建一个标识符。
    猜你喜欢
    • 1970-01-01
    • 2015-06-18
    • 2021-08-01
    • 1970-01-01
    • 2020-09-04
    • 2011-12-07
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    相关资源
    最近更新 更多