mappostgis.h 5.02 KB
Newer Older
1
/******************************************************************************
2
 * $Id$
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Project:  MapServer
 * Purpose:  PostGIS CONNECTIONTYPE support.
 * Author:   Paul Ramsey <pramsey@cleverelephant.ca>
 *           Dave Blasby <dblasby@gmail.com>
 *
 ******************************************************************************
 * Copyright (c) 2010 Paul Ramsey
 * Copyright (c) 2002 Refractions Research
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of this Software or works derived from this Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

32 33 34 35 36 37 38 39 40 41 42 43 44 45
#ifdef USE_POSTGIS

#include "libpq-fe.h"

#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN 1
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 2
#endif

/* HEX = 16 or BASE64 = 64*/
#define TRANSFER_ENCODING 16

46 47 48 49
/* Substitution token for box hackery */
#define BOXTOKEN "!BOX!"
#define BOXTOKENLENGTH 5

50 51 52 53 54 55
/*
** msPostGISLayerInfo
**
** Specific information needed for managing this layer.
*/
typedef struct {
56 57 58 59 60 61 62 63 64 65 66
  char        *sql;        /* SQL query to send to database */
  PGconn      *pgconn;     /* Connection to database */
  long        rownum;      /* What row is the next to be read (for random access) */
  PGresult    *pgresult;   /* For fetching rows from the database */
  char        *uid;        /* Name of user-specified unique identifier, if set */
  char        *srid;       /* Name of user-specified SRID: zero-length => calculate; non-zero => use this value! */
  char        *geomcolumn; /* Specified geometry column, eg "THEGEOM from thetable" */
  char        *fromsource; /* Specified record source, ed "thegeom from THETABLE" or "thegeom from (SELECT..) AS FOO" */
  int         endian;      /* Endianness of the mapserver host */
  int         version;     /* PostGIS version of the database */
  int         paging;      /* Driver handling of pagination, enabled by default */
67 68 69
}
msPostGISLayerInfo;

70 71

/*
72
** Utility structure for handling the WKB returned by the database while
73 74 75
** reading.
*/
typedef struct {
76 77 78 79
  char *wkb; /* Pointer to front of WKB */
  char *ptr; /* Pointer to current write point */
  size_t size; /* Size of allocated space */
  int *typemap; /* Look-up array to valid OGC types */
80 81 82 83 84 85 86
} wkbObj;

/*
** Utility structure used when building up stroked lines while
** handling curved feature types.
*/
typedef struct {
87 88 89
  pointObj *data; /* Re-sizeable point buffer */
  int npoints;  /* How many points are we currently storing */
  int maxpoints; /* How big is our point buffer */
90 91 92
} pointArrayObj;

/*
93
** All the WKB type numbers from the OGC
94 95
*/
typedef enum {
96 97 98 99 100 101 102 103 104 105 106 107
  WKB_POINT=1,
  WKB_LINESTRING=2,
  WKB_POLYGON=3,
  WKB_MULTIPOINT=4,
  WKB_MULTILINESTRING=5,
  WKB_MULTIPOLYGON=6,
  WKB_GEOMETRYCOLLECTION=7,
  WKB_CIRCULARSTRING=8,
  WKB_COMPOUNDCURVE=9,
  WKB_CURVEPOLYGON=10,
  WKB_MULTICURVE=11,
  WKB_MULTISURFACE=12
108 109
} wkb_typenum;

110
/*
111 112 113 114 115
** See below.
*/
#define WKB_TYPE_COUNT 16

/*
116
** Map the WKB type numbers returned by PostGIS < 2.0 to the
117 118 119
** valid OGC numbers
*/
static int wkb_postgis15[WKB_TYPE_COUNT] = {
120 121 122 123 124 125 126 127 128 129 130 131 132 133
  0,
  WKB_POINT,
  WKB_LINESTRING,
  WKB_POLYGON,
  WKB_MULTIPOINT,
  WKB_MULTILINESTRING,
  WKB_MULTIPOLYGON,
  WKB_GEOMETRYCOLLECTION,
  WKB_CIRCULARSTRING,
  WKB_COMPOUNDCURVE,
  0,0,0,
  WKB_CURVEPOLYGON,
  WKB_MULTICURVE,
  WKB_MULTISURFACE
134 135 136
};

/*
137
** Map the WKB type numbers returned by PostGIS >= 2.0 to the
138 139 140
** valid OGC numbers
*/
static int wkb_postgis20[WKB_TYPE_COUNT] = {
141 142 143 144 145 146 147 148 149 150 151 152 153 154
  0,
  WKB_POINT,
  WKB_LINESTRING,
  WKB_POLYGON,
  WKB_MULTIPOINT,
  WKB_MULTILINESTRING,
  WKB_MULTIPOLYGON,
  WKB_GEOMETRYCOLLECTION,
  WKB_CIRCULARSTRING,
  WKB_COMPOUNDCURVE,
  WKB_CURVEPOLYGON,
  WKB_MULTICURVE,
  WKB_MULTISURFACE,
  0,0,0
155 156 157
};


158 159 160 161 162 163 164
/*
** Prototypes
*/
void msPostGISFreeLayerInfo(layerObj *layer);
msPostGISLayerInfo *msPostGISCreateLayerInfo(void);
char *msPostGISBuildSQL(layerObj *layer, rectObj *rect, long *uid);
int msPostGISParseData(layerObj *layer);
165 166 167 168
int arcStrokeCircularString(wkbObj *w, double segment_angle, lineObj *line);
int wkbConvGeometryToShape(wkbObj *w, shapeObj *shape);
pointArrayObj* pointArrayNew(int maxpoints);
void pointArrayFree(pointArrayObj *d);
169 170 171

#endif /* USE_POSTGIS */