PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geometry_to_polygon()

Datum geometry_to_polygon ( PG_FUNCTION_ARGS  )

Definition at line 181 of file geometry_inout.c.

182 {
183  POLYGON *polygon;
184  LWPOLY *lwpoly;
185  LWGEOM *lwgeom;
186  GSERIALIZED *geom;
187  POINTARRAY *pa;
188  GBOX gbox;
189  uint32_t i;
190  size_t size;
191 
192  POSTGIS_DEBUG(2, "geometry_to_polygon called");
193 
194  if ( PG_ARGISNULL(0) )
195  PG_RETURN_NULL();
196 
197  geom = PG_GETARG_GSERIALIZED_P(0);
198 
199  if ( gserialized_get_type(geom) != POLYGONTYPE )
200  elog(ERROR, "geometry_to_polygon only accepts Polygons");
201 
202  lwgeom = lwgeom_from_gserialized(geom);
203  if ( lwgeom_is_empty(lwgeom) )
204  PG_RETURN_NULL();
205  lwpoly = lwgeom_as_lwpoly(lwgeom);
206 
207  pa = lwpoly->rings[0];
208 
209  size = offsetof(POLYGON, p[0]) + sizeof(polygon->p[0]) * pa->npoints;
210  polygon = (POLYGON*)palloc0(size); /* zero any holes */
211  SET_VARSIZE(polygon, size);
212 
213  polygon->npts = pa->npoints;
214 
215  lwgeom_calculate_gbox(lwgeom, &gbox);
216  polygon->boundbox.low.x = gbox.xmin;
217  polygon->boundbox.low.y = gbox.ymin;
218  polygon->boundbox.high.x = gbox.xmax;
219  polygon->boundbox.high.y = gbox.ymax;
220 
221  for ( i = 0; i < pa->npoints; i++ )
222  {
223  const POINT2D *pt = getPoint2d_cp(pa, i);
224  (polygon->p[i]).x = pt->x;
225  (polygon->p[i]).y = pt->y;
226  }
227 
228  lwgeom_free(lwgeom);
229  PG_FREE_IF_COPY(geom,0);
230 
231  PG_RETURN_POLYGON_P(polygon);
232 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define POLYGONTYPE
Definition: liblwgeom.h:104
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
Definition: lwgeom.c:755
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:215
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
POINTARRAY ** rings
Definition: liblwgeom.h:519
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

References getPoint2d_cp(), gserialized_get_type(), lwgeom_as_lwpoly(), lwgeom_calculate_gbox(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), POINTARRAY::npoints, POLYGONTYPE, LWPOLY::rings, POINT2D::x, pixval::x, GBOX::xmax, GBOX::xmin, POINT2D::y, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: