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

◆ 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;
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);
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
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
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:992
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition lwgeom.c:372
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.
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition lwpoint.c:163
#define LW_TRUE
Return types for functions with status returns.
Definition liblwgeom.h:93
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:97
POINTARRAY * points
Definition liblwgeom.h:483
int32_t srid
Definition liblwgeom.h:548
LWLINE ** geoms
Definition liblwgeom.h:547
uint32_t ngeoms
Definition liblwgeom.h:552
double y
Definition liblwgeom.h:390
double x
Definition liblwgeom.h:390
double m
Definition liblwgeom.h:408
double x
Definition liblwgeom.h:408
double y
Definition liblwgeom.h:408
uint32_t npoints
Definition liblwgeom.h:427

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, result, 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: