PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum BOX3D_to_LWGEOM ( PG_FUNCTION_ARGS  )

Alter BOX3D cast so that a valid geometry is always returned depending upon the size of the BOX3D. The code makes the following assumptions:

  • If the BOX3D is a single point then return a POINT geometry
  • If the BOX3D represents either a horizontal or vertical line, return a LINESTRING geometry
  • Otherwise return a POLYGON

Definition at line 177 of file lwgeom_box3d.c.

References geometry_serialize(), gserialized_set_srid(), LW_TRUE, lwline_as_lwgeom(), lwline_construct(), lwpoint_as_lwgeom(), lwpoint_construct(), lwpoly_as_lwgeom(), lwpoly_construct(), ptarray_append_point(), ptarray_construct_empty(), result, BOX3D::srid, SRID_UNKNOWN, POINT4D::x, BOX3D::xmax, BOX3D::xmin, POINT4D::y, BOX3D::ymax, and BOX3D::ymin.

178 {
179  BOX3D *box = (BOX3D *)PG_GETARG_POINTER(0);
180  POINTARRAY *pa;
182  POINT4D pt;
183 
184 
196  pa = ptarray_construct_empty(0, 0, 5);
197 
198  if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) )
199  {
200  LWPOINT *lwpt = lwpoint_construct(SRID_UNKNOWN, NULL, pa);
201 
202  pt.x = box->xmin;
203  pt.y = box->ymin;
204  ptarray_append_point(pa, &pt, LW_TRUE);
205 
206  result = geometry_serialize(lwpoint_as_lwgeom(lwpt));
207  }
208  else if (box->xmin == box->xmax ||
209  box->ymin == box->ymax)
210  {
211  LWLINE *lwline = lwline_construct(SRID_UNKNOWN, NULL, pa);
212 
213  pt.x = box->xmin;
214  pt.y = box->ymin;
215  ptarray_append_point(pa, &pt, LW_TRUE);
216  pt.x = box->xmax;
217  pt.y = box->ymax;
218  ptarray_append_point(pa, &pt, LW_TRUE);
219 
220  result = geometry_serialize(lwline_as_lwgeom(lwline));
221  }
222  else
223  {
224  LWPOLY *lwpoly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, &pa);
225 
226  pt.x = box->xmin;
227  pt.y = box->ymin;
228  ptarray_append_point(pa, &pt, LW_TRUE);
229  pt.x = box->xmin;
230  pt.y = box->ymax;
231  ptarray_append_point(pa, &pt, LW_TRUE);
232  pt.x = box->xmax;
233  pt.y = box->ymax;
234  ptarray_append_point(pa, &pt, LW_TRUE);
235  pt.x = box->xmax;
236  pt.y = box->ymin;
237  ptarray_append_point(pa, &pt, LW_TRUE);
238  pt.x = box->xmin;
239  pt.y = box->ymin;
240  ptarray_append_point(pa, &pt, LW_TRUE);
241 
242  result = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
243 
244  }
245 
246  gserialized_set_srid(result, box->srid);
247  PG_RETURN_POINTER(result);
248 }
double x
Definition: liblwgeom.h:308
int32_t srid
Definition: liblwgeom.h:235
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:57
void gserialized_set_srid(GSERIALIZED *s, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:87
double ymin
Definition: liblwgeom.h:233
char ** result
Definition: liblwgeom.h:218
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:141
double xmin
Definition: liblwgeom.h:233
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
double xmax
Definition: liblwgeom.h:234
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:96
double y
Definition: liblwgeom.h:308
double ymax
Definition: liblwgeom.h:234

Here is the call graph for this function: