forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwcollection.c
597 lines (518 loc) · 13.9 KB
/
lwcollection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2001-2006 Refractions Research Inc.
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
#define CHECK_LWGEOM_ZM 1
void
lwcollection_release(LWCOLLECTION *lwcollection)
{
lwgeom_release(lwcollection_as_lwgeom(lwcollection));
}
LWCOLLECTION *
lwcollection_construct(uint8_t type, int srid, GBOX *bbox,
uint32_t ngeoms, LWGEOM **geoms)
{
LWCOLLECTION *ret;
int hasz, hasm;
#ifdef CHECK_LWGEOM_ZM
char zm;
uint32_t i;
#endif
LWDEBUGF(2, "lwcollection_construct called with %d, %d, %p, %d, %p.", type, srid, bbox, ngeoms, geoms);
if( ! lwtype_is_collection(type) )
lwerror("Non-collection type specified in collection constructor!");
hasz = 0;
hasm = 0;
if ( ngeoms > 0 )
{
hasz = FLAGS_GET_Z(geoms[0]->flags);
hasm = FLAGS_GET_M(geoms[0]->flags);
#ifdef CHECK_LWGEOM_ZM
zm = FLAGS_GET_ZM(geoms[0]->flags);
LWDEBUGF(3, "lwcollection_construct type[0]=%d", geoms[0]->type);
for (i=1; i<ngeoms; i++)
{
LWDEBUGF(3, "lwcollection_construct type=[%d]=%d", i, geoms[i]->type);
if ( zm != FLAGS_GET_ZM(geoms[i]->flags) )
lwerror("lwcollection_construct: mixed dimension geometries: %d/%d", zm, FLAGS_GET_ZM(geoms[i]->flags));
}
#endif
}
ret = lwalloc(sizeof(LWCOLLECTION));
ret->type = type;
ret->flags = gflags(hasz,hasm,0);
FLAGS_SET_BBOX(ret->flags, bbox?1:0);
ret->srid = srid;
ret->ngeoms = ngeoms;
ret->maxgeoms = ngeoms;
ret->geoms = geoms;
ret->bbox = bbox;
return ret;
}
LWCOLLECTION *
lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
{
LWCOLLECTION *ret;
if( ! lwtype_is_collection(type) )
lwerror("Non-collection type specified in collection constructor!");
ret = lwalloc(sizeof(LWCOLLECTION));
ret->type = type;
ret->flags = gflags(hasz,hasm,0);
ret->srid = srid;
ret->ngeoms = 0;
ret->maxgeoms = 1; /* Allocate room for sub-members, just in case. */
ret->geoms = lwalloc(ret->maxgeoms * sizeof(LWGEOM*));
ret->bbox = NULL;
return ret;
}
LWGEOM *
lwcollection_getsubgeom(LWCOLLECTION *col, int gnum)
{
return (LWGEOM *)col->geoms[gnum];
}
/**
* @brief Clone #LWCOLLECTION object. #POINTARRAY are not copied.
* Bbox is cloned if present in input.
*/
LWCOLLECTION *
lwcollection_clone(const LWCOLLECTION *g)
{
uint32_t i;
LWCOLLECTION *ret = lwalloc(sizeof(LWCOLLECTION));
memcpy(ret, g, sizeof(LWCOLLECTION));
if ( g->ngeoms > 0 )
{
ret->geoms = lwalloc(sizeof(LWGEOM *)*g->ngeoms);
for (i=0; i<g->ngeoms; i++)
{
ret->geoms[i] = lwgeom_clone(g->geoms[i]);
}
if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
}
else
{
ret->bbox = NULL; /* empty collection */
ret->geoms = NULL;
}
return ret;
}
/**
* @brief Deep clone #LWCOLLECTION object. #POINTARRAY are copied.
*/
LWCOLLECTION *
lwcollection_clone_deep(const LWCOLLECTION *g)
{
uint32_t i;
LWCOLLECTION *ret = lwalloc(sizeof(LWCOLLECTION));
memcpy(ret, g, sizeof(LWCOLLECTION));
if ( g->ngeoms > 0 )
{
ret->geoms = lwalloc(sizeof(LWGEOM *)*g->ngeoms);
for (i=0; i<g->ngeoms; i++)
{
ret->geoms[i] = lwgeom_clone_deep(g->geoms[i]);
}
if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
}
else
{
ret->bbox = NULL; /* empty collection */
ret->geoms = NULL;
}
return ret;
}
/**
* Ensure the collection can hold up at least ngeoms
*/
void lwcollection_reserve(LWCOLLECTION *col, int ngeoms)
{
if ( ngeoms <= col->maxgeoms ) return;
/* Allocate more space if we need it */
do { col->maxgeoms *= 2; } while ( col->maxgeoms < ngeoms );
col->geoms = lwrealloc(col->geoms, sizeof(LWGEOM*) * col->maxgeoms);
}
/**
* Appends geom to the collection managed by col. Does not copy or
* clone, simply takes a reference on the passed geom.
*/
LWCOLLECTION* lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
{
if ( col == NULL || geom == NULL ) return NULL;
if ( col->geoms == NULL && (col->ngeoms || col->maxgeoms) ) {
lwerror("Collection is in inconsistent state. Null memory but non-zero collection counts.");
return NULL;
}
/* Check type compatibility */
if ( ! lwcollection_allows_subtype(col->type, geom->type) ) {
lwerror("%s cannot contain %s element", lwtype_name(col->type), lwtype_name(geom->type));
return NULL;
}
/* In case this is a truly empty, make some initial space */
if ( col->geoms == NULL )
{
col->maxgeoms = 2;
col->ngeoms = 0;
col->geoms = lwalloc(col->maxgeoms * sizeof(LWGEOM*));
}
/* Allocate more space if we need it */
lwcollection_reserve(col, col->ngeoms + 1);
#if PARANOIA_LEVEL > 1
/* See http://trac.osgeo.org/postgis/ticket/2933 */
/* Make sure we don't already have a reference to this geom */
{
int i = 0;
for ( i = 0; i < col->ngeoms; i++ )
{
if ( col->geoms[i] == geom )
{
LWDEBUGF(4, "Found duplicate geometry in collection %p == %p", col->geoms[i], geom);
return col;
}
}
}
#endif
col->geoms[col->ngeoms] = (LWGEOM*)geom;
col->ngeoms++;
return col;
}
LWCOLLECTION *
lwcollection_segmentize2d(LWCOLLECTION *col, double dist)
{
uint32_t i;
LWGEOM **newgeoms;
if ( ! col->ngeoms ) return lwcollection_clone(col);
newgeoms = lwalloc(sizeof(LWGEOM *)*col->ngeoms);
for (i=0; i<col->ngeoms; i++)
{
newgeoms[i] = lwgeom_segmentize2d(col->geoms[i], dist);
if ( ! newgeoms[i] ) {
while (i--) lwgeom_free(newgeoms[i]);
lwfree(newgeoms);
return NULL;
}
}
return lwcollection_construct(col->type, col->srid, NULL, col->ngeoms, newgeoms);
}
/** @brief check for same geometry composition
*
*/
char
lwcollection_same(const LWCOLLECTION *c1, const LWCOLLECTION *c2)
{
uint32_t i;
LWDEBUG(2, "lwcollection_same called");
if ( c1->type != c2->type ) return LW_FALSE;
if ( c1->ngeoms != c2->ngeoms ) return LW_FALSE;
for ( i = 0; i < c1->ngeoms; i++ )
{
if ( ! lwgeom_same(c1->geoms[i], c2->geoms[i]) )
return LW_FALSE;
}
/* Former method allowed out-of-order equality between collections
hit = lwalloc(sizeof(uint32_t)*c1->ngeoms);
memset(hit, 0, sizeof(uint32_t)*c1->ngeoms);
for (i=0; i<c1->ngeoms; i++)
{
char found=0;
for (j=0; j<c2->ngeoms; j++)
{
if ( hit[j] ) continue;
if ( lwgeom_same(c1->geoms[i], c2->geoms[j]) )
{
hit[j] = 1;
found=1;
break;
}
}
if ( ! found ) return LW_FALSE;
}
*/
return LW_TRUE;
}
int lwcollection_ngeoms(const LWCOLLECTION *col)
{
int i;
int ngeoms = 0;
if ( ! col )
{
lwerror("Null input geometry.");
return 0;
}
for ( i = 0; i < col->ngeoms; i++ )
{
if ( col->geoms[i])
{
switch (col->geoms[i]->type)
{
case POINTTYPE:
case LINETYPE:
case CIRCSTRINGTYPE:
case POLYGONTYPE:
ngeoms += 1;
break;
case MULTIPOINTTYPE:
case MULTILINETYPE:
case MULTICURVETYPE:
case MULTIPOLYGONTYPE:
ngeoms += col->ngeoms;
break;
case COLLECTIONTYPE:
ngeoms += lwcollection_ngeoms((LWCOLLECTION*)col->geoms[i]);
break;
}
}
}
return ngeoms;
}
void lwcollection_free(LWCOLLECTION *col)
{
int i;
if ( ! col ) return;
if ( col->bbox )
{
lwfree(col->bbox);
}
for ( i = 0; i < col->ngeoms; i++ )
{
LWDEBUGF(4,"freeing geom[%d]", i);
if ( col->geoms && col->geoms[i] )
lwgeom_free(col->geoms[i]);
}
if ( col->geoms )
{
lwfree(col->geoms);
}
lwfree(col);
}
/**
* Takes a potentially heterogeneous collection and returns a homogeneous
* collection consisting only of the specified type.
*/
LWCOLLECTION* lwcollection_extract(LWCOLLECTION *col, int type)
{
int i = 0;
LWGEOM **geomlist;
LWCOLLECTION *outcol;
int geomlistsize = 16;
int geomlistlen = 0;
uint8_t outtype;
if ( ! col ) return NULL;
switch (type)
{
case POINTTYPE:
outtype = MULTIPOINTTYPE;
break;
case LINETYPE:
outtype = MULTILINETYPE;
break;
case POLYGONTYPE:
outtype = MULTIPOLYGONTYPE;
break;
default:
lwerror("Only POLYGON, LINESTRING and POINT are supported by lwcollection_extract. %s requested.", lwtype_name(type));
return NULL;
}
geomlist = lwalloc(sizeof(LWGEOM*) * geomlistsize);
/* Process each sub-geometry */
for ( i = 0; i < col->ngeoms; i++ )
{
int subtype = col->geoms[i]->type;
/* Don't bother adding empty sub-geometries */
if ( lwgeom_is_empty(col->geoms[i]) )
{
continue;
}
/* Copy our sub-types into the output list */
if ( subtype == type )
{
/* We've over-run our buffer, double the memory segment */
if ( geomlistlen == geomlistsize )
{
geomlistsize *= 2;
geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
}
geomlist[geomlistlen] = lwgeom_clone(col->geoms[i]);
geomlistlen++;
}
/* Recurse into sub-collections */
if ( lwtype_is_collection( subtype ) )
{
int j = 0;
LWCOLLECTION *tmpcol = lwcollection_extract((LWCOLLECTION*)col->geoms[i], type);
for ( j = 0; j < tmpcol->ngeoms; j++ )
{
/* We've over-run our buffer, double the memory segment */
if ( geomlistlen == geomlistsize )
{
geomlistsize *= 2;
geomlist = lwrealloc(geomlist, sizeof(LWGEOM*) * geomlistsize);
}
geomlist[geomlistlen] = tmpcol->geoms[j];
geomlistlen++;
}
lwfree(tmpcol);
}
}
if ( geomlistlen > 0 )
{
GBOX gbox;
outcol = lwcollection_construct(outtype, col->srid, NULL, geomlistlen, geomlist);
lwgeom_calculate_gbox((LWGEOM *) outcol, &gbox);
outcol->bbox = gbox_copy(&gbox);
}
else
{
lwfree(geomlist);
outcol = lwcollection_construct_empty(outtype, col->srid, FLAGS_GET_Z(col->flags), FLAGS_GET_M(col->flags));
}
return outcol;
}
LWGEOM*
lwcollection_remove_repeated_points(const LWCOLLECTION *coll, double tolerance)
{
uint32_t i;
LWGEOM **newgeoms;
newgeoms = lwalloc(sizeof(LWGEOM *)*coll->ngeoms);
for (i=0; i<coll->ngeoms; i++)
{
newgeoms[i] = lwgeom_remove_repeated_points(coll->geoms[i], tolerance);
}
return (LWGEOM*)lwcollection_construct(coll->type,
coll->srid, coll->bbox ? gbox_copy(coll->bbox) : NULL,
coll->ngeoms, newgeoms);
}
LWCOLLECTION*
lwcollection_force_dims(const LWCOLLECTION *col, int hasz, int hasm)
{
LWCOLLECTION *colout;
/* Return 2D empty */
if( lwcollection_is_empty(col) )
{
colout = lwcollection_construct_empty(col->type, col->srid, hasz, hasm);
}
else
{
int i;
LWGEOM **geoms = NULL;
geoms = lwalloc(sizeof(LWGEOM*) * col->ngeoms);
for( i = 0; i < col->ngeoms; i++ )
{
geoms[i] = lwgeom_force_dims(col->geoms[i], hasz, hasm);
}
colout = lwcollection_construct(col->type, col->srid, NULL, col->ngeoms, geoms);
}
return colout;
}
int lwcollection_is_empty(const LWCOLLECTION *col)
{
int i;
if ( (col->ngeoms == 0) || (!col->geoms) )
return LW_TRUE;
for( i = 0; i < col->ngeoms; i++ )
{
if ( ! lwgeom_is_empty(col->geoms[i]) ) return LW_FALSE;
}
return LW_TRUE;
}
int lwcollection_count_vertices(LWCOLLECTION *col)
{
int i = 0;
int v = 0; /* vertices */
assert(col);
for ( i = 0; i < col->ngeoms; i++ )
{
v += lwgeom_count_vertices(col->geoms[i]);
}
return v;
}
LWCOLLECTION* lwcollection_simplify(const LWCOLLECTION *igeom, double dist, int preserve_collapsed)
{
int i;
LWCOLLECTION *out = lwcollection_construct_empty(igeom->type, igeom->srid, FLAGS_GET_Z(igeom->flags), FLAGS_GET_M(igeom->flags));
if( lwcollection_is_empty(igeom) )
return out; /* should we return NULL instead ? */
for( i = 0; i < igeom->ngeoms; i++ )
{
LWGEOM *ngeom = lwgeom_simplify(igeom->geoms[i], dist, preserve_collapsed);
if ( ngeom ) out = lwcollection_add_lwgeom(out, ngeom);
}
return out;
}
int lwcollection_allows_subtype(int collectiontype, int subtype)
{
if ( collectiontype == COLLECTIONTYPE )
return LW_TRUE;
if ( collectiontype == MULTIPOINTTYPE &&
subtype == POINTTYPE )
return LW_TRUE;
if ( collectiontype == MULTILINETYPE &&
subtype == LINETYPE )
return LW_TRUE;
if ( collectiontype == MULTIPOLYGONTYPE &&
subtype == POLYGONTYPE )
return LW_TRUE;
if ( collectiontype == COMPOUNDTYPE &&
(subtype == LINETYPE || subtype == CIRCSTRINGTYPE) )
return LW_TRUE;
if ( collectiontype == CURVEPOLYTYPE &&
(subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE) )
return LW_TRUE;
if ( collectiontype == MULTICURVETYPE &&
(subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE) )
return LW_TRUE;
if ( collectiontype == MULTISURFACETYPE &&
(subtype == POLYGONTYPE || subtype == CURVEPOLYTYPE) )
return LW_TRUE;
if ( collectiontype == POLYHEDRALSURFACETYPE &&
subtype == POLYGONTYPE )
return LW_TRUE;
if ( collectiontype == TINTYPE &&
subtype == TRIANGLETYPE )
return LW_TRUE;
/* Must be a bad combination! */
return LW_FALSE;
}
int
lwcollection_startpoint(const LWCOLLECTION* col, POINT4D* pt)
{
if ( col->ngeoms < 1 )
return LW_FAILURE;
return lwgeom_startpoint(col->geoms[0], pt);
}
LWCOLLECTION* lwcollection_grid(const LWCOLLECTION *coll, const gridspec *grid)
{
uint32_t i;
LWCOLLECTION *newcoll;
newcoll = lwcollection_construct_empty(coll->type, coll->srid, lwgeom_has_z((LWGEOM*)coll), lwgeom_has_m((LWGEOM*)coll));
for (i=0; i<coll->ngeoms; i++)
{
LWGEOM *g = lwgeom_grid(coll->geoms[i], grid);
if ( g )
lwcollection_add_lwgeom(newcoll, g);
}
return newcoll;
}