Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgis/liblwgeom/lwcollection.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
 *
23
 **********************************************************************/
24
25
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include "liblwgeom_internal.h"
30
#include "lwgeom_log.h"
31
32
33
#define CHECK_LWGEOM_ZM 1
34
35
void
36
lwcollection_release(LWCOLLECTION *lwcollection)
37
0
{
38
0
  lwgeom_release(lwcollection_as_lwgeom(lwcollection));
39
0
}
40
41
LWCOLLECTION *
42
lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
43
0
{
44
0
  LWCOLLECTION *ret;
45
0
  int hasz, hasm;
46
0
#ifdef CHECK_LWGEOM_ZM
47
0
  char zm;
48
0
  uint32_t i;
49
0
#endif
50
51
0
  LWDEBUGF(2, "lwcollection_construct called with %d, %d, %p, %d, %p.", type, srid, bbox, ngeoms, geoms);
52
53
0
  if( ! lwtype_is_collection(type) )
54
0
    lwerror("Non-collection type specified in collection constructor!");
55
56
0
  hasz = 0;
57
0
  hasm = 0;
58
0
  if ( ngeoms > 0 )
59
0
  {
60
0
    hasz = FLAGS_GET_Z(geoms[0]->flags);
61
0
    hasm = FLAGS_GET_M(geoms[0]->flags);
62
0
#ifdef CHECK_LWGEOM_ZM
63
0
    zm = FLAGS_GET_ZM(geoms[0]->flags);
64
65
0
    LWDEBUGF(3, "lwcollection_construct type[0]=%d", geoms[0]->type);
66
67
0
    for (i=1; i<ngeoms; i++)
68
0
    {
69
0
      LWDEBUGF(3, "lwcollection_construct type=[%d]=%d", i, geoms[i]->type);
70
71
0
      if ( zm != FLAGS_GET_ZM(geoms[i]->flags) )
72
0
        lwerror("lwcollection_construct: mixed dimension geometries: %d/%d", zm, FLAGS_GET_ZM(geoms[i]->flags));
73
0
    }
74
0
#endif
75
0
  }
76
77
78
0
  ret = lwalloc(sizeof(LWCOLLECTION));
79
0
  ret->type = type;
80
0
  ret->flags = lwflags(hasz,hasm,0);
81
0
  FLAGS_SET_BBOX(ret->flags, bbox?1:0);
82
0
  ret->srid = srid;
83
0
  ret->ngeoms = ngeoms;
84
0
  ret->maxgeoms = ngeoms;
85
0
  ret->geoms = geoms;
86
0
  ret->bbox = bbox;
87
88
0
  return ret;
89
0
}
90
91
LWCOLLECTION *
92
lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
93
0
{
94
0
  LWCOLLECTION *ret;
95
96
0
  if( ! lwtype_is_collection(type) )
97
0
  {
98
0
    lwerror("Non-collection type specified in collection constructor!");
99
0
    return NULL;
100
0
  }
101
102
0
  ret = lwalloc(sizeof(LWCOLLECTION));
103
0
  ret->type = type;
104
0
  ret->flags = lwflags(hasz,hasm,0);
105
0
  ret->srid = srid;
106
0
  ret->ngeoms = 0;
107
0
  ret->maxgeoms = 1; /* Allocate room for sub-members, just in case. */
108
0
  ret->geoms = lwalloc(ret->maxgeoms * sizeof(LWGEOM*));
109
0
  ret->bbox = NULL;
110
111
0
  return ret;
112
0
}
113
114
const LWGEOM *
115
lwcollection_getsubgeom(LWCOLLECTION *col, uint32_t gnum)
116
0
{
117
0
  return (const LWGEOM *)col->geoms[gnum];
118
0
}
119
120
/**
121
 * @brief Clone #LWCOLLECTION object. #POINTARRAY are not copied.
122
 *      Bbox is cloned if present in input.
123
 */
124
LWCOLLECTION *
125
lwcollection_clone(const LWCOLLECTION *g)
126
0
{
127
0
  uint32_t i;
128
0
  LWCOLLECTION *ret = lwalloc(sizeof(LWCOLLECTION));
129
0
  memcpy(ret, g, sizeof(LWCOLLECTION));
130
0
  if ( g->ngeoms > 0 )
131
0
  {
132
0
    ret->geoms = lwalloc(sizeof(LWGEOM *)*g->ngeoms);
133
0
    for (i=0; i<g->ngeoms; i++)
134
0
    {
135
0
      ret->geoms[i] = lwgeom_clone(g->geoms[i]);
136
0
    }
137
0
    if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
138
0
  }
139
0
  else
140
0
  {
141
0
    ret->bbox = NULL; /* empty collection */
142
0
    ret->geoms = NULL;
143
0
  }
144
0
  return ret;
145
0
}
146
147
/**
148
* @brief Deep clone #LWCOLLECTION object. #POINTARRAY are copied.
149
*/
150
LWCOLLECTION *
151
lwcollection_clone_deep(const LWCOLLECTION *g)
152
0
{
153
0
  uint32_t i;
154
0
  LWCOLLECTION *ret = lwalloc(sizeof(LWCOLLECTION));
155
0
  memcpy(ret, g, sizeof(LWCOLLECTION));
156
0
  if ( g->ngeoms > 0 )
157
0
  {
158
0
    ret->geoms = lwalloc(sizeof(LWGEOM *)*g->ngeoms);
159
0
    for (i=0; i<g->ngeoms; i++)
160
0
    {
161
0
      ret->geoms[i] = lwgeom_clone_deep(g->geoms[i]);
162
0
    }
163
0
    if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
164
0
  }
165
0
  else
166
0
  {
167
0
    ret->bbox = NULL; /* empty collection */
168
0
    ret->geoms = NULL;
169
0
  }
170
0
  return ret;
171
0
}
172
173
/**
174
 * Ensure the collection can hold up at least ngeoms
175
 */
176
void lwcollection_reserve(LWCOLLECTION *col, uint32_t ngeoms)
177
0
{
178
0
  if ( ngeoms <= col->maxgeoms ) return;
179
180
  /* Allocate more space if we need it */
181
0
  do { col->maxgeoms *= 2; } while ( col->maxgeoms < ngeoms );
182
0
  col->geoms = lwrealloc(col->geoms, sizeof(LWGEOM*) * col->maxgeoms);
183
0
}
184
185
/**
186
 * Return LW_TRUE if geom nests collections more than max_depth levels deep.
187
 * Bails out as soon as the limit is passed, so the recursion here is bounded
188
 * by max_depth.
189
 */
190
int
191
lwgeom_depth_exceeds(const LWGEOM *geom, uint32_t max_depth)
192
0
{
193
0
  const LWCOLLECTION *col;
194
0
  uint32_t i;
195
196
0
  if (!geom || !lwgeom_is_collection(geom))
197
0
    return LW_FALSE;
198
199
0
  if (!max_depth)
200
0
    return LW_TRUE;
201
202
0
  col = (const LWCOLLECTION *)geom;
203
0
  for (i = 0; i < col->ngeoms; i++)
204
0
  {
205
0
    if (lwgeom_depth_exceeds(col->geoms[i], max_depth - 1))
206
0
      return LW_TRUE;
207
0
  }
208
209
0
  return LW_FALSE;
210
0
}
211
212
/**
213
* Appends geom to the collection managed by col. Does not copy or
214
* clone, simply takes a reference on the passed geom.
215
*/
216
LWCOLLECTION* lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
217
0
{
218
0
  if (!col || !geom) return NULL;
219
220
0
  if (!col->geoms && (col->ngeoms || col->maxgeoms))
221
0
  {
222
0
    lwerror("Collection is in inconsistent state. Null memory but non-zero collection counts.");
223
0
    return NULL;
224
0
  }
225
226
  /* Check type compatibility */
227
0
  if ( ! lwcollection_allows_subtype(col->type, geom->type) ) {
228
0
    lwerror("%s cannot contain %s element", lwtype_name(col->type), lwtype_name(geom->type));
229
0
    return NULL;
230
0
  }
231
232
  /* Recursive readers and writers cannot walk arbitrarily deep nesting */
233
0
  if (lwgeom_depth_exceeds(geom, LW_PARSER_MAX_DEPTH - 1))
234
0
  {
235
0
    lwerror("Collection is nested more than %d levels deep", LW_PARSER_MAX_DEPTH);
236
0
    return NULL;
237
0
  }
238
239
  /* In case this is a truly empty, make some initial space  */
240
0
  if (!col->geoms)
241
0
  {
242
0
    col->maxgeoms = 2;
243
0
    col->ngeoms = 0;
244
0
    col->geoms = lwalloc(col->maxgeoms * sizeof(LWGEOM*));
245
0
  }
246
247
  /* Allocate more space if we need it */
248
0
  lwcollection_reserve(col, col->ngeoms + 1);
249
250
#ifndef NDEBUG
251
  /* See http://trac.osgeo.org/postgis/ticket/2933 */
252
  /* Make sure we don't already have a reference to this geom */
253
  {
254
    for (uint32_t i = 0; i < col->ngeoms; i++)
255
    {
256
      if (col->geoms[i] == geom)
257
      {
258
        lwerror("%s [%d] found duplicate geometry in collection", __FILE__, __LINE__);
259
        return col;
260
      }
261
    }
262
  }
263
#endif
264
265
0
  col->geoms[col->ngeoms] = (LWGEOM*)geom;
266
0
  col->ngeoms++;
267
0
  return col;
268
0
}
269
270
/**
271
 * Appends all geometries from col2 to col1 in place.
272
 * Caller is responsible to release col2.
273
 */
274
LWCOLLECTION *
275
lwcollection_concat_in_place(LWCOLLECTION *col1, const LWCOLLECTION *col2)
276
0
{
277
0
  uint32_t i;
278
0
  if (!col1 || !col2) return NULL;
279
0
  for (i = 0; i < col2->ngeoms; i++)
280
0
    col1 = lwcollection_add_lwgeom(col1, col2->geoms[i]);
281
0
  return col1;
282
0
}
283
284
LWCOLLECTION*
285
lwcollection_segmentize2d(const LWCOLLECTION* col, double dist)
286
0
{
287
0
  uint32_t i, j;
288
0
  LWGEOM** newgeoms;
289
290
0
  if (!col->ngeoms) return lwcollection_clone(col);
291
292
0
  newgeoms = lwalloc(sizeof(LWGEOM*) * col->ngeoms);
293
0
  for (i = 0; i < col->ngeoms; i++)
294
0
  {
295
0
    newgeoms[i] = lwgeom_segmentize2d(col->geoms[i], dist);
296
0
    if (!newgeoms[i])
297
0
    {
298
0
      for (j = 0; j < i; j++)
299
0
        lwgeom_free(newgeoms[j]);
300
0
      lwfree(newgeoms);
301
0
      return NULL;
302
0
    }
303
0
  }
304
305
0
  return lwcollection_construct(
306
0
      col->type, col->srid, NULL, col->ngeoms, newgeoms);
307
0
}
308
309
/** @brief check for same geometry composition
310
 *
311
 */
312
char
313
lwcollection_same(const LWCOLLECTION *c1, const LWCOLLECTION *c2)
314
0
{
315
0
  uint32_t i;
316
317
0
  LWDEBUG(2, "lwcollection_same called");
318
319
0
  if ( c1->type != c2->type ) return LW_FALSE;
320
0
  if ( c1->ngeoms != c2->ngeoms ) return LW_FALSE;
321
322
0
  for ( i = 0; i < c1->ngeoms; i++ )
323
0
  {
324
0
    if ( ! lwgeom_same(c1->geoms[i], c2->geoms[i]) )
325
0
      return LW_FALSE;
326
0
  }
327
328
  /* Former method allowed out-of-order equality between collections
329
330
    hit = lwalloc(sizeof(uint32_t)*c1->ngeoms);
331
    memset(hit, 0, sizeof(uint32_t)*c1->ngeoms);
332
333
    for (i=0; i<c1->ngeoms; i++)
334
    {
335
      char found=0;
336
      for (j=0; j<c2->ngeoms; j++)
337
      {
338
        if ( hit[j] ) continue;
339
        if ( lwgeom_same(c1->geoms[i], c2->geoms[j]) )
340
        {
341
          hit[j] = 1;
342
          found=1;
343
          break;
344
        }
345
      }
346
      if ( ! found ) return LW_FALSE;
347
    }
348
  */
349
350
0
  return LW_TRUE;
351
0
}
352
353
int lwcollection_ngeoms(const LWCOLLECTION *col)
354
0
{
355
0
  uint32_t i;
356
0
  int ngeoms = 0;
357
358
0
  if ( ! col )
359
0
  {
360
0
    lwerror("Null input geometry.");
361
0
    return 0;
362
0
  }
363
364
0
  for ( i = 0; i < col->ngeoms; i++ )
365
0
  {
366
0
    if ( col->geoms[i])
367
0
    {
368
0
      switch (col->geoms[i]->type)
369
0
      {
370
0
      case POINTTYPE:
371
0
      case LINETYPE:
372
0
      case CIRCSTRINGTYPE:
373
0
      case POLYGONTYPE:
374
0
        ngeoms += 1;
375
0
        break;
376
0
      case MULTIPOINTTYPE:
377
0
      case MULTILINETYPE:
378
0
      case MULTICURVETYPE:
379
0
      case MULTIPOLYGONTYPE:
380
0
        ngeoms += col->ngeoms;
381
0
        break;
382
0
      case COLLECTIONTYPE:
383
0
        ngeoms += lwcollection_ngeoms((LWCOLLECTION*)col->geoms[i]);
384
0
        break;
385
0
      }
386
0
    }
387
0
  }
388
0
  return ngeoms;
389
0
}
390
391
void lwcollection_free(LWCOLLECTION *col)
392
0
{
393
0
  uint32_t i;
394
0
  if ( ! col ) return;
395
396
0
  if ( col->bbox )
397
0
  {
398
0
    lwfree(col->bbox);
399
0
  }
400
0
  for ( i = 0; i < col->ngeoms; i++ )
401
0
  {
402
0
    LWDEBUGF(4,"freeing geom[%d]", i);
403
0
    if ( col->geoms && col->geoms[i] )
404
0
      lwgeom_free(col->geoms[i]);
405
0
  }
406
0
  if ( col->geoms )
407
0
  {
408
0
    lwfree(col->geoms);
409
0
  }
410
0
  lwfree(col);
411
0
}
412
413
/**
414
* Examines contents of collection and finds the largest coordinate
415
* dimension of all components. Areal > linear > puntal.
416
*/
417
static uint32_t
418
lwcollection_largest_dimension(const LWCOLLECTION *col)
419
0
{
420
0
  int largest_type = 0;
421
0
  size_t i;
422
0
  for (i = 0; i < col->ngeoms; i++)
423
0
  {
424
0
    LWGEOM *g = col->geoms[i];
425
0
    int gtype = lwgeom_get_type(g);
426
0
    if (lwgeom_is_collection(g))
427
0
    {
428
0
      gtype = lwcollection_largest_dimension((LWCOLLECTION*)g);
429
0
    }
430
431
0
    if (gtype == POINTTYPE || gtype == LINETYPE || gtype == POLYGONTYPE)
432
0
    {
433
0
      if (gtype > largest_type)
434
0
        largest_type = gtype;
435
0
    }
436
0
  }
437
0
  return largest_type;
438
0
}
439
440
441
static int
442
lwcollection_extract_recursive(const LWCOLLECTION* col, uint32_t type, LWCOLLECTION *col_out)
443
0
{
444
0
  size_t i;
445
0
  size_t geoms_added = 0;
446
447
0
  for (i = 0; i < col->ngeoms; i++)
448
0
  {
449
0
    LWGEOM *g = col->geoms[i];
450
0
    if (lwgeom_is_collection(g))
451
0
    {
452
0
      LWCOLLECTION *col_part = lwgeom_as_lwcollection(g);
453
0
      geoms_added += lwcollection_extract_recursive(col_part, type, col_out);
454
0
    }
455
456
0
    if (lwgeom_get_type(g) == type && !lwgeom_is_empty(g))
457
0
    {
458
0
      lwcollection_add_lwgeom(col_out, lwgeom_clone(col->geoms[i]));
459
0
      geoms_added++;
460
0
    }
461
0
  }
462
0
  return geoms_added;
463
0
}
464
465
LWCOLLECTION*
466
lwcollection_extract(const LWCOLLECTION* col, uint32_t type)
467
0
{
468
0
  LWCOLLECTION* outcol;
469
470
0
  if (!col) return NULL;
471
472
  /* Self-discover output type when it is not specified */
473
0
  if (!type)
474
0
    type = lwcollection_largest_dimension(col);
475
476
  /*
477
  * If self-discovery failed, there were no primitive points
478
  * lines or polygons in the collection, so send back an
479
  * empty collection.
480
  */
481
0
  if (!type)
482
0
  {
483
0
    return lwcollection_construct_empty(COLLECTIONTYPE,
484
0
             col->srid,
485
0
             FLAGS_GET_Z(col->flags),
486
0
             FLAGS_GET_M(col->flags));
487
0
  }
488
489
0
  if (!(type == POINTTYPE || type == LINETYPE || type == POLYGONTYPE))
490
0
  {
491
0
    lwerror(
492
0
        "Only POLYGON, LINESTRING and POINT are supported by "
493
0
        "lwcollection_extract. %s requested.",
494
0
        lwtype_name(type));
495
0
    return NULL;
496
0
  }
497
498
0
  outcol = lwcollection_construct_empty(lwtype_multitype(type),
499
0
             col->srid,
500
0
             FLAGS_GET_Z(col->flags),
501
0
             FLAGS_GET_M(col->flags));
502
503
0
  lwcollection_extract_recursive(col, type, outcol);
504
0
  lwgeom_add_bbox(lwcollection_as_lwgeom(outcol));
505
0
  return outcol;
506
0
}
507
508
509
LWCOLLECTION*
510
lwcollection_force_dims(const LWCOLLECTION *col, int hasz, int hasm, double zval, double mval)
511
0
{
512
0
  LWCOLLECTION *colout;
513
514
  /* Return 2D empty */
515
0
  if( lwcollection_is_empty(col) )
516
0
  {
517
0
    colout = lwcollection_construct_empty(col->type, col->srid, hasz, hasm);
518
0
  }
519
0
  else
520
0
  {
521
0
    uint32_t i;
522
0
    LWGEOM **geoms = NULL;
523
0
    geoms = lwalloc(sizeof(LWGEOM*) * col->ngeoms);
524
0
    for( i = 0; i < col->ngeoms; i++ )
525
0
    {
526
0
      geoms[i] = lwgeom_force_dims(col->geoms[i], hasz, hasm, zval, mval);
527
0
    }
528
0
    colout = lwcollection_construct(col->type, col->srid, NULL, col->ngeoms, geoms);
529
0
  }
530
0
  return colout;
531
0
}
532
533
534
uint32_t lwcollection_count_vertices(const LWCOLLECTION *col)
535
0
{
536
0
  uint32_t i = 0;
537
0
  uint32_t v = 0; /* vertices */
538
0
  assert(col);
539
0
  for ( i = 0; i < col->ngeoms; i++ )
540
0
  {
541
0
    v += lwgeom_count_vertices(col->geoms[i]);
542
0
  }
543
0
  return v;
544
0
}
545
546
547
int lwcollection_allows_subtype(int collectiontype, int subtype)
548
0
{
549
0
  if ( collectiontype == COLLECTIONTYPE )
550
0
    return LW_TRUE;
551
0
  if ( collectiontype == MULTIPOINTTYPE &&
552
0
          subtype == POINTTYPE )
553
0
    return LW_TRUE;
554
0
  if ( collectiontype == MULTILINETYPE &&
555
0
          subtype == LINETYPE )
556
0
    return LW_TRUE;
557
0
  if ( collectiontype == MULTIPOLYGONTYPE &&
558
0
          subtype == POLYGONTYPE )
559
0
    return LW_TRUE;
560
0
  if ( collectiontype == COMPOUNDTYPE &&
561
0
          (subtype == LINETYPE || subtype == CIRCSTRINGTYPE || subtype == NURBSCURVETYPE) )
562
0
    return LW_TRUE;
563
0
  if ( collectiontype == CURVEPOLYTYPE &&
564
0
          (subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE ||
565
0
           subtype == NURBSCURVETYPE) )
566
0
    return LW_TRUE;
567
0
  if ( collectiontype == MULTICURVETYPE &&
568
0
          (subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE ||
569
0
           subtype == NURBSCURVETYPE) )
570
0
    return LW_TRUE;
571
0
  if ( collectiontype == MULTISURFACETYPE &&
572
0
          (subtype == POLYGONTYPE || subtype == CURVEPOLYTYPE) )
573
0
    return LW_TRUE;
574
0
  if ( collectiontype == POLYHEDRALSURFACETYPE &&
575
0
          subtype == POLYGONTYPE )
576
0
    return LW_TRUE;
577
0
  if ( collectiontype == TINTYPE &&
578
0
          subtype == TRIANGLETYPE )
579
0
    return LW_TRUE;
580
581
  /* Must be a bad combination! */
582
0
  return LW_FALSE;
583
0
}
584
585
int
586
lwcollection_startpoint(const LWCOLLECTION* col, POINT4D* pt)
587
0
{
588
0
  if ( col->ngeoms < 1 )
589
0
    return LW_FAILURE;
590
591
0
  return lwgeom_startpoint(col->geoms[0], pt);
592
0
}