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

Definition at line 418 of file lwgeom_box.c.

References geometry_serialize(), LW_TRUE, lwalloc(), lwline_as_lwgeom(), lwline_construct(), lwline_free(), lwpoint_as_lwgeom(), lwpoint_free(), lwpoint_make2d(), lwpoly_as_lwgeom(), lwpoly_construct(), lwpoly_free(), ptarray_append_point(), ptarray_construct_empty(), result, SRID_UNKNOWN, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT4D::y, GBOX::ymax, and GBOX::ymin.

419 {
420  GBOX *box = (GBOX *)PG_GETARG_POINTER(0);
421  POINTARRAY *pa = ptarray_construct_empty(0, 0, 5);
422  POINT4D pt;
424 
425 
426  /*
427  * Alter BOX2D cast so that a valid geometry is always
428  * returned depending upon the size of the BOX2D. The
429  * code makes the following assumptions:
430  * - If the BOX2D is a single point then return a
431  * POINT geometry
432  * - If the BOX2D represents either a horizontal or
433  * vertical line, return a LINESTRING geometry
434  * - Otherwise return a POLYGON
435  */
436 
437  if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) )
438  {
439  /* Construct and serialize point */
440  LWPOINT *point = lwpoint_make2d(SRID_UNKNOWN, box->xmin, box->ymin);
441  result = geometry_serialize(lwpoint_as_lwgeom(point));
442  lwpoint_free(point);
443  }
444  else if ( (box->xmin == box->xmax) || (box->ymin == box->ymax) )
445  {
446  LWLINE *line;
447 
448  /* Assign coordinates to point array */
449  pt.x = box->xmin;
450  pt.y = box->ymin;
451  ptarray_append_point(pa, &pt, LW_TRUE);
452  pt.x = box->xmax;
453  pt.y = box->ymax;
454  ptarray_append_point(pa, &pt, LW_TRUE);
455 
456  /* Construct and serialize linestring */
457  line = lwline_construct(SRID_UNKNOWN, NULL, pa);
458  result = geometry_serialize(lwline_as_lwgeom(line));
459  lwline_free(line);
460  }
461  else
462  {
463  LWPOLY *poly;
464  POINTARRAY **ppa = lwalloc(sizeof(POINTARRAY*));
465 
466  /* Assign coordinates to point array */
467  pt.x = box->xmin;
468  pt.y = box->ymin;
469  ptarray_append_point(pa, &pt, LW_TRUE);
470  pt.x = box->xmin;
471  pt.y = box->ymax;
472  ptarray_append_point(pa, &pt, LW_TRUE);
473  pt.x = box->xmax;
474  pt.y = box->ymax;
475  ptarray_append_point(pa, &pt, LW_TRUE);
476  pt.x = box->xmax;
477  pt.y = box->ymin;
478  ptarray_append_point(pa, &pt, LW_TRUE);
479  pt.x = box->xmin;
480  pt.y = box->ymin;
481  ptarray_append_point(pa, &pt, LW_TRUE);
482 
483  /* Construct polygon */
484  ppa[0] = pa;
485  poly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, ppa);
486  result = geometry_serialize(lwpoly_as_lwgeom(poly));
487  lwpoly_free(poly);
488  }
489 
490  PG_RETURN_POINTER(result);
491 }
double x
Definition: liblwgeom.h:308
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:130
double xmax
Definition: liblwgeom.h:249
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:57
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:180
void lwline_free(LWLINE *line)
Definition: lwline.c:63
char ** result
Definition: liblwgeom.h:218
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
double ymin
Definition: liblwgeom.h:250
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
double xmin
Definition: liblwgeom.h:248
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
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:79
#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 ymax
Definition: liblwgeom.h:251
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
void * lwalloc(size_t size)
Definition: lwutil.c:175
double y
Definition: liblwgeom.h:308

Here is the call graph for this function: