Using OpenGIS Standards

The OpenGIS "Simple Features Specification for SQL" defines standard GIS object types, the functions required to manipulate them, and a set of meta-data tables. In order to ensure that meta-data remain consistent, operations such as creating and removing a spatial column are carried out through special procedures defined by OpenGIS.

There are two OpenGIS meta-data tables: SPATIAL_REF_SYS and GEOMETRY_COLUMNS. The SPATIAL_REF_SYS table holds the numeric IDs and textual descriptions of coordinate systems used in the spatial database.

The SPATIAL_REF_SYS Table

The SPATIAL_REF_SYS table definition is as follows:

  CREATE TABLE SPATIAL_REF_SYS ( 
    SRID INTEGER NOT NULL PRIMARY KEY, 
    AUTH_NAME VARCHAR(256), 
    AUTH_SRID INTEGER, 
    SRTEXT VARCHAR(2048), 
    PROJ4TEXT VARCHAR(2048)
  )

The SPATIAL_REF_SYS columns are as follows:

SRID

An integer value that uniquely identifies the Spatial Referencing System within the database.

AUTH_NAME

The name of the standard or standards body that is being cited for this reference system. For example, "EPSG" would be a valid AUTH_NAME.

AUTH_SRID

The ID of the Spatial Reference System as defined by the Authority cited in the AUTH_NAME. In the case of EPSG, this is where the EPSG projection code would go.

SRTEXT

The Well-Known Text representation of the Spatial Reference System. An example of a WKT SRS representation is:

  PROJCS["NAD83 / UTM Zone 10N", 
    GEOGCS["NAD83",
  			 DATUM["North_American_Datum_1983", 
        SPHEROID["GRS 1980",6378137,298.257222101]
  				], 
      PRIMEM["Greenwich",0], 
      UNIT["degree",0.0174532925199433] 
    ],
  		PROJECTION["Transverse_Mercator"], 
    PARAMETER["latitude_of_origin",0],
  	 PARAMETER["central_meridian",-123], 
    PARAMETER["scale_factor",0.9996],
  	 PARAMETER["false_easting",500000], 
    PARAMETER["false_northing",0],
  		UNIT["metre",1] 
  ]

For a listing of EPSG projection codes and their corresponding WKT representations, see http://www.opengis.org/techno/interop/EPSG2WKT.TXT. For a discussion of WKT in general, see the OpenGIS "Coordinate Transformation Services Implementation Specification" at http://www.opengis.org/techno/specs.htm.

PROJ4TEXT

PostGIS uses the Proj4 library to provide coordinate transformation capabilities. The PROJ4TEXT column contains the Proj4 coordinate definition string for a particular SRID. For example:

  +proj=utm +zone=10 +ellps=clrk66 +datum=NAD27 +units=m

For more information about, see the Proj4 web site at http://www.remotesensing.org/proj. The spatial_ref_sys.sql file contains both SRTEXT and PROJ4TEXT definitions for all EPSG projections.

The GEOMETRY_COLUMNS Table

The GEOMETRY_COLUMNS table definition is as follows:

  CREATE TABLE GEOMETRY_COLUMNS ( 
    F_TABLE_CATALOG VARCHAR(256) NOT NULL, 
    F_TABLE_SCHEMA VARCHAR(256) NOT NULL, 
    F_TABLE_NAME VARCHAR(256) NOT NULL, 
    F_GEOMETRY_COLUMN VARCHAR(256) NOT NULL,
  		COORD_DIMENSION INTEGER NOT NULL, 
    SRID INTEGER NOT NULL, 
    TYPE VARCHAR(30) NOT NULL 
  )

The columns are as follows:

F_TABLE_CATALOG, F_TABLE_SCHEMA, F_TABLE_NAME

The fully qualified name of the feature table containing the geometry column. Note that the terms "catalog" and "schema" are Oracle-ish. There is not PostgreSQL analogue of "catalog" so that column is left blank -- for "schema" the database name is used.

F_GEOMETRY_COLUMN

The name of the geometry column in the feature table.

COORD_DIMENSION

The spatial dimension (2 or 3 dimensional) of the column.

SRID

The ID of the spatial reference system used for the coordinate geometry in this table. It is a foreign key reference to the SPATIAL_REF_SYS.

TYPE

The type of the spatial object. To restrict the spatial column to a single type, use one of: POINT, LINESTRING, POLYGON, MULTPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION. For heterogeneous (mixed-type) collections, you can use "GEOMETRY" as the type.

Note: This attribute is (probably) not part of the OpenGIS specification, but is required for ensuring type homogeneity.

Creating a Spatial Table

Creating a table with spatial data is done in two stages:

Here is an example of SQL used to create a table and add a spatial column (assuming the db is 'parks_db' and that an SRID of 128 exists already):

  CREATE TABLE PARKS ( PARK_ID int4, PARK_NAME varchar(128), PARK_DATE date, PARK_TYPE varchar(2) );
  SELECT AddGeometryColumn('parks_db', 'parks', 'park_geom', 128, 'MULTIPOLYGON', 2 );

Here is another example, using the generic "geometry" type and the undefined SRID value of -1:

  CREATE TABLE ROADS ( ROAD_ID int4, ROAD_NAME varchar(128) ); 
  SELECT AddGeometryColumn( 'roads_db', 'roads', 'roads_geom',	-1, 'GEOMETRY', 3 );