PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ BOX2D_to_LWGEOM()

Datum BOX2D_to_LWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 469 of file lwgeom_box.c.

470{
471 GBOX *box = (GBOX *)PG_GETARG_POINTER(0);
472 POINTARRAY *pa = ptarray_construct_empty(0, 0, 5);
473 POINT4D pt;
475
476
477 /*
478 * Alter BOX2D cast so that a valid geometry is always
479 * returned depending upon the size of the BOX2D. The
480 * code makes the following assumptions:
481 * - If the BOX2D is a single point then return a
482 * POINT geometry
483 * - If the BOX2D represents either a horizontal or
484 * vertical line, return a LINESTRING geometry
485 * - Otherwise return a POLYGON
486 */
487
488 if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) )
489 {
490 /* Construct and serialize point */
491 LWPOINT *point = lwpoint_make2d(SRID_UNKNOWN, box->xmin, box->ymin);
492 result = geometry_serialize(lwpoint_as_lwgeom(point));
493 lwpoint_free(point);
494 }
495 else if ( (box->xmin == box->xmax) || (box->ymin == box->ymax) )
496 {
497 LWLINE *line;
498
499 /* Assign coordinates to point array */
500 pt.x = box->xmin;
501 pt.y = box->ymin;
503 pt.x = box->xmax;
504 pt.y = box->ymax;
506
507 /* Construct and serialize linestring */
508 line = lwline_construct(SRID_UNKNOWN, NULL, pa);
509 result = geometry_serialize(lwline_as_lwgeom(line));
510 lwline_free(line);
511 }
512 else
513 {
514 POINT4D points[4];
515 LWPOLY *poly;
516
517 /* Initialize the 4 vertices of the polygon */
518 points[0] = (POINT4D) { box->xmin, box->ymin, 0.0, 0.0 };
519 points[1] = (POINT4D) { box->xmin, box->ymax, 0.0, 0.0 };
520 points[2] = (POINT4D) { box->xmax, box->ymax, 0.0, 0.0 };
521 points[3] = (POINT4D) { box->xmax, box->ymin, 0.0, 0.0 };
522
523 /* Construct polygon */
524 poly = lwpoly_construct_rectangle(LW_FALSE, LW_FALSE, &points[0], &points[1],
525 &points[2], &points[3]);
526 result = geometry_serialize(lwpoly_as_lwgeom(poly));
527 lwpoly_free(poly);
528 }
529
530 PG_RETURN_POINTER(result);
531}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition lwgeom.c:372
#define LW_FALSE
Definition liblwgeom.h:94
void lwpoint_free(LWPOINT *pt)
Definition lwpoint.c:213
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition ptarray.c:59
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition lwline.c:42
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition lwpoint.c:163
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition lwgeom.c:367
LWPOLY * lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2, POINT4D *p3, POINT4D *p4)
Definition lwpoly.c:80
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_FALSE,...
Definition ptarray.c:147
void lwpoly_free(LWPOLY *poly)
Definition lwpoly.c:175
#define LW_TRUE
Return types for functions with status returns.
Definition liblwgeom.h:93
#define SRID_UNKNOWN
Unknown SRID value.
Definition liblwgeom.h:215
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition lwgeom.c:357
void lwline_free(LWLINE *line)
Definition lwline.c:67
double ymax
Definition liblwgeom.h:357
double xmax
Definition liblwgeom.h:355
double ymin
Definition liblwgeom.h:356
double xmin
Definition liblwgeom.h:354
double x
Definition liblwgeom.h:414
double y
Definition liblwgeom.h:414

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

Here is the call graph for this function: