PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geometry_to_polygon()

Datum geometry_to_polygon ( PG_FUNCTION_ARGS  )

Definition at line 168 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().

169 {
170  POLYGON *polygon;
171  LWPOLY *lwpoly;
172  LWGEOM *lwgeom;
173  GSERIALIZED *geom;
174  POINTARRAY *pa;
175  GBOX gbox;
176  int i;
177  size_t size;
178 
179  POSTGIS_DEBUG(2, "geometry_to_polygon called");
180 
181  if ( PG_ARGISNULL(0) )
182  PG_RETURN_NULL();
183 
184  geom = PG_GETARG_GSERIALIZED_P(0);
185 
186  if ( gserialized_get_type(geom) != POLYGONTYPE )
187  elog(ERROR, "geometry_to_polygon only accepts Polygons");
188 
189  lwgeom = lwgeom_from_gserialized(geom);
190  if ( lwgeom_is_empty(lwgeom) )
191  PG_RETURN_NULL();
192  lwpoly = lwgeom_as_lwpoly(lwgeom);
193 
194  pa = lwpoly->rings[0];
195 
196  size = offsetof(POLYGON, p[0]) + sizeof(polygon->p[0]) * pa->npoints;
197  polygon = (POLYGON*)palloc0(size); /* zero any holes */
198  SET_VARSIZE(polygon, size);
199 
200  polygon->npts = pa->npoints;
201 
202  lwgeom_calculate_gbox(lwgeom, &gbox);
203  polygon->boundbox.low.x = gbox.xmin;
204  polygon->boundbox.low.y = gbox.ymin;
205  polygon->boundbox.high.x = gbox.xmax;
206  polygon->boundbox.high.y = gbox.ymax;
207 
208  for ( i = 0; i < pa->npoints; i++ )
209  {
210  const POINT2D *pt = getPoint2d_cp(pa, i);
211  (polygon->p[i]).x = pt->x;
212  (polygon->p[i]).y = pt->y;
213  }
214 
215  lwgeom_free(lwgeom);
216  PG_FREE_IF_COPY(geom,0);
217 
218  PG_RETURN_POLYGON_P(polygon);
219 }
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:55
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:355
#define POLYGONTYPE
Definition: liblwgeom.h:72
double xmax
Definition: liblwgeom.h:277
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:125
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:652
double x
Definition: liblwgeom.h:312
double ymin
Definition: liblwgeom.h:278
double xmin
Definition: liblwgeom.h:276
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, int n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:472
POINTARRAY ** rings
Definition: liblwgeom.h:441
double ymax
Definition: liblwgeom.h:279
double y
Definition: liblwgeom.h:312
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:1297
Here is the call graph for this function:
Here is the caller graph for this function: