Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgis/liblwgeom/lwgeom.c
Line
Count
Source
1
/**********************************************************************
2
 *
3
 * PostGIS - Spatial Types for PostgreSQL
4
 * http://postgis.net
5
 *
6
 * PostGIS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * PostGIS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with PostGIS.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 **********************************************************************
20
 *
21
 * Copyright (C) 2001-2006 Refractions Research Inc.
22
 * Copyright (C) 2017-2018 Daniel Baston <dbaston@gmail.com>
23
 * Copyright (C) 2026 Darafei Praliaskouski <me@komzpa.net>
24
 *
25
 **********************************************************************/
26
27
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <stdarg.h>
31
#include <string.h>
32
33
#include "liblwgeom_internal.h"
34
#include "lwgeom_log.h"
35
36
#define out_stack_size 32
37
38
static char
39
lwnurbscurve_weight_same(const LWNURBSCURVE *curve1, const LWNURBSCURVE *curve2)
40
0
{
41
0
  uint32_t npoints = curve1->points->npoints;
42
43
0
  if ((curve1->nweights && curve1->nweights != npoints) ||
44
0
      (curve2->nweights && curve2->nweights != npoints))
45
0
    return LW_FALSE;
46
47
0
  for (uint32_t i = 0; i < npoints; i++)
48
0
  {
49
0
    double weight1 = curve1->weights ? curve1->weights[i] : 1.0;
50
0
    double weight2 = curve2->weights ? curve2->weights[i] : 1.0;
51
0
    if (!FP_EQUALS(weight1, weight2))
52
0
      return LW_FALSE;
53
0
  }
54
55
0
  return LW_TRUE;
56
0
}
57
58
static double
59
lwnurbscurve_uniform_knot_value(uint32_t degree, uint32_t npoints, uint32_t idx)
60
0
{
61
0
  uint32_t nknots = npoints + degree + 1;
62
0
  uint32_t internal_knots = nknots - 2 * (degree + 1);
63
64
0
  if (idx <= degree)
65
0
    return 0.0;
66
0
  if (idx >= nknots - degree - 1)
67
0
    return 1.0;
68
69
0
  return (double)(idx - degree) / (internal_knots + 1);
70
0
}
71
72
static char
73
lwnurbscurve_knot_same(const LWNURBSCURVE *curve1, const LWNURBSCURVE *curve2)
74
0
{
75
0
  uint32_t npoints = curve1->points->npoints;
76
0
  uint32_t nknots;
77
78
0
  if (npoints < curve1->degree + 1)
79
0
    return LW_FALSE;
80
81
0
  nknots = npoints + curve1->degree + 1;
82
0
  if ((curve1->nknots && curve1->nknots != nknots) ||
83
0
      (curve2->nknots && curve2->nknots != nknots))
84
0
    return LW_FALSE;
85
86
0
  for (uint32_t i = 0; i < nknots; i++)
87
0
  {
88
0
    double knot1 = curve1->knots ? curve1->knots[i] :
89
0
      lwnurbscurve_uniform_knot_value(curve1->degree, npoints, i);
90
0
    double knot2 = curve2->knots ? curve2->knots[i] :
91
0
      lwnurbscurve_uniform_knot_value(curve2->degree, npoints, i);
92
0
    if (!(isnan(knot1) && isnan(knot2)) && !FP_EQUALS(knot1, knot2))
93
0
      return LW_FALSE;
94
0
  }
95
96
0
  return LW_TRUE;
97
0
}
98
99
static char
100
lwnurbscurve_same(const LWNURBSCURVE *curve1, const LWNURBSCURVE *curve2)
101
0
{
102
0
  if (lwnurbscurve_is_empty(curve1) && lwnurbscurve_is_empty(curve2))
103
0
    return LW_TRUE;
104
105
0
  if (curve1->degree != curve2->degree)
106
0
    return LW_FALSE;
107
0
  if (!curve1->points || !curve2->points || !ptarray_same(curve1->points, curve2->points))
108
0
    return LW_FALSE;
109
0
  if (!lwnurbscurve_weight_same(curve1, curve2))
110
0
    return LW_FALSE;
111
0
  if (!lwnurbscurve_knot_same(curve1, curve2))
112
0
    return LW_FALSE;
113
114
0
  return LW_TRUE;
115
0
}
116
117
static void lwcurvepoly_force_orientation(LWCURVEPOLY *poly, int orientation);
118
119
/**
120
 * Force an orientation onto geometries made up of rings,
121
 * so the rings circle in a particular direction, clockwise
122
 * or counter-clockwise. The orientation is one of
123
 * LW_CLOCKWISE or LW_COUNTERCLOCKWISE. Non-polygonal
124
 * geometries are ignored and returned untouched.
125
 */
126
static void
127
lwgeom_force_orientation(LWGEOM *lwgeom, int orientation)
128
0
{
129
0
  if (lwgeom->type == CURVEPOLYTYPE)
130
0
  {
131
0
    lwcurvepoly_force_orientation(lwgeom_as_lwcurvepoly(lwgeom), orientation);
132
0
    return;
133
0
  }
134
0
  switch (lwgeom->type)
135
0
  {
136
0
    case POLYGONTYPE:
137
0
      lwpoly_force_orientation(lwgeom_as_lwpoly(lwgeom), orientation);
138
0
      return;
139
140
0
    case TRIANGLETYPE:
141
0
      lwtriangle_force_orientation(lwgeom_as_lwtriangle(lwgeom), orientation);
142
0
      return;
143
144
    /* Not handle POLYHEDRALSURFACE and TIN
145
       as they are supposed to be well oriented */
146
0
    case MULTIPOLYGONTYPE:
147
0
    case COLLECTIONTYPE:
148
0
    {
149
0
      LWCOLLECTION* coll = lwgeom_as_lwcollection(lwgeom);
150
0
      for (uint32_t i = 0; coll && i < coll->ngeoms; i++)
151
0
      {
152
0
        lwgeom_force_orientation(coll->geoms[i], orientation);
153
0
      }
154
0
      return;
155
0
    }
156
0
  }
157
0
}
158
159
void
160
lwgeom_force_clockwise(LWGEOM *lwgeom)
161
0
{
162
0
  return lwgeom_force_orientation(lwgeom, LW_CLOCKWISE);
163
0
}
164
165
void
166
lwgeom_force_counterclockwise(LWGEOM *lwgeom)
167
0
{
168
0
  return lwgeom_force_orientation(lwgeom, LW_COUNTERCLOCKWISE);
169
0
}
170
171
static int
172
lwcurvepoly_contains_nurbs(const LWGEOM *geom)
173
0
{
174
0
  switch (geom->type)
175
0
  {
176
0
  case NURBSCURVETYPE:
177
0
    return 1;
178
0
  case LINETYPE:
179
0
  case CIRCSTRINGTYPE:
180
0
    return 0;
181
0
  default:
182
0
    break;
183
0
  }
184
0
  if (lwgeom_is_collection(geom))
185
0
  {
186
0
    const LWCOLLECTION *coll = lwgeom_as_lwcollection(geom);
187
0
    for (uint32_t i = 0; i < coll->ngeoms; i++)
188
0
      if (lwcurvepoly_contains_nurbs(coll->geoms[i]))
189
0
        return 1;
190
0
  }
191
0
  return 0;
192
0
}
193
194
static double
195
lwcurve_scaled_difference(double coordinate, double origin, double scale)
196
0
{
197
0
  double difference = coordinate - origin;
198
0
  return isfinite(difference) ? difference / scale : coordinate / scale - origin / scale;
199
0
}
200
201
static double
202
lwcurve_chord_area(const POINT2D *a, const POINT2D *b)
203
0
{ return (a->x * b->y - b->x * a->y) / 2.0; }
204
205
static double
206
lwcurve_ring_area(const LWGEOM *ring, const POINT2D *origin, double scale)
207
0
{
208
0
  double area = 0.0;
209
0
  switch (ring->type)
210
0
  {
211
0
  case LINETYPE: {
212
0
    const POINTARRAY *points = lwgeom_as_lwline(ring)->points;
213
0
    for (uint32_t i = 1; i < points->npoints; i++)
214
0
    {
215
0
      POINT2D a, b;
216
0
      getPoint2d_p(points, i - 1, &a);
217
0
      getPoint2d_p(points, i, &b);
218
0
      a.x = lwcurve_scaled_difference(a.x, origin->x, scale);
219
0
      a.y = lwcurve_scaled_difference(a.y, origin->y, scale);
220
0
      b.x = lwcurve_scaled_difference(b.x, origin->x, scale);
221
0
      b.y = lwcurve_scaled_difference(b.y, origin->y, scale);
222
0
      area -= lwcurve_chord_area(&a, &b);
223
0
    }
224
0
    break;
225
0
  }
226
0
  case CIRCSTRINGTYPE: {
227
0
    const POINTARRAY *points = lwgeom_as_lwcircstring(ring)->points;
228
0
    for (uint32_t i = 2; i < points->npoints; i += 2)
229
0
    {
230
0
      POINT2D arc[3];
231
0
      for (uint32_t j = 0; j < 3; j++)
232
0
        getPoint2d_p(points, i - 2 + j, &arc[j]);
233
0
      POINT2D a = {lwcurve_scaled_difference(arc[0].x, origin->x, scale),
234
0
             lwcurve_scaled_difference(arc[0].y, origin->y, scale)};
235
0
      POINT2D b = {lwcurve_scaled_difference(arc[2].x, origin->x, scale),
236
0
             lwcurve_scaled_difference(arc[2].y, origin->y, scale)};
237
0
      area -= lwcurve_chord_area(&a, &b);
238
0
      if (p2d_same(&arc[0], &arc[2]))
239
0
      {
240
        /* Equal endpoints encode a canonical CCW circle; the middle point is opposite. */
241
0
        double radius = hypot(lwcurve_scaled_difference(arc[1].x, arc[0].x, scale),
242
0
                  lwcurve_scaled_difference(arc[1].y, arc[0].y, scale)) /
243
0
            2.0;
244
0
        area -= M_PI * radius * radius;
245
0
        continue;
246
0
      }
247
0
      double ux = lwcurve_scaled_difference(arc[1].x, arc[0].x, scale);
248
0
      double uy = lwcurve_scaled_difference(arc[1].y, arc[0].y, scale);
249
0
      double vx = lwcurve_scaled_difference(arc[2].x, arc[1].x, scale);
250
0
      double vy = lwcurve_scaled_difference(arc[2].y, arc[1].y, scale);
251
0
      double cross = ux * vy - uy * vx;
252
0
      double dot = ux * vx + uy * vy;
253
0
      if (cross == 0.0)
254
0
        continue;
255
0
      double chord1 = hypot(ux, uy);
256
0
      double chord2 = hypot(vx, vy);
257
0
      double chord3 = hypot(lwcurve_scaled_difference(arc[2].x, arc[0].x, scale),
258
0
                lwcurve_scaled_difference(arc[2].y, arc[0].y, scale));
259
0
      double radius = (chord1 / 2.0) * (chord2 / (fabs(cross) / chord3));
260
0
      double sweep = 2.0 * atan2(cross, dot);
261
0
      if (fabs(sweep) < 1e-3)
262
0
      {
263
0
        area -= chord3 * chord3 * sweep *
264
0
          (1.0 / 12.0 + sweep * sweep / 360.0 + sweep * sweep * sweep * sweep / 10080.0);
265
0
      }
266
0
      else
267
0
      {
268
0
        area -= radius * radius * (sweep - sin(sweep)) / 2.0;
269
0
      }
270
0
    }
271
0
    break;
272
0
  }
273
0
  case COMPOUNDTYPE: {
274
0
    const LWCOLLECTION *compound = lwgeom_as_lwcollection(ring);
275
0
    for (uint32_t i = 0; i < compound->ngeoms; i++)
276
0
      area += lwcurve_ring_area(compound->geoms[i], origin, scale);
277
0
    break;
278
0
  }
279
0
  case NURBSCURVETYPE:
280
0
    return NAN;
281
0
  default:
282
0
    break;
283
0
  }
284
0
  return area;
285
0
}
286
287
static int
288
lwcurvepoly_ring_has_orientation(const LWGEOM *ring, int orientation, int *is_full_circle, int *is_determinate)
289
0
{
290
0
  const LWGEOM *circle = ring;
291
0
  if (ring->type == COMPOUNDTYPE)
292
0
  {
293
0
    const LWCOLLECTION *compound = lwgeom_as_lwcollection(ring);
294
0
    if (compound->ngeoms == 1)
295
0
      circle = compound->geoms[0];
296
0
  }
297
0
  if (circle->type == CIRCSTRINGTYPE)
298
0
  {
299
0
    const POINTARRAY *points = lwgeom_as_lwcircstring(circle)->points;
300
0
    POINT2D first, last;
301
0
    if (points->npoints == 3 && getPoint2d_p(points, 0, &first) && getPoint2d_p(points, 2, &last) &&
302
0
        p2d_same(&first, &last))
303
0
    {
304
0
      if (is_full_circle)
305
0
        *is_full_circle = LW_TRUE;
306
0
      *is_determinate = LW_TRUE;
307
      /* This ring has no winding. Calling it CCW (GT-1049 did) makes force-CW
308
       * unable to satisfy is-CW because reversal leaves its bytes unchanged;
309
       * splitting it into semicircles changes the stored geometry and differs
310
       * from 3.7.0. Treat it as either orientation and leave it untouched. */
311
0
      return LW_TRUE;
312
0
    }
313
0
  }
314
0
  if (is_full_circle)
315
0
    *is_full_circle = LW_FALSE;
316
0
  if (ring->type == LINETYPE || (ring->type == COMPOUNDTYPE && !lwgeom_has_arc(ring)))
317
0
  {
318
0
    LWGEOM *linear = ring->type == LINETYPE
319
0
             ? NULL
320
0
             : lwcurve_linearize(ring, 0.0, LW_LINEARIZE_TOLERANCE_TYPE_SEGS_PER_QUAD, 0);
321
0
    const POINTARRAY *points = lwgeom_as_lwline(linear ? linear : (LWGEOM *)ring)->points;
322
0
    double area = ptarray_signed_area(points);
323
0
    if (linear)
324
0
      lwgeom_free(linear);
325
0
    *is_determinate = area != 0.0;
326
0
    return orientation == (area > 0.0 ? LW_CLOCKWISE : LW_COUNTERCLOCKWISE);
327
0
  }
328
0
  POINT4D start;
329
0
  POINT2D origin = {0.0, 0.0};
330
0
  if (lwgeom_startpoint(ring, &start))
331
0
    origin = (POINT2D){start.x, start.y};
332
0
  GBOX bbox;
333
0
  double scale = 1.0;
334
0
  if (lwgeom_calculate_gbox(ring, &bbox) == LW_SUCCESS)
335
0
  {
336
0
    scale = fmax(bbox.xmax - bbox.xmin, bbox.ymax - bbox.ymin);
337
0
    if (!isfinite(scale))
338
0
      scale = fmax(fmax(fabs(bbox.xmax), fabs(bbox.xmin)), fmax(fabs(bbox.ymax), fabs(bbox.ymin)));
339
0
    if (!(scale > 0.0))
340
0
      scale = 1.0;
341
0
  }
342
0
  double area = lwcurve_ring_area(ring, &origin, scale);
343
0
  *is_determinate = isfinite(area) && area != 0.0;
344
0
  if (!*is_determinate)
345
0
    return LW_FALSE; /* Cancellation to zero leaves the ring without a winding. */
346
0
  return orientation == (area > 0.0 ? LW_CLOCKWISE : LW_COUNTERCLOCKWISE);
347
0
}
348
349
static int
350
lwcurvepoly_orientation(LWCURVEPOLY *poly, int orientation, int force)
351
0
{
352
0
  if (lwcurvepoly_contains_nurbs(lwcurvepoly_as_lwgeom(poly)))
353
0
    return LW_TRUE;
354
0
  int has_orientation = LW_TRUE;
355
0
  for (uint32_t i = 0; i < poly->nrings; i++)
356
0
  {
357
0
    int ring_orientation = i == 0 ? orientation : -orientation;
358
0
    int is_full_circle;
359
0
    int is_determinate;
360
0
    int ring_has_orientation = lwcurvepoly_ring_has_orientation(
361
0
        poly->rings[i], ring_orientation, &is_full_circle, &is_determinate);
362
0
    if (force)
363
0
    {
364
0
      if (!is_full_circle && is_determinate && !ring_has_orientation)
365
0
        lwgeom_reverse_in_place(poly->rings[i]);
366
0
    }
367
0
    else if (!ring_has_orientation)
368
0
      has_orientation = LW_FALSE;
369
0
  }
370
0
  return has_orientation;
371
0
}
372
373
static int
374
lwcurvepoly_has_orientation(const LWCURVEPOLY *poly, int orientation)
375
0
{ return lwcurvepoly_orientation((LWCURVEPOLY *)poly, orientation, LW_FALSE); }
376
377
static void
378
lwcurvepoly_force_orientation(LWCURVEPOLY *poly, int orientation)
379
0
{ (void)lwcurvepoly_orientation(poly, orientation, LW_TRUE); }
380
381
/**
382
 * Check clockwise orientation on LWGEOM polygons
383
 * returns LW_CLOCKWISE, LW_NONE, LW_COUNTERCLOCKWISE
384
 * Non-polygonal geometries return LW_NONE
385
 * Geometries must have *all* rings correctly oriented
386
 * to return a non-none orientation
387
 */
388
int
389
lwgeom_has_orientation(const LWGEOM *lwgeom, int orientation)
390
0
{
391
0
  switch (lwgeom->type)
392
0
  {
393
0
    case POLYGONTYPE:
394
0
      return lwpoly_has_orientation(lwgeom_as_lwpoly(lwgeom), orientation);
395
396
0
    case CURVEPOLYTYPE:
397
0
      return lwcurvepoly_has_orientation(lwgeom_as_lwcurvepoly(lwgeom), orientation);
398
399
0
    case TRIANGLETYPE:
400
0
      return lwtriangle_has_orientation(lwgeom_as_lwtriangle(lwgeom), orientation);
401
402
0
    case MULTIPOLYGONTYPE:
403
0
    case COLLECTIONTYPE:
404
0
    {
405
0
      LWCOLLECTION* coll = lwgeom_as_lwcollection(lwgeom);
406
0
      for (uint32_t i = 0; coll && i < coll->ngeoms; i++)
407
0
      {
408
0
        if(!lwgeom_has_orientation(coll->geoms[i], orientation))
409
0
          return LW_FALSE;
410
0
      }
411
0
      return LW_TRUE;
412
0
    }
413
414
0
    default:
415
0
      return LW_TRUE;
416
0
  }
417
0
  return LW_FALSE;
418
0
}
419
420
LWGEOM *
421
lwgeom_reverse(const LWGEOM *geom)
422
0
{
423
0
  LWGEOM *geomout = lwgeom_clone_deep(geom);
424
0
  lwgeom_reverse_in_place(geomout);
425
0
  return geomout;
426
0
}
427
428
/** Reverse vertex order of LWGEOM **/
429
void
430
lwgeom_reverse_in_place(LWGEOM *geom)
431
0
{
432
0
  uint32_t i;
433
0
  LWCOLLECTION *col;
434
0
  if (!geom)
435
0
    return;
436
437
0
  switch (geom->type)
438
0
  {
439
0
    case MULTIPOINTTYPE:
440
0
    case POINTTYPE:
441
0
    {
442
0
      return;
443
0
    }
444
0
    case TRIANGLETYPE:
445
0
    case CIRCSTRINGTYPE:
446
0
    case LINETYPE:
447
0
    {
448
0
      LWLINE *line = (LWLINE *)(geom);
449
0
      ptarray_reverse_in_place(line->points);
450
0
      return;
451
0
    }
452
0
    case POLYGONTYPE:
453
0
    {
454
0
      LWPOLY *poly = (LWPOLY *)(geom);
455
0
      if (!poly->rings)
456
0
        return;
457
0
      uint32_t r;
458
0
      for (r = 0; r < poly->nrings; r++)
459
0
        ptarray_reverse_in_place(poly->rings[r]);
460
0
      return;
461
0
    }
462
    /* CompoundCurve needs to also reverse the sub-geometries */
463
    /* so that the end-points remain coincident */
464
0
    case COMPOUNDTYPE:
465
0
    {
466
0
      uint32_t ngeoms;
467
0
      col = (LWCOLLECTION *)(geom);
468
0
      if (!col->geoms)
469
0
        return;
470
0
      ngeoms = col->ngeoms;
471
0
      for (i=0; i<ngeoms; i++)
472
0
        lwgeom_reverse_in_place(col->geoms[i]);
473
0
      for (i=0; i<col->ngeoms/2; i++) {
474
0
        LWGEOM* tmp = col->geoms[i];
475
0
        col->geoms[i] = col->geoms[ngeoms-i-1];
476
0
        col->geoms[ngeoms-i-1] = tmp;
477
0
      }
478
0
      return;
479
0
    }
480
0
    case MULTICURVETYPE:
481
0
    case MULTILINETYPE:
482
0
    case MULTIPOLYGONTYPE:
483
0
    case MULTISURFACETYPE:
484
0
    case POLYHEDRALSURFACETYPE:
485
0
    case TINTYPE:
486
0
    case COLLECTIONTYPE:
487
0
    case CURVEPOLYTYPE:
488
0
    {
489
0
      col = (LWCOLLECTION *)(geom);
490
0
      if (!col->geoms)
491
0
        return;
492
0
      for (i=0; i<col->ngeoms; i++)
493
0
        lwgeom_reverse_in_place(col->geoms[i]);
494
0
      return;
495
0
    }
496
0
    default:
497
0
    {
498
0
      lwerror("%s: Unknown geometry type: %s", __func__, lwtype_name(geom->type));
499
0
      return;
500
0
    }
501
502
0
  }
503
0
}
504
505
LWLINE *
506
lwgeom_as_lwline(const LWGEOM *lwgeom)
507
0
{
508
0
  if ( lwgeom == NULL ) return NULL;
509
0
  if ( lwgeom->type == LINETYPE )
510
0
    return (LWLINE *)lwgeom;
511
0
  else return NULL;
512
0
}
513
514
LWCIRCSTRING *
515
lwgeom_as_lwcircstring(const LWGEOM *lwgeom)
516
0
{
517
0
  if ( lwgeom == NULL ) return NULL;
518
0
  if ( lwgeom->type == CIRCSTRINGTYPE )
519
0
    return (LWCIRCSTRING *)lwgeom;
520
0
  else return NULL;
521
0
}
522
523
LWCOMPOUND *
524
lwgeom_as_lwcompound(const LWGEOM *lwgeom)
525
0
{
526
0
  if ( lwgeom == NULL ) return NULL;
527
0
  if ( lwgeom->type == COMPOUNDTYPE )
528
0
    return (LWCOMPOUND *)lwgeom;
529
0
  else return NULL;
530
0
}
531
532
LWCURVEPOLY *
533
lwgeom_as_lwcurvepoly(const LWGEOM *lwgeom)
534
0
{
535
0
  if ( lwgeom == NULL ) return NULL;
536
0
  if ( lwgeom->type == CURVEPOLYTYPE )
537
0
    return (LWCURVEPOLY *)lwgeom;
538
0
  else return NULL;
539
0
}
540
541
LWPOLY *
542
lwgeom_as_lwpoly(const LWGEOM *lwgeom)
543
0
{
544
0
  if ( lwgeom == NULL ) return NULL;
545
0
  if ( lwgeom->type == POLYGONTYPE )
546
0
    return (LWPOLY *)lwgeom;
547
0
  else return NULL;
548
0
}
549
550
LWTRIANGLE *
551
lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
552
0
{
553
0
  if ( lwgeom == NULL ) return NULL;
554
0
  if ( lwgeom->type == TRIANGLETYPE )
555
0
    return (LWTRIANGLE *)lwgeom;
556
0
  else return NULL;
557
0
}
558
559
LWCOLLECTION *
560
lwgeom_as_lwcollection(const LWGEOM *lwgeom)
561
0
{
562
0
  if ( lwgeom == NULL ) return NULL;
563
0
  if ( lwgeom_is_collection(lwgeom) )
564
0
    return (LWCOLLECTION*)lwgeom;
565
0
  else return NULL;
566
0
}
567
568
LWMPOINT *
569
lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
570
0
{
571
0
  if ( lwgeom == NULL ) return NULL;
572
0
  if ( lwgeom->type == MULTIPOINTTYPE )
573
0
    return (LWMPOINT *)lwgeom;
574
0
  else return NULL;
575
0
}
576
577
LWMLINE *
578
lwgeom_as_lwmline(const LWGEOM *lwgeom)
579
0
{
580
0
  if ( lwgeom == NULL ) return NULL;
581
0
  if ( lwgeom->type == MULTILINETYPE )
582
0
    return (LWMLINE *)lwgeom;
583
0
  else return NULL;
584
0
}
585
586
LWMPOLY *
587
lwgeom_as_lwmpoly(const LWGEOM *lwgeom)
588
0
{
589
0
  if ( lwgeom == NULL ) return NULL;
590
0
  if ( lwgeom->type == MULTIPOLYGONTYPE )
591
0
    return (LWMPOLY *)lwgeom;
592
0
  else return NULL;
593
0
}
594
595
LWPSURFACE *
596
lwgeom_as_lwpsurface(const LWGEOM *lwgeom)
597
0
{
598
0
  if ( lwgeom->type == POLYHEDRALSURFACETYPE )
599
0
    return (LWPSURFACE *)lwgeom;
600
0
  else return NULL;
601
0
}
602
603
LWTIN *
604
lwgeom_as_lwtin(const LWGEOM *lwgeom)
605
0
{
606
0
  if ( lwgeom->type == TINTYPE )
607
0
    return (LWTIN *)lwgeom;
608
0
  else return NULL;
609
0
}
610
611
LWGEOM *lwtin_as_lwgeom(const LWTIN *obj)
612
0
{
613
0
  return (LWGEOM *)obj;
614
0
}
615
616
LWGEOM *lwpsurface_as_lwgeom(const LWPSURFACE *obj)
617
0
{
618
0
  return (LWGEOM *)obj;
619
0
}
620
621
LWGEOM *lwmpoly_as_lwgeom(const LWMPOLY *obj)
622
0
{
623
0
  if ( obj == NULL ) return NULL;
624
0
  return (LWGEOM *)obj;
625
0
}
626
LWGEOM *lwmline_as_lwgeom(const LWMLINE *obj)
627
0
{
628
0
  if ( obj == NULL ) return NULL;
629
0
  return (LWGEOM *)obj;
630
0
}
631
LWGEOM *lwmpoint_as_lwgeom(const LWMPOINT *obj)
632
0
{
633
0
  if ( obj == NULL ) return NULL;
634
0
  return (LWGEOM *)obj;
635
0
}
636
LWGEOM *lwcollection_as_lwgeom(const LWCOLLECTION *obj)
637
0
{
638
0
  if ( obj == NULL ) return NULL;
639
0
  return (LWGEOM *)obj;
640
0
}
641
LWGEOM *lwcircstring_as_lwgeom(const LWCIRCSTRING *obj)
642
0
{
643
0
  if ( obj == NULL ) return NULL;
644
0
  return (LWGEOM *)obj;
645
0
}
646
LWGEOM *lwcurvepoly_as_lwgeom(const LWCURVEPOLY *obj)
647
0
{
648
0
  if ( obj == NULL ) return NULL;
649
0
  return (LWGEOM *)obj;
650
0
}
651
LWGEOM *lwcompound_as_lwgeom(const LWCOMPOUND *obj)
652
0
{
653
0
  if ( obj == NULL ) return NULL;
654
0
  return (LWGEOM *)obj;
655
0
}
656
LWGEOM *lwpoly_as_lwgeom(const LWPOLY *obj)
657
0
{
658
0
  if ( obj == NULL ) return NULL;
659
0
  return (LWGEOM *)obj;
660
0
}
661
LWGEOM *lwtriangle_as_lwgeom(const LWTRIANGLE *obj)
662
0
{
663
0
  if ( obj == NULL ) return NULL;
664
0
  return (LWGEOM *)obj;
665
0
}
666
LWGEOM *lwline_as_lwgeom(const LWLINE *obj)
667
0
{
668
0
  if ( obj == NULL ) return NULL;
669
0
  return (LWGEOM *)obj;
670
0
}
671
LWGEOM *lwpoint_as_lwgeom(const LWPOINT *obj)
672
0
{
673
0
  if ( obj == NULL ) return NULL;
674
0
  return (LWGEOM *)obj;
675
0
}
676
677
678
/**
679
** Look-up for the correct MULTI* type promotion for singleton types.
680
*/
681
uint8_t MULTITYPE[NUMTYPES] =
682
{
683
  0,
684
  MULTIPOINTTYPE,        /*  1 */
685
  MULTILINETYPE,         /*  2 */
686
  MULTIPOLYGONTYPE,      /*  3 */
687
  0,0,0,0,
688
  MULTICURVETYPE,        /*  8 */
689
  MULTICURVETYPE,        /*  9 */
690
  MULTISURFACETYPE,      /* 10 */
691
  POLYHEDRALSURFACETYPE, /* 11 */
692
  0, 0,
693
  TINTYPE,               /* 14 */
694
  0,
695
  MULTICURVETYPE         /* 16 */
696
};
697
698
uint8_t lwtype_multitype(uint8_t type)
699
0
{
700
0
  if (type >= NUMTYPES) return 0;
701
0
  return MULTITYPE[type];
702
0
}
703
704
/**
705
* Create a new LWGEOM of the appropriate MULTI* type.
706
*/
707
LWGEOM *
708
lwgeom_as_multi(const LWGEOM *lwgeom)
709
0
{
710
0
  LWGEOM **ogeoms;
711
0
  LWGEOM *ogeom = NULL;
712
0
  GBOX *box = NULL;
713
0
  int type;
714
715
0
  type = lwgeom->type;
716
717
  /*
718
  * PolyhedralSurface and MultiPolygon have identical structures
719
  * (both are collections of LWPOLY). Convert PolyhedralSurface
720
  * to MultiPolygon by cloning and changing the type.
721
  */
722
0
  if (type == POLYHEDRALSURFACETYPE)
723
0
  {
724
0
    ogeom = (LWGEOM *)lwcollection_clone((LWCOLLECTION *)lwgeom);
725
0
    ogeom->type = MULTIPOLYGONTYPE;
726
0
    return ogeom;
727
0
  }
728
729
  /*
730
  * TIN to MultiPolygon: convert each triangle to a polygon.
731
  * LWTRIANGLE and LWLINE have compatible memory layouts,
732
  * so we can use lwpoly_from_lwlines to create polygons.
733
  */
734
0
  if (type == TINTYPE)
735
0
  {
736
0
    uint32_t i;
737
0
    LWCOLLECTION *col = (LWCOLLECTION *)lwgeom;
738
0
    LWMPOLY *mpoly = lwmpoly_construct_empty(lwgeom->srid,
739
0
      FLAGS_GET_Z(lwgeom->flags),
740
0
      FLAGS_GET_M(lwgeom->flags));
741
742
0
    for (i = 0; i < col->ngeoms; i++)
743
0
    {
744
0
      LWPOLY *poly = lwpoly_from_lwlines((LWLINE *)col->geoms[i], 0, NULL);
745
0
      lwmpoly_add_lwpoly(mpoly, poly);
746
0
    }
747
748
0
    if (lwgeom->bbox)
749
0
      mpoly->bbox = gbox_clone(lwgeom->bbox);
750
751
0
    return lwmpoly_as_lwgeom(mpoly);
752
0
  }
753
754
0
  if ( ! MULTITYPE[type] ) return lwgeom_clone(lwgeom);
755
756
0
  if( lwgeom_is_empty(lwgeom) )
757
0
  {
758
0
    ogeom = (LWGEOM *)lwcollection_construct_empty(
759
0
      MULTITYPE[type],
760
0
      lwgeom->srid,
761
0
      FLAGS_GET_Z(lwgeom->flags),
762
0
      FLAGS_GET_M(lwgeom->flags)
763
0
    );
764
0
  }
765
0
  else
766
0
  {
767
0
    ogeoms = lwalloc(sizeof(LWGEOM*));
768
0
    ogeoms[0] = lwgeom_clone(lwgeom);
769
770
    /* Sub-geometries are not allowed to have bboxes or SRIDs, move the bbox to the collection */
771
0
    box = ogeoms[0]->bbox;
772
0
    ogeoms[0]->bbox = NULL;
773
0
    ogeoms[0]->srid = SRID_UNKNOWN;
774
775
0
    ogeom = (LWGEOM *)lwcollection_construct(MULTITYPE[type], lwgeom->srid, box, 1, ogeoms);
776
0
  }
777
778
0
  return ogeom;
779
0
}
780
781
/**
782
* Create a new LWGEOM of the appropriate CURVE* type.
783
*/
784
LWGEOM *
785
lwgeom_as_curve(const LWGEOM *lwgeom)
786
0
{
787
0
  LWGEOM *ogeom;
788
0
  int type = lwgeom->type;
789
  /*
790
  int hasz = FLAGS_GET_Z(lwgeom->flags);
791
  int hasm = FLAGS_GET_M(lwgeom->flags);
792
  int32_t srid = lwgeom->srid;
793
  */
794
795
0
  switch(type)
796
0
  {
797
0
    case LINETYPE:
798
      /* turn to COMPOUNDCURVE */
799
0
      ogeom = (LWGEOM*)lwcompound_construct_from_lwline((LWLINE*)lwgeom);
800
0
      break;
801
0
    case POLYGONTYPE:
802
0
      ogeom = (LWGEOM*)lwcurvepoly_construct_from_lwpoly(lwgeom_as_lwpoly(lwgeom));
803
0
      break;
804
0
    case MULTILINETYPE:
805
      /* turn to MULTICURVE */
806
0
      ogeom = lwgeom_clone(lwgeom);
807
0
      ogeom->type = MULTICURVETYPE;
808
0
      break;
809
0
    case MULTIPOLYGONTYPE:
810
      /* turn to MULTISURFACE */
811
0
      ogeom = lwgeom_clone(lwgeom);
812
0
      ogeom->type = MULTISURFACETYPE;
813
0
      break;
814
0
    case COLLECTIONTYPE:
815
0
    default:
816
0
      ogeom = lwgeom_clone(lwgeom);
817
0
      break;
818
0
  }
819
820
  /* TODO: copy bbox from input geom ? */
821
822
0
  return ogeom;
823
0
}
824
825
826
/**
827
* Free the containing LWGEOM and the associated BOX. Leave the underlying
828
* geoms/points/point objects intact. Useful for functions that are stripping
829
* out subcomponents of complex objects, or building up new temporary objects
830
* on top of subcomponents.
831
*/
832
void
833
lwgeom_release(LWGEOM *lwgeom)
834
0
{
835
0
  if ( ! lwgeom )
836
0
    lwerror("lwgeom_release: someone called on 0x0");
837
838
0
  LWDEBUGF(3, "releasing type %s", lwtype_name(lwgeom->type));
839
840
  /* Drop bounding box (always a copy) */
841
0
  if ( lwgeom->bbox )
842
0
  {
843
0
    LWDEBUGF(3, "lwgeom_release: releasing bbox. %p", lwgeom->bbox);
844
0
    lwfree(lwgeom->bbox);
845
0
  }
846
0
  lwfree(lwgeom);
847
848
0
}
849
850
851
/* @brief Clone LWGEOM object. Serialized point lists are not copied.
852
 *
853
 * @see ptarray_clone
854
 */
855
LWGEOM *
856
lwgeom_clone(const LWGEOM *lwgeom)
857
0
{
858
0
  LWDEBUGF(2, "lwgeom_clone called with %p, %s",
859
0
           lwgeom, lwtype_name(lwgeom->type));
860
861
0
  switch (lwgeom->type)
862
0
  {
863
0
  case POINTTYPE:
864
0
    return (LWGEOM *)lwpoint_clone((LWPOINT *)lwgeom);
865
0
  case LINETYPE:
866
0
    return (LWGEOM *)lwline_clone((LWLINE *)lwgeom);
867
0
  case CIRCSTRINGTYPE:
868
0
    return (LWGEOM *)lwcircstring_clone((LWCIRCSTRING *)lwgeom);
869
0
  case NURBSCURVETYPE:
870
0
    return (LWGEOM *)lwnurbscurve_clone_deep((LWNURBSCURVE *)lwgeom);
871
0
  case POLYGONTYPE:
872
0
    return (LWGEOM *)lwpoly_clone((LWPOLY *)lwgeom);
873
0
  case TRIANGLETYPE:
874
0
    return (LWGEOM *)lwtriangle_clone((LWTRIANGLE *)lwgeom);
875
0
  case COMPOUNDTYPE:
876
0
  case CURVEPOLYTYPE:
877
0
  case MULTICURVETYPE:
878
0
  case MULTISURFACETYPE:
879
0
  case MULTIPOINTTYPE:
880
0
  case MULTILINETYPE:
881
0
  case MULTIPOLYGONTYPE:
882
0
  case POLYHEDRALSURFACETYPE:
883
0
  case TINTYPE:
884
0
  case COLLECTIONTYPE:
885
0
    return (LWGEOM *)lwcollection_clone((LWCOLLECTION *)lwgeom);
886
0
  default:
887
0
    lwerror("lwgeom_clone: Unknown geometry type: %s", lwtype_name(lwgeom->type));
888
0
    return NULL;
889
0
  }
890
0
}
891
892
/**
893
 * Create a deep copy of an LWGEOM.
894
 *
895
 * Performs a full (deep) clone of the provided geometry, including copying
896
 * internal vertex/point arrays so the returned geometry is independent of the
897
 * original. Dispatches to type-specific deep-clone helpers (e.g. line, polygon,
898
 * collection, NURBS curve).
899
 *
900
 * @param lwgeom Source geometry to clone. Must be non-NULL.
901
 * @returns Pointer to a newly allocated LWGEOM that is a deep clone of
902
 *          `lwgeom`, or NULL if `lwgeom->type` is unrecognized (an error is
903
 *          reported in that case).
904
 */
905
LWGEOM *
906
lwgeom_clone_deep(const LWGEOM *lwgeom)
907
0
{
908
0
  LWDEBUGF(2, "lwgeom_clone called with %p, %s",
909
0
           lwgeom, lwtype_name(lwgeom->type));
910
911
0
  switch (lwgeom->type)
912
0
  {
913
0
  case POINTTYPE:
914
0
  case LINETYPE:
915
0
  case CIRCSTRINGTYPE:
916
0
  case TRIANGLETYPE:
917
0
    return (LWGEOM *)lwline_clone_deep((LWLINE *)lwgeom);
918
0
  case POLYGONTYPE:
919
0
    return (LWGEOM *)lwpoly_clone_deep((LWPOLY *)lwgeom);
920
0
  case COMPOUNDTYPE:
921
0
  case CURVEPOLYTYPE:
922
0
  case MULTICURVETYPE:
923
0
  case MULTISURFACETYPE:
924
0
  case MULTIPOINTTYPE:
925
0
  case MULTILINETYPE:
926
0
  case MULTIPOLYGONTYPE:
927
0
  case POLYHEDRALSURFACETYPE:
928
0
  case TINTYPE:
929
0
  case COLLECTIONTYPE:
930
0
    return (LWGEOM *)lwcollection_clone_deep((LWCOLLECTION *)lwgeom);
931
0
  case NURBSCURVETYPE:
932
0
    return (LWGEOM *)lwnurbscurve_clone_deep((LWNURBSCURVE *)lwgeom);
933
0
  default:
934
0
    lwerror("lwgeom_clone_deep: Unknown geometry type: %s", lwtype_name(lwgeom->type));
935
0
    return NULL;
936
0
  }
937
0
}
938
939
940
/**
941
 * Return an allocated string
942
 */
943
char*
944
lwgeom_to_ewkt(const LWGEOM *lwgeom)
945
0
{
946
0
  char* wkt = NULL;
947
0
  size_t wkt_size = 0;
948
949
0
  wkt = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, 12, &wkt_size);
950
951
0
  if ( ! wkt )
952
0
  {
953
0
    lwerror("Error writing geometry to WKT");
954
0
  }
955
956
0
  return wkt;
957
0
}
958
959
/**
960
 * @brief geom1 same as geom2
961
 *    iff
962
 *        + have same type
963
 *      + have same # objects
964
 *        + have same bvol
965
 *        + each object in geom1 has a corresponding object in geom2 (see above)
966
 *  @param lwgeom1
967
 *  @param lwgeom2
968
 */
969
char
970
lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
971
0
{
972
0
  LWDEBUGF(2, "lwgeom_same(%s, %s) called",
973
0
           lwtype_name(lwgeom1->type),
974
0
           lwtype_name(lwgeom2->type));
975
976
0
  if ( lwgeom1->type != lwgeom2->type )
977
0
  {
978
0
    LWDEBUG(3, " type differ");
979
980
0
    return LW_FALSE;
981
0
  }
982
983
0
  if ( FLAGS_GET_ZM(lwgeom1->flags) != FLAGS_GET_ZM(lwgeom2->flags) )
984
0
  {
985
0
    LWDEBUG(3, " ZM flags differ");
986
987
0
    return LW_FALSE;
988
0
  }
989
990
  /* Check boxes if both already computed  */
991
0
  if ( lwgeom1->bbox && lwgeom2->bbox )
992
0
  {
993
    /*lwnotice("bbox1:%p, bbox2:%p", lwgeom1->bbox, lwgeom2->bbox);*/
994
0
    if ( ! gbox_same(lwgeom1->bbox, lwgeom2->bbox) )
995
0
    {
996
0
      LWDEBUG(3, " bounding boxes differ");
997
998
0
      return LW_FALSE;
999
0
    }
1000
0
  }
1001
1002
  /* geoms have same type, invoke type-specific function */
1003
0
  switch (lwgeom1->type)
1004
0
  {
1005
0
  case POINTTYPE:
1006
0
    return lwpoint_same((LWPOINT *)lwgeom1,
1007
0
                        (LWPOINT *)lwgeom2);
1008
0
  case LINETYPE:
1009
0
    return lwline_same((LWLINE *)lwgeom1,
1010
0
                       (LWLINE *)lwgeom2);
1011
0
  case POLYGONTYPE:
1012
0
    return lwpoly_same((LWPOLY *)lwgeom1,
1013
0
                       (LWPOLY *)lwgeom2);
1014
0
  case TRIANGLETYPE:
1015
0
    return lwtriangle_same((LWTRIANGLE *)lwgeom1,
1016
0
                           (LWTRIANGLE *)lwgeom2);
1017
0
  case CIRCSTRINGTYPE:
1018
0
    return lwcircstring_same((LWCIRCSTRING *)lwgeom1,
1019
0
           (LWCIRCSTRING *)lwgeom2);
1020
0
  case NURBSCURVETYPE:
1021
0
    return lwnurbscurve_same((LWNURBSCURVE *)lwgeom1,
1022
0
                             (LWNURBSCURVE *)lwgeom2);
1023
0
  case MULTIPOINTTYPE:
1024
0
  case MULTILINETYPE:
1025
0
  case MULTIPOLYGONTYPE:
1026
0
  case MULTICURVETYPE:
1027
0
  case MULTISURFACETYPE:
1028
0
  case COMPOUNDTYPE:
1029
0
  case CURVEPOLYTYPE:
1030
0
  case POLYHEDRALSURFACETYPE:
1031
0
  case TINTYPE:
1032
0
  case COLLECTIONTYPE:
1033
0
    return lwcollection_same((LWCOLLECTION *)lwgeom1,
1034
0
                             (LWCOLLECTION *)lwgeom2);
1035
0
  default:
1036
0
    lwerror("lwgeom_same: unsupported geometry type: %s",
1037
0
            lwtype_name(lwgeom1->type));
1038
0
    return LW_FALSE;
1039
0
  }
1040
1041
0
}
1042
1043
int
1044
lwpoint_inside_circle(const LWPOINT *p, double cx, double cy, double rad)
1045
0
{
1046
0
  const POINT2D *pt;
1047
0
  POINT2D center;
1048
1049
0
  if ( ! p || ! p->point )
1050
0
    return LW_FALSE;
1051
1052
0
  pt = getPoint2d_cp(p->point, 0);
1053
1054
0
  center.x = cx;
1055
0
  center.y = cy;
1056
1057
0
  if ( distance2d_pt_pt(pt, &center) < rad )
1058
0
    return LW_TRUE;
1059
1060
0
  return LW_FALSE;
1061
0
}
1062
1063
void
1064
lwgeom_drop_bbox(LWGEOM *lwgeom)
1065
0
{
1066
0
  if ( lwgeom->bbox ) lwfree(lwgeom->bbox);
1067
0
  lwgeom->bbox = NULL;
1068
0
  FLAGS_SET_BBOX(lwgeom->flags, 0);
1069
0
}
1070
1071
/**
1072
 * Ensure there's a box in the LWGEOM.
1073
 * If the box is already there just return,
1074
 * else compute it.
1075
 */
1076
void
1077
lwgeom_add_bbox(LWGEOM *lwgeom)
1078
0
{
1079
  /* an empty LWGEOM has no bbox */
1080
0
  if ( lwgeom_is_empty(lwgeom) ) return;
1081
1082
0
  if ( lwgeom->bbox ) return;
1083
0
  FLAGS_SET_BBOX(lwgeom->flags, 1);
1084
0
  lwgeom->bbox = gbox_new(lwgeom->flags);
1085
0
  lwgeom_calculate_gbox(lwgeom, lwgeom->bbox);
1086
0
}
1087
1088
void
1089
lwgeom_refresh_bbox(LWGEOM *lwgeom)
1090
0
{
1091
0
  lwgeom_drop_bbox(lwgeom);
1092
0
  lwgeom_add_bbox(lwgeom);
1093
0
}
1094
1095
void
1096
lwgeom_add_bbox_deep(LWGEOM *lwgeom, GBOX *gbox)
1097
0
{
1098
0
  if ( lwgeom_is_empty(lwgeom) ) return;
1099
1100
0
  FLAGS_SET_BBOX(lwgeom->flags, 1);
1101
1102
0
  if ( ! ( gbox || lwgeom->bbox ) )
1103
0
  {
1104
0
    lwgeom->bbox = gbox_new(lwgeom->flags);
1105
0
    lwgeom_calculate_gbox(lwgeom, lwgeom->bbox);
1106
0
  }
1107
0
  else if ( gbox && ! lwgeom->bbox )
1108
0
  {
1109
0
    lwgeom->bbox = gbox_clone(gbox);
1110
0
  }
1111
1112
0
  if ( lwgeom_is_collection(lwgeom) )
1113
0
  {
1114
0
    uint32_t i;
1115
0
    LWCOLLECTION *lwcol = (LWCOLLECTION*)lwgeom;
1116
1117
0
    for ( i = 0; i < lwcol->ngeoms; i++ )
1118
0
    {
1119
0
      lwgeom_add_bbox_deep(lwcol->geoms[i], lwgeom->bbox);
1120
0
    }
1121
0
  }
1122
0
}
1123
1124
const GBOX *
1125
lwgeom_get_bbox(const LWGEOM *lwg)
1126
0
{
1127
  /* add it if not already there */
1128
0
  lwgeom_add_bbox((LWGEOM *)lwg);
1129
0
  return lwg->bbox;
1130
0
}
1131
1132
1133
/**
1134
* Calculate the gbox for this geometry, a cartesian box or
1135
* geodetic box, depending on how it is flagged.
1136
*/
1137
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
1138
0
{
1139
0
  gbox->flags = lwgeom->flags;
1140
0
  if( FLAGS_GET_GEODETIC(lwgeom->flags) )
1141
0
    return lwgeom_calculate_gbox_geodetic(lwgeom, gbox);
1142
0
  else
1143
0
    return lwgeom_calculate_gbox_cartesian(lwgeom, gbox);
1144
0
}
1145
1146
void
1147
lwgeom_drop_srid(LWGEOM *lwgeom)
1148
0
{
1149
0
  lwgeom->srid = SRID_UNKNOWN; /* TODO: To be changed to SRID_UNKNOWN */
1150
0
}
1151
1152
LWGEOM *
1153
lwgeom_segmentize2d(const LWGEOM *lwgeom, double dist)
1154
0
{
1155
0
  switch (lwgeom->type)
1156
0
  {
1157
0
  case LINETYPE:
1158
0
    return (LWGEOM *)lwline_segmentize2d((LWLINE *)lwgeom,
1159
0
                                         dist);
1160
0
  case POLYGONTYPE:
1161
0
    return (LWGEOM *)lwpoly_segmentize2d((LWPOLY *)lwgeom,
1162
0
                                         dist);
1163
0
  case MULTILINETYPE:
1164
0
  case MULTIPOLYGONTYPE:
1165
0
  case COLLECTIONTYPE:
1166
0
    return (LWGEOM *)lwcollection_segmentize2d(
1167
0
               (LWCOLLECTION *)lwgeom, dist);
1168
1169
0
  default:
1170
0
    return lwgeom_clone(lwgeom);
1171
0
  }
1172
0
}
1173
1174
LWGEOM*
1175
lwgeom_force_2d(const LWGEOM *geom)
1176
0
{
1177
0
  return lwgeom_force_dims(geom, 0, 0, 0, 0);
1178
0
}
1179
1180
LWGEOM*
1181
lwgeom_force_3dz(const LWGEOM *geom, double zval)
1182
0
{
1183
0
  return lwgeom_force_dims(geom, 1, 0, zval, 0);
1184
0
}
1185
1186
LWGEOM*
1187
lwgeom_force_3dm(const LWGEOM *geom, double mval)
1188
0
{
1189
0
  return lwgeom_force_dims(geom, 0, 1, 0, mval);
1190
0
}
1191
1192
LWGEOM*
1193
lwgeom_force_4d(const LWGEOM *geom, double zval, double mval)
1194
0
{
1195
0
  return lwgeom_force_dims(geom, 1, 1, zval, mval);
1196
0
}
1197
1198
LWGEOM*
1199
lwgeom_force_dims(const LWGEOM *geom, int hasz, int hasm, double zval, double mval)
1200
0
{
1201
0
  if (!geom)
1202
0
    return NULL;
1203
0
  switch(geom->type)
1204
0
  {
1205
0
    case POINTTYPE:
1206
0
      return lwpoint_as_lwgeom(lwpoint_force_dims((LWPOINT*)geom, hasz, hasm, zval, mval));
1207
0
    case CIRCSTRINGTYPE:
1208
0
    case LINETYPE:
1209
0
    case TRIANGLETYPE:
1210
0
      return lwline_as_lwgeom(lwline_force_dims((LWLINE*)geom, hasz, hasm, zval, mval));
1211
0
    case POLYGONTYPE:
1212
0
      return lwpoly_as_lwgeom(lwpoly_force_dims((LWPOLY*)geom, hasz, hasm, zval, mval));
1213
0
    case COMPOUNDTYPE:
1214
0
    case CURVEPOLYTYPE:
1215
0
    case MULTICURVETYPE:
1216
0
    case MULTISURFACETYPE:
1217
0
    case MULTIPOINTTYPE:
1218
0
    case MULTILINETYPE:
1219
0
    case MULTIPOLYGONTYPE:
1220
0
    case POLYHEDRALSURFACETYPE:
1221
0
    case TINTYPE:
1222
0
    case COLLECTIONTYPE:
1223
0
      return lwcollection_as_lwgeom(lwcollection_force_dims((LWCOLLECTION*)geom, hasz, hasm, zval, mval));
1224
0
    case NURBSCURVETYPE:
1225
0
      return lwnurbscurve_as_lwgeom(lwnurbscurve_force_dims((LWNURBSCURVE*)geom, hasz, hasm, zval, mval));
1226
0
    default:
1227
0
      lwerror("lwgeom_force_2d: unsupported geom type: %s", lwtype_name(geom->type));
1228
0
      return NULL;
1229
0
  }
1230
0
}
1231
1232
LWGEOM*
1233
lwgeom_force_sfs(LWGEOM *geom, int version)
1234
0
{
1235
0
  LWCOLLECTION *col;
1236
0
  uint32_t i;
1237
0
  LWGEOM *g;
1238
1239
  /* SFS 1.2 version */
1240
0
  if (version == 120)
1241
0
  {
1242
0
    switch(geom->type)
1243
0
    {
1244
      /* SQL/MM types */
1245
0
      case CIRCSTRINGTYPE:
1246
0
      case COMPOUNDTYPE:
1247
0
      case CURVEPOLYTYPE:
1248
0
      case MULTICURVETYPE:
1249
0
      case MULTISURFACETYPE:
1250
0
        return lwgeom_stroke(geom, 32);
1251
1252
0
      case COLLECTIONTYPE:
1253
0
        col = (LWCOLLECTION*)geom;
1254
0
        for ( i = 0; i < col->ngeoms; i++ )
1255
0
          col->geoms[i] = lwgeom_force_sfs((LWGEOM*)col->geoms[i], version);
1256
1257
0
        return lwcollection_as_lwgeom((LWCOLLECTION*)geom);
1258
1259
0
      default:
1260
0
        return (LWGEOM *)geom;
1261
0
    }
1262
0
  }
1263
1264
1265
  /* SFS 1.1 version */
1266
0
  switch(geom->type)
1267
0
  {
1268
    /* SQL/MM types */
1269
0
    case CIRCSTRINGTYPE:
1270
0
    case COMPOUNDTYPE:
1271
0
    case CURVEPOLYTYPE:
1272
0
    case MULTICURVETYPE:
1273
0
    case MULTISURFACETYPE:
1274
0
      return lwgeom_stroke(geom, 32);
1275
1276
    /* SFS 1.2 types */
1277
0
    case TRIANGLETYPE:
1278
0
      g = lwpoly_as_lwgeom(lwpoly_from_lwlines((LWLINE*)geom, 0, NULL));
1279
0
      lwgeom_free(geom);
1280
0
      return g;
1281
1282
0
    case TINTYPE:
1283
0
      col = (LWCOLLECTION*) geom;
1284
0
      for ( i = 0; i < col->ngeoms; i++ )
1285
0
      {
1286
0
        g = lwpoly_as_lwgeom(lwpoly_from_lwlines((LWLINE*)col->geoms[i], 0, NULL));
1287
0
        lwgeom_free(col->geoms[i]);
1288
0
        col->geoms[i] = g;
1289
0
      }
1290
0
      col->type = COLLECTIONTYPE;
1291
0
      return lwmpoly_as_lwgeom((LWMPOLY*)geom);
1292
1293
0
    case POLYHEDRALSURFACETYPE:
1294
0
      geom->type = COLLECTIONTYPE;
1295
0
      return (LWGEOM *)geom;
1296
1297
    /* Collection */
1298
0
    case COLLECTIONTYPE:
1299
0
      col = (LWCOLLECTION*)geom;
1300
0
      for ( i = 0; i < col->ngeoms; i++ )
1301
0
        col->geoms[i] = lwgeom_force_sfs((LWGEOM*)col->geoms[i], version);
1302
1303
0
      return lwcollection_as_lwgeom((LWCOLLECTION*)geom);
1304
1305
0
    default:
1306
0
      return (LWGEOM *)geom;
1307
0
  }
1308
0
}
1309
1310
int32_t
1311
lwgeom_get_srid(const LWGEOM *geom)
1312
0
{
1313
0
  if ( ! geom ) return SRID_UNKNOWN;
1314
0
  return geom->srid;
1315
0
}
1316
1317
int
1318
lwgeom_has_z(const LWGEOM *geom)
1319
0
{
1320
0
  if ( ! geom ) return LW_FALSE;
1321
0
  return FLAGS_GET_Z(geom->flags);
1322
0
}
1323
1324
int
1325
lwgeom_has_m(const LWGEOM *geom)
1326
0
{
1327
0
  if ( ! geom ) return LW_FALSE;
1328
0
  return FLAGS_GET_M(geom->flags);
1329
0
}
1330
1331
int
1332
lwgeom_is_solid(const LWGEOM *geom)
1333
0
{
1334
0
  if ( ! geom ) return LW_FALSE;
1335
0
  return FLAGS_GET_SOLID(geom->flags);
1336
0
}
1337
1338
int
1339
lwgeom_ndims(const LWGEOM *geom)
1340
0
{
1341
0
  if ( ! geom ) return 0;
1342
0
  return FLAGS_NDIMS(geom->flags);
1343
0
}
1344
1345
1346
1347
void
1348
lwgeom_set_geodetic(LWGEOM *geom, int value)
1349
0
{
1350
0
  LWPOINT *pt;
1351
0
  LWLINE *ln;
1352
0
  LWPOLY *ply;
1353
0
  LWCOLLECTION *col;
1354
0
  uint32_t i;
1355
1356
0
  FLAGS_SET_GEODETIC(geom->flags, value);
1357
0
  if ( geom->bbox )
1358
0
    FLAGS_SET_GEODETIC(geom->bbox->flags, value);
1359
1360
0
  switch(geom->type)
1361
0
  {
1362
0
    case POINTTYPE:
1363
0
      pt = (LWPOINT*)geom;
1364
0
      if ( pt->point )
1365
0
        FLAGS_SET_GEODETIC(pt->point->flags, value);
1366
0
      break;
1367
0
    case LINETYPE:
1368
0
      ln = (LWLINE*)geom;
1369
0
      if ( ln->points )
1370
0
        FLAGS_SET_GEODETIC(ln->points->flags, value);
1371
0
      break;
1372
0
    case POLYGONTYPE:
1373
0
      ply = (LWPOLY*)geom;
1374
0
      for ( i = 0; i < ply->nrings; i++ )
1375
0
        FLAGS_SET_GEODETIC(ply->rings[i]->flags, value);
1376
0
      break;
1377
0
    case MULTIPOINTTYPE:
1378
0
    case MULTILINETYPE:
1379
0
    case MULTIPOLYGONTYPE:
1380
0
    case COLLECTIONTYPE:
1381
0
      col = (LWCOLLECTION*)geom;
1382
0
      for ( i = 0; i < col->ngeoms; i++ )
1383
0
        lwgeom_set_geodetic(col->geoms[i], value);
1384
0
      break;
1385
0
    default:
1386
0
      lwerror("lwgeom_set_geodetic: unsupported geom type: %s", lwtype_name(geom->type));
1387
0
      return;
1388
0
  }
1389
0
}
1390
1391
void
1392
lwgeom_longitude_shift(LWGEOM *lwgeom)
1393
0
{
1394
0
  uint32_t i;
1395
0
  switch (lwgeom->type)
1396
0
  {
1397
0
    LWPOINT *point;
1398
0
    LWLINE *line;
1399
0
    LWPOLY *poly;
1400
0
    LWTRIANGLE *triangle;
1401
0
    LWCOLLECTION *coll;
1402
1403
0
  case POINTTYPE:
1404
0
    point = (LWPOINT *)lwgeom;
1405
0
    ptarray_longitude_shift(point->point);
1406
0
    return;
1407
0
  case LINETYPE:
1408
0
    line = (LWLINE *)lwgeom;
1409
0
    ptarray_longitude_shift(line->points);
1410
0
    return;
1411
0
  case POLYGONTYPE:
1412
0
    poly = (LWPOLY *)lwgeom;
1413
0
    for (i=0; i<poly->nrings; i++)
1414
0
      ptarray_longitude_shift(poly->rings[i]);
1415
0
    return;
1416
0
  case TRIANGLETYPE:
1417
0
    triangle = (LWTRIANGLE *)lwgeom;
1418
0
    ptarray_longitude_shift(triangle->points);
1419
0
    return;
1420
0
  case MULTIPOINTTYPE:
1421
0
  case MULTILINETYPE:
1422
0
  case MULTIPOLYGONTYPE:
1423
0
  case POLYHEDRALSURFACETYPE:
1424
0
  case TINTYPE:
1425
0
  case COLLECTIONTYPE:
1426
0
    coll = (LWCOLLECTION *)lwgeom;
1427
0
    for (i=0; i<coll->ngeoms; i++)
1428
0
      lwgeom_longitude_shift(coll->geoms[i]);
1429
0
    return;
1430
0
  default:
1431
0
    lwerror("lwgeom_longitude_shift: unsupported geom type: %s",
1432
0
            lwtype_name(lwgeom->type));
1433
0
  }
1434
0
}
1435
1436
int
1437
lwgeom_is_closed(const LWGEOM *geom)
1438
0
{
1439
0
  int type = geom->type;
1440
1441
0
  if( lwgeom_is_empty(geom) )
1442
0
    return LW_FALSE;
1443
1444
  /* Test linear types for closure */
1445
0
  switch (type)
1446
0
  {
1447
0
  case LINETYPE:
1448
0
    return lwline_is_closed((LWLINE*)geom);
1449
0
  case POLYGONTYPE:
1450
0
    return lwpoly_is_closed((LWPOLY*)geom);
1451
0
  case CIRCSTRINGTYPE:
1452
0
    return lwcircstring_is_closed((LWCIRCSTRING*)geom);
1453
0
  case NURBSCURVETYPE:
1454
0
  {
1455
0
    POINT4D start, end;
1456
0
    LWPOINT *startpt = lwnurbscurve_evaluate((const LWNURBSCURVE*)geom, 0.0);
1457
0
    LWPOINT *endpt = lwnurbscurve_evaluate((const LWNURBSCURVE*)geom, 1.0);
1458
0
    int closed = LW_FALSE;
1459
1460
0
    if (startpt && endpt &&
1461
0
        lwpoint_getPoint4d_p(startpt, &start) &&
1462
0
        lwpoint_getPoint4d_p(endpt, &end))
1463
0
    {
1464
0
      if (FLAGS_GET_Z(geom->flags))
1465
0
        closed = FP_EQUALS(start.x, end.x) && FP_EQUALS(start.y, end.y) && FP_EQUALS(start.z, end.z);
1466
0
      else
1467
0
        closed = FP_EQUALS(start.x, end.x) && FP_EQUALS(start.y, end.y);
1468
0
    }
1469
1470
0
    lwpoint_free(startpt);
1471
0
    lwpoint_free(endpt);
1472
0
    return closed;
1473
0
  }
1474
0
  case COMPOUNDTYPE:
1475
0
    return lwcompound_is_closed((LWCOMPOUND*)geom);
1476
0
  case TINTYPE:
1477
0
    return lwtin_is_closed((LWTIN*)geom);
1478
0
  case POLYHEDRALSURFACETYPE:
1479
0
    return lwpsurface_is_closed((LWPSURFACE*)geom);
1480
0
  }
1481
1482
  /* Recurse into collections and see if anything is not closed */
1483
0
  if ( lwgeom_is_collection(geom) )
1484
0
  {
1485
0
    LWCOLLECTION *col = lwgeom_as_lwcollection(geom);
1486
0
    uint32_t i;
1487
0
    int closed;
1488
0
    for ( i = 0; i < col->ngeoms; i++ )
1489
0
    {
1490
0
      closed = lwgeom_is_closed(col->geoms[i]);
1491
0
      if ( ! closed )
1492
0
        return LW_FALSE;
1493
0
    }
1494
0
    return LW_TRUE;
1495
0
  }
1496
1497
  /* All non-linear non-collection types we will call closed */
1498
0
  return LW_TRUE;
1499
0
}
1500
1501
int
1502
lwgeom_is_collection(const LWGEOM *geom)
1503
0
{
1504
0
  if( ! geom ) return LW_FALSE;
1505
0
  return lwtype_is_collection(geom->type);
1506
0
}
1507
1508
int
1509
lwtype_is_unitary(uint32_t lwtype)
1510
0
{
1511
0
  switch (lwtype)
1512
0
  {
1513
0
  case POINTTYPE:
1514
0
  case LINETYPE:
1515
0
  case POLYGONTYPE:
1516
0
  case CURVEPOLYTYPE:
1517
0
  case COMPOUNDTYPE:
1518
0
  case CIRCSTRINGTYPE:
1519
0
  case NURBSCURVETYPE:
1520
0
  case TRIANGLETYPE:
1521
0
  case POLYHEDRALSURFACETYPE:
1522
0
  case TINTYPE:
1523
0
    return LW_TRUE;
1524
0
    break;
1525
1526
0
  default:
1527
0
    return LW_FALSE;
1528
0
  }
1529
0
}
1530
1531
int
1532
lwgeom_has_patches(const LWGEOM *geom)
1533
0
{
1534
0
  switch (geom->type)
1535
0
  {
1536
0
  case TINTYPE:
1537
0
  case POLYHEDRALSURFACETYPE:
1538
0
    return LW_TRUE;
1539
0
    break;
1540
0
  default:
1541
0
    return LW_FALSE;
1542
0
  }
1543
0
}
1544
1545
int
1546
lwgeom_is_unitary(const LWGEOM *geom)
1547
0
{
1548
0
  return lwtype_is_unitary(geom->type);
1549
0
}
1550
1551
int
1552
lwgeom_has_rings(const LWGEOM *geom)
1553
0
{
1554
0
  switch (geom->type)
1555
0
  {
1556
0
  case POLYGONTYPE:
1557
0
  case CURVEPOLYTYPE:
1558
0
  case TRIANGLETYPE:
1559
0
    return LW_TRUE;
1560
0
    break;
1561
1562
0
  default:
1563
0
    return LW_FALSE;
1564
0
  }
1565
0
}
1566
1567
/**
1568
 * Return TRUE if the geometry is structured as a wrapper on a geoms/ngeoms
1569
 * list of sub-geometries.
1570
 * Use lwgeom_is_unitary to determine if the numgeometries/geometryn accessor
1571
 * pattery makes sense
1572
 */
