PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ geometry_to_polygon()

Datum geometry_to_polygon ( PG_FUNCTION_ARGS  )

Definition at line 192 of file geometry_inout.c.

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

Referenced by path_to_geometry().

193 {
194  POLYGON *polygon;
195  LWPOLY *lwpoly;
196  LWGEOM *lwgeom;
197  GSERIALIZED *geom;
198  POINTARRAY *pa;
199  GBOX gbox;
200  uint32_t i;
201  size_t size;
202 
203  POSTGIS_DEBUG(2, "geometry_to_polygon called");
204 
205  if ( PG_ARGISNULL(0) )
206  PG_RETURN_NULL();
207 
208  geom = PG_GETARG_GSERIALIZED_P(0);
209 
210  if ( gserialized_get_type(geom) != POLYGONTYPE )
211  elog(ERROR, "geometry_to_polygon only accepts Polygons");
212 
213  lwgeom = lwgeom_from_gserialized(geom);
214  if ( lwgeom_is_empty(lwgeom) )
215  PG_RETURN_NULL();
216  lwpoly = lwgeom_as_lwpoly(lwgeom);
217 
218  pa = lwpoly->rings[0];
219 
220  size = offsetof(POLYGON, p[0]) + sizeof(polygon->p[0]) * pa->npoints;
221  polygon = (POLYGON*)palloc0(size); /* zero any holes */
222  SET_VARSIZE(polygon, size);
223 
224  polygon->npts = pa->npoints;
225 
226  lwgeom_calculate_gbox(lwgeom, &gbox);
227  polygon->boundbox.low.x = gbox.xmin;
228  polygon->boundbox.low.y = gbox.ymin;
229  polygon->boundbox.high.x = gbox.xmax;
230  polygon->boundbox.high.y = gbox.ymax;
231 
232  for ( i = 0; i < pa->npoints; i++ )
233  {
234  const POINT2D *pt = getPoint2d_cp(pa, i);
235  (polygon->p[i]).x = pt->x;
236  (polygon->p[i]).y = pt->y;
237  }
238 
239  lwgeom_free(lwgeom);
240  PG_FREE_IF_COPY(geom,0);
241 
242  PG_RETURN_POLYGON_P(polygon);
243 }
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:86
double xmax
Definition: liblwgeom.h:295
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:206
unsigned int uint32_t
Definition: uthash.h:78
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:746
double x
Definition: liblwgeom.h:330
double ymin
Definition: liblwgeom.h:296
double xmin
Definition: liblwgeom.h:294
POINTARRAY ** rings
Definition: liblwgeom.h:459
double ymax
Definition: liblwgeom.h:297
double y
Definition: liblwgeom.h:330
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1393
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: