Chapter 4. Using PostGIS

Table of Contents
GIS Objects
Using OpenGIS Standards
Loading GIS Data
Retrieving GIS Data
Building Indexes
Java Clients (JDBC)
C Clients (libpq)

GIS Objects

The GIS objects supported by PostGIS are the "Simple Features" defined by the OpenGIS Consortium (OGC). Note that PostGIS currently support the features and the representation APIs, but the various comparison and convolution operators given in the OGC "Simple Features for SQL" specification.

Examples of the text representations of the features are as follows:

Note that in the examples above there are features with both 2-dimensional and 3-dimensional coordinates. PostGIS supports both 2d and 3d coordinates -- if you describe a feature with 2D coordinates when you insert it, the database will return that feature to you with 2D coordinates when you extract it. See the sections on the 2d() and 3d() functions for information on converting features to a particular coordinate dimension representation.

Standard versus Canonical Forms

The OpenGIS specification defines two standard ways of expressing spatial objects: the Well-Known Text (WKT) form (shown in the previous section) and the Well-Known Binary (WKB) form. Both WKT and WKB include information about the type of the object and the coordinates which form the object.

However, the OpenGIS specification also requires that the internal storage format of spatial objects include a spatial referencing system identifier (SRID). The SRID is required when creating spatial objects for insertion into the database. For example, a valid insert statement to create and insert a spatial object would be:

  INSERT INTO SPATIALTABLE ( THE_GEOM, THE_NAME ) 
    VALUES ( 
      GeometryFromText('POINT(-126.4 45.32)', 312), 
      'A Place' 
    ) 

Note that the "GeometryFromText" function requires an SRID number.

The "canonical form" of the spatial objects in PostgreSQL is a text representation which includes all the information necessary to construct the object. Unlike the OpenGIS standard forms, it includes the type, coordinate, and SRID information. The canonical form is the default form returned from a SELECT query. The example below shows the difference between the OGC standard and PostGIS canonical forms:

  db=> SELECT AsText(geom) AS OGCGeom FROM thetable;
  OGCGeom
  -------------------------------------------------
  LINESTRING(-123.741378393049 48.9124018962261,-123.741587115639 48.9123981907507)
  (1 row)
  
  db=> SELECT geom AS PostGISGeom FROM thetable;
  PostGISGeom
  -------------------------------------------------
  SRID=123;LINESTRING(-123.741378393049 48.9124018962261,-123.741587115639 48.9123981907507)
  (1 row)