1573
int
1574
lwtype_is_collection(uint8_t type)
1575
0
{
1576
0
  switch (type)
1577
0
  {
1578
0
  case MULTIPOINTTYPE:
1579
0
  case MULTILINETYPE:
1580
0
  case MULTIPOLYGONTYPE:
1581
0
  case COLLECTIONTYPE:
1582
0
  case CURVEPOLYTYPE:
1583
0
  case COMPOUNDTYPE:
1584
0
  case MULTICURVETYPE:
1585
0
  case MULTISURFACETYPE:
1586
0
  case POLYHEDRALSURFACETYPE:
1587
0
  case TINTYPE:
1588
0
    return LW_TRUE;
1589
0
    break;
1590
1591
0
  default:
1592
0
    return LW_FALSE;
1593
0
  }
1594
0
}
1595
1596
/**
1597
* Given an lwtype number, what homogeneous collection can hold it?
1598
*/
1599
uint32_t
1600
lwtype_get_collectiontype(uint8_t type)
1601
0
{
1602
0
  switch (type)
1603
0
  {
1604
0
    case POINTTYPE:
1605
0
      return MULTIPOINTTYPE;
1606
0
    case LINETYPE:
1607
0
      return MULTILINETYPE;
1608
0
    case POLYGONTYPE:
1609
0
      return MULTIPOLYGONTYPE;
1610
0
    case CIRCSTRINGTYPE:
1611
0
      return MULTICURVETYPE;
1612
0
    case COMPOUNDTYPE:
1613
0
      return MULTICURVETYPE;
1614
0
    case NURBSCURVETYPE:
1615
0
      return MULTICURVETYPE;
1616
0
    case CURVEPOLYTYPE:
1617
0
      return MULTISURFACETYPE;
1618
0
    case TRIANGLETYPE:
1619
0
      return TINTYPE;
1620
0
    default:
1621
0
      return COLLECTIONTYPE;
1622
0
  }
1623
0
}
1624
1625
1626
/**
1627
 * Free an LWGEOM and its associated resources.
1628
 *
1629
 * Dispatches to the appropriate type-specific free function based on lwgeom->type.
1630
 * If `lwgeom` is NULL the function is a no-op. Logs an error if the geometry type
1631
 * is unknown.
1632
 *
1633
 * @param lwgeom Pointer to the LWGEOM to free (may be NULL). After return the
1634
 *               memory for the provided geometry will have been released.
1635
 */
1636
void lwgeom_free(LWGEOM *lwgeom)
1637
0
{
1638
1639
  /* There's nothing here to free... */
1640
0
  if( ! lwgeom ) return;
1641
1642
0
  LWDEBUGF(5,"freeing a %s",lwtype_name(lwgeom->type));
1643
1644
0
  switch (lwgeom->type)
1645
0
  {
1646
0
  case POINTTYPE:
1647
0
    lwpoint_free((LWPOINT *)lwgeom);
1648
0
    break;
1649
0
  case LINETYPE:
1650
0
    lwline_free((LWLINE *)lwgeom);
1651
0
    break;
1652
0
  case POLYGONTYPE:
1653
0
    lwpoly_free((LWPOLY *)lwgeom);
1654
0
    break;
1655
0
  case CIRCSTRINGTYPE:
1656
0
    lwcircstring_free((LWCIRCSTRING *)lwgeom);
1657
0
    break;
1658
0
  case TRIANGLETYPE:
1659
0
    lwtriangle_free((LWTRIANGLE *)lwgeom);
1660
0
    break;
1661
0
  case MULTIPOINTTYPE:
1662
0
    lwmpoint_free((LWMPOINT *)lwgeom);
1663
0
    break;
1664
0
  case MULTILINETYPE:
1665
0
    lwmline_free((LWMLINE *)lwgeom);
1666
0
    break;
1667
0
  case MULTIPOLYGONTYPE:
1668
0
    lwmpoly_free((LWMPOLY *)lwgeom);
1669
0
    break;
1670
0
  case POLYHEDRALSURFACETYPE:
1671
0
    lwpsurface_free((LWPSURFACE *)lwgeom);
1672
0
    break;
1673
0
  case TINTYPE:
1674
0
    lwtin_free((LWTIN *)lwgeom);
1675
0
    break;
1676
0
  case CURVEPOLYTYPE:
1677
0
  case COMPOUNDTYPE:
1678
0
  case MULTICURVETYPE:
1679
0
  case MULTISURFACETYPE:
1680
0
  case COLLECTIONTYPE:
1681
0
    lwcollection_free((LWCOLLECTION *)lwgeom);
1682
0
    break;
1683
0
  case NURBSCURVETYPE:
1684
0
    lwnurbscurve_free((LWNURBSCURVE *)lwgeom);
1685
0
    break;
1686
0
  default:
1687
0
    lwerror("lwgeom_free called with unknown type (%d) %s", lwgeom->type, lwtype_name(lwgeom->type));
1688
0
  }
1689
0
  return;
1690
0
}
1691
1692
/**
1693
 * Determine whether a geometry requires a bounding box.
1694
 *
1695
 * Returns LW_TRUE if callers should attach/maintain a bounding box for the
1696
 * provided geometry type and state, or LW_FALSE when a bbox is unnecessary
1697
 * (e.g., point-like or trivially short line geometries).
1698
 *
1699
 * Detailed behavior:
1700
 * - POINTTYPE: returns LW_FALSE.
1701
 * - LINETYPE: returns LW_FALSE for lines with 0–2 vertices, otherwise LW_TRUE.
1702
 * - NURBSCURVETYPE: currently treated as not requiring a bbox (LW_FALSE).
1703
 * - MULTIPOINTTYPE: returns LW_FALSE for a single-member collection, otherwise LW_TRUE.
1704
 * - MULTILINETYPE: returns LW_FALSE when the collection has one member and that member has ≤2 vertices, otherwise LW_TRUE.
1705
 * - All other types: returns LW_TRUE.
1706
 *
1707
 * @param geom Geometry to inspect (must not be NULL).
1708
 * @return LW_TRUE if a bbox is needed, LW_FALSE otherwise.
1709
 */
1710
int lwgeom_needs_bbox(const LWGEOM *geom)
1711
0
{
1712
0
  assert(geom);
1713
0
  if ( geom->type == POINTTYPE )
1714
0
  {
1715
0
    return LW_FALSE;
1716
0
  }
1717
0
  else if ( geom->type == LINETYPE )
1718
0
  {
1719
0
    if ( lwgeom_count_vertices(geom) <= 2 )
1720
0
      return LW_FALSE;
1721
0
    else
1722
0
      return LW_TRUE;
1723
0
  }
1724
0
  else if ( geom->type == NURBSCURVETYPE )
1725
0
  {
1726
0
    const LWNURBSCURVE *nurbs = (LWNURBSCURVE*)geom;
1727
    /* Empty curves don't need bbox */
1728
0
    if ( !nurbs->points || nurbs->points->npoints == 0 )
1729
0
      return LW_FALSE;
1730
    /* Single control point doesn't need bbox */
1731
0
    else if ( nurbs->points->npoints == 1 )
1732
0
      return LW_FALSE;
1733
0
    else
1734
0
      return LW_TRUE;
1735
0
  }
1736
0
  else if ( geom->type == MULTIPOINTTYPE )
1737
0
  {
1738
0
    if ( ((LWCOLLECTION*)geom)->ngeoms == 1 )
1739
0
      return LW_FALSE;
1740
0
    else
1741
0
      return LW_TRUE;
1742
0
  }
1743
0
  else if ( geom->type == MULTILINETYPE )
1744
0
  {
1745
0
    if ( ((LWCOLLECTION*)geom)->ngeoms == 1 && lwgeom_count_vertices(geom) <= 2 )
1746
0
      return LW_FALSE;
1747
0
    else
1748
0
      return LW_TRUE;
1749
0
  }
1750
0
  else
1751
0
  {
1752
0
    return LW_TRUE;
1753
0
  }
1754
0
}
1755
1756
/**
1757
* Count points in an #LWGEOM.
1758
* TODO: Make sure the internal functions don't overflow
1759
*/
1760
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
1761
0
{
1762
0
  int result = 0;
1763
1764
  /* Null? Zero. */
1765
0
  if( ! geom ) return 0;
1766
1767
0
  LWDEBUGF(4, "lwgeom_count_vertices got type %s",
1768
0
           lwtype_name(geom->type));
1769
1770
  /* Empty? Zero. */
1771
0
  if( lwgeom_is_empty(geom) ) return 0;
1772
1773
0
  switch (geom->type)
1774
0
  {
1775
0
  case POINTTYPE:
1776
0
    result = 1;
1777
0
    break;
1778
0
  case TRIANGLETYPE:
1779
0
  case CIRCSTRINGTYPE:
1780
0
  case LINETYPE:
1781
0
    result = lwline_count_vertices((const LWLINE *)geom);
1782
0
    break;
1783
0
  case NURBSCURVETYPE:
1784
0
    {
1785
0
      const LWNURBSCURVE *nurbs = (const LWNURBSCURVE *)geom;
1786
0
      result = (nurbs->points != NULL) ? nurbs->points->npoints : 0;
1787
0
    }
1788
0
    break;
1789
0
  case POLYGONTYPE:
1790
0
    result = lwpoly_count_vertices((const LWPOLY *)geom);
1791
0
    break;
1792
0
  case COMPOUNDTYPE:
1793
0
  case CURVEPOLYTYPE:
1794
0
  case MULTICURVETYPE:
1795
0
  case MULTISURFACETYPE:
1796
0
  case MULTIPOINTTYPE:
1797
0
  case MULTILINETYPE:
1798
0
  case MULTIPOLYGONTYPE:
1799
0
  case POLYHEDRALSURFACETYPE:
1800
0
  case TINTYPE:
1801
0
  case COLLECTIONTYPE:
1802
0
    result = lwcollection_count_vertices((const LWCOLLECTION *)geom);
1803
0
    break;
1804
0
  default:
1805
0
    lwerror("%s: unsupported input geometry type: %s",
1806
0
            __func__, lwtype_name(geom->type));
1807
0
    break;
1808
0
  }
1809
0
  LWDEBUGF(3, "counted %d vertices", result);
1810
0
  return result;
1811
0
}
1812
1813
/**
1814
 * Return the topological dimension of a geometry.
1815
 *
1816
 * Determines the topological dimension for the given LWGEOM:
1817
 * - 0 for point-like geometries,
1818
 * - 1 for curve-like geometries (lines, circular strings, compound/curve collections, NURBS),
1819
 * - 2 for surface-like geometries (polygons, triangles, surfaces, TIN),
1820
 * - 3 for a closed polyhedral surface (volume).
1821
 * For a COLLECTION, returns the maximum dimension among its members.
1822
 *
1823
 * @param geom Pointer to the geometry; may be NULL.
1824
 * @return 0..3 for the geometry's topological dimension, the maximum member dimension for collections,
1825
 *         -1 if geom is NULL or its type is unsupported.
1826
 */
1827
int lwgeom_dimension(const LWGEOM *geom)
1828
0
{
1829
1830
  /* Null? Zero. */
1831
0
  if( ! geom ) return -1;
1832
1833
0
  LWDEBUGF(4, "lwgeom_dimension got type %s",
1834
0
           lwtype_name(geom->type));
1835
1836
  /* Empty? Zero. */
1837
  /* if( lwgeom_is_empty(geom) ) return 0; */
1838
1839
0
  switch (geom->type)
1840
0
  {
1841
0
  case POINTTYPE:
1842
0
  case MULTIPOINTTYPE:
1843
0
    return 0;
1844
0
  case CIRCSTRINGTYPE:
1845
0
  case LINETYPE:
1846
0
  case COMPOUNDTYPE:
1847
0
  case MULTICURVETYPE:
1848
0
  case MULTILINETYPE:
1849
0
  case NURBSCURVETYPE:
1850
0
    return 1;
1851
0
  case TRIANGLETYPE:
1852
0
  case POLYGONTYPE:
1853
0
  case CURVEPOLYTYPE:
1854
0
  case MULTISURFACETYPE:
1855
0
  case MULTIPOLYGONTYPE:
1856
0
    return 2;
1857
0
  case TINTYPE:
1858
    /* A 3D TIN surface is dimension 3 only when explicitly marked solid. */
1859
0
    return (FLAGS_GET_SOLID(geom->flags) && FLAGS_GET_Z(geom->flags)) ? 3 : 2;
1860
0
  case POLYHEDRALSURFACETYPE:
1861
0
  {
1862
    /* A closed polyhedral surface contains a volume. */
1863
0
    int closed = lwpsurface_is_closed((LWPSURFACE*)geom);
1864
0
    return ( closed ? 3 : 2 );
1865
0
  }
1866
0
  case COLLECTIONTYPE:
1867
0
  {
1868
0
    int maxdim = 0;
1869
0
    uint32_t i;
1870
0
    LWCOLLECTION *col = (LWCOLLECTION*)geom;
1871
0
    for( i = 0; i < col->ngeoms; i++ )
1872
0
    {
1873
0
      int dim = lwgeom_dimension(col->geoms[i]);
1874
0
      maxdim = ( dim > maxdim ? dim : maxdim );
1875
0
    }
1876
0
    return maxdim;
1877
0
  }
1878
0
  default:
1879
0
    lwerror("%s: unsupported input geometry type: %s",
1880
0
            __func__, lwtype_name(geom->type));
1881
0
  }
1882
0
  return -1;
1883
0
}
1884
1885
/**
1886
* Count rings in an #LWGEOM.
1887
*/
1888
uint32_t lwgeom_count_rings(const LWGEOM *geom)
1889
0
{
1890
0
  int result = 0;
1891
1892
  /* Null? Empty? Zero. */
1893
0
  if( ! geom || lwgeom_is_empty(geom) )
1894
0
    return 0;
1895
1896
0
  switch (geom->type)
1897
0
  {
1898
0
  case POINTTYPE:
1899
0
  case CIRCSTRINGTYPE:
1900
0
  case COMPOUNDTYPE:
1901
0
  case MULTICURVETYPE:
1902
0
  case MULTIPOINTTYPE:
1903
0
  case MULTILINETYPE:
1904
0
  case LINETYPE:
1905
0
    result = 0;
1906
0
    break;
1907
0
  case TRIANGLETYPE:
1908
0
    result = 1;
1909
0
    break;
1910
0
  case POLYGONTYPE:
1911
0
    result = ((LWPOLY *)geom)->nrings;
1912
0
    break;
1913
0
  case CURVEPOLYTYPE:
1914
0
    result = ((LWCURVEPOLY *)geom)->nrings;
1915
0
    break;
1916
0
  case MULTISURFACETYPE:
1917
0
  case MULTIPOLYGONTYPE:
1918
0
  case POLYHEDRALSURFACETYPE:
1919
0
  case TINTYPE:
1920
0
  case COLLECTIONTYPE:
1921
0
  {
1922
0
    LWCOLLECTION *col = (LWCOLLECTION*)geom;
1923
0
    uint32_t i = 0;
1924
0
    for( i = 0; i < col->ngeoms; i++ )
1925
0
      result += lwgeom_count_rings(col->geoms[i]);
1926
0
    break;
1927
0
  }
1928
0
  default:
1929
0
    lwerror("lwgeom_count_rings: unsupported input geometry type: %s", lwtype_name(geom->type));
1930
0
    break;
1931
0
  }
1932
0
  LWDEBUGF(3, "counted %d rings", result);
1933
0
  return result;
1934
0
}
1935
1936
int lwgeom_has_srid(const LWGEOM *geom)
1937
0
{
1938
0
  if ( geom->srid != SRID_UNKNOWN )
1939
0
    return LW_TRUE;
1940
1941
0
  return LW_FALSE;
1942
0
}
1943
1944
1945
static int lwcollection_dimensionality(const LWCOLLECTION *col)
1946
0
{
1947
0
  uint32_t i;
1948
0
  int dimensionality = 0;
1949
0
  for ( i = 0; i < col->ngeoms; i++ )
1950
0
  {
1951
0
    int d = lwgeom_dimensionality(col->geoms[i]);
1952
0
    if ( d > dimensionality )
1953
0
      dimensionality = d;
1954
0
  }
1955
0
  return dimensionality;
1956
0
}
1957
1958
/**
1959
 * Determine the topological dimensionality of a geometry.
1960
 *
1961
 * Returns the maximal topological dimension for the input geometry:
1962
 * - 0 for points and multipoints,
1963
 * - 1 for linear/curve types (lines, circular strings, compound/multi-curves, NURBS),
1964
 * - 2 for polygonal/surface types,
1965
 * Closure-aware dimensionality used by internal validity/repair checks.
1966
 *
1967
 * This is intentionally not the same contract as lwgeom_dimension(), which
1968
 * backs ST_Dimension.  Closed polyhedral surfaces and TINs are reported as 3
1969
 * here so callers that reason about solid-like inputs keep their legacy
1970
 * behavior.
1971
 *
1972
 * For COLLECTIONTYPE the result is the maximum dimensionality of its members.
1973
 *
1974
 * @param geom Geometry to inspect (must be non-NULL).
1975
 * @return The closure-aware dimensionality (0..3). On unsupported types the function logs an error and returns 0.
1976
 */
1977
extern int lwgeom_dimensionality(const LWGEOM *geom)
1978
0
{
1979
0
  int dim;
1980
1981
0
  LWDEBUGF(3, "lwgeom_dimensionality got type %s",
1982
0
           lwtype_name(geom->type));
1983
1984
0
  switch (geom->type)
1985
0
  {
1986
0
  case POINTTYPE:
1987
0
  case MULTIPOINTTYPE:
1988
0
    return 0;
1989
0
    break;
1990
0
  case LINETYPE:
1991
0
  case CIRCSTRINGTYPE:
1992
0
  case MULTILINETYPE:
1993
0
  case COMPOUNDTYPE:
1994
0
  case MULTICURVETYPE:
1995
0
  case NURBSCURVETYPE:
1996
0
    return 1;
1997
0
    break;
1998
0
  case POLYGONTYPE:
1999
0
  case TRIANGLETYPE:
2000
0
  case CURVEPOLYTYPE:
2001
0
  case MULTIPOLYGONTYPE:
2002
0
  case MULTISURFACETYPE:
2003
0
    return 2;
2004
0
    break;
2005
2006
0
  case POLYHEDRALSURFACETYPE:
2007
0
  case TINTYPE:
2008
0
    dim = lwgeom_is_closed(geom)?3:2;
2009
0
    return dim;
2010
0
    break;
2011
2012
0
  case COLLECTIONTYPE:
2013
0
    return lwcollection_dimensionality((const LWCOLLECTION *)geom);
2014
0
    break;
2015
0
  default:
2016
0
    lwerror("lwgeom_dimensionality: unsupported input geometry type: %s",
2017
0
            lwtype_name(geom->type));
2018
0
    break;
2019
0
  }
2020
0
  return 0;
2021
0
}
2022
2023
extern LWGEOM* lwgeom_remove_repeated_points(const LWGEOM *in, double tolerance)
2024
0
{
2025
0
  LWGEOM *out = lwgeom_clone_deep(in);
2026
0
  lwgeom_remove_repeated_points_in_place(out, tolerance);
2027
0
  return out;
2028
0
}
2029
2030
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
2031
0
{
2032
0
  LWCOLLECTION *col;
2033
0
  LWPOLY *poly;
2034
0
  uint32_t i;
2035
2036
0
  if ( (!in) || lwgeom_is_empty(in) ) return;
2037
2038
  /* TODO: check for lwgeom NOT having the specified dimension ? */
2039
2040
0
  LWDEBUGF(4, "lwgeom_flip_coordinates, got type: %s",
2041
0
           lwtype_name(in->type));
2042
2043
0
  switch (in->type)
2044
0
  {
2045
0
  case POINTTYPE:
2046
0
    ptarray_swap_ordinates(lwgeom_as_lwpoint(in)->point, o1, o2);
2047
0
    break;
2048
2049
0
  case LINETYPE:
2050
0
    ptarray_swap_ordinates(lwgeom_as_lwline(in)->points, o1, o2);
2051
0
    break;
2052
2053
0
  case CIRCSTRINGTYPE:
2054
0
    ptarray_swap_ordinates(lwgeom_as_lwcircstring(in)->points, o1, o2);
2055
0
    break;
2056
2057
0
  case NURBSCURVETYPE:
2058
0
  {
2059
0
    LWNURBSCURVE *n = (LWNURBSCURVE *)in;
2060
0
    if (n->points)
2061
0
      ptarray_swap_ordinates(n->points, o1, o2);
2062
0
    break;
2063
0
  }
2064
2065
0
  case POLYGONTYPE:
2066
0
    poly = (LWPOLY *) in;
2067
0
    for (i=0; i<poly->nrings; i++)
2068
0
    {
2069
0
      ptarray_swap_ordinates(poly->rings[i], o1, o2);
2070
0
    }
2071
0
    break;
2072
2073
0
  case TRIANGLETYPE:
2074
0
    ptarray_swap_ordinates(lwgeom_as_lwtriangle(in)->points, o1, o2);
2075
0
    break;
2076
2077
0
  case MULTIPOINTTYPE:
2078
0
  case MULTILINETYPE:
2079
0
  case MULTIPOLYGONTYPE:
2080
0
  case COLLECTIONTYPE:
2081
0
  case COMPOUNDTYPE:
2082
0
  case CURVEPOLYTYPE:
2083
0
  case MULTISURFACETYPE:
2084
0
  case MULTICURVETYPE:
2085
0
  case POLYHEDRALSURFACETYPE:
2086
0
  case TINTYPE:
2087
0
    col = (LWCOLLECTION *) in;
2088
0
    for (i=0; i<col->ngeoms; i++)
2089
0
    {
2090
0
      lwgeom_swap_ordinates(col->geoms[i], o1, o2);
2091
0
    }
2092
0
    break;
2093
2094
0
  default:
2095
0
    lwerror("lwgeom_swap_ordinates: unsupported geometry type: %s",
2096
0
            lwtype_name(in->type));
2097
0
    return;
2098
0
  }
2099
2100
  /* only refresh bbox if X or Y changed */
2101
0
  if ( in->bbox && (o1 < 2 || o2 < 2) )
2102
0
  {
2103
0
    lwgeom_refresh_bbox(in);
2104
0
  }
2105
0
}
2106
2107
void lwgeom_set_srid(LWGEOM *geom, int32_t srid)
2108
0
{
2109
0
  uint32_t i;
2110
2111
0
  LWDEBUGF(4,"entered with srid=%d",srid);
2112
2113
0
  geom->srid = srid;
2114
2115
0
  if ( lwgeom_is_collection(geom) )
2116
0
  {
2117
    /* All the children are set to the same SRID value */
2118
0
    LWCOLLECTION *col = lwgeom_as_lwcollection(geom);
2119
0
    for ( i = 0; i < col->ngeoms; i++ )
2120
0
    {
2121
0
      lwgeom_set_srid(col->geoms[i], srid);
2122
0
    }
2123
0
  }
2124
0
}
2125
2126
2127
/**************************************************************/
2128
2129
static int
2130
cmp_point_x(const void *pa, const void *pb)
2131
0
{
2132
0
  LWPOINT *p1 = *((LWPOINT **)pa);
2133
0
  LWPOINT *p2 = *((LWPOINT **)pb);
2134
2135
0
  const POINT2D *pt1 = getPoint2d_cp(p1->point, 0);
2136
0
  const POINT2D *pt2 = getPoint2d_cp(p2->point, 0);
2137
2138
0
  if (pt1 && pt2)
2139
0
    return (pt1->x > pt2->x) ? 1 : ((pt1->x < pt2->x) ? -1 : 0);
2140
2141
0
  if (pt1) return -1;
2142
0
  if (pt2) return 1;
2143
0
  return 0;
2144
0
}
2145
2146
static int
2147
cmp_point_y(const void *pa, const void *pb)
2148
0
{
2149
0
  LWPOINT *p1 = *((LWPOINT **)pa);
2150
0
  LWPOINT *p2 = *((LWPOINT **)pb);
2151
2152
0
  const POINT2D *pt1 = getPoint2d_cp(p1->point, 0);
2153
0
  const POINT2D *pt2 = getPoint2d_cp(p2->point, 0);
2154
2155
0
  if (pt1 && pt2)
2156
0
    return (pt1->y > pt2->y) ? 1 : ((pt1->y < pt2->y) ? -1 : 0);
2157
2158
0
  if (pt1) return -1;
2159
0
  if (pt2) return 1;
2160
0
  return 0;
2161
0
}
2162
2163
static int
2164
cmp_coord(const double a, const double b)
2165
0
{
2166
0
  if (isnan(a))
2167
0
    return isnan(b) ? 0 : 1;
2168
0
  if (isnan(b))
2169
0
    return -1;
2170
0
  return (a > b) ? 1 : ((a < b) ? -1 : 0);
2171
0
}
2172
2173
static int
2174
cmp_point_x_y(const void *pa, const void *pb)
2175
0
{
2176
0
  LWPOINT *const *p1 = pa;
2177
0
  LWPOINT *const *p2 = pb;
2178
0
  const POINT2D *pt1 = lwpoint_is_empty(*p1) ? NULL : getPoint2d_cp((*p1)->point, 0);
2179
0
  const POINT2D *pt2 = lwpoint_is_empty(*p2) ? NULL : getPoint2d_cp((*p2)->point, 0);
2180
0
  if (pt1 && pt2)
2181
0
  {
2182
0
    int cmp_x = cmp_coord(pt1->x, pt2->x);
2183
0
    return cmp_x ? cmp_x : cmp_coord(pt1->y, pt2->y);
2184
0
  }
2185
0
  if (pt1)
2186
0
    return -1;
2187
0
  if (pt2)
2188
0
    return 1;
2189
0
  return 0;
2190
0
}
2191
2192
int
2193
lwgeom_remove_repeated_points_in_place(LWGEOM *geom, double tolerance)
2194
0
{
2195
0
  int geometry_modified = LW_FALSE;
2196
0
  switch (geom->type)
2197
0
  {
2198
  /* No-op! Cannot remove points */
2199
0
  case POINTTYPE:
2200
0
  case TRIANGLETYPE:
2201
0
    return geometry_modified;
2202
0
  case LINETYPE: {
2203
0
    LWLINE *g = (LWLINE *)(geom);
2204
0
    POINTARRAY *pa = g->points;
2205
0
    uint32_t npoints = pa->npoints;
2206
0
    ptarray_remove_repeated_points_in_place(pa, tolerance, 2);
2207
0
    geometry_modified = npoints != pa->npoints;
2208
    /* Invalid input, discard the collapsed line */
2209
0
    if (pa->npoints < 2)
2210
0
    {
2211
0
      pa->npoints = 0;
2212
0
      geometry_modified = LW_TRUE;
2213
0
    }
2214
0
    break;
2215
0
  }
2216
0
  case POLYGONTYPE: {
2217
0
    uint32_t j = 0;
2218
0
    LWPOLY *g = (LWPOLY *)(geom);
2219
0
    for (uint32_t i = 0; i < g->nrings; i++)
2220
0
    {
2221
0
      POINTARRAY *pa = g->rings[i];
2222
0
      uint32_t npoints = pa->npoints;
2223
0
      ptarray_remove_repeated_points_in_place(pa, tolerance, 4);
2224
0
      geometry_modified |= npoints != pa->npoints;
2225
      /* Drop collapsed rings */
2226
0
      if (pa->npoints < 4)
2227
0
      {
2228
0
        geometry_modified = LW_TRUE;
2229
0
        ptarray_free(pa);
2230
0
        continue;
2231
0
      }
2232
0
      g->rings[j++] = pa;
2233
0
    }
2234
    /* Update ring count */
2235
0
    g->nrings = j;
2236
0
    break;
2237
0
  }
2238
0
  case MULTIPOINTTYPE: {
2239
0
    double tolsq = tolerance * tolerance;
2240
0
    LWMPOINT *mpt = (LWMPOINT *)geom;
2241
0
    if (!mpt->ngeoms)
2242
0
      break;
2243
2244
0
    if (tolerance == 0)
2245
0
    {
2246
0
      qsort(mpt->geoms, mpt->ngeoms, sizeof(LWPOINT *), cmp_point_x_y);
2247
0
      uint32_t out = 0;
2248
0
      for (uint32_t i = 0; i < mpt->ngeoms; i++)
2249
0
      {
2250
0
        LWPOINT *point = mpt->geoms[i];
2251
0
        if (lwpoint_is_empty(point))
2252
0
        {
2253
0
          lwpoint_free(point);
2254
0
          geometry_modified = LW_TRUE;
2255
0
          continue;
2256
0
        }
2257
0
        const POINT2D *current = getPoint2d_cp(point->point, 0);
2258
0
        if (out > 0)
2259
0
        {
2260
0
          const POINT2D *previous = getPoint2d_cp(mpt->geoms[out - 1]->point, 0);
2261
0
          if (previous->x == current->x && previous->y == current->y)
2262
0
          {
2263
0
            lwpoint_free(point);
2264
0
            geometry_modified = LW_TRUE;
2265
0
            continue;
2266
0
          }
2267
0
        }
2268
0
        mpt->geoms[out++] = point;
2269
0
      }
2270
0
      mpt->ngeoms = out;
2271
0
      break;
2272
0
    }
2273
2274
0
    for (uint8_t dim = 0; dim < 2; dim++)
2275
0
    {
2276
      /* sort by y, then by x - this way the result is sorted by x */
2277
0
      qsort(mpt->geoms, mpt->ngeoms, sizeof(LWPOINT *), dim ? cmp_point_x : cmp_point_y);
2278
0
      for (uint32_t i = 0; i < mpt->ngeoms; i++)
2279
0
      {
2280
0
        if (!mpt->geoms[i])
2281
0
          continue;
2282
2283
0
        const POINT2D *pti = getPoint2d_cp(mpt->geoms[i]->point, 0);
2284
0
        if (!pti) continue;
2285
2286
        /* check upcoming points if they're within tolerance of current one */
2287
0
        for (uint32_t j = i + 1; j < mpt->ngeoms; j++)
2288
0
        {
2289
0
          if (!mpt->geoms[j])
2290
0
            continue;
2291
2292
0
          const POINT2D *ptj = getPoint2d_cp(mpt->geoms[j]->point, 0);
2293
0
          if (!ptj) continue;
2294
2295
          /* check that the point is in the strip of tolerance around the point */
2296
0
          if ((dim ? ptj->x - pti->x : ptj->y - pti->y) > tolerance)
2297
0
            break;
2298
2299
          /* remove any upcoming point that is within tolerance circle */
2300
0
          if (distance2d_sqr_pt_pt(pti, ptj) <= tolsq)
2301
0
          {
2302
0
            lwpoint_free(mpt->geoms[j]);
2303
0
            mpt->geoms[j] = NULL;
2304
0
            geometry_modified = LW_TRUE;
2305
0
          }
2306
0
        }
2307
0
      }
2308
2309
      /* null out empties */
2310
0
      for (uint32_t j = 0; j < mpt->ngeoms; j++)
2311
0
      {
2312
0
        if (mpt->geoms[j] && lwpoint_is_empty(mpt->geoms[j]))
2313
0
        {
2314
0
          lwpoint_free(mpt->geoms[j]);
2315
0
          mpt->geoms[j] = NULL;
2316
0
          geometry_modified = LW_TRUE;
2317
0
        }
2318
0
      }
2319
2320
      /* compactify array of points */
2321
0
      uint32_t i = 0;
2322
0
      for (uint32_t j = 0; j < mpt->ngeoms; j++)
2323
0
        if (mpt->geoms[j])
2324
0
          mpt->geoms[i++] = mpt->geoms[j];
2325
0
      mpt->ngeoms = i;
2326
0
    }
2327
2328
0
    break;
2329
0
  }
2330
2331
0
    case CIRCSTRINGTYPE:
2332
0
    case NURBSCURVETYPE:
2333
      /* Dunno how to handle these, will return untouched */
2334
0
      return geometry_modified;
2335
2336
  /* Can process most multi* types as generic collection */
2337
0
  case MULTILINETYPE:
2338
0
  case MULTIPOLYGONTYPE:
2339
0
  case TINTYPE:
2340
0
  case COLLECTIONTYPE:
2341
  /* Curve types we mostly ignore, but allow the linear */
2342
  /* portions to be processed by recursing into them */
2343
0
  case MULTICURVETYPE:
2344
0
  case CURVEPOLYTYPE:
2345
0
  case MULTISURFACETYPE:
2346
0
  case COMPOUNDTYPE: {
2347
0
    uint32_t i, j = 0;
2348
0
    LWCOLLECTION *col = (LWCOLLECTION *)(geom);
2349
0
    for (i = 0; i < col->ngeoms; i++)
2350
0
    {
2351
0
      LWGEOM *g = col->geoms[i];
2352
0
      if (!g)
2353
0
        continue;
2354
0
      geometry_modified |= lwgeom_remove_repeated_points_in_place(g, tolerance);
2355
      /* Drop zero'ed out geometries */
2356
0
      if (lwgeom_is_empty(g))
2357
0
      {
2358
0
        lwgeom_free(g);
2359
0
        continue;
2360
0
      }
2361
0
      col->geoms[j++] = g;
2362
0
    }
2363
    /* Update geometry count */
2364
0
    col->ngeoms = j;
2365
0
    break;
2366
0
  }
2367
0
  default: {
2368
0
    lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
2369
0
    break;
2370
0
  }
2371
0
  }
2372
2373
0
  if (geometry_modified)
2374
0
    lwgeom_drop_bbox(geom);
2375
0
  return geometry_modified;
2376
0
}
2377
2378
2379
/**************************************************************/
2380
2381
int
2382
lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
2383
0
{
2384
0
  int modified = LW_FALSE;
2385
2386
0
  if (!geom)
2387
0
  {
2388
0
    lwerror("NULL geometry encountered");
2389
0
    return LW_FALSE;
2390
0
  }
2391
2392
0
  switch (geom->type)
2393
0
  {
2394
    /* No-op! Cannot simplify points or triangles */
2395
0
    case POINTTYPE:
2396
0
      return modified;
2397
0
    case TRIANGLETYPE:
2398
0
    {
2399
0
      if (preserve_collapsed)
2400
0
        return modified;
2401
0
      LWTRIANGLE *t = lwgeom_as_lwtriangle(geom);
2402
0
      POINTARRAY *pa = t->points;
2403
0
      ptarray_simplify_in_place(pa, epsilon, 0);
2404
0
      if (pa->npoints < 3)
2405
0
      {
2406
0
        pa->npoints = 0;
2407
0
        modified = LW_TRUE;
2408
0
      }
2409
0
      break;
2410
0
    }
2411
0
    case LINETYPE:
2412
0
    {
2413
0
      LWLINE *g = (LWLINE*)(geom);
2414
0
      POINTARRAY *pa = g->points;
2415
0
      uint32_t in_npoints = pa->npoints;
2416
0
      ptarray_simplify_in_place(pa, epsilon, 2);
2417
0
      modified = in_npoints != pa->npoints;
2418
      /* Invalid output */
2419
0
      if (pa->npoints == 1 && pa->maxpoints > 1)
2420
0
      {
2421
        /* Use first point as last point */
2422
0
        if (preserve_collapsed)
2423
0
        {
2424
0
          pa->npoints = 2;
2425
0
          ptarray_copy_point(pa, 0, 1);
2426
0
        }
2427
        /* Finish the collapse process */
2428
0
        else
2429
0
        {
2430
0
          pa->npoints = 0;
2431
0
        }
2432
0
      }
2433
      /* Duped output, force collapse */
2434
0
      if (pa->npoints == 2 && !preserve_collapsed)
2435
0
      {
2436
0
        if (p2d_same(getPoint2d_cp(pa, 0), getPoint2d_cp(pa, 1)))
2437
0
          pa->npoints = 0;
2438
0
      }
2439
0
      break;
2440
0
    }
2441
0
    case POLYGONTYPE:
2442
0
    {
2443
0
      uint32_t i, j = 0;
2444
0
      LWPOLY *g = (LWPOLY*)(geom);
2445
0
      for (i = 0; i < g->nrings; i++)
2446
0
      {
2447
0
        POINTARRAY *pa = g->rings[i];
2448
        /* Only stop collapse on first ring */
2449
0
        int minpoints = (preserve_collapsed && i == 0) ? 4 : 0;
2450
        /* Skip zero'ed out rings */
2451
0
        if(!pa)
2452
0
          continue;
2453
0
        uint32_t in_npoints = pa->npoints;
2454
0
        ptarray_simplify_in_place(pa, epsilon, minpoints);
2455
0
        modified |= in_npoints != pa->npoints;
2456
        /* Drop collapsed rings */
2457
0
        if(pa->npoints < 4)
2458
0
        {
2459
0
          if (i == 0)
2460
0
          {
2461
            /* If the outer ring is dropped, all can be dropped */
2462
0
            for (i = 0; i < g->nrings; i++)
2463
0
            {
2464
0
              pa = g->rings[i];
2465
0
              ptarray_free(pa);
2466
0
            }
2467
0
            break;
2468
0
          }
2469
0
          else
2470
0
          {
2471
            /* Drop this inner ring only */
2472
0
            ptarray_free(pa);
2473
0
            continue;
2474
0
          }
2475
0
        }
2476
0
        g->rings[j++] = pa;
2477
0
      }
2478
      /* Update ring count */
2479
0
      g->nrings = j;
2480
0
      break;
2481
0
    }
2482
    /* Can process all multi* types as generic collection */
2483
0
    case MULTIPOINTTYPE:
2484
0
    case MULTILINETYPE:
2485
0
    case MULTIPOLYGONTYPE:
2486
0
    case TINTYPE:
2487
0
    case COLLECTIONTYPE:
2488
0
    {
2489
0
      uint32_t i, j = 0;
2490
0
      LWCOLLECTION *col = (LWCOLLECTION*)geom;
2491
0
      for (i = 0; i < col->ngeoms; i++)
2492
0
      {
2493
0
        LWGEOM *g = col->geoms[i];
2494
0
        if (!g) continue;
2495
0
        modified |= lwgeom_simplify_in_place(g, epsilon, preserve_collapsed);
2496
        /* Drop zero'ed out geometries */
2497
0
        if(lwgeom_is_empty(g))
2498
0
        {
2499
0
          lwgeom_free(g);
2500
0
          continue;
2501
0
        }
2502
0
        col->geoms[j++] = g;
2503
0
      }
2504
      /* Update geometry count */
2505
0
      col->ngeoms = j;
2506
0
      break;
2507
0
    }
2508
0
    default:
2509
0
    {
2510
0
      lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
2511
0
      break;
2512
0
    }
2513
0
  }
2514
2515
0
  if (modified)
2516
0
  {
2517
0
    lwgeom_drop_bbox(geom);
2518
0
  }
2519
0
  return modified;
2520
0
}
2521
2522
LWGEOM* lwgeom_simplify(const LWGEOM *igeom, double dist, int preserve_collapsed)
2523
0
{
2524
0
  LWGEOM *lwgeom_out = lwgeom_clone_deep(igeom);
2525
0
  lwgeom_simplify_in_place(lwgeom_out, dist, preserve_collapsed);
2526
0
  if (lwgeom_is_empty(lwgeom_out))
2527
0
  {
2528
0
    lwgeom_free(lwgeom_out);
2529
0
    return NULL;
2530
0
  }
2531
0
  return lwgeom_out;
2532
0
}
2533
2534
/**************************************************************/
2535
2536
2537
double lwgeom_area(const LWGEOM *geom)
2538
0
{
2539
0
  int type = geom->type;
2540
2541
0
  if ( type == POLYGONTYPE )
2542
0
    return lwpoly_area((LWPOLY*)geom);
2543
0
  else if ( type == CURVEPOLYTYPE )
2544
0
    return lwcurvepoly_area((LWCURVEPOLY*)geom);
2545
0
  else if (type ==  TRIANGLETYPE )
2546
0
    return lwtriangle_area((LWTRIANGLE*)geom);
2547
0
  else if ( lwgeom_is_collection(geom) )
2548
0
  {
2549
0
    double area = 0.0;
2550
0
    uint32_t i;
2551
0
    LWCOLLECTION *col = (LWCOLLECTION*)geom;
2552
0
    for ( i = 0; i < col->ngeoms; i++ )
2553
0
      area += lwgeom_area(col->geoms[i]);
2554
0
    return area;
2555
0
  }
2556
0
  else
2557
0
    return 0.0;
2558
0
}
2559
2560
double lwgeom_perimeter(const LWGEOM *geom)
2561
0
{
2562
0
  int type = geom->type;
2563
0
  if ( type == POLYGONTYPE )
2564
0
    return lwpoly_perimeter((LWPOLY*)geom);
2565
0
  else if ( type == CURVEPOLYTYPE )
2566
0
    return lwcurvepoly_perimeter((LWCURVEPOLY*)geom);
2567
0
  else if ( type == TRIANGLETYPE )
2568
0
    return lwtriangle_perimeter((LWTRIANGLE*)geom);
2569
0
  else if ( lwgeom_is_collection(geom) )
2570
0
  {
2571
0
    double perimeter = 0.0;
2572
0
    uint32_t i;
2573
0
    LWCOLLECTION *col = (LWCOLLECTION*)geom;
2574
0
    for ( i = 0; i < col->ngeoms; i++ )
2575
0
      perimeter += lwgeom_perimeter(col->geoms[i]);
2576
0
    return perimeter;
2577
0
  }
2578
0
  else
2579
0
    return 0.0;
2580
0
}
2581
2582
double lwgeom_perimeter_2d(const LWGEOM *geom)
2583
0
{
2584
0
  int type = geom->type;
2585
0
  if ( type == POLYGONTYPE )
2586
0
    return lwpoly_perimeter_2d((LWPOLY*)geom);
2587
0
  else if ( type == CURVEPOLYTYPE )
2588
0
    return lwcurvepoly_perimeter_2d((LWCURVEPOLY*)geom);
2589
0
  else if ( type == TRIANGLETYPE )
2590
0
    return lwtriangle_perimeter_2d((LWTRIANGLE*)geom);
2591
0
  else if ( lwgeom_is_collection(geom) )
2592
0
  {
2593
0
    double perimeter = 0.0;
2594
0
    uint32_t i;
2595
0
    LWCOLLECTION *col = (LWCOLLECTION*)geom;
2596
0
    for ( i = 0; i < col->ngeoms; i++ )
2597
0
      perimeter += lwgeom_perimeter_2d(col->geoms[i]);
2598
0
    return perimeter;
2599
0
  }
2600
0
  else
2601
0
    return 0.0;
2602
0
}
2603
2604
double lwgeom_length(const LWGEOM *geom)
2605
0
{
2606
0
  int type = geom->type;
2607
0
  if ( type == LINETYPE )
2608
0
    return lwline_length((LWLINE*)geom);
2609
0
  else if ( type == CIRCSTRINGTYPE )
2610
0
    return lwcircstring_length((LWCIRCSTRING*)geom);
2611
0
  else if ( type == NURBSCURVETYPE )
2612
0
  {
2613
0
    double length;
2614
0
    LWLINE *line = lwnurbscurve_to_linestring((LWNURBSCURVE*)geom, 32);
2615
0
    length = lwline_length(line);
2616
0
    lwline_free(line);
2617
0
    return length;
2618
0
  }
2619
0
  else if ( type == COMPOUNDTYPE )
2620
0
    return lwcompound_length((LWCOMPOUND*)geom);
2621
0
  else if ( lwgeom_is_collection(geom) )
2622
0
  {
2623
0
    double length = 0.0;
2624
0
    uint32_t i;
2625
0
    LWCOLLECTION *col = (LWCOLLECTION*)geom;
2626
0
    for ( i = 0; i < col->ngeoms; i++ )
2627
0
      length += lwgeom_length(col->geoms[i]);
2628
0
    return length;
2629
0
  }
2630
0
  else
2631
0
    return 0.0;
2632
0
}
2633
2634
double lwgeom_length_2d(const LWGEOM *geom)
2635
0
{
2636
0
  int type = geom->type;
2637
0
  if ( type == LINETYPE )
2638
0
    return lwline_length_2d((LWLINE*)geom);
2639
0
  else if ( type == CIRCSTRINGTYPE )
2640
0
    return lwcircstring_length_2d((LWCIRCSTRING*)geom);
2641
0
  else if ( type == NURBSCURVETYPE )
2642
0
  {
2643
0
    double length;
2644
0
    LWLINE *line = lwnurbscurve_to_linestring((LWNURBSCURVE*)geom, 32);
2645
0
    length = lwline_length_2d(line);
2646
0
    lwline_free(line);
2647
0
    return length;
2648
0
  }
2649
0
  else if ( type == COMPOUNDTYPE )
2650
0
    return lwcompound_length_2d((LWCOMPOUND*)geom);
2651
0
  else if ( type != CURVEPOLYTYPE && lwgeom_is_collection(geom) )
2652
0
  {
2653
0
    double length = 0.0;
2654
0
    uint32_t i;
2655
0
    LWCOLLECTION *col = (LWCOLLECTION*)geom;
2656
0
    for ( i = 0; i < col->ngeoms; i++ )
2657
0
      length += lwgeom_length_2d(col->geoms[i]);
2658
0
    return length;
2659
0
  }
2660
0
  else
2661
0
    return 0.0;
2662
0
}
2663
2664
void
2665
lwgeom_affine(LWGEOM *geom, const AFFINE *affine)
2666
0
{
2667
0
  int type = geom->type;
2668
0
  uint32_t i;
2669
2670
0
  switch(type)
2671
0
  {
2672
    /* Take advantage of fact that pt/ln/circ/tri have same memory structure */
2673
0
    case POINTTYPE:
2674
0
    case LINETYPE:
2675
0
    case CIRCSTRINGTYPE:
2676
0
    case TRIANGLETYPE:
2677
0
    {
2678
0
      LWLINE *l = (LWLINE*)geom;
2679
0
      ptarray_affine(l->points, affine);
2680
0
      break;
2681
0
    }
2682
0
    case NURBSCURVETYPE:
2683
0
    {
2684
0
      LWNURBSCURVE *n = (LWNURBSCURVE*)geom;
2685
0
      if (n->points != NULL)
2686
0
        ptarray_affine(n->points, affine);
2687
0
      break;
2688
0
    }
2689
0
    case POLYGONTYPE:
2690
0
    {
2691
0
      LWPOLY *p = (LWPOLY*)geom;
2692
0
      for( i = 0; i < p->nrings; i++ )
2693
0
        ptarray_affine(p->rings[i], affine);
2694
0
      break;
2695
0
    }
2696
0
    case CURVEPOLYTYPE:
2697
0
    {
2698
0
      LWCURVEPOLY *c = (LWCURVEPOLY*)geom;
2699
0
      for( i = 0; i < c->nrings; i++ )
2700
0
        lwgeom_affine(c->rings[i], affine);
2701
0
      break;
2702
0
    }
2703
0
    default:
2704
0
    {
2705
0
      if( lwgeom_is_collection(geom) )
2706
0
      {
2707
0
        LWCOLLECTION *c = (LWCOLLECTION*)geom;
2708
0
        for( i = 0; i < c->ngeoms; i++ )
2709
0
        {
2710
0
          lwgeom_affine(c->geoms[i], affine);
2711
0
        }
2712
0
      }
2713
0
      else
2714
0
      {
2715
0
        lwerror("lwgeom_affine: unable to handle type '%s'", lwtype_name(type));
2716
0
      }
2717
0
    }
2718
0
  }
2719
2720
  /* Recompute bbox if needed */
2721
0
  if (geom->bbox)
2722
0
    lwgeom_refresh_bbox(geom);
2723
0
}
2724
2725
/**
2726
 * Scale a geometry in-place by per-ordinate factors.
2727
 *
2728
 * Applies the provided scale factors to all coordinates of the given geometry.
2729
 * Scaling is applied component-wise: X by factor->x, Y by factor->y, Z by factor->z
2730
 * and M by factor->m when those ordinates are present. The operation mutates
2731
 * the input geometry and recurses into collections and curve/polygon components.
2732
 * If the geometry has an attached bounding box it is refreshed after scaling.
2733
 *
2734
 * @param geom Geometry to be scaled (modified in place). Behavior is undefined if NULL.
2735
 * @param factor Per-ordinate scale factors (X, Y, Z, M).
2736
 */
2737
void
2738
lwgeom_scale(LWGEOM *geom, const POINT4D *factor)
2739
0
{
2740
0
  int type = geom->type;
2741
0
  uint32_t i;
2742
2743
0
  switch(type)
2744
0
  {
2745
    /* Take advantage of fact that pt/ln/circ/tri have same memory structure */
2746
0
    case POINTTYPE:
2747
0
    case LINETYPE:
2748
0
    case CIRCSTRINGTYPE:
2749
0
    case TRIANGLETYPE:
2750
0
    {
2751
0
      LWLINE *l = (LWLINE*)geom;
2752
0
      ptarray_scale(l->points, factor);
2753
0
      break;
2754
0
    }
2755
0
    case NURBSCURVETYPE:
2756
0
    {
2757
0
      LWNURBSCURVE *n = (LWNURBSCURVE*)geom;
2758
0
      if (n->points != NULL)
2759
0
        ptarray_scale(n->points, factor);
2760
0
      break;
2761
0
    }
2762
0
    case POLYGONTYPE:
2763
0
    {
2764
0
      LWPOLY *p = (LWPOLY*)geom;
2765
0
      for( i = 0; i < p->nrings; i++ )
2766
0
        ptarray_scale(p->rings[i], factor);
2767
0
      break;
2768
0
    }
2769
0
    case CURVEPOLYTYPE:
2770
0
    {
2771
0
      LWCURVEPOLY *c = (LWCURVEPOLY*)geom;
2772
0
      for( i = 0; i < c->nrings; i++ )
2773
0
        lwgeom_scale(c->rings[i], factor);
2774
0
      break;
2775
0
    }
2776
0
    default:
2777
0
    {
2778
0
      if( lwgeom_is_collection(geom) )
2779
0
      {
2780
0
        LWCOLLECTION *c = (LWCOLLECTION*)geom;
2781
0
        for( i = 0; i < c->ngeoms; i++ )
2782
0
        {
2783
0
          lwgeom_scale(c->geoms[i], factor);
2784
0
        }
2785
0
      }
2786
0
      else
2787
0
      {
2788
0
        lwerror("lwgeom_scale: unable to handle type '%s'", lwtype_name(type));
2789
0
      }
2790
0
    }
2791
0
  }
2792
2793
  /* Recompute bbox if needed */
2794
0
  if (geom->bbox)
2795
0
    lwgeom_refresh_bbox(geom);
2796
0
}
2797
2798
/**
2799
 * Construct an empty LWGEOM of the requested geometry type.
2800
 *
2801
 * Creates and returns a newly allocated geometry instance with no coordinates
2802
 * (an "empty" geometry), using the provided SRID and dimensionality flags.
2803
 * For homogeneous collection types (MULTI*, COLLECTION, COMPOUND) the `type`
2804
 * argument selects the specific collection subtype to construct.
2805
 *
2806
 * @param type Geometry type code (e.g., POINTTYPE, LINETYPE, POLYGONTYPE,
2807
 *             CURVEPOLYTYPE, CIRCSTRINGTYPE, TRIANGLETYPE, COMPOUNDTYPE,
2808
 *             MULTIPOINTTYPE, MULTILINETYPE, MULTIPOLYGONTYPE, COLLECTIONTYPE,
2809
 *             NURBSCURVETYPE). Unsupported types cause an error to be logged
2810
 *             and the function to return NULL.
2811
 * @param srid Spatial reference identifier to assign to the constructed geometry
2812
 *             (use SRID_UNKNOWN if not set).
2813
 * @param hasz Non-zero to include a Z ordinate in the geometry, zero to omit Z.
2814
 * @param hasm Non-zero to include an M ordinate in the geometry, zero to omit M.
2815
 *
2816
 * @return Pointer to the newly constructed LWGEOM, or NULL if `type` is not
2817
 *         supported.
2818
 */
2819
LWGEOM *
2820
lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
2821
0
{
2822
0
  switch(type)
2823
0
  {
2824
0
    case POINTTYPE:
2825
0
      return lwpoint_as_lwgeom(lwpoint_construct_empty(srid, hasz, hasm));
2826
0
    case LINETYPE:
2827
0
      return lwline_as_lwgeom(lwline_construct_empty(srid, hasz, hasm));
2828
0
    case POLYGONTYPE:
2829
0
      return lwpoly_as_lwgeom(lwpoly_construct_empty(srid, hasz, hasm));
2830
0
    case CURVEPOLYTYPE:
2831
0
      return lwcurvepoly_as_lwgeom(lwcurvepoly_construct_empty(srid, hasz, hasm));
2832
0
    case CIRCSTRINGTYPE:
2833
0
      return lwcircstring_as_lwgeom(lwcircstring_construct_empty(srid, hasz, hasm));
2834
0
    case TRIANGLETYPE:
2835
0
      return lwtriangle_as_lwgeom(lwtriangle_construct_empty(srid, hasz, hasm));
2836
0
    case COMPOUNDTYPE:
2837
0
    case MULTIPOINTTYPE:
2838
0
    case MULTILINETYPE:
2839
0
    case MULTIPOLYGONTYPE:
2840
0
    case COLLECTIONTYPE:
2841
0
      return lwcollection_as_lwgeom(lwcollection_construct_empty(type, srid, hasz, hasm));
2842
0
    case NURBSCURVETYPE:
2843
0
      return lwnurbscurve_as_lwgeom(lwnurbscurve_construct_empty(srid, hasz, hasm));
2844
0
    default:
2845
0
      lwerror("lwgeom_construct_empty: unsupported geometry type: %s",
2846
0
              lwtype_name(type));
2847
0
      return NULL;
2848
0
  }
2849
0
}
2850
2851
int
2852
lwgeom_startpoint(const LWGEOM *lwgeom, POINT4D *pt)
2853
0
{
2854
0
  if ( ! lwgeom || lwgeom_is_empty(lwgeom) )
2855
0
    return LW_FAILURE;
2856
2857
0
  switch( lwgeom->type )
2858
0
  {
2859
0
    case POINTTYPE:
2860
0
      return ptarray_startpoint(((LWPOINT*)lwgeom)->point, pt);
2861
0
    case TRIANGLETYPE:
2862
0
    case CIRCSTRINGTYPE:
2863
0
    case LINETYPE:
2864
0
      return ptarray_startpoint(((LWLINE*)lwgeom)->points, pt);
2865
0
    case NURBSCURVETYPE:
2866
0
    {
2867
0
      LWPOINT *start = lwnurbscurve_evaluate((const LWNURBSCURVE*)lwgeom, 0.0);
2868
0
      int rv = LW_FAILURE;
2869
0
      if ( start && start->point && start->point->npoints > 0 )
2870
0
        rv = getPoint4d_p(start->point, 0, pt) ? LW_SUCCESS : LW_FAILURE;
2871
0
      lwpoint_free(start);
2872
0
      return rv;
2873
0
    }
2874
0
    case POLYGONTYPE:
2875
0
      return lwpoly_startpoint((LWPOLY*)lwgeom, pt);
2876
0
    case TINTYPE:
2877
0
    case CURVEPOLYTYPE:
2878
0
    case COMPOUNDTYPE:
2879
0
    case MULTIPOINTTYPE:
2880
0
    case MULTILINETYPE:
2881
0
    case MULTIPOLYGONTYPE:
2882
0
    case COLLECTIONTYPE:
2883
0
    case POLYHEDRALSURFACETYPE:
2884
0
      return lwcollection_startpoint((LWCOLLECTION*)lwgeom, pt);
2885
0
    default:
2886
0
      lwerror("lwgeom_startpoint: unsupported geometry type: %s", lwtype_name(lwgeom->type));
2887
0
      return LW_FAILURE;
2888
0
  }
2889
0
}
2890
2891
static inline double
2892
snap_to_int(double val)
2893
0
{
2894
0
  const double tolerance = 1e-6;
2895
0
    double rintval = rint(val);
2896
0
    if (fabs(val - rintval) < tolerance)
2897
0
    {
2898
0
        return rintval;
2899
0
    }
2900
0
    return val;
2901
0
}
2902
2903
/*
2904
 * See https://github.com/libgeos/geos/pull/956
2905
 * We use scale for rounding when gridsize is < 1 and
2906
 * gridsize for rounding when scale < 1.
2907
 */
2908
static inline void
2909
condition_gridspec_scale(gridspec *grid)
2910
0
{
2911
0
  if(grid->xsize > 0)
2912
0
  {
2913
0
    if(grid->xsize < 1)
2914
0
      grid->xscale = snap_to_int(1/grid->xsize);
2915
0
    else
2916
0
      grid->xsize = snap_to_int(grid->xsize);
2917
0
  }
2918
0
  if(grid->ysize > 0)
2919
0
  {
2920
0
    if(grid->ysize < 1)
2921
0
      grid->yscale = snap_to_int(1/grid->ysize);
2922
0
    else
2923
0
      grid->ysize = snap_to_int(grid->ysize);
2924
0
  }
2925
0
}
2926
2927
2928
void
2929
lwgeom_grid_in_place(LWGEOM *geom, gridspec *grid)
2930
0
{
2931
0
  if (!geom) return;
2932
0
  if (lwgeom_is_empty(geom)) return;
2933
2934
0
  condition_gridspec_scale(grid);
2935
2936
0
  switch ( geom->type )
2937
0
  {
2938
0
    case POINTTYPE:
2939
0
    {
2940
0
      LWPOINT *pt = (LWPOINT*)(geom);
2941
0
      ptarray_grid_in_place(pt->point, grid);
2942
0
      return;
2943
0
    }
2944
0
    case CIRCSTRINGTYPE:
2945
0
    case TRIANGLETYPE:
2946
0
    case LINETYPE:
2947
0
    {
2948
0
      LWLINE *ln = (LWLINE*)(geom);
2949
0
      ptarray_grid_in_place(ln->points, grid);
2950
      /* For invalid line, return an EMPTY */
2951
0
      if (ln->points->npoints < 2)
2952
0
        ln->points->npoints = 0;
2953
0
      return;
2954
0
    }
2955
0
    case NURBSCURVETYPE:
2956
0
    {
2957
0
      LWNURBSCURVE *n = (LWNURBSCURVE *)(geom);
2958
0
      uint32_t old_npoints;
2959
0
      if (!n->points)
2960
0
        return;
2961
2962
0
      old_npoints = n->points->npoints;
2963
0
      ptarray_grid_in_place(n->points, grid);
2964
0
      if (n->points->npoints < n->degree + 1)
2965
0
        n->points->npoints = 0;
2966
2967
0
      if (n->points->npoints != old_npoints)
2968
0
      {
2969
0
        if (n->weights)
2970
0
          lwfree(n->weights);
2971
0
        if (n->knots)
2972
0
          lwfree(n->knots);
2973
0
        n->weights = NULL;
2974
0
        n->knots = NULL;
2975
0
        n->nweights = 0;
2976
0
        n->nknots = 0;
2977
0
      }
2978
0
      return;
2979
0
    }
2980
0
    case POLYGONTYPE:
2981
0
    {
2982
0
      LWPOLY *ply = (LWPOLY*)(geom);
2983
0
      if (!ply->rings) return;
2984
2985
      /* Check first the external ring */
2986
0
      uint32_t i = 0;
2987
0
      POINTARRAY *pa = ply->rings[0];
2988
0
      ptarray_grid_in_place(pa, grid);
2989
0
      if (pa->npoints < 4)
2990
0
      {
2991
        /* External ring collapsed: free everything */
2992
0
        for (i = 0; i < ply->nrings; i++)
2993
0
        {
2994
0
          ptarray_free(ply->rings[i]);
2995
0
        }
2996
0
        ply->nrings = 0;
2997
0
        return;
2998
0
      }
2999
3000
      /* Check the other rings */
3001
0
      uint32_t j = 1;
3002
0
      for (i = 1; i < ply->nrings; i++)
3003
0
      {
3004
0
        POINTARRAY *pa = ply->rings[i];
3005
0
        ptarray_grid_in_place(pa, grid);
3006
3007
        /* Skip bad rings */
3008
0
        if (pa->npoints >= 4)
3009
0
        {
3010
0
          ply->rings[j++] = pa;
3011
0
        }
3012
0
        else
3013
0
        {
3014
0
          ptarray_free(pa);
3015
0
        }
3016
0
      }
3017
      /* Adjust ring count appropriately */
3018
0
      ply->nrings = j;
3019
0
      return;
3020
0
    }
3021
0
    case MULTIPOINTTYPE:
3022
0
    case MULTILINETYPE:
3023
0
    case MULTIPOLYGONTYPE:
3024
0
    case TINTYPE:
3025
0
    case COLLECTIONTYPE:
3026
0
    case COMPOUNDTYPE:
3027
0
    {
3028
0
      LWCOLLECTION *col = (LWCOLLECTION*)(geom);
3029
0
      uint32_t i, j = 0;
3030
0
      if (!col->geoms) return;
3031
0
      for (i = 0; i < col->ngeoms; i++)
3032
0
      {
3033
0
        LWGEOM *g = col->geoms[i];
3034
0
        lwgeom_grid_in_place(g, grid);
3035
        /* Empty geoms need to be freed */
3036
        /* before we move on */
3037
0
        if (lwgeom_is_empty(g))
3038
0
        {
3039
0
          lwgeom_free(g);
3040
0
          continue;
3041
0
        }
3042
0
        col->geoms[j++] = g;
3043
0
      }
3044
0
      col->ngeoms = j;
3045
0
      return;
3046
0
    }
3047
0
    default:
3048
0
    {
3049
0
      lwerror("%s: Unsupported geometry type: %s", __func__,
3050
0
              lwtype_name(geom->type));
3051
0
      return;
3052
0
    }
3053
0
  }
3054
0
}
3055
3056
3057
LWGEOM *
3058
lwgeom_grid(const LWGEOM *lwgeom, gridspec *grid)
3059
0
{
3060
0
  LWGEOM *lwgeom_out = lwgeom_clone_deep(lwgeom);
3061
0
  lwgeom_grid_in_place(lwgeom_out, grid);
3062
0
  return lwgeom_out;
3063
0
}
3064
3065
3066
/* Prototype for recursion */
3067
static void lwgeom_subdivide_recursive(const LWGEOM *geom,
3068
               uint8_t dimension,
3069
               uint32_t maxvertices,
3070
               uint32_t depth,
3071
               LWCOLLECTION *col,
3072
               double gridSize);
3073
3074
static void
3075
lwgeom_subdivide_recursive(const LWGEOM *geom,
3076
         uint8_t dimension,
3077
         uint32_t maxvertices,
3078
         uint32_t depth,
3079
         LWCOLLECTION *col,
3080
         double gridSize)
