PostGIS  3.0.6dev-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 256 of file geography_centroid.c.

257 {
258  double_t tolerance = 0.0;
259  uint32_t size = 0;
260  uint32_t i, k, j = 0;
261  POINT3DM* points;
262  LWPOINT* result;
263 
264  /* get total number of points */
265  for (i = 0; i < mline->ngeoms; i++) {
266  size += (mline->geoms[i]->points->npoints - 1) * 2;
267  }
268 
269  points = palloc(size*sizeof(POINT3DM));
270 
271  for (i = 0; i < mline->ngeoms; i++) {
272  LWLINE* line = mline->geoms[i];
273 
274  /* add both points of line segment as weighted point */
275  for (k = 0; k < line->points->npoints - 1; k++) {
276  const POINT2D* p1 = getPoint2d_cp(line->points, k);
277  const POINT2D* p2 = getPoint2d_cp(line->points, k+1);
278  double_t weight;
279 
280  /* use line-segment length as weight */
281  LWPOINT* lwp1 = lwpoint_make2d(mline->srid, p1->x, p1->y);
282  LWPOINT* lwp2 = lwpoint_make2d(mline->srid, p2->x, p2->y);
283  LWGEOM* lwgeom1 = lwpoint_as_lwgeom(lwp1);
284  LWGEOM* lwgeom2 = lwpoint_as_lwgeom(lwp2);
285  lwgeom_set_geodetic(lwgeom1, LW_TRUE);
286  lwgeom_set_geodetic(lwgeom2, LW_TRUE);
287 
288  /* use point distance as weight */
289  weight = lwgeom_distance_spheroid(lwgeom1, lwgeom2, s, tolerance);
290 
291  points[j].x = p1->x;
292  points[j].y = p1->y;
293  points[j].m = weight;
294  j++;
295 
296  points[j].x = p2->x;
297  points[j].y = p2->y;
298  points[j].m = weight;
299  j++;
300 
301  lwgeom_free(lwgeom1);
302  lwgeom_free(lwgeom2);
303  }
304  }
305 
306  result = geography_centroid_from_wpoints(mline->srid, points, size);
307  pfree(points);
308  return result;
309 }
char * s
Definition: cu_in_wkt.c:23
LWPOINT * geography_centroid_from_wpoints(const int32_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...
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
Definition: lwgeom.c:946
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition: lwpoint.c:163
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:2187
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:326
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
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
POINTARRAY * points
Definition: liblwgeom.h:469
int32_t srid
Definition: liblwgeom.h:534
LWLINE ** geoms
Definition: liblwgeom.h:533
uint32_t ngeoms
Definition: liblwgeom.h:538
double y
Definition: liblwgeom.h:376
double x
Definition: liblwgeom.h:376
double m
Definition: liblwgeom.h:394
double x
Definition: liblwgeom.h:394
double y
Definition: liblwgeom.h:394
uint32_t npoints
Definition: liblwgeom.h:413

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, s, LWMLINE::srid, POINT2D::x, POINT3DM::x, POINT2D::y, and POINT3DM::y.

Referenced by geography_centroid().

Here is the call graph for this function:
Here is the caller graph for this function: