【问题标题】:How to add a centroid column from the geom column using postgres?如何使用 postgres 从 geom 列添加质心列?
【发布时间】:2021-07-10 01:38:04
【问题描述】:

我在 postgres 的数据库中有下表(table_example):

      id         geom              class        area_total                 
     1255      65f56f6565...       type_1         1244589
     1256      65f56f6566...       type_2         2542542

我需要创建一个新列,从多边形的几何形状中接收多边形质心的值。

基于此链接 (How to add geometry column using pgAdmin),我尝试按如下方式进行所需的操作:

         -- The idea is to first create a column that is of type geometry
         ALTER TABLE table_example 
         ADD COLUMN column_geom_centroid geometry(geom);

        -- And then assign the centroid value to the column
        UPDATE table_example
        SET column_geom_centroid = ST_Centroid(geom)

但是,当我运行第一个命令 (ALTER...) 时,会出现以下错误:

        ERROR:  Invalid geometry type modifier: geom
        LINE 2: ADD COLUMN column_geom_centroid geometry(geom);
                                     ^
        SQL state: 22023
        Character: 70

【问题讨论】:

    标签: sql postgresql pgadmin


    【解决方案1】:

    最好使用 postgis 函数AddGeometryColumn,用于创建几何列 https://postgis.net/docs/AddGeometryColumn.html

    例子

    SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom_c',4326,'POINT',2, false);

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2011-11-11
      • 1970-01-01
      • 2021-06-02
      • 2021-10-07
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多