3081
0
{
3082
0
  const uint32_t maxdepth = 50;
3083
3084
0
  if (!geom)
3085
0
    return;
3086
3087
0
  const GBOX *box_in = lwgeom_get_bbox(geom);
3088
0
  if (!box_in)
3089
0
    return;
3090
3091
0
  LW_ON_INTERRUPT(return);
3092
3093
0
  GBOX clip;
3094
0
  gbox_duplicate(box_in, &clip);
3095
0
  double width = clip.xmax - clip.xmin;
3096
0
  double height = clip.ymax - clip.ymin;
3097
3098
0
  if ( geom->type == POLYHEDRALSURFACETYPE || geom->type == TINTYPE )
3099
0
    lwerror("%s: unsupported geometry type '%s'", __func__, lwtype_name(geom->type));
3100
3101
0
  if ( width == 0.0 && height == 0.0 )
3102
0
  {
3103
0
    if ( geom->type == POINTTYPE && dimension == 0)
3104
0
      lwcollection_add_lwgeom(col, lwgeom_clone_deep(geom));
3105
0
    return;
3106
0
  }
3107
3108
0
  if (width == 0.0)
3109
0
  {
3110
0
    clip.xmax += FP_TOLERANCE;
3111
0
    clip.xmin -= FP_TOLERANCE;
3112
0
    width = 2 * FP_TOLERANCE;
3113
0
  }
3114
0
  if (height == 0.0)
3115
0
  {
3116
0
    clip.ymax += FP_TOLERANCE;
3117
0
    clip.ymin -= FP_TOLERANCE;
3118
0
    height = 2 * FP_TOLERANCE;
3119
0
  }
3120
3121
  /* Always just recurse into collections */
3122
0
  if ( lwgeom_is_collection(geom) && geom->type != MULTIPOINTTYPE )
3123
0
  {
3124
0
    LWCOLLECTION *incol = (LWCOLLECTION*)geom;
3125
    /* Don't increment depth yet, since we aren't actually
3126
     * subdividing geometries yet */
3127
0
    for (uint32_t i = 0; i < incol->ngeoms; i++ )
3128
0
      lwgeom_subdivide_recursive(incol->geoms[i], dimension, maxvertices, depth, col, gridSize);
3129
0
    return;
3130
0
  }
3131
3132
0
  if (lwgeom_dimension(geom) < dimension)
3133
0
  {
3134
    /* We've hit a lower dimension object produced by clipping at
3135
     * a shallower recursion level. Ignore it. */
3136
0
    return;
3137
0
  }
3138
3139
  /* But don't go too far. 2^50 ~= 10^15, that's enough subdivision */
3140
  /* Just add what's left */
3141
0
  if ( depth > maxdepth )
3142
0
  {
3143
0
    lwcollection_add_lwgeom(col, lwgeom_clone_deep(geom));
3144
0
    return;
3145
0
  }
3146
3147
0
  uint32_t nvertices = lwgeom_count_vertices(geom);
3148
3149
  /* Skip empties entirely */
3150
0
  if (nvertices == 0)
3151
0
    return;
3152
3153
  /* If it is under the vertex tolerance, just add it, we're done */
3154
0
  if (nvertices <= maxvertices)
3155
0
  {
3156
0
    lwcollection_add_lwgeom(col, lwgeom_clone_deep(geom));
3157
0
    return;
3158
0
  }
3159
3160
0
  uint8_t split_ordinate = (width > height) ? 0 : 1;
3161
0
  double center = (split_ordinate == 0) ? (clip.xmin + clip.xmax) / 2 : (clip.ymin + clip.ymax) / 2;
3162
0
  double pivot = DBL_MAX;
3163
0
  if (geom->type == POLYGONTYPE)
3164
0
  {
3165
0
    uint32_t ring_to_trim = 0;
3166
0
    double ring_area = 0;
3167
0
    double pivot_eps = DBL_MAX;
3168
0
    double pt_eps = DBL_MAX;
3169
0
    POINTARRAY *pa;
3170
0
    LWPOLY *lwpoly = (LWPOLY *)geom;
3171
3172
    /* if there are more points in holes than in outer ring */
3173
0
    if (nvertices >= 2 * lwpoly->rings[0]->npoints)
3174
0
    {
3175
      /* trim holes starting from biggest */
3176
0
      for (uint32_t i = 1; i < lwpoly->nrings; i++)
3177
0
      {
3178
0
        double current_ring_area = fabs(ptarray_signed_area(lwpoly->rings[i]));
3179
0
        if (current_ring_area >= ring_area)
3180
0
        {
3181
0
          ring_area = current_ring_area;
3182
0
          ring_to_trim = i;
3183
0
        }
3184
0
      }
3185
0
    }
3186
3187
0
    pa = lwpoly->rings[ring_to_trim];
3188
3189
    /* find most central point in chosen ring */
3190
0
    for (uint32_t i = 0; i < pa->npoints; i++)
3191
0
    {
3192
0
      double pt;
3193
0
      if (split_ordinate == 0)
3194
0
        pt = getPoint2d_cp(pa, i)->x;
3195
0
      else
3196
0
        pt = getPoint2d_cp(pa, i)->y;
3197
0
      pt_eps = fabs(pt - center);
3198
0
      if (pivot_eps > pt_eps)
3199
0
      {
3200
0
        pivot = pt;
3201
0
        pivot_eps = pt_eps;
3202
0
      }
3203
0
    }
3204
0
  }
3205
0
  GBOX subbox1, subbox2;
3206
0
  gbox_duplicate(&clip, &subbox1);
3207
0
  gbox_duplicate(&clip, &subbox2);
3208
3209
0
  if (pivot == DBL_MAX)
3210
0
    pivot = center;
3211
3212
0
  if (split_ordinate == 0)
3213
0
  {
3214
0
    if (FP_NEQUALS(subbox1.xmax, pivot) && FP_NEQUALS(subbox1.xmin, pivot))
3215
0
      subbox1.xmax = subbox2.xmin = pivot;
3216
0
    else
3217
0
      subbox1.xmax = subbox2.xmin = center;
3218
0
  }
3219
0
  else
3220
0
  {
3221
0
    if (FP_NEQUALS(subbox1.ymax, pivot) && FP_NEQUALS(subbox1.ymin, pivot))
3222
0
      subbox1.ymax = subbox2.ymin = pivot;
3223
0
    else
3224
0
      subbox1.ymax = subbox2.ymin = center;
3225
0
  }
3226
3227
0
  ++depth;
3228
3229
0
  {
3230
0
    LWGEOM *subbox = (LWGEOM *)lwpoly_construct_envelope(
3231
0
        geom->srid, subbox1.xmin, subbox1.ymin, subbox1.xmax, subbox1.ymax);
3232
0
    LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
3233
0
    lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
3234
0
    lwgeom_free(subbox);
3235
0
    if (clipped && !lwgeom_is_empty(clipped))
3236
0
    {
3237
0
      lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
3238
0
      lwgeom_free(clipped);
3239
0
    }
3240
0
  }
3241
0
  {
3242
0
    LWGEOM *subbox = (LWGEOM *)lwpoly_construct_envelope(
3243
0
        geom->srid, subbox2.xmin, subbox2.ymin, subbox2.xmax, subbox2.ymax);
3244
0
    LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
3245
0
    lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
3246
0
    lwgeom_free(subbox);
3247
0
    if (clipped && !lwgeom_is_empty(clipped))
3248
0
    {
3249
0
      lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
3250
0
      lwgeom_free(clipped);
3251
0
    }
3252
0
  }
3253
0
}
3254
3255
LWCOLLECTION *
3256
lwgeom_subdivide_prec(const LWGEOM *geom, uint32_t maxvertices, double gridSize)
3257
0
{
3258
0
  static uint32_t startdepth = 0;
3259
0
  static uint32_t minmaxvertices = 5;
3260
0
  LWCOLLECTION *col;
3261
3262
0
  col = lwcollection_construct_empty(COLLECTIONTYPE, geom->srid, lwgeom_has_z(geom), lwgeom_has_m(geom));
3263
3264
0
  if ( lwgeom_is_empty(geom) )
3265
0
    return col;
3266
3267
0
  if ( maxvertices < minmaxvertices )
3268
0
  {
3269
0
    lwcollection_free(col);
3270
0
    lwerror("%s: cannot subdivide to fewer than %d vertices per output", __func__, minmaxvertices);
3271
0
  }
3272
3273
0
  lwgeom_subdivide_recursive(geom, lwgeom_dimension(geom), maxvertices, startdepth, col, gridSize);
3274
0
  lwgeom_set_srid((LWGEOM*)col, geom->srid);
3275
0
  return col;
3276
0
}
3277
3278
LWCOLLECTION *
3279
lwgeom_subdivide(const LWGEOM *geom, uint32_t maxvertices)
3280
0
{
3281
0
  return lwgeom_subdivide_prec(geom, maxvertices, -1);
3282
0
}
3283
3284
3285
3286
int
3287
lwgeom_is_trajectory(const LWGEOM *geom)
3288
0
{
3289
0
  int type = geom->type;
3290
3291
0
  if( type != LINETYPE )
3292
0
  {
3293
0
    lwnotice("Geometry is not a LINESTRING");
3294
0
    return LW_FALSE;
3295
0
  }
3296
0
  return lwline_is_trajectory((LWLINE*)geom);
3297
0
}
3298
3299
#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]
3300
STATIC_ASSERT(sizeof(double) == sizeof(uint64_t),this_should_be_true);
3301
3302
static double trim_preserve_decimal_digits(double d, int32_t decimal_digits)
3303
0
{
3304
0
  uint64_t dint = 0;
3305
0
  memcpy(&dint, &d, sizeof(double));
3306
  /* Extract the exponent from the IEEE 754 integer representation, which */
3307
  /* corresponds to floor(log2(fabs(d))) */
3308
0
  const int exponent = (int)((dint >> 52) & 2047) - 1023;
3309
  /* (x * 851 + 255) / 256 == 1 + (int)(x * log2(10)) for x in [0,30] */
3310
0
  int bits_needed = 1 + exponent + (decimal_digits * 851 + 255) / 256;
3311
  /* for negative values, (x * 851 + 255) / 256 == (int)(x * log2(10)), so */
3312
  /* subtract one */
3313
0
  if (decimal_digits < 0)
3314
0
    bits_needed --;
3315
3316
  /* This will also handle NaN and Inf since exponent = 1023, and thus for */
3317
  /* reasonable decimal_digits values bits_needed will be > 52 */
3318
0
  if (bits_needed >= 52)
3319
0
  {
3320
0
    return d;
3321
0
  }
3322
0
  if (bits_needed < 1 )
3323
0
    bits_needed = 1;
3324
0
  const uint64_t mask = 0xffffffffffffffffULL << (52 - bits_needed);
3325
0
  dint &= mask;
3326
0
  memcpy(&d, &dint, sizeof(double));
3327
0
  return d;
3328
0
}
3329
3330
void lwgeom_trim_bits_in_place(LWGEOM* geom, int32_t prec_x, int32_t prec_y, int32_t prec_z, int32_t prec_m)
3331
0
{
3332
0
  LWPOINTITERATOR* it = lwpointiterator_create_rw(geom);
3333
0
  POINT4D p;
3334
3335
0
  while (lwpointiterator_has_next(it))
3336
0
  {
3337
0
    lwpointiterator_peek(it, &p);
3338
0
    p.x = trim_preserve_decimal_digits(p.x, prec_x);
3339
0
    p.y = trim_preserve_decimal_digits(p.y, prec_y);
3340
0
    if (lwgeom_has_z(geom))
3341
0
      p.z = trim_preserve_decimal_digits(p.z, prec_z);
3342
0
    if (lwgeom_has_m(geom))
3343
0
      p.m = trim_preserve_decimal_digits(p.m, prec_m);
3344
0
    lwpointiterator_modify_next(it, &p);
3345
0
  }
3346
3347
0
  lwpointiterator_destroy(it);
3348
0
}
3349
3350
LWGEOM *
3351
lwgeom_boundary(LWGEOM *lwgeom)
3352
0
{
3353
0
  int32_t srid = lwgeom_get_srid(lwgeom);
3354
0
  uint8_t hasz = lwgeom_has_z(lwgeom);
3355
0
  uint8_t hasm = lwgeom_has_m(lwgeom);
3356
3357
0
  switch (lwgeom->type)
3358
0
  {
3359
0
  case POINTTYPE:
3360
0
  case MULTIPOINTTYPE: {
3361
0
    return lwgeom_construct_empty(lwgeom->type, srid, hasz, hasm);
3362
0
  }
3363
0
  case LINETYPE:
3364
0
  case CIRCSTRINGTYPE: {
3365
0
    if (lwgeom_is_closed(lwgeom) || lwgeom_is_empty(lwgeom))
3366
0
      return (LWGEOM *)lwmpoint_construct_empty(srid, hasz, hasm);
3367
0
    else
3368
0
    {
3369
0
      LWLINE *lwline = (LWLINE *)lwgeom;
3370
0
      LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
3371
0
      POINT4D pt;
3372
0
      getPoint4d_p(lwline->points, 0, &pt);
3373
0
      lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
3374
0
      getPoint4d_p(lwline->points, lwline->points->npoints - 1, &pt);
3375
0
      lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
3376
3377
0
      return (LWGEOM *)lwmpoint;
3378
0
    }
3379
0
  }
3380
0
  case NURBSCURVETYPE: {
3381
0
    if (lwgeom_is_closed(lwgeom) || lwgeom_is_empty(lwgeom))
3382
0
      return (LWGEOM *)lwmpoint_construct_empty(srid, hasz, hasm);
3383
0
    else
3384
0
    {
3385
0
      LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
3386
0
      POINT4D pt;
3387
0
      LWPOINT *startpt = lwnurbscurve_evaluate((const LWNURBSCURVE *)lwgeom, 0.0);
3388
0
      LWPOINT *endpt   = lwnurbscurve_evaluate((const LWNURBSCURVE *)lwgeom, 1.0);
3389
0
      if (startpt)
3390
0
      {
3391
0
        lwpoint_getPoint4d_p(startpt, &pt);
3392
0
        lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
3393
0
        lwpoint_free(startpt);
3394
0
      }
3395
0
      if (endpt)
3396
0
      {
3397
0
        lwpoint_getPoint4d_p(endpt, &pt);
3398
0
        lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
3399
0
        lwpoint_free(endpt);
3400
0
      }
3401
0
      return (LWGEOM *)lwmpoint;
3402
0
    }
3403
0
  }
3404
0
  case MULTILINETYPE:
3405
0
  case MULTICURVETYPE: {
3406
0
    LWMLINE *lwmline = (LWMLINE *)lwgeom;
3407
0
    POINT4D *out = lwalloc(sizeof(POINT4D) * lwmline->ngeoms * 2);
3408
0
    uint32_t n = 0;
3409
3410
0
    for (uint32_t i = 0; i < lwmline->ngeoms; i++)
3411
0
    {
3412
0
      LWMPOINT *points = lwgeom_as_lwmpoint(lwgeom_boundary((LWGEOM *)lwmline->geoms[i]));
3413
0
      if (!points)
3414
0
        continue;
3415
3416
0
      for (uint32_t k = 0; k < points->ngeoms; k++)
3417
0
      {
3418
0
        POINT4D pt = getPoint4d(points->geoms[k]->point, 0);
3419
3420
0
        uint8_t seen = LW_FALSE;
3421
0
        for (uint32_t j = 0; j < n; j++)
3422
0
        {
3423
0
          if (memcmp(&(out[j]), &pt, sizeof(POINT4D)) == 0)
3424
0
          {
3425
0
            seen = LW_TRUE;
3426
0
            out[j] = out[--n];
3427
0
            break;
3428
0
          }
3429
0
        }
3430
0
        if (!seen)
3431
0
          out[n++] = pt;
3432
0
      }
3433
3434
0
      lwgeom_free((LWGEOM *)points);
3435
0
    }
3436
3437
0
    LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
3438
3439
0
    for (uint32_t i = 0; i < n; i++)
3440
0
      lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &(out[i])));
3441
3442
0
    lwfree(out);
3443
3444
0
    return (LWGEOM *)lwmpoint;
3445
0
  }
3446
0
  case TRIANGLETYPE: {
3447
0
    LWTRIANGLE *lwtriangle = (LWTRIANGLE *)lwgeom;
3448
0
    POINTARRAY *points = ptarray_clone_deep(lwtriangle->points);
3449
0
    return (LWGEOM *)lwline_construct(srid, 0, points);
3450
0
  }
3451
0
  case POLYGONTYPE: {
3452
0
    LWPOLY *lwpoly = (LWPOLY *)lwgeom;
3453
3454
0
    LWMLINE *lwmline = lwmline_construct_empty(srid, hasz, hasm);
3455
0
    for (uint32_t i = 0; i < lwpoly->nrings; i++)
3456
0
    {
3457
0
      POINTARRAY *ring = ptarray_clone_deep(lwpoly->rings[i]);
3458
0
      lwmline_add_lwline(lwmline, lwline_construct(srid, 0, ring));
3459
0
    }
3460
3461
    /* Homogenize the multilinestring to hopefully get a single LINESTRING */
3462
0
    LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwmline);
3463
0
    lwgeom_free((LWGEOM *)lwmline);
3464
0
    return lwout;
3465
0
  }
3466
0
  case CURVEPOLYTYPE: {
3467
0
    LWCURVEPOLY *lwcurvepoly = (LWCURVEPOLY *)lwgeom;
3468
0
    LWCOLLECTION *lwcol = lwcollection_construct_empty(MULTICURVETYPE, srid, hasz, hasm);
3469
3470
0
    for (uint32_t i = 0; i < lwcurvepoly->nrings; i++)
3471
0
      lwcol = lwcollection_add_lwgeom(lwcol, lwgeom_clone_deep(lwcurvepoly->rings[i]));
3472
3473
0
    return (LWGEOM *)lwcol;
3474
0
  }
3475
0
  case MULTIPOLYGONTYPE:
3476
0
  case COLLECTIONTYPE:
3477
0
  case TINTYPE: {
3478
0
    LWCOLLECTION *lwcol = (LWCOLLECTION *)lwgeom;
3479
0
    LWCOLLECTION *lwcol_boundary = lwcollection_construct_empty(COLLECTIONTYPE, srid, hasz, hasm);
3480
3481
0
    for (uint32_t i = 0; i < lwcol->ngeoms; i++)
3482
0
      lwcollection_add_lwgeom(lwcol_boundary, lwgeom_boundary(lwcol->geoms[i]));
3483
3484
0
    LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwcol_boundary);
3485
0
    lwgeom_free((LWGEOM *)lwcol_boundary);
3486
3487
0
    return lwout;
3488
0
  }
3489
0
  default:
3490
0
    lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(lwgeom->type));
3491
0
    return NULL;
3492
0
  }
3493
0
}
3494
3495
int
3496
lwgeom_isfinite(const LWGEOM *lwgeom)
3497
0
{
3498
0
  LWPOINTITERATOR* it = lwpointiterator_create(lwgeom);
3499
0
  int hasz = lwgeom_has_z(lwgeom);
3500
0
  int hasm = lwgeom_has_m(lwgeom);
3501
3502
0
  while (lwpointiterator_has_next(it))
3503
0
  {
3504
0
    POINT4D p;
3505
0
    lwpointiterator_next(it, &p);
3506
0
    int finite = isfinite(p.x) &&
3507
0
      isfinite(p.y) &&
3508
0
      (hasz ? isfinite(p.z) : 1) &&
3509
0
      (hasm ? isfinite(p.m) : 1);
3510
3511
0
    if (!finite)
3512
0
    {
3513
0
      lwpointiterator_destroy(it);
3514
0
      return LW_FALSE;
3515
0
    }
3516
0
  }
3517
0
  lwpointiterator_destroy(it);
3518
0
  return LW_TRUE;
3519
0
}