Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgis/liblwgeom/lwcompound.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
uint32_t
34
lwcompound_num_curves(const LWCOMPOUND *compound)
35
0
{
36
0
  if ( compound->type != COMPOUNDTYPE )
37
0
    lwerror("%s only supports compound curves", __func__);
38
0
  return compound->ngeoms;
39
0
}
40
41
const LWGEOM *
42
lwcollection_getsubcurve(const LWCOMPOUND *compound, uint32_t curvenum)
43
0
{
44
0
  return (const LWGEOM *)compound->geoms[curvenum];
45
0
}
46
47
static int
48
lwcompound_curve_endpoint(const LWGEOM *geom, POINT4D *pt)
49
0
{
50
0
  if (!geom || lwgeom_is_empty(geom))
51
0
    return LW_FAILURE;
52
53
0
  if (geom->type == NURBSCURVETYPE)
54
0
  {
55
0
    LWPOINT *endpoint = lwnurbscurve_evaluate((const LWNURBSCURVE*)geom, 1.0);
56
0
    int rv = LW_FAILURE;
57
0
    if (endpoint && endpoint->point && endpoint->point->npoints > 0)
58
0
      rv = getPoint4d_p(endpoint->point, 0, pt) ? LW_SUCCESS : LW_FAILURE;
59
0
    lwpoint_free(endpoint);
60
0
    return rv;
61
0
  }
62
63
0
  if (geom->type == LINETYPE || geom->type == CIRCSTRINGTYPE)
64
0
  {
65
0
    const LWLINE *line = (const LWLINE *)geom;
66
0
    if (!line->points || line->points->npoints < 1)
67
0
      return LW_FAILURE;
68
0
    return getPoint4d_p(line->points, line->points->npoints - 1, pt) ? LW_SUCCESS : LW_FAILURE;
69
0
  }
70
71
0
  return LW_FAILURE;
72
0
}
73
74
static int
75
lwcompound_points_equal(const POINT4D *a, const POINT4D *b, int hasz)
76
0
{
77
0
  if (!FP_EQUALS(a->x, b->x) || !FP_EQUALS(a->y, b->y))
78
0
    return LW_FALSE;
79
80
0
  if (hasz && !FP_EQUALS(a->z, b->z))
81
0
    return LW_FALSE;
82
83
0
  return LW_TRUE;
84
0
}
85
86
int
87
lwcompound_is_closed(const LWCOMPOUND *compound)
88
0
{
89
0
  POINT4D pt_start, pt_end;
90
91
0
  int hasz = lwgeom_has_z(lwcompound_as_lwgeom(compound));
92
0
  if (lwgeom_is_empty(lwcompound_as_lwgeom(compound)))
93
0
    return LW_FALSE;
94
95
  /* Single entry, closes on itself */
96
0
  if (compound->ngeoms == 1 && lwgeom_is_closed(compound->geoms[0]))
97
0
      return LW_TRUE;
98
99
  /* If internal connectivity is lacking, so is closure */
100
0
  if (!lwcompound_is_valid(compound))
101
0
    return LW_FALSE;
102
103
0
  if (lwgeom_startpoint(compound->geoms[0], &pt_start) == LW_FAILURE ||
104
0
      lwcompound_curve_endpoint(compound->geoms[compound->ngeoms-1], &pt_end) == LW_FAILURE)
105
0
    return LW_FALSE;
106
107
0
  return lwcompound_points_equal(&pt_start, &pt_end, hasz);
108
0
}
109
110
int
111
lwcompound_is_valid(const LWCOMPOUND *compound)
112
0
{
113
0
  int hasz = lwgeom_has_z(lwcompound_as_lwgeom(compound));
114
0
  if (lwgeom_is_empty(lwcompound_as_lwgeom(compound)))
115
0
    return LW_TRUE;
116
117
  /* Only one component, do not need to test connectivity */
118
0
  if (compound->ngeoms == 1)
119
0
    return LW_TRUE;
120
121
  /* Check internal connectivity between components */
122
0
  for (uint32_t i = 1; i < compound->ngeoms; i++)
123
0
  {
124
0
    POINT4D pt_start, pt_end;
125
0
    const LWGEOM *geom_start = compound->geoms[i];
126
0
    const LWGEOM *geom_end = compound->geoms[i-1];
127
128
    /* Empty cannot be a compound component, because it joins nothing */
129
0
    if (lwgeom_is_empty(geom_start) || lwgeom_is_empty(geom_end))
130
0
      return LW_FALSE;
131
132
0
    if (lwgeom_startpoint(geom_start, &pt_start) == LW_FAILURE ||
133
0
        lwcompound_curve_endpoint(geom_end, &pt_end) == LW_FAILURE)
134
0
      return LW_FALSE;
135
136
0
    if (!lwcompound_points_equal(&pt_start, &pt_end, hasz))
137
0
      return LW_FALSE;
138
0
  }
139
140
0
  return LW_TRUE;
141
0
}
142
143
double lwcompound_length(const LWCOMPOUND *comp)
144
0
{
145
0
  return lwcompound_length_2d(comp);
146
0
}
147
148
double lwcompound_length_2d(const LWCOMPOUND *comp)
149
0
{
150
0
  uint32_t i;
151
0
  double length = 0.0;
152
0
  if ( lwgeom_is_empty((LWGEOM*)comp) )
153
0
    return 0.0;
154
155
0
  for (i = 0; i < comp->ngeoms; i++)
156
0
  {
157
0
    length += lwgeom_length_2d(comp->geoms[i]);
158
0
  }
159
0
  return length;
160
0
}
161
162
int lwcompound_add_lwgeom(LWCOMPOUND *comp, LWGEOM *geom)
163
0
{
164
0
  LWCOLLECTION *col = (LWCOLLECTION*)comp;
165
166
  /* Empty things can't continuously join up with other things */
167
0
  if ( lwgeom_is_empty(geom) )
168
0
  {
169
0
    LWDEBUG(4, "Got an empty component for a compound curve!");
170
0
    return LW_FAILURE;
171
0
  }
172
173
0
  if( col->ngeoms > 0 )
174
0
  {
175
0
    POINT4D last, first;
176
0
    if (lwgeom_startpoint(geom, &first) == LW_FAILURE ||
177
0
        lwcompound_curve_endpoint(col->geoms[col->ngeoms - 1], &last) == LW_FAILURE)
178
0
      return LW_FAILURE;
179
180
0
    if ( !(FP_EQUALS(first.x,last.x) && FP_EQUALS(first.y,last.y)) )
181
0
    {
182
0
      LWDEBUG(4, "Components don't join up end-to-end!");
183
0
      LWDEBUGF(4, "first pt (%g %g %g %g) last pt (%g %g %g %g)", first.x, first.y, first.z, first.m, last.x, last.y, last.z, last.m);
184
0
      return LW_FAILURE;
185
0
    }
186
0
  }
187
188
0
  col = lwcollection_add_lwgeom(col, geom);
189
0
  return LW_SUCCESS;
190
0
}
191
192
LWCOMPOUND *
193
lwcompound_construct_empty(int32_t srid, char hasz, char hasm)
194
0
{
195
0
  LWCOMPOUND *ret = (LWCOMPOUND*)lwcollection_construct_empty(COMPOUNDTYPE, srid, hasz, hasm);
196
0
  return ret;
197
0
}
198
199
int lwgeom_contains_point(const LWGEOM *geom, const POINT2D *pt)
200
0
{
201
0
  switch( geom->type )
202
0
  {
203
0
    case LINETYPE:
204
0
      return ptarray_contains_point(((LWLINE*)geom)->points, pt);
205
0
    case CIRCSTRINGTYPE:
206
0
      return ptarrayarc_contains_point(((LWCIRCSTRING*)geom)->points, pt);
207
0
    case COMPOUNDTYPE:
208
0
      return lwcompound_contains_point((LWCOMPOUND*)geom, pt);
209
0
    case NURBSCURVETYPE:
210
0
    {
211
0
      LWLINE *lwline = lwnurbscurve_to_linestring((LWNURBSCURVE*)geom, 32);
212
0
      if (!lwline)
213
0
      {
214
0
        lwerror("lwgeom_contains_point failed to linearize NURBS curve");
215
0
        return LW_OUTSIDE;
216
0
      }
217
0
      int result = ptarray_contains_point(lwline->points, pt);
218
0
      lwline_free(lwline);
219
0
      return result;
220
0
    }
221
0
  }
222
0
  lwerror("lwgeom_contains_point failed");
223
0
  return LW_OUTSIDE;
224
0
}
225
226
/*
227
 * Use a ray-casting count to determine if the point
228
 * is inside or outside of the compound curve. Ray-casting
229
 * is run against each component of the overall arc, and
230
 * the even/odd test run against the total of all components.
231
 * Returns LW_INSIDE / LW_BOUNDARY / LW_OUTSIDE
232
 */
