PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ BOX3D_to_LWGEOM()

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 176 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(), BOX3D::srid, SRID_UNKNOWN, POINT4D::x, BOX3D::xmax, BOX3D::xmin, POINT4D::y, BOX3D::ymax, and BOX3D::ymin.

Referenced by BOX3D_to_BOX().

177 {
178  BOX3D *box = (BOX3D *)PG_GETARG_POINTER(0);
179  POINTARRAY *pa;
180  GSERIALIZED *result;
181  POINT4D pt;
182 
183 
195  pa = ptarray_construct_empty(0, 0, 5);
196 
197  if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) )
198  {
199  LWPOINT *lwpt = lwpoint_construct(SRID_UNKNOWN, NULL, pa);
200 
201  pt.x = box->xmin;
202  pt.y = box->ymin;
203  ptarray_append_point(pa, &pt, LW_TRUE);
204 
205  result = geometry_serialize(lwpoint_as_lwgeom(lwpt));
206  }
207  else if (box->xmin == box->xmax ||
208  box->ymin == box->ymax)
209  {
210  LWLINE *lwline = lwline_construct(SRID_UNKNOWN, NULL, pa);
211 
212  pt.x = box->xmin;
213  pt.y = box->ymin;
214  ptarray_append_point(pa, &pt, LW_TRUE);
215  pt.x = box->xmax;
216  pt.y = box->ymax;
217  ptarray_append_point(pa, &pt, LW_TRUE);
218 
219  result = geometry_serialize(lwline_as_lwgeom(lwline));
220  }
221  else
222  {
223  LWPOLY *lwpoly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, &pa);
224 
225  pt.x = box->xmin;
226  pt.y = box->ymin;
227  ptarray_append_point(pa, &pt, LW_TRUE);
228  pt.x = box->xmin;
229  pt.y = box->ymax;
230  ptarray_append_point(pa, &pt, LW_TRUE);
231  pt.x = box->xmax;
232  pt.y = box->ymax;
233  ptarray_append_point(pa, &pt, LW_TRUE);
234  pt.x = box->xmax;
235  pt.y = box->ymin;
236  ptarray_append_point(pa, &pt, LW_TRUE);
237  pt.x = box->xmin;
238  pt.y = box->ymin;
239  ptarray_append_point(pa, &pt, LW_TRUE);
240 
241  result = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
242 
243  }
244 
245  gserialized_set_srid(result, box->srid);
246  PG_RETURN_POINTER(result);
247 }
double x
Definition: liblwgeom.h:336
int32_t srid
Definition: liblwgeom.h:263
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
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:86
double ymin
Definition: liblwgeom.h:261
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:156
double xmin
Definition: liblwgeom.h:261
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:61
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
double xmax
Definition: liblwgeom.h:262
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:98
double y
Definition: liblwgeom.h:336
double ymax
Definition: liblwgeom.h:262
Here is the call graph for this function:
Here is the caller graph for this function: