Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-04-30 9f2ef60949ced4fab4d077a7e1c6d913f3620c9b
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
/**********************************************************************
 *
 * 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 2019 Raúl Marín
 *
 **********************************************************************/
 
#include "lwgeom_wagyu.h"
 
#define USE_WAGYU_INTERRUPT
#include "mapbox/geometry/wagyu/wagyu.hpp"
 
#include <iterator>
#include <limits>
#include <vector>
 
using namespace mapbox::geometry;
 
using wagyu_coord_type = std::int32_t;
using wagyu_polygon = mapbox::geometry::polygon<wagyu_coord_type>;
using wagyu_multipolygon = mapbox::geometry::multi_polygon<wagyu_coord_type>;
using wagyu_linearring = mapbox::geometry::linear_ring<wagyu_coord_type>;
using vwpolygon = std::vector<wagyu_polygon>;
using wagyu_point = mapbox::geometry::point<wagyu_coord_type>;
 
static wagyu_linearring
ptarray_to_wglinearring(const POINTARRAY *pa)
{
    wagyu_linearring lr;
    lr.reserve(pa->npoints);
 
    size_t point_size = ptarray_point_size(pa);
    size_t pa_size = pa->npoints;
    uint8_t *buffer = pa->serialized_pointlist;
    for (std::uint32_t i = 0; i < pa_size; i++)
    {
        const wagyu_coord_type x = static_cast<wagyu_coord_type>(*(double*) buffer);
        const wagyu_coord_type y = static_cast<wagyu_coord_type>(((double*) buffer)[1]);
        buffer += point_size;
 
        lr.emplace_back(static_cast<wagyu_coord_type>(x), static_cast<wagyu_coord_type>(y));
    }
 
    return lr;
}
 
static vwpolygon
lwpoly_to_vwgpoly(const LWPOLY *geom, const GBOX *box)
{
    LWCOLLECTION *geom_clipped_x = lwgeom_clip_to_ordinate_range((LWGEOM *)geom, 'X', box->xmin, box->xmax, 0);
    LWCOLLECTION *geom_clipped = lwgeom_clip_to_ordinate_range((LWGEOM *)geom_clipped_x, 'Y', box->ymin, box->ymax, 0);
 
    vwpolygon vp;
    for (std::uint32_t i = 0; i < geom_clipped->ngeoms; i++)
    {
        assert(geom_clipped->geoms[i]->type == POLYGONTYPE);
        LWPOLY *poly = (LWPOLY *)geom_clipped->geoms[i];
        for (std::uint32_t ring = 0; ring < poly->nrings; ring++)
        {
            wagyu_polygon pol;
            pol.push_back(ptarray_to_wglinearring(poly->rings[ring]));
 
            /* If it has an inner ring, push it too */
            ring++;
            if (ring != poly->nrings)
            {
                pol.push_back(ptarray_to_wglinearring(poly->rings[ring]));
            }
 
            vp.push_back(pol);
        }
    }
 
    lwgeom_free((LWGEOM *)geom_clipped_x);
    lwgeom_free((LWGEOM *)geom_clipped);
 
    return vp;
}
 
static vwpolygon
lwmpoly_to_vwgpoly(const LWMPOLY *geom, const GBOX *box)
{
    vwpolygon vp;
    for (std::uint32_t i = 0; i < geom->ngeoms; i++)
    {
        vwpolygon vp_intermediate = lwpoly_to_vwgpoly(geom->geoms[i], box);
        vp.insert(vp.end(),
              std::make_move_iterator(vp_intermediate.begin()),
              std::make_move_iterator(vp_intermediate.end()));
    }
 
    return vp;
}
 
/**
 * Transforms a liblwgeom geometry (types POLYGONTYPE or MULTIPOLYGONTYPE)
 * into an array of mapbox::geometry (polygon)
 * A single lwgeom polygon can lead to N mapbox polygon as odd numbered rings
 * are treated as new polygons (and even numbered treated as holes in the
 * previous rings)
 */
static vwpolygon
lwgeom_to_vwgpoly(const LWGEOM *geom, const GBOX *box)
{
    switch (geom->type)
    {
    case POLYGONTYPE:
        return lwpoly_to_vwgpoly(reinterpret_cast<const LWPOLY *>(geom), box);
    case MULTIPOLYGONTYPE:
        return lwmpoly_to_vwgpoly(reinterpret_cast<const LWMPOLY *>(geom), box);
    default:
        return vwpolygon();
    }
}
 
static POINTARRAY *
wglinearring_to_ptarray(const wagyu_polygon::linear_ring_type &lr)
{
    const uint32_t npoints = lr.size();
    POINTARRAY *pa = ptarray_construct(0, 0, npoints);
 
    for (uint32_t i = 0; i < npoints; i++)
    {
        const wagyu_polygon::linear_ring_type::point_type &p = lr[i];
        POINT4D point = {static_cast<double>(p.x), static_cast<double>(p.y), 0.0, 0.0};
        ptarray_set_point4d(pa, i, &point);
    }
 
    return pa;
}
 
static LWGEOM *
wgpoly_to_lwgeom(const wagyu_multipolygon::polygon_type &p)
{
    const uint32_t nrings = p.size();
    POINTARRAY **ppa = reinterpret_cast<POINTARRAY **>(lwalloc(sizeof(POINTARRAY *) * nrings));
 
    for (uint32_t i = 0; i < nrings; i++)
    {
        ppa[i] = wglinearring_to_ptarray(p[i]);
    }
 
    return reinterpret_cast<LWGEOM *>(lwpoly_construct(0, NULL, nrings, ppa));
}
 
static LWGEOM *
wgmpoly_to_lwgeom(const wagyu_multipolygon &mp)
{
    const uint32_t ngeoms = mp.size();
    if (ngeoms == 0)
    {
        return NULL;
    }
    else if (ngeoms == 1)
    {
        return wgpoly_to_lwgeom(mp[0]);
    }
    else
    {
        LWGEOM **geoms = reinterpret_cast<LWGEOM **>(lwalloc(sizeof(LWGEOM *) * ngeoms));
 
        for (uint32_t i = 0; i < ngeoms; i++)
        {
            geoms[i] = wgpoly_to_lwgeom(mp[i]);
        }
 
        return reinterpret_cast<LWGEOM *>(lwcollection_construct(MULTIPOLYGONTYPE, 0, NULL, ngeoms, geoms));
    }
}
 
static LWGEOM *
__lwgeom_wagyu_clip_by_box(const LWGEOM *geom, const GBOX *bbox)
{
    if (!geom || !bbox)
    {
        return NULL;
    }
 
    if (geom->type != POLYGONTYPE && geom->type != MULTIPOLYGONTYPE)
    {
        return NULL;
    }
 
    if (lwgeom_is_empty(geom))
    {
        LWGEOM *out = lwgeom_construct_empty(MULTIPOLYGONTYPE, geom->srid, 0, 0);
        out->flags = geom->flags;
        return out;
    }
 
    wagyu_multipolygon solution;
    vwpolygon vpsubject = lwgeom_to_vwgpoly(geom, bbox);
    if (vpsubject.size() == 0)
    {
        LWGEOM *out = lwgeom_construct_empty(MULTIPOLYGONTYPE, geom->srid, 0, 0);
        out->flags = geom->flags;
        return out;
    }
 
    wagyu::wagyu<wagyu_coord_type> clipper;
    for (auto &poly : vpsubject)
    {
        for (auto &lr : poly)
        {
            if (!lr.empty())
            {
                clipper.add_ring(lr, wagyu::polygon_type_subject);
            }
        }
    }
 
    clipper.execute(wagyu::clip_type_union, solution, wagyu::fill_type_even_odd, wagyu::fill_type_even_odd);
 
    LWGEOM *g = wgmpoly_to_lwgeom(solution);
    if (g)
        g->srid = geom->srid;
 
    return g;
}
 
LWGEOM *
lwgeom_wagyu_clip_by_box(const LWGEOM *geom, const GBOX *bbox)
{
    mapbox::geometry::wagyu::interrupt_reset();
    try
    {
        return __lwgeom_wagyu_clip_by_box(geom, bbox);
    }
    catch (...)
    {
        return NULL;
    }
}
 
const char *
libwagyu_version()
{
    static char str[50] = {0};
    snprintf(
        str, sizeof(str), "%d.%d.%d (Internal)", WAGYU_MAJOR_VERSION, WAGYU_MINOR_VERSION, WAGYU_PATCH_VERSION);
 
    return str;
}
 
void
lwgeom_wagyu_interruptRequest()
{
    mapbox::geometry::wagyu::interrupt_request();
}
 
void
lwgeom_wagyu_interruptReset()
{
    mapbox::geometry::wagyu::interrupt_reset();
}