233
int
234
lwcompound_contains_point(const LWCOMPOUND *comp, const POINT2D *pt)
235
0
{
236
0
  int intersections = 0;
237
238
0
  if (lwgeom_is_empty(lwcompound_as_lwgeom(comp)))
239
0
    return LW_OUTSIDE;
240
241
0
  for (uint32_t j = 0; j < comp->ngeoms; j++)
242
0
  {
243
0
    int on_boundary = LW_FALSE;
244
0
    const LWGEOM *sub = comp->geoms[j];
245
0
    if (sub->type == LINETYPE)
246
0
    {
247
0
      LWLINE *lwline = lwgeom_as_lwline(sub);
248
0
      intersections += ptarray_raycast_intersections(lwline->points, pt, &on_boundary);
249
0
    }
250
0
    else if (sub->type == CIRCSTRINGTYPE)
251
0
    {
252
0
      LWCIRCSTRING *lwcirc = lwgeom_as_lwcircstring(sub);
253
0
      intersections += ptarrayarc_raycast_intersections(lwcirc->points, pt, &on_boundary);
254
0
    }
255
0
    else if (sub->type == NURBSCURVETYPE)
256
0
    {
257
0
      LWLINE *lwline = lwnurbscurve_to_linestring((LWNURBSCURVE*)sub, 32);
258
0
      intersections += ptarray_raycast_intersections(lwline->points, pt, &on_boundary);
259
0
      lwline_free(lwline);
260
0
    }
261
0
    else
262
0
    {
263
0
      lwerror("%s: unsupported type %s", __func__, lwtype_name(sub->type));
264
0
    }
265
0
    if (on_boundary)
266
0
      return LW_BOUNDARY;
267
0
  }
268
269
  /*
270
   * Odd number of intersections means inside.
271
   * Even means outside.
272
   */
273
0
  return (intersections % 2) ? LW_INSIDE : LW_OUTSIDE;
274
0
}
275
276
LWCOMPOUND *
277
lwcompound_construct_from_lwline(const LWLINE *lwline)
278
0
{
279
0
  LWCOMPOUND* ogeom = lwcompound_construct_empty(lwline->srid, FLAGS_GET_Z(lwline->flags), FLAGS_GET_M(lwline->flags));
280
0
  lwcompound_add_lwgeom(ogeom, lwgeom_clone((LWGEOM*)lwline));
281
  /* ogeom->bbox = lwline->bbox; */
282
0
  return ogeom;
283
0
}
284
285
LWPOINT*
286
lwcompound_get_lwpoint(const LWCOMPOUND *lwcmp, uint32_t where)
287
0
{
288
0
  uint32_t i;
289
0
  uint32_t count = 0;
290
0
  uint32_t npoints = 0;
291
0
  if ( lwgeom_is_empty((LWGEOM*)lwcmp) )
292
0
    return NULL;
293
294
0
  npoints = lwgeom_count_vertices((LWGEOM*)lwcmp);
295
0
  if ( where >= npoints )
296
0
  {
297
0
    lwerror("%s: index %d is not in range of number of vertices (%d) in input", __func__, where, npoints);
298
0
    return NULL;
299
0
  }
300
301
0
  for ( i = 0; i < lwcmp->ngeoms; i++ )
302
0
  {
303
0
    LWGEOM* part = lwcmp->geoms[i];
304
0
    uint32_t npoints_part = lwgeom_count_vertices(part);
305
0
    if ( where >= count && where < count + npoints_part )
306
0
    {
307
0
      if (part->type == NURBSCURVETYPE)
308
0
      {
309
0
        const LWNURBSCURVE *curve = (const LWNURBSCURVE*)part;
310
0
        POINTARRAY *pa;
311
0
        POINT4D pt;
312
313
0
        if (!curve->points || where - count >= curve->points->npoints)
314
0
          return NULL;
315
316
0
        pa = ptarray_construct_empty(FLAGS_GET_Z(curve->flags), FLAGS_GET_M(curve->flags), 1);
317
0
        pt = getPoint4d(curve->points, where - count);
318
0
        ptarray_append_point(pa, &pt, LW_TRUE);
319
0
        return lwpoint_construct(curve->srid, NULL, pa);
320
0
      }
321
0
      return lwline_get_lwpoint((LWLINE*)part, where - count);
322
0
    }
323
0
    else
324
0
    {
325
0
      count += npoints_part;
326
0
    }
327
0
  }
328
329
0
  return NULL;
330
0
}
331
332
333
334
LWPOINT *
335
lwcompound_get_startpoint(const LWCOMPOUND *lwcmp)
336
0
{
337
0
  if ( lwcmp->ngeoms < 1 )
338
0
    return NULL;
339
340
0
  if ( lwcmp->geoms[0]->type == NURBSCURVETYPE )
341
0
    return lwnurbscurve_evaluate((const LWNURBSCURVE*)lwcmp->geoms[0], 0.0);
342
343
0
  return lwcompound_get_lwpoint(lwcmp, 0);
344
0
}
345
346
LWPOINT *
347
lwcompound_get_endpoint(const LWCOMPOUND *lwcmp)
348
0
{
349
0
  LWGEOM *last;
350
0
  LWLINE *lwline;
351
0
  if ( lwcmp->ngeoms < 1 )
352
0
  {
353
0
    return NULL;
354
0
  }
355
356
0
  last = lwcmp->geoms[lwcmp->ngeoms-1];
357
0
  if ( last->type == NURBSCURVETYPE )
358
0
    return lwnurbscurve_evaluate((const LWNURBSCURVE*)last, 1.0);
359
360
0
  lwline = (LWLINE*)last;
361
362
0
  if ( (!lwline) || (!lwline->points) || (lwline->points->npoints < 1) )
363
0
  {
364
0
    return NULL;
365
0
  }
366
367
0
  return lwline_get_lwpoint(lwline, lwline->points->npoints-1);
368
0
}