forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwgeom_wrapx.c
221 lines (190 loc) · 6 KB
/
lwgeom_wrapx.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
/**********************************************************************
*
* 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 2016 Sandro Santilli <strk@kbt.io>
*
**********************************************************************/
#include "../postgis_config.h"
/*#define POSTGIS_DEBUG_LEVEL 4*/
#include "lwgeom_geos.h"
#include "liblwgeom_internal.h"
#include <string.h>
#include <assert.h>
LWGEOM* lwgeom_wrapx(const LWGEOM* lwgeom_in, double cutx, double amount);
static LWCOLLECTION* lwcollection_wrapx(const LWCOLLECTION* lwcoll_in, double cutx, double amount);
static LWGEOM*
lwgeom_split_wrapx(const LWGEOM* geom_in, double cutx, double amount)
{
LWGEOM *blade, *split;
POINTARRAY *bladepa;
POINT4D pt;
const GBOX *box_in;
AFFINE affine = {
1, 0, 0,
0, 1, 0,
0, 0, 1,
amount, 0, 0,
};
/* Extract box */
/* TODO: check if the bbox should be force-recomputed */
box_in = lwgeom_get_bbox(geom_in);
if ( ! box_in ) {
/* must be empty */
return lwgeom_clone_deep(geom_in);
}
LWDEBUGF(2, "BOX X range is %g..%g, cutx:%g, amount:%g", box_in->xmin, box_in->xmax, cutx, amount);
/* Check if geometry is fully on the side needing shift */
if ( ( amount < 0 && box_in->xmin >= cutx ) || ( amount > 0 && box_in->xmax <= cutx ) )
{
split = lwgeom_clone_deep(geom_in);
lwgeom_affine(split, &affine);
LWDEBUGG(2, split, "returning the translated geometry");
return split;
}
/* Check if geometry is fully on the side needing no shift */
if ( ( amount < 0 && box_in->xmax <= cutx ) || ( amount > 0 && box_in->xmin >= cutx ) )
{
split = lwgeom_clone_deep(geom_in);
LWDEBUGG(2, split, "returning the cloned geometry");
return split;
}
/* We need splitting here */
/* construct blade */
bladepa = ptarray_construct(0, 0, 2);
pt.x = cutx;
pt.y = box_in->ymin - 1;
ptarray_set_point4d(bladepa, 0, &pt);
pt.y = box_in->ymax + 1;
ptarray_set_point4d(bladepa, 1, &pt);
blade = lwline_as_lwgeom(lwline_construct(geom_in->srid, NULL, bladepa));
LWDEBUG(2, "splitting the geometry");
/* split by blade */
split = lwgeom_split(geom_in, blade);
lwgeom_free(blade);
if ( ! split ) {
lwerror("%s:%d - lwgeom_split_wrapx: %s", __FILE__, __LINE__, lwgeom_geos_errmsg);
return NULL;
}
LWDEBUGG(2, split, "split geometry");
/* iterate over components, translate if needed */
const LWCOLLECTION *col = lwgeom_as_lwcollection(split);
if ( ! col ) {
/* not split, this is unexpected */
lwnotice("WARNING: unexpected lack of split in lwgeom_split_wrapx");
return lwgeom_clone_deep(geom_in);
}
LWCOLLECTION *col_out = lwcollection_wrapx(col, cutx, amount);
lwgeom_free(split);
/* unary-union the result (homogenize too ?) */
LWGEOM* out = lwgeom_unaryunion(lwcollection_as_lwgeom(col_out));
LWDEBUGF(2, "col_out:%p, unaryunion_out:%p", col_out, out);
LWDEBUGG(2, out, "unary-unioned");
lwcollection_free(col_out);
return out;
}
static LWCOLLECTION*
lwcollection_wrapx(const LWCOLLECTION* lwcoll_in, double cutx, double amount)
{
LWGEOM** wrap_geoms;
LWCOLLECTION* out;
int i;
int outtype = lwcoll_in->type;
wrap_geoms = lwalloc(lwcoll_in->ngeoms * sizeof(LWGEOM*));
if ( ! wrap_geoms )
{
lwerror("Out of virtual memory");
return NULL;
}
for (i=0; i<lwcoll_in->ngeoms; ++i)
{
LWDEBUGF(3, "Wrapping collection element %d", i);
wrap_geoms[i] = lwgeom_wrapx(lwcoll_in->geoms[i], cutx, amount);
/* an exception should prevent this from ever returning NULL */
if ( ! wrap_geoms[i] ) {
lwnotice("Error wrapping geometry, cleaning up");
while ((--i)>=0) {
lwnotice("cleaning geometry %d (%p)", i, wrap_geoms[i]);
lwgeom_free(wrap_geoms[i]);
}
lwfree(wrap_geoms);
lwnotice("cleanup complete");
return NULL;
}
if ( outtype != COLLECTIONTYPE ) {
if ( MULTITYPE[wrap_geoms[i]->type] != outtype )
{
outtype = COLLECTIONTYPE;
}
}
}
/* Now wrap_geoms has wrap_geoms_size geometries */
out = lwcollection_construct(outtype, lwcoll_in->srid, NULL,
lwcoll_in->ngeoms, wrap_geoms);
return out;
}
/* exported */
LWGEOM*
lwgeom_wrapx(const LWGEOM* lwgeom_in, double cutx, double amount)
{
/* Nothing to wrap in an empty geom */
if ( lwgeom_is_empty(lwgeom_in) )
{
LWDEBUG(2, "geom is empty, cloning");
return lwgeom_clone_deep(lwgeom_in);
}
/* Nothing to wrap if shift amount is zero */
if ( amount == 0 )
{
LWDEBUG(2, "amount is zero, cloning");
return lwgeom_clone_deep(lwgeom_in);
}
switch (lwgeom_in->type)
{
case LINETYPE:
case POLYGONTYPE:
LWDEBUG(2, "split-wrapping line or polygon");
return lwgeom_split_wrapx(lwgeom_in, cutx, amount);
case POINTTYPE:
{
const LWPOINT *pt = lwgeom_as_lwpoint(lwgeom_clone_deep(lwgeom_in));
POINT4D pt4d;
getPoint4d_p(pt->point, 0, &pt4d);
LWDEBUGF(2, "POINT X is %g, cutx:%g, amount:%g", pt4d.x, cutx, amount);
if ( ( amount < 0 && pt4d.x > cutx ) || ( amount > 0 && pt4d.x < cutx ) )
{
pt4d.x += amount;
ptarray_set_point4d(pt->point, 0, &pt4d);
}
return lwpoint_as_lwgeom(pt);
}
case MULTIPOINTTYPE:
case MULTIPOLYGONTYPE:
case MULTILINETYPE:
case COLLECTIONTYPE:
LWDEBUG(2, "collection-wrapping multi");
return lwcollection_as_lwgeom(
lwcollection_wrapx((const LWCOLLECTION*)lwgeom_in, cutx, amount)
);
default:
lwerror("Wrapping of %s geometries is unsupported",
lwtype_name(lwgeom_in->type));
return NULL;
}
}