forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwpoly.c
557 lines (465 loc) · 12.1 KB
/
lwpoly.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
/**********************************************************************
*
* 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) 2012 Sandro Santilli <strk@kbt.io>
* Copyright (C) 2001-2006 Refractions Research Inc.
*
**********************************************************************/
/* basic LWPOLY manipulation */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
#define CHECK_POLY_RINGS_ZM 1
/* construct a new LWPOLY. arrays (points/points per ring) will NOT be copied
* use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
*/
LWPOLY*
lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
{
LWPOLY *result;
int hasz, hasm;
#ifdef CHECK_POLY_RINGS_ZM
char zm;
uint32_t i;
#endif
if ( nrings < 1 ) lwerror("lwpoly_construct: need at least 1 ring");
hasz = FLAGS_GET_Z(points[0]->flags);
hasm = FLAGS_GET_M(points[0]->flags);
#ifdef CHECK_POLY_RINGS_ZM
zm = FLAGS_GET_ZM(points[0]->flags);
for (i=1; i<nrings; i++)
{
if ( zm != FLAGS_GET_ZM(points[i]->flags) )
lwerror("lwpoly_construct: mixed dimensioned rings");
}
#endif
result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
result->type = POLYGONTYPE;
result->flags = gflags(hasz, hasm, 0);
FLAGS_SET_BBOX(result->flags, bbox?1:0);
result->srid = srid;
result->nrings = nrings;
result->maxrings = nrings;
result->rings = points;
result->bbox = bbox;
return result;
}
LWPOLY*
lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2,
POINT4D *p3, POINT4D *p4)
{
POINTARRAY *pa = ptarray_construct_empty(hasz, hasm, 5);
LWPOLY *lwpoly = lwpoly_construct_empty(SRID_UNKNOWN, hasz, hasm);
ptarray_append_point(pa, p1, LW_TRUE);
ptarray_append_point(pa, p2, LW_TRUE);
ptarray_append_point(pa, p3, LW_TRUE);
ptarray_append_point(pa, p4, LW_TRUE);
ptarray_append_point(pa, p1, LW_TRUE);
lwpoly_add_ring(lwpoly, pa);
return lwpoly;
}
LWPOLY *
lwpoly_construct_envelope(int srid, double x1, double y1, double x2, double y2)
{
POINT4D p1, p2, p3, p4;
LWPOLY *poly;
p1.x = x1;
p1.y = y1;
p2.x = x1;
p2.y = y2;
p3.x = x2;
p3.y = y2;
p4.x = x2;
p4.y = y1;
poly = lwpoly_construct_rectangle(0, 0, &p1, &p2, &p3, &p4);
lwgeom_set_srid(lwpoly_as_lwgeom(poly), srid);
lwgeom_add_bbox(lwpoly_as_lwgeom(poly));
return poly;
}
LWPOLY*
lwpoly_construct_circle(int srid, double x, double y, double radius, uint32_t segments_per_quarter, char exterior)
{
const uint32_t segments = 4*segments_per_quarter;
double theta;
LWPOLY *lwpoly;
POINTARRAY *pa;
POINT4D pt;
uint32_t i;
if (segments_per_quarter == 0)
{
lwerror("Need at least one segment per quarter-circle.");
return NULL;
}
if (radius < 0)
{
lwerror("Radius must be positive.");
return NULL;
}
theta = 2*M_PI / segments;
lwpoly = lwpoly_construct_empty(srid, LW_FALSE, LW_FALSE);
pa = ptarray_construct_empty(LW_FALSE, LW_FALSE, segments + 1);
if (exterior)
radius *= sqrt(1 + pow(tan(theta/2), 2));
for (i = 0; i <= segments; i++)
{
pt.x = x + radius*sin(i * theta);
pt.y = y + radius*cos(i * theta);
ptarray_append_point(pa, &pt, LW_TRUE);
}
lwpoly_add_ring(lwpoly, pa);
return lwpoly;
}
LWPOLY*
lwpoly_construct_empty(int srid, char hasz, char hasm)
{
LWPOLY *result = lwalloc(sizeof(LWPOLY));
result->type = POLYGONTYPE;
result->flags = gflags(hasz,hasm,0);
result->srid = srid;
result->nrings = 0;
result->maxrings = 1; /* Allocate room for ring, just in case. */
result->rings = lwalloc(result->maxrings * sizeof(POINTARRAY*));
result->bbox = NULL;
return result;
}
void
lwpoly_free(LWPOLY* poly)
{
uint32_t t;
if (!poly) return;
if (poly->bbox) lwfree(poly->bbox);
if ( poly->rings )
{
for (t = 0; t < poly->nrings; t++)
if (poly->rings[t]) ptarray_free(poly->rings[t]);
lwfree(poly->rings);
}
lwfree(poly);
}
void printLWPOLY(LWPOLY *poly)
{
uint32_t t;
lwnotice("LWPOLY {");
lwnotice(" ndims = %i", (int)FLAGS_NDIMS(poly->flags));
lwnotice(" SRID = %i", (int)poly->srid);
lwnotice(" nrings = %i", (int)poly->nrings);
for (t=0; t<poly->nrings; t++)
{
lwnotice(" RING # %i :",t);
printPA(poly->rings[t]);
}
lwnotice("}");
}
/* @brief Clone LWLINE object. Serialized point lists are not copied.
*
* @see ptarray_clone
*/
LWPOLY *
lwpoly_clone(const LWPOLY *g)
{
uint32_t i;
LWPOLY *ret = lwalloc(sizeof(LWPOLY));
memcpy(ret, g, sizeof(LWPOLY));
ret->rings = lwalloc(sizeof(POINTARRAY *)*g->nrings);
for ( i = 0; i < g->nrings; i++ ) {
ret->rings[i] = ptarray_clone(g->rings[i]);
}
if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
return ret;
}
/* Deep clone LWPOLY object. POINTARRAY are copied, as is ring array */
LWPOLY *
lwpoly_clone_deep(const LWPOLY *g)
{
uint32_t i;
LWPOLY *ret = lwalloc(sizeof(LWPOLY));
memcpy(ret, g, sizeof(LWPOLY));
if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
ret->rings = lwalloc(sizeof(POINTARRAY *)*g->nrings);
for ( i = 0; i < ret->nrings; i++ )
{
ret->rings[i] = ptarray_clone_deep(g->rings[i]);
}
FLAGS_SET_READONLY(ret->flags,0);
return ret;
}
/**
* Add a ring to a polygon. Point array will be referenced, not copied.
*/
int
lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
{
if( ! poly || ! pa )
return LW_FAILURE;
/* We have used up our storage, add some more. */
if( poly->nrings >= poly->maxrings )
{
int new_maxrings = 2 * (poly->nrings + 1);
poly->rings = lwrealloc(poly->rings, new_maxrings * sizeof(POINTARRAY*));
poly->maxrings = new_maxrings;
}
/* Add the new ring entry. */
poly->rings[poly->nrings] = pa;
poly->nrings++;
return LW_SUCCESS;
}
void
lwpoly_force_clockwise(LWPOLY *poly)
{
uint32_t i;
/* No-op empties */
if ( lwpoly_is_empty(poly) )
return;
/* External ring */
if ( ptarray_isccw(poly->rings[0]) )
ptarray_reverse_in_place(poly->rings[0]);
/* Internal rings */
for (i=1; i<poly->nrings; i++)
if ( ! ptarray_isccw(poly->rings[i]) )
ptarray_reverse_in_place(poly->rings[i]);
}
int
lwpoly_is_clockwise(LWPOLY *poly)
{
uint32_t i;
if ( lwpoly_is_empty(poly) )
return LW_TRUE;
if ( ptarray_isccw(poly->rings[0]) )
return LW_FALSE;
for ( i = 1; i < poly->nrings; i++)
if ( !ptarray_isccw(poly->rings[i]) )
return LW_FALSE;
return LW_TRUE;
}
void
lwpoly_release(LWPOLY *lwpoly)
{
lwgeom_release(lwpoly_as_lwgeom(lwpoly));
}
LWPOLY *
lwpoly_segmentize2d(const LWPOLY *poly, double dist)
{
POINTARRAY **newrings;
uint32_t i;
newrings = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
for (i=0; i<poly->nrings; i++)
{
newrings[i] = ptarray_segmentize2d(poly->rings[i], dist);
if ( ! newrings[i] )
{
uint32_t j = 0;
for (j = 0; j < i; j++)
ptarray_free(newrings[j]);
lwfree(newrings);
return NULL;
}
}
return lwpoly_construct(poly->srid, NULL,
poly->nrings, newrings);
}
/*
* check coordinate equality
* ring and coordinate order is considered
*/
char
lwpoly_same(const LWPOLY *p1, const LWPOLY *p2)
{
uint32_t i;
if ( p1->nrings != p2->nrings ) return 0;
for (i=0; i<p1->nrings; i++)
{
if ( ! ptarray_same(p1->rings[i], p2->rings[i]) )
return 0;
}
return 1;
}
/*
* Construct a polygon from a LWLINE being
* the shell and an array of LWLINE (possibly NULL) being holes.
* Pointarrays from intput geoms are cloned.
* SRID must be the same for each input line.
* Input lines must have at least 4 points, and be closed.
*/
LWPOLY *
lwpoly_from_lwlines(const LWLINE *shell,
uint32_t nholes, const LWLINE **holes)
{
uint32_t nrings;
POINTARRAY **rings = lwalloc((nholes+1)*sizeof(POINTARRAY *));
int srid = shell->srid;
LWPOLY *ret;
if ( shell->points->npoints < 4 )
lwerror("lwpoly_from_lwlines: shell must have at least 4 points");
if ( ! ptarray_is_closed_2d(shell->points) )
lwerror("lwpoly_from_lwlines: shell must be closed");
rings[0] = ptarray_clone_deep(shell->points);
for (nrings=1; nrings<=nholes; nrings++)
{
const LWLINE *hole = holes[nrings-1];
if ( hole->srid != srid )
lwerror("lwpoly_from_lwlines: mixed SRIDs in input lines");
if ( hole->points->npoints < 4 )
lwerror("lwpoly_from_lwlines: holes must have at least 4 points");
if ( ! ptarray_is_closed_2d(hole->points) )
lwerror("lwpoly_from_lwlines: holes must be closed");
rings[nrings] = ptarray_clone_deep(hole->points);
}
ret = lwpoly_construct(srid, NULL, nrings, rings);
return ret;
}
LWPOLY*
lwpoly_force_dims(const LWPOLY *poly, int hasz, int hasm)
{
LWPOLY *polyout;
/* Return 2D empty */
if( lwpoly_is_empty(poly) )
{
polyout = lwpoly_construct_empty(poly->srid, hasz, hasm);
}
else
{
POINTARRAY **rings = NULL;
uint32_t i;
rings = lwalloc(sizeof(POINTARRAY*) * poly->nrings);
for( i = 0; i < poly->nrings; i++ )
{
rings[i] = ptarray_force_dims(poly->rings[i], hasz, hasm);
}
polyout = lwpoly_construct(poly->srid, NULL, poly->nrings, rings);
}
polyout->type = poly->type;
return polyout;
}
int lwpoly_is_empty(const LWPOLY *poly)
{
if ( (poly->nrings < 1) || (!poly->rings) || (!poly->rings[0]) || (poly->rings[0]->npoints < 1) )
return LW_TRUE;
return LW_FALSE;
}
uint32_t lwpoly_count_vertices(LWPOLY *poly)
{
uint32_t i = 0;
uint32_t v = 0; /* vertices */
assert(poly);
for ( i = 0; i < poly->nrings; i ++ )
{
v += poly->rings[i]->npoints;
}
return v;
}
/**
* Find the area of the outer ring - sum (area of inner rings).
*/
double
lwpoly_area(const LWPOLY *poly)
{
double poly_area = 0.0;
uint32_t i;
if ( ! poly )
lwerror("lwpoly_area called with null polygon pointer!");
for ( i=0; i < poly->nrings; i++ )
{
POINTARRAY *ring = poly->rings[i];
double ringarea = 0.0;
/* Empty or messed-up ring. */
if ( ring->npoints < 3 )
continue;
ringarea = fabs(ptarray_signed_area(ring));
if ( i == 0 ) /* Outer ring, positive area! */
poly_area += ringarea;
else /* Inner ring, negative area! */
poly_area -= ringarea;
}
return poly_area;
}
/**
* Compute the sum of polygon rings length.
* Could use a more numerically stable calculator...
*/
double
lwpoly_perimeter(const LWPOLY *poly)
{
double result=0.0;
uint32_t i;
LWDEBUGF(2, "in lwgeom_polygon_perimeter (%d rings)", poly->nrings);
for (i=0; i<poly->nrings; i++)
result += ptarray_length(poly->rings[i]);
return result;
}
/**
* Compute the sum of polygon rings length (forcing 2d computation).
* Could use a more numerically stable calculator...
*/
double
lwpoly_perimeter_2d(const LWPOLY *poly)
{
double result=0.0;
uint32_t i;
LWDEBUGF(2, "in lwgeom_polygon_perimeter (%d rings)", poly->nrings);
for (i=0; i<poly->nrings; i++)
result += ptarray_length_2d(poly->rings[i]);
return result;
}
int
lwpoly_is_closed(const LWPOLY *poly)
{
uint32_t i = 0;
if ( poly->nrings == 0 )
return LW_TRUE;
for ( i = 0; i < poly->nrings; i++ )
{
if (FLAGS_GET_Z(poly->flags))
{
if ( ! ptarray_is_closed_3d(poly->rings[i]) )
return LW_FALSE;
}
else
{
if ( ! ptarray_is_closed_2d(poly->rings[i]) )
return LW_FALSE;
}
}
return LW_TRUE;
}
int
lwpoly_startpoint(const LWPOLY* poly, POINT4D* pt)
{
if ( poly->nrings < 1 )
return LW_FAILURE;
return ptarray_startpoint(poly->rings[0], pt);
}
int
lwpoly_contains_point(const LWPOLY *poly, const POINT2D *pt)
{
uint32_t i;
if ( lwpoly_is_empty(poly) )
return LW_FALSE;
if ( ptarray_contains_point(poly->rings[0], pt) == LW_OUTSIDE )
return LW_FALSE;
for ( i = 1; i < poly->nrings; i++ )
{
if ( ptarray_contains_point(poly->rings[i], pt) == LW_INSIDE )
return LW_FALSE;
}
return LW_TRUE;
}