PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_centroid_from_mpoly()

LWPOINT * geography_centroid_from_mpoly ( const LWMPOLY mpoly,
bool  use_spheroid,
SPHEROID s 
)

Split polygons into triangles and use centroid of the triangle with the triangle area as weight to calculate the centroid of a (multi)polygon.

Definition at line 309 of file geography_centroid.c.

References geography_centroid_from_wpoints(), LWMPOLY::geoms, getPoint2d_cp(), LW_TRUE, lwgeom_area_sphere(), lwgeom_area_spheroid(), lwgeom_free(), lwgeom_set_geodetic(), lwpoint_free(), lwpoint_get_x(), lwpoint_get_y(), lwpoly_add_ring(), lwpoly_as_lwgeom(), lwpoly_construct_empty(), POINT3DM::m, LWMPOLY::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, ptarray_construct_empty(), ptarray_insert_point(), LWPOLY::rings, LWMPOLY::srid, POINT3DM::x, POINT4D::x, POINT3DM::y, and POINT4D::y.

Referenced by geography_centroid().

310 {
311  uint32_t size = 0;
312  uint32_t i, ir, ip;
313  for (ip = 0; ip < mpoly->ngeoms; ip++) {
314  for (ir = 0; ir < mpoly->geoms[ip]->nrings; ir++) {
315  size += mpoly->geoms[ip]->rings[ir]->npoints - 1;
316  }
317  }
318 
319  POINT3DM* points = palloc(size*sizeof(POINT3DM));
320  uint32_t j = 0;
321 
322  /* use first point as reference to create triangles */
323  const POINT4D* reference_point = (const POINT4D*) getPoint2d_cp(mpoly->geoms[0]->rings[0], 0);
324 
325  for (ip = 0; ip < mpoly->ngeoms; ip++) {
326  LWPOLY* poly = mpoly->geoms[ip];
327 
328  for (ir = 0; ir < poly->nrings; ir++) {
329  POINTARRAY* ring = poly->rings[ir];
330 
331  /* split into triangles (two points + reference point) */
332  for (i = 0; i < ring->npoints - 1; i++) {
333  LWPOLY* poly_tri;
334  LWGEOM* geom_tri;
335  LWPOINT* tri_centroid;
336  POINT3DM triangle[3];
337  double_t weight;
338  const POINT4D* p1 = (const POINT4D*) getPoint2d_cp(ring, i);
339  const POINT4D* p2 = (const POINT4D*) getPoint2d_cp(ring, i+1);
340  POINTARRAY* pa = ptarray_construct_empty(0, 0, 4);
341 
342  ptarray_insert_point(pa, p1, 0);
343  ptarray_insert_point(pa, p2, 1);
344  ptarray_insert_point(pa, reference_point, 2);
345  ptarray_insert_point(pa, p1, 3);
346 
347  poly_tri = lwpoly_construct_empty(mpoly->srid, 0, 0);
348  lwpoly_add_ring(poly_tri, pa);
349 
350  geom_tri = lwpoly_as_lwgeom(poly_tri);
351  lwgeom_set_geodetic(geom_tri, LW_TRUE);
352 
353  /* Calculate the weight of the triangle. If counter clockwise,
354  * the weight is negative (e.g. for holes in polygons)
355  */
356 
357  if ( use_spheroid )
358  weight = lwgeom_area_spheroid(geom_tri, s);
359  else
360  weight = lwgeom_area_sphere(geom_tri, s);
361 
362 
363  triangle[0].x = p1->x;
364  triangle[0].y = p1->y;
365  triangle[0].m = 1;
366 
367  triangle[1].x = p2->x;
368  triangle[1].y = p2->y;
369  triangle[1].m = 1;
370 
371  triangle[2].x = reference_point->x;
372  triangle[2].y = reference_point->y;
373  triangle[2].m = 1;
374 
375  /* get center of triangle */
376  tri_centroid = geography_centroid_from_wpoints(mpoly->srid, triangle, 3);
377 
378  points[j].x = lwpoint_get_x(tri_centroid);
379  points[j].y = lwpoint_get_y(tri_centroid);
380  points[j].m = weight;
381  j++;
382 
383  lwpoint_free(tri_centroid);
384  lwgeom_free(geom_tri);
385  }
386  }
387  }
388  LWPOINT* result = geography_centroid_from_wpoints(mpoly->srid, points, size);
389  pfree(points);
390  return result;
391 }
double x
Definition: liblwgeom.h:352
double x
Definition: liblwgeom.h:346
int npoints
Definition: liblwgeom.h:371
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:288
unsigned int uint32_t
Definition: uthash.h:78
double lwpoint_get_x(const LWPOINT *point)
Definition: lwpoint.c:63
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:373
double m
Definition: liblwgeom.h:346
LWPOLY ** geoms
Definition: liblwgeom.h:496
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
POINTARRAY ** rings
Definition: liblwgeom.h:457
int nrings
Definition: liblwgeom.h:455
int ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, int where)
Insert a point into an existing POINTARRAY.
Definition: ptarray.c:96
double y
Definition: liblwgeom.h:346
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the sphere.
Definition: lwgeodetic.c:2027
int ngeoms
Definition: liblwgeom.h:494
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
Definition: lwgeom.c:907
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:161
double lwpoint_get_y(const LWPOINT *point)
Definition: lwpoint.c:76
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the spheroid.
Definition: lwspheroid.c:642
int lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
Add a ring, allocating extra space if necessary.
Definition: lwpoly.c:249
double y
Definition: liblwgeom.h:352
int32_t srid
Definition: liblwgeom.h:493
LWPOINT * geography_centroid_from_wpoints(const uint32_t srid, const POINT3DM *points, const uint32_t size)
Convert lat-lon-points to x-y-z-coordinates, calculate a weighted average point and return lat-lon-co...
Here is the call graph for this function:
Here is the caller graph for this function: