Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgis/liblwgeom/lwpoly.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) 2012 Sandro Santilli <strk@kbt.io>
22
 * Copyright (C) 2001-2006 Refractions Research Inc.
23
 * Copyright (C) 2026 Darafei Praliaskouski <me@komzpa.net>
24
 *
25
 **********************************************************************/
26
27
/* basic LWPOLY manipulation */
28
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <math.h>
33
#include "liblwgeom_internal.h"
34
#include "lwgeom_log.h"
35
36
37
#define CHECK_POLY_RINGS_ZM 1
38
39
/* construct a new LWPOLY.  arrays (points/points per ring) will NOT be copied
40
 * use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
41
 */
42
LWPOLY *
43
lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
44
0
{
45
0
  LWPOLY *result;
46
0
  int hasz, hasm;
47
0
#ifdef CHECK_POLY_RINGS_ZM
48
0
  char zm;
49
0
  uint32_t i;
50
0
#endif
51
52
0
  if ( nrings < 1 ) lwerror("lwpoly_construct: need at least 1 ring");
53
54
0
  hasz = FLAGS_GET_Z(points[0]->flags);
55
0
  hasm = FLAGS_GET_M(points[0]->flags);
56
57
0
#ifdef CHECK_POLY_RINGS_ZM
58
0
  zm = FLAGS_GET_ZM(points[0]->flags);
59
0
  for (i=1; i<nrings; i++)
60
0
  {
61
0
    if ( zm != FLAGS_GET_ZM(points[i]->flags) )
62
0
      lwerror("lwpoly_construct: mixed dimensioned rings");
63
0
  }
64
0
#endif
65
66
0
  result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
67
0
  result->type = POLYGONTYPE;
68
0
  result->flags = lwflags(hasz, hasm, 0);
69
0
  FLAGS_SET_BBOX(result->flags, bbox?1:0);
70
0
  result->srid = srid;
71
0
  result->nrings = nrings;
72
0
  result->maxrings = nrings;
73
0
  result->rings = points;
74
0
  result->bbox = bbox;
75
76
0
  return result;
77
0
}
78
79
LWPOLY*
80
lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2,
81
    POINT4D *p3, POINT4D *p4)
82
0
{
83
0
  POINTARRAY *pa = ptarray_construct_empty(hasz, hasm, 5);
84
0
  LWPOLY *lwpoly = lwpoly_construct_empty(SRID_UNKNOWN, hasz, hasm);
85
86
0
  ptarray_append_point(pa, p1, LW_TRUE);
87
0
  ptarray_append_point(pa, p2, LW_TRUE);
88
0
  ptarray_append_point(pa, p3, LW_TRUE);
89
0
  ptarray_append_point(pa, p4, LW_TRUE);
90
0
  ptarray_append_point(pa, p1, LW_TRUE);
91
92
0
  lwpoly_add_ring(lwpoly, pa);
93
94
0
  return lwpoly;
95
0
}
96
97
LWPOLY *
98
lwpoly_construct_envelope(int32_t srid, double x1, double y1, double x2, double y2)
99
0
{
100
0
  POINT4D p1, p2, p3, p4;
101
0
  LWPOLY *poly;
102
103
0
  p1.x = x1;
104
0
  p1.y = y1;
105
0
  p2.x = x1;
106
0
  p2.y = y2;
107
0
  p3.x = x2;
108
0
  p3.y = y2;
109
0
  p4.x = x2;
110
0
  p4.y = y1;
111
112
0
  poly = lwpoly_construct_rectangle(0, 0, &p1, &p2, &p3, &p4);
113
0
  lwgeom_set_srid(lwpoly_as_lwgeom(poly), srid);
114
0
  lwgeom_add_bbox(lwpoly_as_lwgeom(poly));
115
116
0
  return poly;
117
0
}
118
119
LWPOLY *
120
lwpoly_construct_circle(int32_t srid, double x, double y, double radius, uint32_t segments_per_quarter, char exterior)
121
0
{
122
0
  const uint32_t segments = 4*segments_per_quarter;
123
0
  double theta;
124
0
  LWPOLY *lwpoly;
125
0
  POINTARRAY *pa;
126
0
  POINT4D pt;
127
0
  uint32_t i;
128
129
0
  if (segments_per_quarter == 0)
130
0
  {
131
0
    lwerror("Need at least one segment per quarter-circle.");
132
0
    return NULL;
133
0
  }
134
135
0
  if (radius < 0)
136
0
  {
137
0
    lwerror("Radius must be positive.");
138
0
    return NULL;
139
0
  }
140
141
0
  theta = 2*M_PI / segments;
142
143
0
  lwpoly = lwpoly_construct_empty(srid, LW_FALSE, LW_FALSE);
144
0
  pa = ptarray_construct_empty(LW_FALSE, LW_FALSE, segments + 1);
145
146
0
  if (exterior)
147
0
    radius *= sqrt(1 + pow(tan(theta/2), 2));
148
149
0
  for (i = 0; i < segments; i++)
150
0
  {
151
0
    pt.x = x + radius*sin(i * theta);
152
0
    pt.y = y + radius*cos(i * theta);
153
0
    ptarray_append_point(pa, &pt, LW_TRUE);
154
0
  }
155
0
  getPoint4d_p(pa, 0, &pt);
156
0
  ptarray_append_point(pa, &pt, LW_TRUE);
157
158
0
  lwpoly_add_ring(lwpoly, pa);
159
0
  return lwpoly;
160
0
}
161
162
LWPOLY *
163
lwpoly_construct_empty(int32_t srid, char hasz, char hasm)
164
104k
{
165
104k
  LWPOLY *result = lwalloc(sizeof(LWPOLY));
166
104k
  result->type = POLYGONTYPE;
167
104k
  result->flags = lwflags(hasz,hasm,0);
168
104k
  result->srid = srid;
169
104k
  result->nrings = 0;
170
104k
  result->maxrings = 1; /* Allocate room for ring, just in case. */
171
104k
  result->rings = lwalloc(result->maxrings * sizeof(POINTARRAY*));
172
104k
  result->bbox = NULL;
173
104k
  return result;
174
104k
}
175
176
void
177
lwpoly_free(LWPOLY* poly)
178
68.2k
{
179
68.2k
  uint32_t t;
180
181
68.2k
  if (!poly) return;
182
183
68.2k
  if (poly->bbox) lwfree(poly->bbox);
184
185
68.2k
  if ( poly->rings )
186
68.2k
  {
187
110k
    for (t = 0; t < poly->nrings; t++)
188
42.4k
      if (poly->rings[t]) ptarray_free(poly->rings[t]);
189
68.2k
    lwfree(poly->rings);
190
68.2k
  }
191
192
68.2k
  lwfree(poly);
193
68.2k
}
194
195
void printLWPOLY(LWPOLY *poly)
196
0
{
197
0
  uint32_t t;
198
0
  lwnotice("LWPOLY {");
199
0
  lwnotice("    ndims = %i", (int)FLAGS_NDIMS(poly->flags));
200
0
  lwnotice("    SRID = %i", (int)poly->srid);
201
0
  lwnotice("    nrings = %i", (int)poly->nrings);
202
0
  for (t=0; t<poly->nrings; t++)
203
0
  {
204
0
    lwnotice("    RING # %i :",t);
205
0
    printPA(poly->rings[t]);
206
0
  }
207
0
  lwnotice("}");
208
0
}
209
210
/* @brief Clone LWLINE object. Serialized point lists are not copied.
211
 *
212
 * @see ptarray_clone
213
 */
214
LWPOLY *
215
lwpoly_clone(const LWPOLY *g)
216
0
{
217
0
  uint32_t i;
218
0
  LWPOLY *ret = lwalloc(sizeof(LWPOLY));
219
0
  memcpy(ret, g, sizeof(LWPOLY));
220
0
  ret->rings = lwalloc(sizeof(POINTARRAY *)*g->nrings);
221
0
  for ( i = 0; i < g->nrings; i++ ) {
222
0
    ret->rings[i] = ptarray_clone(g->rings[i]);
223
0
  }
224
0
  if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
225
0
  return ret;
226
0
}
227
228
/* Deep clone LWPOLY object. POINTARRAY are copied, as is ring array */
229
LWPOLY *
230
lwpoly_clone_deep(const LWPOLY *g)
231
0
{
232
0
  uint32_t i;
233
0
  LWPOLY *ret = lwalloc(sizeof(LWPOLY));
234
0
  memcpy(ret, g, sizeof(LWPOLY));
235
0
  if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
236
0
  ret->rings = lwalloc(sizeof(POINTARRAY *)*g->nrings);
237
0
  for ( i = 0; i < ret->nrings; i++ )
238
0
  {
239
0
    ret->rings[i] = ptarray_clone_deep(g->rings[i]);
240
0
  }
241
0
  FLAGS_SET_READONLY(ret->flags,0);
242
0
  return ret;
243
0
}
244
245
/**
246
* Add a ring to a polygon. Point array will be referenced, not copied.
247
*/
248
int
249
lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
250
56.7k
{
251
56.7k
  if( ! poly || ! pa )
252
0
    return LW_FAILURE;
253
254
  /* We have used up our storage, add some more. */
255
56.7k
  if( poly->nrings >= poly->maxrings )
256
1.34k
  {
257
1.34k
    size_t new_maxrings, ring_array_size;
258
1.34k
    if (!lw_size_add((size_t)poly->nrings, 1, &new_maxrings) ||
259
1.34k
        !lw_size_mul(new_maxrings, 2, &new_maxrings) || new_maxrings > UINT32_MAX ||
260
1.34k
        !lw_size_mul(new_maxrings, sizeof(POINTARRAY *), &ring_array_size) ||
261
1.34k
        ring_array_size > LW_MAX_ALLOC_SIZE)
262
0
    {
263
0
      lwerror("polygon ring array exceeds the maximum allocation size");
264
0
      return LW_FAILURE;
265
0
    }
266
1.34k
    poly->rings = lwrealloc(poly->rings, ring_array_size);
267
1.34k
    poly->maxrings = (uint32_t)new_maxrings;
268
1.34k
  }
269
270
  /* Add the new ring entry. */
271
56.7k
  poly->rings[poly->nrings] = pa;
272
56.7k
  poly->nrings++;
273
274
56.7k
  return LW_SUCCESS;
275
56.7k
}
276
277
void
278
lwpoly_force_orientation(LWPOLY *poly, int orientation)
279
0
{
280
  /* No-op no orientation */
281
0
  if (orientation == LW_NONE) return;
282
283
  /* No-op empties */
284
0
  if (lwpoly_is_empty(poly)) return;
285
286
  /* External ring */
287
0
  if (!ptarray_has_orientation(poly->rings[0], orientation))
288
0
    ptarray_reverse_in_place(poly->rings[0]);
289
290
  /* Internal rings must run opposite to external */
291
0
  for (uint32_t i = 1; i < poly->nrings; i++)
292
0
    if (!ptarray_has_orientation(poly->rings[i], -1 * orientation))
293
0
      ptarray_reverse_in_place(poly->rings[i]);
294
0
}
295
296
int
297
lwpoly_has_orientation(const LWPOLY *poly, int orientation)
298
0
{
299
0
  if (lwpoly_is_empty(poly))
300
0
    return LW_TRUE;
301
302
  /* Exterior ring matches orientation? */
303
0
  if(!ptarray_has_orientation(poly->rings[0], orientation))
304
0
    return LW_FALSE;
305
306
  /* Interior rings are opposite of orientation? */
307
0
  for (uint32_t i = 1; i < poly->nrings; i++)
308
0
    if (!ptarray_has_orientation(poly->rings[i], -1 * orientation))
309
0
      return LW_FALSE;
310
311
0
  return LW_TRUE;
312
0
}
313
314
void
315
lwpoly_release(LWPOLY *lwpoly)
316
0
{
317
0
  lwgeom_release(lwpoly_as_lwgeom(lwpoly));
318
0
}
319
320
LWPOLY *
321
lwpoly_segmentize2d(const LWPOLY *poly, double dist)
322
0
{
323
0
  POINTARRAY **newrings;
324
0
  uint32_t i;
325
326
0
  newrings = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
327
0
  for (i=0; i<poly->nrings; i++)
328
0
  {
329
0
    newrings[i] = ptarray_segmentize2d(poly->rings[i], dist);
330
0
    if ( ! newrings[i] )
331
0
    {
332
0
      uint32_t j = 0;
333
0
      for (j = 0; j < i; j++)
334
0
        ptarray_free(newrings[j]);
335
0
      lwfree(newrings);
336
0
      return NULL;
337
0
    }
338
0
  }
339
0
  return lwpoly_construct(poly->srid, NULL,
340
0
                          poly->nrings, newrings);
341
0
}
342
343
/*
344
 * check coordinate equality
345
 * ring and coordinate order is considered
346
 */
347
char
348
lwpoly_same(const LWPOLY *p1, const LWPOLY *p2)
349
0
{
350
0
  uint32_t i;
351
352
0
  if ( p1->nrings != p2->nrings ) return 0;
353
0
  for (i=0; i<p1->nrings; i++)
354
0
  {
355
0
    if ( ! ptarray_same(p1->rings[i], p2->rings[i]) )
356
0
      return 0;
357
0
  }
358
0
  return 1;
359
0
}
360
361
/*
362
 * Construct a polygon from a LWLINE being
363
 * the shell and an array of LWLINE (possibly NULL) being holes.
364
 * Pointarrays from input geoms are cloned.
365
 * SRID must be the same for each input line.
366
 * Input lines must have at least 4 points, and be closed.
367
 */
368
LWPOLY *
369
lwpoly_from_lwlines(const LWLINE *shell,
370
                    uint32_t nholes, const LWLINE **holes)
371
0
{
372
0
  uint32_t nrings;
373
0
  POINTARRAY **rings = lwalloc((nholes+1)*sizeof(POINTARRAY *));
374
0
  int32_t srid = shell->srid;
375
0
  LWPOLY *ret;
376
377
0
  if ( shell->points->npoints < 4 )
378
0
    lwerror("lwpoly_from_lwlines: shell must have at least 4 points");
379
0
  if ( ! ptarray_is_closed_2d(shell->points) )
380
0
    lwerror("lwpoly_from_lwlines: shell must be closed");
381
0
  rings[0] = ptarray_clone_deep(shell->points);
382
383
0
  for (nrings=1; nrings<=nholes; nrings++)
384
0
  {
385
0
    const LWLINE *hole = holes[nrings-1];
386
387
0
    if ( hole->srid != srid )
388
0
      lwerror("lwpoly_from_lwlines: mixed SRIDs in input lines");
389
390
0
    if ( hole->points->npoints < 4 )
391
0
      lwerror("lwpoly_from_lwlines: holes must have at least 4 points");
392
0
    if ( ! ptarray_is_closed_2d(hole->points) )
393
0
      lwerror("lwpoly_from_lwlines: holes must be closed");
394
395
0
    rings[nrings] = ptarray_clone_deep(hole->points);
396
0
  }
397
398
0
  ret = lwpoly_construct(srid, NULL, nrings, rings);
399
0
  return ret;
400
0
}
401
402
LWPOLY*
403
lwpoly_force_dims(const LWPOLY *poly, int hasz, int hasm, double zval, double mval)
404
0
{
405
0
  LWPOLY *polyout;
406
407
  /* Return 2D empty */
408
0
  if( lwpoly_is_empty(poly) )
409
0
  {
410
0
    polyout = lwpoly_construct_empty(poly->srid, hasz, hasm);
411
0
  }
412
0
  else
413
0
  {
414
0
    POINTARRAY **rings = NULL;
415
0
    uint32_t i;
416
0
    rings = lwalloc(sizeof(POINTARRAY*) * poly->nrings);
417
0
    for( i = 0; i < poly->nrings; i++ )
418
0
    {
419
0
      rings[i] = ptarray_force_dims(poly->rings[i], hasz, hasm, zval, mval);
420
0
    }
421
0
    polyout = lwpoly_construct(poly->srid, NULL, poly->nrings, rings);
422
0
  }
423
0
  polyout->type = poly->type;
424
0
  return polyout;
425
0
}
426
427
uint32_t lwpoly_count_vertices(const LWPOLY *poly)
428
0
{
429
0
  uint32_t i = 0;
430
0
  uint32_t v = 0; /* vertices */
431
0
  assert(poly);
432
0
  for ( i = 0; i < poly->nrings; i ++ )
433
0
  {
434
0
    v += poly->rings[i]->npoints;
435
0
  }
436
0
  return v;
437
0
}
438
439
/**
440
* Find the area of the outer ring - sum (area of inner rings).
441
*/
442
double
443
lwpoly_area(const LWPOLY *poly)
444
0
{
445
0
  double poly_area = 0.0;
446
0
  uint32_t i;
447
448
0
  if ( ! poly )
449
0
    lwerror("lwpoly_area called with null polygon pointer!");
450
451
0
  for ( i=0; i < poly->nrings; i++ )
452
0
  {
453
0
    POINTARRAY *ring = poly->rings[i];
454
0
    double ringarea = 0.0;
455
456
    /* Empty or messed-up ring. */
457
0
    if ( ring->npoints < 3 )
458
0
      continue;
459
460
0
    ringarea = fabs(ptarray_signed_area(ring));
461
0
    if ( i == 0 ) /* Outer ring, positive area! */
462
0
      poly_area += ringarea;
463
0
    else /* Inner ring, negative area! */
464
0
      poly_area -= ringarea;
465
0
  }
466
467
0
  return poly_area;
468
0
}
469
470
471
/**
472
 * Compute the sum of polygon rings length.
473
 * Could use a more numerically stable calculator...
474
 */
475
double
476
lwpoly_perimeter(const LWPOLY *poly)
477
0
{
478
0
  double result=0.0;
479
0
  uint32_t i;
480
481
0
  LWDEBUGF(2, "in lwgeom_polygon_perimeter (%d rings)", poly->nrings);
482
483
0
  for (i=0; i<poly->nrings; i++)
484
0
    result += ptarray_length(poly->rings[i]);
485
486
0
  return result;
487
0
}
488
489
/**
490
 * Compute the sum of polygon rings length (forcing 2d computation).
491
 * Could use a more numerically stable calculator...
492
 */
493
double
494
lwpoly_perimeter_2d(const LWPOLY *poly)
495
0
{
496
0
  double result=0.0;
497
0
  uint32_t i;
498
499
0
  LWDEBUGF(2, "in lwgeom_polygon_perimeter (%d rings)", poly->nrings);
500
501
0
  for (i=0; i<poly->nrings; i++)
502
0
    result += ptarray_length_2d(poly->rings[i]);
503
504
0
  return result;
505
0
}
506
507
int
508
lwpoly_is_closed(const LWPOLY *poly)
509
0
{
510
0
  uint32_t i = 0;
511
512
0
  if ( poly->nrings == 0 )
513
0
    return LW_TRUE;
514
515
0
  for ( i = 0; i < poly->nrings; i++ )
516
0
  {
517
0
    if (FLAGS_GET_Z(poly->flags))
518
0
    {
519
0
      if ( ! ptarray_is_closed_3d(poly->rings[i]) )
520
0
        return LW_FALSE;
521
0
    }
522
0
    else
523
0
    {
524
0
      if ( ! ptarray_is_closed_2d(poly->rings[i]) )
525
0
        return LW_FALSE;
526
0
    }
527
0
  }
528
529
0
  return LW_TRUE;
530
0
}
531
532
int
533
lwpoly_startpoint(const LWPOLY* poly, POINT4D* pt)
534
0
{
535
0
  if ( poly->nrings < 1 )
536
0
    return LW_FAILURE;
537
0
  return ptarray_startpoint(poly->rings[0], pt);
538
0
}
539
540
int
541
lwpoly_contains_point(const LWPOLY *poly, const POINT2D *pt)
542
0
{
543
0
  uint32_t i;
544
0
  int t;
545
546
0
  if ( lwpoly_is_empty(poly) )
547
0
    return LW_OUTSIDE;
548
549
0
  t = ptarray_contains_point(poly->rings[0], pt);
550
551
0
  if (t == LW_INSIDE)
552
0
  {
553
0
    for (i = 1; i < poly->nrings; i++)
554
0
    {
555
0
      t = ptarray_contains_point(poly->rings[i], pt);
556
0
      if (t == LW_INSIDE)
557
0
        return LW_OUTSIDE;
558
0
      if (t == LW_BOUNDARY)
559
0
      {
560
0
        return LW_BOUNDARY;
561
0
      }
562
0
    }
563
0
    return LW_INSIDE;
564
0
  }
565
0
  else
566
0
    return t;
567
0
}