-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathflex-lua-geom.cpp
More file actions
324 lines (266 loc) · 9.13 KB
/
flex-lua-geom.cpp
File metadata and controls
324 lines (266 loc) · 9.13 KB
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
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "flex-lua-geom.hpp"
#include "geom-box.hpp"
#include "geom-functions.hpp"
#include "geom-pole-of-inaccessibility.hpp"
#include "lua-utils.hpp"
#include "projection.hpp"
#include <lua.hpp>
static char const *const OSM2PGSQL_GEOMETRY_CLASS = "osm2pgsql.Geometry";
geom::geometry_t *create_lua_geometry_object(lua_State *lua_state)
{
void *ptr = lua_newuserdata(lua_state, sizeof(geom::geometry_t));
new (ptr) geom::geometry_t{};
// Set the metatable of this object
luaL_getmetatable(lua_state, OSM2PGSQL_GEOMETRY_CLASS);
lua_setmetatable(lua_state, -2);
return static_cast<geom::geometry_t *>(ptr);
}
geom::geometry_t *unpack_geometry(lua_State *lua_state, int n) noexcept
{
void *user_data = luaL_checkudata(lua_state, n, OSM2PGSQL_GEOMETRY_CLASS);
luaL_argcheck(lua_state, user_data != nullptr, n, "'Geometry' expected");
return static_cast<geom::geometry_t *>(user_data);
}
namespace {
/**
* This function is called by Lua garbage collection when a geometry object
* needs cleaning up. It calls the destructor of the C++ object. After that
* Lua will release the memory.
*/
int geom_gc(lua_State *lua_state) noexcept
{
void *geom = lua_touserdata(lua_state, 1);
if (geom) {
static_cast<geom::geometry_t *>(geom)->~geometry_t();
}
return 0;
}
// The following functions are called when their counterparts in Lua are
// called on geometry objects.
int geom_area(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
try {
lua_pushnumber(lua_state, geom::area(*input_geometry));
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'area()'.\n");
}
return 1;
}
int geom_spherical_area(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
if (input_geometry->srid() != PROJ_LATLONG) {
throw std::runtime_error{"Can only calculate spherical area for "
"geometries in WGS84 (4326) coordinates."};
}
try {
lua_pushnumber(lua_state, geom::spherical_area(*input_geometry));
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'spherical_area()'.\n");
}
return 1;
}
int geom_length(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
try {
lua_pushnumber(lua_state, geom::length(*input_geometry));
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'length()'.\n");
}
return 1;
}
int geom_centroid(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
try {
auto *geom = create_lua_geometry_object(lua_state);
*geom = geom::centroid(*input_geometry);
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'centroid()'.\n");
}
return 1;
}
int geom_geometry_n(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
auto const index = static_cast<int>(luaL_checkinteger(lua_state, 2));
try {
auto *geom = create_lua_geometry_object(lua_state);
geom::geometry_n(geom, *input_geometry, index);
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'geometry_n()'.\n");
}
return 1;
}
int geom_geometry_type(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
try {
auto const type = geometry_type(*input_geometry);
lua_pushlstring(lua_state, type.data(), type.size());
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'geometry_type()'.\n");
}
return 1;
}
int geom_is_null(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
lua_pushboolean(lua_state, input_geometry->is_null());
return 1;
}
int geom_reverse(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
try {
auto *geom = create_lua_geometry_object(lua_state);
geom::reverse(geom, *input_geometry);
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'reverse()'.\n");
}
return 1;
}
int geom_line_merge(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
try {
auto *geom = create_lua_geometry_object(lua_state);
geom::line_merge(geom, *input_geometry);
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'line_merge()'.\n");
}
return 1;
}
int geom_num_geometries(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
lua_pushinteger(lua_state,
static_cast<lua_Integer>(num_geometries(*input_geometry)));
return 1;
}
int geom_pole_of_inaccessibility(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
double stretch = 1.0;
if (lua_gettop(lua_state) > 1) {
if (lua_type(lua_state, 2) != LUA_TTABLE) {
throw std::runtime_error{
"Argument #2 to 'pole_of_inaccessibility' must be a table."};
}
lua_getfield(lua_state, 2, "stretch");
if (lua_isnumber(lua_state, -1)) {
stretch = lua_tonumber(lua_state, -1);
if (stretch <= 0.0) {
throw std::runtime_error{"The 'stretch' factor must be > 0."};
}
} else {
throw std::runtime_error{"The 'stretch' factor must be a number."};
}
}
auto *geom = create_lua_geometry_object(lua_state);
geom::pole_of_inaccessibility(geom, *input_geometry, 0, stretch);
return 1;
}
int geom_segmentize(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
double const max_segment_length = luaL_checknumber(lua_state, 2);
try {
auto *geom = create_lua_geometry_object(lua_state);
geom::segmentize(geom, *input_geometry, max_segment_length);
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'segmentize()'.\n");
}
return 1;
}
int geom_simplify(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
double const tolerance = luaL_checknumber(lua_state, 2);
try {
auto *geom = create_lua_geometry_object(lua_state);
geom::simplify(geom, *input_geometry, tolerance);
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'simplify()'.\n");
}
return 1;
}
int geom_get_bbox(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
try {
auto const box = geom::envelope(*input_geometry);
lua_pushnumber(lua_state, box.min_x());
lua_pushnumber(lua_state, box.min_y());
lua_pushnumber(lua_state, box.max_x());
lua_pushnumber(lua_state, box.max_y());
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'get_bbox()'.\n");
}
return 4;
}
int geom_srid(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
lua_pushinteger(lua_state,
static_cast<lua_Integer>(input_geometry->srid()));
return 1;
}
// XXX Implementation for Lua __tostring function on geometries. Currently
// just returns the type as string. This could be improved, for instance by
// showing a WKT representation of the geometry.
int geom_tostring(lua_State *lua_state)
{
return geom_geometry_type(lua_state);
}
int geom_transform(lua_State *lua_state)
{
auto const *const input_geometry = unpack_geometry(lua_state);
auto const srid = static_cast<int>(luaL_checkinteger(lua_state, 2));
try {
if (input_geometry->srid() != PROJ_LATLONG) {
throw std::runtime_error{
"Can not transform already transformed geometry."};
}
auto *geom = create_lua_geometry_object(lua_state);
geom::transform(geom, *input_geometry, get_projection(srid));
} catch (...) {
return luaL_error(lua_state, "Unknown error in 'transform()'.\n");
}
return 1;
}
} // anonymous namespace
void init_geometry_class(lua_State *lua_state)
{
luaX_set_up_metatable(
lua_state, "Geometry", OSM2PGSQL_GEOMETRY_CLASS,
{{"__gc", geom_gc},
{"__len", geom_num_geometries},
{"__tostring", geom_tostring},
{"area", geom_area},
{"length", geom_length},
{"centroid", geom_centroid},
{"get_bbox", geom_get_bbox},
{"geometry_n", geom_geometry_n},
{"geometry_type", geom_geometry_type},
{"is_null", geom_is_null},
{"line_merge", geom_line_merge},
{"reverse", geom_reverse},
{"num_geometries", geom_num_geometries},
{"pole_of_inaccessibility", geom_pole_of_inaccessibility},
{"segmentize", geom_segmentize},
{"simplify", geom_simplify},
{"spherical_area", geom_spherical_area},
{"srid", geom_srid},
{"transform", geom_transform}});
}