名稱

AddGeometryColumn — 將幾何欄位新增至現有表格。

概要

text AddGeometryColumn(varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);

text AddGeometryColumn(varchar schema_name, varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);

text AddGeometryColumn(varchar catalog_name, varchar schema_name, varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);

描述

將幾何欄位新增至現有屬性表格。schema_name 是表格綱要的名稱。srid 必須是 SPATIAL_REF_SYS 表格中條目的整數值參考。type 必須是與幾何類型對應的字串,例如 'POLYGON' 或 'MULTILINESTRING'。如果綱要名稱不存在(或在目前的 search_path 中不可見)或指定的 SRID、幾何類型或維度無效,則會拋出錯誤。

[Note]

變更:2.0.0 此函數不再更新 geometry_columns,因為 geometry_columns 是從系統目錄讀取的視圖。預設情況下,它也不會建立約束,而是使用 PostgreSQL 的內建類型修飾符行為。因此,例如使用此函數建立 wgs84 POINT 欄位現在等同於:ALTER TABLE some_table ADD COLUMN geom geometry(Point,4326);

變更:2.0.0 如果您需要舊的約束行為,請使用預設的 use_typmod,但將其設定為 false。

[Note]

變更:2.0.0 視圖不再能在 geometry_columns 中手動註冊,但是針對幾何類型修飾符表格幾何建立且未使用封裝函式的視圖會正確地自行註冊,因為它們繼承了其父表格欄位的類型修飾符行為。使用輸出其他幾何圖形之幾何函式的視圖,需要轉換為類型修飾符幾何圖形,以便這些視圖幾何欄位在 geometry_columns 中正確註冊。請參閱第 4.6.3 節,「手動註冊幾何欄位」

此方法實作OGC Simple Features Implementation Specification for SQL 1.1。

此函數支援 3D,並且不會捨棄 z 索引。

此方法支援圓弧字串和曲線。

增強:2.0.0 引入 use_typmod 引數。預設為建立類型修飾符幾何欄位,而不是基於約束的欄位。

範例

-- Create schema to hold data
CREATE SCHEMA my_schema;
-- Create a new simple PostgreSQL table
CREATE TABLE my_schema.my_spatial_table (id serial);

-- Describing the table shows a simple table with a single "id" column.
postgis=# \d my_schema.my_spatial_table
							 Table "my_schema.my_spatial_table"
 Column |  Type   |                                Modifiers
--------+---------+-------------------------------------------------------------------------
 id     | integer | not null default nextval('my_schema.my_spatial_table_id_seq'::regclass)

-- Add a spatial column to the table
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom',4326,'POINT',2);

-- Add a point using the old constraint based behavior
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom_c',4326,'POINT',2, false);

--Add a curvepolygon using old constraint behavior
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geomcp_c',4326,'CURVEPOLYGON',2, false);

-- Describe the table again reveals the addition of a new geometry columns.
\d my_schema.my_spatial_table
                            addgeometrycolumn
-------------------------------------------------------------------------
 my_schema.my_spatial_table.geomcp_c SRID:4326 TYPE:CURVEPOLYGON DIMS:2
(1 row)

                                    Table "my_schema.my_spatial_table"
  Column  |         Type         |                                Modifiers
----------+----------------------+-------------------------------------------------------------------------
 id       | integer              | not null default nextval('my_schema.my_spatial_table_id_seq'::regclass)
 geom     | geometry(Point,4326) |
 geom_c   | geometry             |
 geomcp_c | geometry             |
Check constraints:
    "enforce_dims_geom_c" CHECK (st_ndims(geom_c) = 2)
    "enforce_dims_geomcp_c" CHECK (st_ndims(geomcp_c) = 2)
    "enforce_geotype_geom_c" CHECK (geometrytype(geom_c) = 'POINT'::text OR geom_c IS NULL)
    "enforce_geotype_geomcp_c" CHECK (geometrytype(geomcp_c) = 'CURVEPOLYGON'::text OR geomcp_c IS NULL)
    "enforce_srid_geom_c" CHECK (st_srid(geom_c) = 4326)
    "enforce_srid_geomcp_c" CHECK (st_srid(geomcp_c) = 4326)

-- geometry_columns view also registers the new columns --
SELECT f_geometry_column As col_name, type, srid, coord_dimension As ndims
    FROM geometry_columns
    WHERE f_table_name = 'my_spatial_table' AND f_table_schema = 'my_schema';

 col_name |     type     | srid | ndims
----------+--------------+------+-------
 geom     | Point        | 4326 |     2
 geom_c   | Point        | 4326 |     2
 geomcp_c | CurvePolygon | 4326 |     2