  PostGIS
  Refractions Research
  May 25, 2001

  PostGIS is an extension to the PostgreSQL object-relational database
  system which allows GIS (Geographic Information Systems) objects to be
  stored in the database. PostGIS includes support for GiST indexes, and
  functions for basic analysis of GIS objects.
  ______________________________________________________________________

  Table of Contents


  1. Introduction

     1.1 Credits
     1.2 To Do List
     1.3 More Information

  2. Installation

     2.1 Requirements
     2.2 Instructions

  3. Frequently Asked Questions

        3..1 What kind of geometric objects can I store?
        3..2 How do I improve searching speed?
        3..3 Why are the operators not giving me the results I expect?
        3..4 How can I get my search to return things that really are inside  the search box, not just overlapping bounding boxes?
        3..5 What are the available functions and what do they do?
        3..6 Why aren't R-Tree's recommended?

  4. GIS Objects

     4.1 External Representation
        4.1.1 2-D versus 3-D
     4.2 Internal Representation
        4.2.1 Basics
        4.2.2 Usage
        4.2.3 Input/Output
     4.3 Sub-Geometry Types
        4.3.1 POINT3D
        4.3.2 LINE3D
        4.3.3 POLYGON3D
        4.3.4 BOX3D

  5. GIS Functions

     5.1 External Functions
     5.2 Internal Functions

  6. Indexing

     6.1 R-Tree
     6.2 GiST


  ______________________________________________________________________

  11..  IInnttrroodduuccttiioonn

  PostGIS is developed by Refractions Research Inc, as a research
  project into spatial database technology. Refractions is a GIS and
  database consulting company in Victoria, British Columbia,
  specializing in data integration and custom software development. We
  plan on supporting and developing PostGIS to support a range of
  important GIS functionality, including advanced topological constructs
  (coverages, surfaces, networks), desktop user interface tools for
  viewing and editing GIS data, and web-based access tools.

  To contact Refractions Research: _P_a_u_l _R_a_m_s_e_y
  _<_p_r_a_m_s_e_y_@_r_e_f_r_a_c_t_i_o_n_s_._n_e_t_>_, _R_e_f_r_a_c_t_i_o_n_s _R_e_s_e_a_r_c_h _I_n_c_, _2_0_9 _- _5_6_0 _J_o_h_n_s_o_n
  _S_t_r_e_e_t_, _V_i_c_t_o_r_i_a_, _B_C_, _C_a_n_a_d_a_, _V_8_W_-_3_C_6_, _P_h_o_n_e_: _(_2_5_0_) _8_8_5_-_0_6_3_2_, _F_a_x_:
  _(_2_5_0_) _3_8_3_-_2_1_4_0

  11..11..  CCrreeddiittss


  +o  Dave Blasby <dblasby@refractions.net> is the principal developer of
     PostGIS. Dave maintains the server side objects and index support,
     as well as the server side analytical functions.

  +o  Paul Ramsey <pramsey@refractions.net> maintains the JDBC objects
     and keeps track of the documentation and packaging.

  +o  Jeff Lounsbury <jeffloun@refractions.net> maintains the Shape
     loader/dumper.

  11..22..  TToo DDoo LLiisstt

  11..33..  MMoorree IInnffoorrmmaattiioonn


  +o  The latest software, documentation and news items are  available at
     the PostGIS web site, http://postgis.refractions.net .

  +o  More information about the PostgreSQL database server is available
     at the PostgreSQL main site http://www.postgresql.org .

  +o  More information about GiST indexing is available at the GiST
     development site, http://www.sai.msu.su/~megera/postgres/gist .

  22..  IInnssttaallllaattiioonn

  22..11..  RReeqquuiirreemmeennttss

  PostGIS was developed using PostgreSQL 7.1.1 as the target server.
  Earlier versions of PgSQL will probably not work with PostGIS.
  Versions within the 7.1.x release series will probably work fine.

  22..22..  IInnssttrruuccttiioonnss


  1. Retrieve the PostGIS source archive from
     http://postgis.refractions.net/postgis-latest.tar.gz.  Uncompress
     and untar the archive in the " contrib" directory of the PostgreSQL
     source tree.

     cd <postgresql source tree>/contrib
     zcat postgis-latest.tar.gz | tar xvf -




  2. Before you can compile the postgis server modules, you must compile
     and install the PostgreSQL package. Once your PostgreSQL
     installation is up-to-date, enter the " postgis" directory, and run
     the compile and install commands.



     cd ./postgis
     make
     make install




  3. To compile the JDBC extensions, enter the " jdbc" sub-directory and
     run the compile command.

     make




  Copy the postgis.jar file to wherever you keep your java libraries.

  33..

  33..00..11..  WWhhaatt kkiinndd ooff ggeeoommeettrriicc oobbjjeeccttss ccaann II ssttoorree??

  You can store point, line, polygon, multipoint, multiline,
  multipolygon, and geometrycollections. These are specified in the Open
  GIS Well Known Text Format (with 3d extentions).


  +o  POINT(1 1) - single point

  +o  LINESTRING(0 0, 1 1, 0 1) - simple line

  +o  POLYGON( (0 0, 10 0, 10 10, 0 10, 0 0),(5 5,7 5, 7 7, 5 7, 5 5) )
     -polygon (with hole)

  +o  MULTIPOINT( 0 0, 1 1) - set of points

  +o  MULTILINESTRING( (0 0, 1 1, 2 2), (10 10, 9 9, 8 8)) - set of lines

  +o  MULTIPOLYGON( ((0 0, 10 0, 10 10, 0 10, 0 0)),((100 100, 200 100,
     200 200, 100 200, 100 100))) - set of polygons (with holes)

  +o  GEOMETRYCOLLECTION(MULTIPOINT(1 1, 2 2), LINESTRING(0 0, 1 1, 0 1),
     POLYGON((0 0, 10 0, 10 10, 0 10, 0 0)) ) - set of base geometries

  33..00..22..  HHooww ddoo II iimmpprroovvee sseeaarrcchhiinngg ssppeeeedd??

  Build an index. The following SQL, for example:


  ______________________________________________________________________
  CREATE INDEX <index name> ON <table name> USING GIST (<geometry column name> GIST_GEOMETRY_OPS) WITH (islossy);
  SET ENABLE_SEQSCAN = OFF;
  SELECT * FROM <table> WHERE <geometry column name> && 'BOX3D(0 0 0, 100 100 0)'::BOX3D
  ______________________________________________________________________



  33..00..33..  WWhhyy aarree tthhee ooppeerraattoorrss nnoott ggiivviinngg mmee tthhee rreessuullttss II eexxppeecctt??

  The operators work on the bounding box of the geometry, not the
  underlying geometry.

  33..00..44..  tthhee sseeaarrcchh bbooxx,, nnoott jjuusstt oovveerrllaappppiinngg bboouunnddiinngg bbooxxeess??  HHooww ccaann
  II ggeett mmyy sseeaarrcchh ttoo rreettuurrnn tthhiinnggss tthhaatt rreeaallllyy aarree iinnssiiddee

  Since the " &&" operator only checks for bounding box overlap, use the
  truly_inside() function. For example:
  ______________________________________________________________________
  SELECTt * FROM <table> WHERE <geometry column> && <BOX3D> AND truly_inside(<geometry column>,<BOX3D>);
  ______________________________________________________________________



  Since the && is quick (and indexable), this will execute quickly.

  33..00..55..  WWhhaatt aarree tthhee aavvaaiillaabbllee ffuunnccttiioonnss aanndd wwhhaatt ddoo tthheeyy ddoo??

  length3d(GEOMETRY) - 3d length of all linestrings in GEOMETRY

  length2d(GEOMETRY) - 2d length of all linestringsin GEOMETRY

  area2d (GEOMETRY) - area of all polygons (projected to the XY plane)

  perimeter2d(GEOMETRY) - 2d length of all polygon rings

  perimeter3d(GEOMETRY) - 3d length of all polygon rings

  truly_inside(GEOMETRY,GEOMETRY) - true if part of A is inside B's
  bounding box

  A <& B - true if A's bounding box overlaps or is to the right of B's

  A << B - true if A's bounding box is strictly to the right of B's

  A &> B - true if A's bounding box overlaps or is strictly to the right
  of B's

  A >> B - true if A's bounding box is strictly to the left of B's

  A ~= B - true if A's geometry is the same as B's (order of sub-
  geometry unimportant)

  A ~ B - true if A's bounding box is contained by B's bounding box

  A @ B - true if A's bounding box contains B's bounding box

  A && B- true if A's bounding box overlaps B's bounding box

  npoints(GEOMETRY) - (info) how many points are in the geometry

  nrings(GEOMETRY) - (info) how many rings are there in the geometry

  mem_size(GEOMETRY) - (info) how much space does this geometry take up

  numb_sub_objs(GEOMETRY) - (info) how many sub objects are inside this
  geometry

  summary(GEOMETRY) - (info) give a text summary of whats inside the
  geometry

  33..00..66..  WWhhyy aarreenn''tt RR--TTrreeee''ss rreeccoommmmeennddeedd??

  They (unlike GiST) cannot handle geometries that are are > 8kb in
  size, and they take a lot longer to create.  Why aren't R-Tree's
  recommended?

  44..  GGIISS OObbjjeeccttss

  PostGIS implements all the Simple Feature types specified by OGIS. The
  PostGIS objects are all 3-dimensional, so the representations are
  slightly different than those given in the OGIS standard. The PostGIS
  object types are as follows:

  +o  Point

  +o  LineString

  +o  Polygon

  +o  MultiPoint

  +o  MultiLineString

  +o  MultiPoint

  +o  GeometryCollection

  In addition, a " Box3d" type is included for indexing, querying and
  retrieval purposes.

  44..11..  EExxtteerrnnaall RReepprreesseennttaattiioonn

  The external representation for all PostGIS objects is the OGIS " Well
  Known Text" (WKT) representation. SQL selects return the GIS objects
  in WKT format, and SQL inserts must represent GIS objects in WKT.
  Examples of the WKT representations are as follows:


  +o  POINT(0 0 0)

  +o  LINESTRING(0 0 0,1 1 0,1 2 1)

  +o  POLYGON((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1 2 0,1
     1 0))

  +o  MULTIPOINT(0 0 0,1 2 1)

  +o  MULTILINESTRING((0 0 0,1 1 0,1 2 1),(2 3 1,3 2 1,5 4 1))

  +o  MULTIPOLYGON(((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1
     2 0,1 1 0)),((-1 -1 0,-1 -2 0,-2 -2 0,-2 -1 0,-1 -1 0)))

  +o  GEOMETRYCOLLECTION(POINT(2 3 9),LINESTRING((2 3 4,3 4 5)))

  44..11..11..  22--DD vveerrssuuss 33--DD

  The internal objects representation stores all objects with three
  coordinates, but the external interface allows users to insert and
  retrieve 2-D objects without supplying a third coordinate. For
  example, a 2-D object can be inserted into the database:


  template=# INSERT INTO GEOMETRIES (GEOM) VALUES ('POINT(2 3)');
  INSERT 2415151 1



  The same feature will be retrieved as a 2-D feature when selected out
  of the database:


  template=# SELECT * FROM GEOMETRIES;
  geom
  --------
  POINT(2 3)
  (1 row)



  44..22..  IInntteerrnnaall RReepprreesseennttaattiioonn

  The internal representation information is useful to people who intend
  to write C functions to directly manipulate the backend objects at the
  server. Reading postgis.h will give the exact type definitions used,
  and reading postgis.c will give some examples of how to manipulate the
  objects.

  44..22..11..  BBaassiiccss

  In the most basic sense, a GEOMETRY type is a list of sub-objects and
  a bounding volume. In an ideal world, the type should be defined as:


  ______________________________________________________________________
  typedef struct
  {
  int32 size; // postgres variable-length type requirement
  int32 type; // this type of geometry
  bool is3d; // true if the points are 3d (only for output)
  BOX3D bvol; // bounding volume of all the geo objects
  int32 nobjs; // how many sub-objects in this object
  int32 objType[1]; // type of object
  int32 objOffset[1]; // offset (in bytes) into this structure where the object is located
  char objData[1]; // store for actual objects
  } GEOMETRY;
  ______________________________________________________________________



  However, because of the way PostgreSQL handles memory allocation for
  objects, the type is actually organized as:


  ______________________________________________________________________
  typedef struct
  {
  int32 size; // postgres variable-length type requirement
  int32 type; // this type of geometry
  bool is3d; // true if the points are 3d (only for output)
  BOX3D bvol; // bounding volume of all the geo objects
  int32 nobjs; // how many sub-objects in this object
  char data[?]; //info about the sub-objects
  } GEOMETRY;
  ______________________________________________________________________



  This is because a Postgres type is a just a chuck of memory - it
  cannot contain any pointers. In a normal program objType and objData
  would be pointers to separate chunks of memory for these two lists (in
  fact, objData would likely be a list of pointers to other memory
  locations). That would look something like:

  Since we cannot have pointers, the geometry structure actually has
  pseudo-pointers to its data portion, and looks like:


  There are several important observations:


  +o  The structure is assumed to start off double-aligned (see the SQL
     define type).

  +o  Inside the structure, bvol and all the objects are double-aligned
     (there is unused space between is3d and bvol, there could be unused
     space between objOffset[n] and obect 0, and there could be unused
     space between object i and object i+1.

  +o  The actual location of objType[0] is always &geometry->objType[0].

  +o  The actual location of objOffset[0] is
     sizeof(int32)*geometry->nobjs after objType[0].

  +o  The actual location of object 0 is objOffset[0] bytes into the
     structure .

  +o  Object 0 is also sizeof(int32)*geometry->nobjs bytes after
     objOffset[0] (could be 4 more bytes along if this isn't double-
     aligned) .

  +o  The actual location of object 1 is objOffset[1] bytes into the
     structure.

  44..22..22..  UUssaaggee

  Normally, one will want to find the location of objOffset[0] so its
  accessible;


  ______________________________________________________________________
  Int32 *offsets;
  offsets = (int32 *) ( ((char *) &(geom1->objType[0] ))+ sizeof(int32) * geom1->nobjs ) ;
  ______________________________________________________________________



  Next, one might want to get object i;


  ______________________________________________________________________
  Char *obji;
  oi = (char *) geom1 +offsets[i] ;
  typei= geom1->objType[i];
  ______________________________________________________________________



  Then one can re-cast the object;


  ______________________________________________________________________
  POLYGON3D *poly;
  LINE3D *line;
  POINT3D *point;
  if (typei == POINTTYPE) //point
  {
  point = (LINE3D *) oi;
  ...
  }
  if (type1 == LINETYPE) //line
  {
  line = (LINE3D *) oi;
  ...
  }
  if (type1 == POLYGONTYPE) //POLYGON
  {
  poly = (POLYGON3D *) oi;
  ...
  }
  ______________________________________________________________________

  What does everything mean?


  +o  Size

     Total size of the structure

  +o  Type

     Generic type for this Structure

     This is mostly used so output knows how the object was originally
     entered into the system.

  +o  Is3d

     TRUE iff, when the object was created, any of the points were given
     in 3D. For example,

     MULTIPOINT(1 1,2 2) -> is3d = FALSE

     MULTIPOINT( 1 1, 2 2, 3 3 3) -> is3d = TRUE

     Since POINT( 1 1) is stored as a POINT(1 1 0), we use the is3d flag
     during output to give POINT( 1 1) or POINT( 1 1 0). Its not used
     for anything else.

  +o  Bvol

     Bounding Volume for all the points in this geometry

  +o  nobjs

     How many point/line/polygons are in this geometry

  +o  objType[i]

     The type of each of the sub-objects


     ___________________________________________________________________
     #define POINTTYPE 1
     #define LINETYPE 2
     #define POLYGONTYPE 3

     ___________________________________________________________________



  +o  objOffset[i]

     Offset, in bytes, into the structure where the object i is actually
     stored.

  +o  objdata

     Data block with all the objects stored in it.

  44..22..33..  IInnppuutt//OOuuttppuutt

  The geometry type tries really hard to consistently reproduce objects.
  For example, if you insert a MULTIPOINT(), you?ll get a MULTIPOINT()
  when you select.

  This is more difficult than you might expect because, internally,
  these objects are stored the same:
  MULTIPOINT( 1 1, 2 2, 3 3)

  GEOMETRYCOLLECTION(POINT(1 1), POINT(2 2), POINT(3 3) )

  GEOMETRYCOLLECTION(MULTIPOINT( 1 1, 2 2, 3 3))

  GEOMETRYCOLLECTION(MULTIPOINT( 1 1, 2 2), POINT( 3 3) )

  Every GEOMETRY is a set (GEOMETRYCOLLECTION) of sub-objects (point,
  line, polygon). So, truthfully, the 2nd example is the best OGC WKT of
  the internal structure.

  Therefore, if you start with a MULTI-geometry (MULTIPOINT, MULTILINE,
  MULTIPOLYGON), the type member will record that fact. On output, it
  does a simple conversion:


  +o  GEOMETRYCOLLECTION(POINT(1 1),POINT(2 2)) becomes MULTIPOINT(1 1 ,
     2 2)

  +o  GEOMETRYCOLLECTION(LINESTRING(1 1,2 2),LINESTRING(10 10, 20 20))
     becomes MULTILINESTRING((1 1,2 2), (10 10, 20 20))

  +o  GEOMETRYCOLLECTION(POLYGON((0 0, 0 1, 1 1, 0 0)), POLYGON((0 0, 0
     10, 10 10 , 0 0) ) becomes MULTIPOLYGON( ((0 0, 0 1, 1 1, 0 0)),
     ((0 0, 0 10, 10 10 , 0 0)))

  Single point, line, or polygon objects are returned as single point,
  line or polygon objects.

  Unfortunately, GEOMETRYCOLLECTIONS() are not always returned the same
  as they are entered. They are always returned as a set of base types.

  For example:


  +o  GEOMETRYCOLLECTION(POINT(1 1),POINT(2 2),POINT(3 3)) becomes
     GEOMETRYCOLLECTION(POINT(1 1),POINT(2 2),POINT(3 3))

  +o  GEOMETRYCOLLECTION(MULTIPOINT(1 1,2 2,3 3)) becomes
     GEOMETRYCOLLECTION(POINT(1 1),POINT(2 2),POINT(3 3))

  44..33..  SSuubb--GGeeoommeettrryy TTyyppeess

  44..33..11..  PPOOIINNTT33DD

  This is the base geometry used by all the other sub-points. Its simply
  the X,Y,Z location represented as a double-precision floating point
  number. 2D locations are represented with the Z location set to 0.0.
  See above for a discussion on how to tell the difference between a 2D
  point, and 3D points with Z=0.0.


  ______________________________________________________________________
  typedef struct
  {
  double x,y,z;
  } POINT3D;
  ______________________________________________________________________



  44..33..22..  LLIINNEE33DD

  A lines is a set of connected points. The line moves from
  points[0]->points[1]->points[2]
  ______________________________________________________________________
  typedef struct
  {
  int32 npoints; // how many points in the line
  int32 junk; // double-word alignment
  POINT3D points[1]; // array of actual points
  } LINE3D;
  ______________________________________________________________________



  44..33..33..  PPOOLLYYGGOONN33DD

  A polygon is a set of rings. Each ring is a closed line.


  +o  The first ring is the outer ring, the others are holes

  +o  Npoints[i] is the number of points in ring i.

  +o  &points[0] is at &polygon ->npoints[0] +
     sizeof(int32)*polygon->nrings

  +o  Once you've calculated &points[0], ensure it is double-aligned

  For example:


  ______________________________________________________________________
  POINT3D *pts;
  pts = (POINT3D *) ( (char *)&(poly->npoints[poly->nrings] ) );
  if ((int32) pts1 != (((int32)pts1>>3)<<3) )
  pts1 = (POINT3D *) ((char *) pts1 + 4);
  ______________________________________________________________________



  Therefore,


  +o  Ring 0's points are points[0] to points[ npoint[0] -1]

  +o  Ring 1's points are from points[npoint[0]] to points[npoints[0] +
     npoints[1] -1]


  ______________________________________________________________________
  typedef struct
  {
  int32 nrings; // how many rings in this polygon
  int32 npoints[1]; //how many points in each ring
  /* could be 4 byes of filler here to make sure points[] is
  double-word aligned*/
  POINT3D points[1]; // array of actual points
  } POLYGON3D;
  ______________________________________________________________________



  44..33..44..  BBOOXX33DD

  Representation of a long diagonal of a cube, from the lower-left
  bottom (LLB) to the upper-right top (URT).



  ______________________________________________________________________
  Minimum X is box3d->LLB.x
  Minimum Y is box3d->LLB.y
  Minimum Z is box3d->LLB.z
  Maximum X is box3d->URT.x
  Maximum y is box3d->URT.y
  Maximum Z is box3d->URT.z
  typedef struct
  {
  POINT3D LLB,URT; /* corner POINT3Ds on long diagonal */
  } BOX3D;
  ______________________________________________________________________



  55..  GGIISS FFuunnccttiioonnss

  55..11..  EExxtteerrnnaall FFuunnccttiioonnss

  55..22..  IInntteerrnnaall FFuunnccttiioonnss

  66..  IInnddeexxiinngg

  66..11..  RR--TTrreeee

  66..22..  GGiiSSTT








































