PostGIS  2.5.2dev-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, j = 0;
256  POINT3DM* points;
257  LWPOINT* result;
258 
259  /* get total number of points */
260  for (i = 0; i < mline->ngeoms; i++) {
261  size += (mline->geoms[i]->points->npoints - 1) * 2;
262  }
263 
264  points = palloc(size*sizeof(POINT3DM));
265 
266  for (i = 0; i < mline->ngeoms; i++) {
267  LWLINE* line = mline->geoms[i];
268 
269  /* add both points of line segment as weighted point */
270  for (k = 0; k < line->points->npoints - 1; k++) {
271  const POINT2D* p1 = getPoint2d_cp(line->points, k);
272  const POINT2D* p2 = getPoint2d_cp(line->points, k+1);
273  double_t weight;
274 
275  /* use line-segment length as weight */
276  LWPOINT* lwp1 = lwpoint_make2d(mline->srid, p1->x, p1->y);
277  LWPOINT* lwp2 = lwpoint_make2d(mline->srid, p2->x, p2->y);
278  LWGEOM* lwgeom1 = lwpoint_as_lwgeom(lwp1);
279  LWGEOM* lwgeom2 = lwpoint_as_lwgeom(lwp2);
280  lwgeom_set_geodetic(lwgeom1, LW_TRUE);
281  lwgeom_set_geodetic(lwgeom2, LW_TRUE);
282 
283  /* use point distance as weight */
284  weight = lwgeom_distance_spheroid(lwgeom1, lwgeom2, s, tolerance);
285 
286  points[j].x = p1->x;
287  points[j].y = p1->y;
288  points[j].m = weight;
289  j++;
290 
291  points[j].x = p2->x;
292  points[j].y = p2->y;
293  points[j].m = weight;
294  j++;
295 
296  lwgeom_free(lwgeom1);
297  lwgeom_free(lwgeom2);
298  }
299  }
300 
301  result = geography_centroid_from_wpoints(mline->srid, points, size);
302  pfree(points);
303  return result;
304 }
double x
Definition: liblwgeom.h:348
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:330
double m
Definition: liblwgeom.h:348
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
double y
Definition: liblwgeom.h:330
double y
Definition: liblwgeom.h:348
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
Definition: lwgeom.c:952
uint32_t ngeoms
Definition: liblwgeom.h:483
LWLINE ** geoms
Definition: liblwgeom.h:485
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
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:2100
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:482
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:374
POINTARRAY * points
Definition: liblwgeom.h:424
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: