forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwcompound.c
271 lines (232 loc) · 6.39 KB
/
lwcompound.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
/**********************************************************************
*
* 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"
int
lwcompound_is_closed(const LWCOMPOUND *compound)
{
size_t size;
int npoints=0;
if ( lwgeom_has_z((LWGEOM*)compound) )
{
size = sizeof(POINT3D);
}
else
{
size = sizeof(POINT2D);
}
if ( compound->geoms[compound->ngeoms - 1]->type == CIRCSTRINGTYPE )
{
npoints = ((LWCIRCSTRING *)compound->geoms[compound->ngeoms - 1])->points->npoints;
}
else if (compound->geoms[compound->ngeoms - 1]->type == LINETYPE)
{
npoints = ((LWLINE *)compound->geoms[compound->ngeoms - 1])->points->npoints;
}
if ( memcmp(getPoint_internal( (POINTARRAY *)compound->geoms[0]->data, 0),
getPoint_internal( (POINTARRAY *)compound->geoms[compound->ngeoms - 1]->data,
npoints - 1),
size) )
{
return LW_FALSE;
}
return LW_TRUE;
}
double lwcompound_length(const LWCOMPOUND *comp)
{
return lwcompound_length_2d(comp);
}
double lwcompound_length_2d(const LWCOMPOUND *comp)
{
uint32_t i;
double length = 0.0;
if ( lwgeom_is_empty((LWGEOM*)comp) )
return 0.0;
for (i = 0; i < comp->ngeoms; i++)
{
length += lwgeom_length_2d(comp->geoms[i]);
}
return length;
}
int lwcompound_add_lwgeom(LWCOMPOUND *comp, LWGEOM *geom)
{
LWCOLLECTION *col = (LWCOLLECTION*)comp;
/* Empty things can't continuously join up with other things */
if ( lwgeom_is_empty(geom) )
{
LWDEBUG(4, "Got an empty component for a compound curve!");
return LW_FAILURE;
}
if( col->ngeoms > 0 )
{
POINT4D last, first;
/* First point of the component we are adding */
LWLINE *newline = (LWLINE*)geom;
/* Last point of the previous component */
LWLINE *prevline = (LWLINE*)(col->geoms[col->ngeoms-1]);
getPoint4d_p(newline->points, 0, &first);
getPoint4d_p(prevline->points, prevline->points->npoints-1, &last);
if ( !(FP_EQUALS(first.x,last.x) && FP_EQUALS(first.y,last.y)) )
{
LWDEBUG(4, "Components don't join up end-to-end!");
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);
return LW_FAILURE;
}
}
col = lwcollection_add_lwgeom(col, geom);
return LW_SUCCESS;
}
LWCOMPOUND *
lwcompound_construct_empty(int srid, char hasz, char hasm)
{
LWCOMPOUND *ret = (LWCOMPOUND*)lwcollection_construct_empty(COMPOUNDTYPE, srid, hasz, hasm);
return ret;
}
int lwgeom_contains_point(const LWGEOM *geom, const POINT2D *pt)
{
switch( geom->type )
{
case LINETYPE:
return ptarray_contains_point(((LWLINE*)geom)->points, pt);
case CIRCSTRINGTYPE:
return ptarrayarc_contains_point(((LWCIRCSTRING*)geom)->points, pt);
case COMPOUNDTYPE:
return lwcompound_contains_point((LWCOMPOUND*)geom, pt);
}
lwerror("lwgeom_contains_point failed");
return LW_FAILURE;
}
int
lwcompound_contains_point(const LWCOMPOUND *comp, const POINT2D *pt)
{
uint32_t i;
LWLINE *lwline;
LWCIRCSTRING *lwcirc;
int wn = 0;
int winding_number = 0;
int result;
for ( i = 0; i < comp->ngeoms; i++ )
{
LWGEOM *lwgeom = comp->geoms[i];
if ( lwgeom->type == LINETYPE )
{
lwline = lwgeom_as_lwline(lwgeom);
if ( comp->ngeoms == 1 )
{
return ptarray_contains_point(lwline->points, pt);
}
else
{
/* Don't check closure while doing p-i-p test */
result = ptarray_contains_point_partial(lwline->points, pt, LW_FALSE, &winding_number);
}
}
else
{
lwcirc = lwgeom_as_lwcircstring(lwgeom);
if ( ! lwcirc ) {
lwerror("Unexpected component of type %s in compound curve", lwtype_name(lwgeom->type));
return 0;
}
if ( comp->ngeoms == 1 )
{
return ptarrayarc_contains_point(lwcirc->points, pt);
}
else
{
/* Don't check closure while doing p-i-p test */
result = ptarrayarc_contains_point_partial(lwcirc->points, pt, LW_FALSE, &winding_number);
}
}
/* Propogate boundary condition */
if ( result == LW_BOUNDARY )
return LW_BOUNDARY;
wn += winding_number;
}
/* Outside */
if (wn == 0)
return LW_OUTSIDE;
/* Inside */
return LW_INSIDE;
}
LWCOMPOUND *
lwcompound_construct_from_lwline(const LWLINE *lwline)
{
LWCOMPOUND* ogeom = lwcompound_construct_empty(lwline->srid, FLAGS_GET_Z(lwline->flags), FLAGS_GET_M(lwline->flags));
lwcompound_add_lwgeom(ogeom, lwgeom_clone((LWGEOM*)lwline));
/* ogeom->bbox = lwline->bbox; */
return ogeom;
}
LWPOINT*
lwcompound_get_lwpoint(const LWCOMPOUND *lwcmp, uint32_t where)
{
uint32_t i;
uint32_t count = 0;
uint32_t npoints = 0;
if ( lwgeom_is_empty((LWGEOM*)lwcmp) )
return NULL;
npoints = lwgeom_count_vertices((LWGEOM*)lwcmp);
if ( where >= npoints )
{
lwerror("%s: index %d is not in range of number of vertices (%d) in input", __func__, where, npoints);
return NULL;
}
for ( i = 0; i < lwcmp->ngeoms; i++ )
{
LWGEOM* part = lwcmp->geoms[i];
uint32_t npoints_part = lwgeom_count_vertices(part);
if ( where >= count && where < count + npoints_part )
{
return lwline_get_lwpoint((LWLINE*)part, where - count);
}
else
{
count += npoints_part;
}
}
return NULL;
}
LWPOINT *
lwcompound_get_startpoint(const LWCOMPOUND *lwcmp)
{
return lwcompound_get_lwpoint(lwcmp, 0);
}
LWPOINT *
lwcompound_get_endpoint(const LWCOMPOUND *lwcmp)
{
LWLINE *lwline;
if ( lwcmp->ngeoms < 1 )
{
return NULL;
}
lwline = (LWLINE*)(lwcmp->geoms[lwcmp->ngeoms-1]);
if ( (!lwline) || (!lwline->points) || (lwline->points->npoints < 1) )
{
return NULL;
}
return lwline_get_lwpoint(lwline, lwline->points->npoints-1);
}