PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_centroid_from_mline()

LWPOINT * geography_centroid_from_mline ( const LWMLINE mline,
SPHEROID s 
)

Split lines into segments and calculate with middle of segment as weighted point.

Definition at line 251 of file geography_centroid.c.

References geography_centroid_from_wpoints(), LWMLINE::geoms, getPoint2d_cp(), LW_TRUE, lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_set_geodetic(), lwpoint_as_lwgeom(), lwpoint_make2d(), POINT3DM::m, LWMLINE::ngeoms, POINTARRAY::npoints, LWLINE::points, LWMLINE::srid, POINT2D::x, POINT3DM::x, POINT2D::y, and POINT3DM::y.

Referenced by geography_centroid().

252 {
253  double_t tolerance = 0.0;
254  uint32_t size = 0;
255  uint32_t i, k;
256 
257  /* get total number of points */
258  for (i = 0; i < mline->ngeoms; i++) {
259  size += (mline->geoms[i]->points->npoints - 1) * 2;
260  }
261 
262  POINT3DM* points = palloc(size*sizeof(POINT3DM));
263  uint32_t j = 0;
264 
265  for (i = 0; i < mline->ngeoms; i++) {
266  LWLINE* line = mline->geoms[i];
267 
268  /* add both points of line segment as weighted point */
269  for (k = 0; k < line->points->npoints - 1; k++) {
270  const POINT2D* p1 = getPoint2d_cp(line->points, k);
271  const POINT2D* p2 = getPoint2d_cp(line->points, k+1);
272 
273  /* use line-segment length as weight */
274  LWPOINT* lwp1 = lwpoint_make2d(mline->srid, p1->x, p1->y);
275  LWPOINT* lwp2 = lwpoint_make2d(mline->srid, p2->x, p2->y);
276  LWGEOM* lwgeom1 = lwpoint_as_lwgeom(lwp1);
277  LWGEOM* lwgeom2 = lwpoint_as_lwgeom(lwp2);
278  lwgeom_set_geodetic(lwgeom1, LW_TRUE);
279  lwgeom_set_geodetic(lwgeom2, LW_TRUE);
280 
281  /* use point distance as weight */
282  double_t weight = lwgeom_distance_spheroid(lwgeom1, lwgeom2, s, tolerance);
283 
284  points[j].x = p1->x;
285  points[j].y = p1->y;
286  points[j].m = weight;
287  j++;
288 
289  points[j].x = p2->x;
290  points[j].y = p2->y;
291  points[j].m = weight;
292  j++;
293 
294  lwgeom_free(lwgeom1);
295  lwgeom_free(lwgeom2);
296  }
297  }
298 
299  LWPOINT* result = geography_centroid_from_wpoints(mline->srid, points, size);
300  pfree(points);
301  return result;
302 }
double x
Definition: liblwgeom.h:346
int npoints
Definition: liblwgeom.h:371
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
int ngeoms
Definition: liblwgeom.h:481
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:328
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
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
double y
Definition: liblwgeom.h:328
double y
Definition: liblwgeom.h:346
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
LWLINE ** geoms
Definition: liblwgeom.h:483
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:303
double lwgeom_distance_spheroid(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2, const SPHEROID *spheroid, double tolerance)
Calculate the geodetic distance from lwgeom1 to lwgeom2 on the spheroid.
Definition: lwgeodetic.c:2183
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...
int32_t srid
Definition: liblwgeom.h:480
POINTARRAY * points
Definition: liblwgeom.h:422
Here is the call graph for this function:
Here is the caller graph for this function: