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

◆ geometry_to_polygon()

Datum geometry_to_polygon ( PG_FUNCTION_ARGS  )

Definition at line 192 of file geometry_inout.c.

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}
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
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:1138
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition lwgeom.c:197
#define POLYGONTYPE
Definition liblwgeom.h:118
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:737
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:193
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:91
double ymax
Definition liblwgeom.h:343
double xmax
Definition liblwgeom.h:341
double ymin
Definition liblwgeom.h:342
double xmin
Definition liblwgeom.h:340
POINTARRAY ** rings
Definition liblwgeom.h:505
double x
Definition liblwgeom.h:376
uint32_t npoints
Definition liblwgeom.h:413

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, GBOX::xmax, GBOX::xmin, POINT2D::y, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: