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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
|
/**********************************************************************
*
* 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 2012-2020 Oslandia <infos@oslandia.com>
*
**********************************************************************/
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "lwgeom_pg.h"
#include "lwgeom_sfcgal.h"
#include "../postgis_config.h"
#include "utils/elog.h"
#include "utils/guc.h"
#include "libpq/pqsignal.h"
#include "../liblwgeom/liblwgeom.h"
/*
* This is required for builds against pgsql
*/
PG_MODULE_MAGIC;
#ifdef WIN32
static void interruptCallback() {
if (UNBLOCKED_SIGNAL_QUEUE())
pgwin32_dispatch_queued_signals();
}
#endif
static pqsigfunc coreIntHandler = 0;
static void handleInterrupt(int sig);
/*
* Module load callback
*/
void _PG_init(void);
void
_PG_init(void)
{
coreIntHandler = pqsignal(SIGINT, handleInterrupt);
#ifdef WIN32
GEOS_interruptRegisterCallback(interruptCallback);
lwgeom_register_interrupt_callback(interruptCallback);
#endif
/* install PostgreSQL handlers */
pg_install_lwgeom_handlers();
}
/*
* Module unload callback
*/
void _PG_fini(void);
void
_PG_fini(void)
{
elog(NOTICE, "Goodbye from PostGIS SFCGAL %s", POSTGIS_VERSION);
pqsignal(SIGINT, coreIntHandler);
}
static void
handleInterrupt(int sig)
{
/* NOTE: printf here would be dangerous, see
* https://trac.osgeo.org/postgis/ticket/3644
*
* TODO: block interrupts during execution, to fix the problem
*/
/* printf("Interrupt requested\n"); fflush(stdout); */
/* request interruption of liblwgeom as well */
lwgeom_request_interrupt();
if ( coreIntHandler ) {
(*coreIntHandler)(sig);
}
}
Datum postgis_sfcgal_version(PG_FUNCTION_ARGS);
Datum sfcgal_from_ewkt(PG_FUNCTION_ARGS);
Datum sfcgal_area3D(PG_FUNCTION_ARGS);
Datum sfcgal_intersection3D(PG_FUNCTION_ARGS);
Datum sfcgal_difference3D(PG_FUNCTION_ARGS);
Datum sfcgal_union3D(PG_FUNCTION_ARGS);
Datum sfcgal_volume(PG_FUNCTION_ARGS);
Datum sfcgal_extrude(PG_FUNCTION_ARGS);
Datum sfcgal_straight_skeleton(PG_FUNCTION_ARGS);
Datum sfcgal_approximate_medial_axis(PG_FUNCTION_ARGS);
Datum sfcgal_is_planar(PG_FUNCTION_ARGS);
Datum sfcgal_orientation(PG_FUNCTION_ARGS);
Datum sfcgal_force_lhr(PG_FUNCTION_ARGS);
Datum ST_ConstrainedDelaunayTriangles(PG_FUNCTION_ARGS);
Datum sfcgal_tesselate(PG_FUNCTION_ARGS);
Datum sfcgal_minkowski_sum(PG_FUNCTION_ARGS);
Datum sfcgal_make_solid(PG_FUNCTION_ARGS);
Datum sfcgal_is_solid(PG_FUNCTION_ARGS);
Datum postgis_sfcgal_noop(PG_FUNCTION_ARGS);
GSERIALIZED *geometry_serialize(LWGEOM *lwgeom);
char *text_to_cstring(const text *textptr);
static int __sfcgal_init = 0;
void
sfcgal_postgis_init(void)
{
if (!__sfcgal_init)
{
sfcgal_init();
sfcgal_set_error_handlers((sfcgal_error_handler_t)(void *)lwpgnotice,
(sfcgal_error_handler_t)(void *)lwpgerror);
sfcgal_set_alloc_handlers(lwalloc, lwfree);
__sfcgal_init = 1;
}
}
/* Conversion from GSERIALIZED* to SFCGAL::Geometry */
sfcgal_geometry_t *
POSTGIS2SFCGALGeometry(GSERIALIZED *pglwgeom)
{
sfcgal_geometry_t *g;
LWGEOM *lwgeom = lwgeom_from_gserialized(pglwgeom);
if (!lwgeom)
lwpgerror("POSTGIS2SFCGALGeometry: Unable to deserialize input");
g = LWGEOM2SFCGAL(lwgeom);
lwgeom_free(lwgeom);
return g;
}
/* Conversion from GSERIALIZED* to SFCGAL::PreparedGeometry */
sfcgal_prepared_geometry_t *
POSTGIS2SFCGALPreparedGeometry(GSERIALIZED *pglwgeom)
{
sfcgal_geometry_t *g;
LWGEOM *lwgeom = lwgeom_from_gserialized(pglwgeom);
if (!lwgeom)
lwpgerror("POSTGIS2SFCGALPreparedGeometry: Unable to deserialize input");
g = LWGEOM2SFCGAL(lwgeom);
lwgeom_free(lwgeom);
return sfcgal_prepared_geometry_create_from_geometry(g, gserialized_get_srid(pglwgeom));
}
/* Conversion from SFCGAL::Geometry to GSERIALIZED */
GSERIALIZED *
SFCGALGeometry2POSTGIS(const sfcgal_geometry_t *geom, int force3D, int32_t SRID)
{
GSERIALIZED *result;
LWGEOM *lwgeom = SFCGAL2LWGEOM(geom, force3D, SRID);
if (lwgeom_needs_bbox(lwgeom) == LW_TRUE)
lwgeom_add_bbox(lwgeom);
result = geometry_serialize(lwgeom);
lwgeom_free(lwgeom);
return result;
}
/* Conversion from SFCGAL::PreparedGeometry to GSERIALIZED */
GSERIALIZED *
SFCGALPreparedGeometry2POSTGIS(const sfcgal_prepared_geometry_t *geom, int force3D)
{
return SFCGALGeometry2POSTGIS(
sfcgal_prepared_geometry_geometry(geom), force3D, sfcgal_prepared_geometry_srid(geom));
}
/* Conversion from EWKT to GSERIALIZED */
PG_FUNCTION_INFO_V1(sfcgal_from_ewkt);
Datum sfcgal_from_ewkt(PG_FUNCTION_ARGS)
{
GSERIALIZED *result;
sfcgal_prepared_geometry_t *g;
text *wkttext = PG_GETARG_TEXT_P(0);
char *cstring = text_to_cstring(wkttext);
sfcgal_postgis_init();
g = sfcgal_io_read_ewkt(cstring, strlen(cstring));
result = SFCGALPreparedGeometry2POSTGIS(g, 0);
sfcgal_prepared_geometry_delete(g);
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(sfcgal_area3D);
Datum sfcgal_area3D(PG_FUNCTION_ARGS)
{
GSERIALIZED *input;
sfcgal_geometry_t *geom;
double result;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
geom = POSTGIS2SFCGALGeometry(input);
result = sfcgal_geometry_area_3d(geom);
sfcgal_geometry_delete(geom);
PG_FREE_IF_COPY(input, 0);
PG_RETURN_FLOAT8(result);
}
PG_FUNCTION_INFO_V1(sfcgal_is_planar);
Datum sfcgal_is_planar(PG_FUNCTION_ARGS)
{
GSERIALIZED *input;
sfcgal_geometry_t *geom;
int result;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
geom = POSTGIS2SFCGALGeometry(input);
result = sfcgal_geometry_is_planar(geom);
sfcgal_geometry_delete(geom);
PG_FREE_IF_COPY(input, 0);
PG_RETURN_BOOL(result);
}
PG_FUNCTION_INFO_V1(sfcgal_orientation);
Datum sfcgal_orientation(PG_FUNCTION_ARGS)
{
GSERIALIZED *input;
sfcgal_geometry_t *geom;
int result;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
geom = POSTGIS2SFCGALGeometry(input);
result = sfcgal_geometry_orientation(geom);
sfcgal_geometry_delete(geom);
PG_FREE_IF_COPY(input, 0);
PG_RETURN_INT32(result);
}
PG_FUNCTION_INFO_V1(sfcgal_tesselate);
Datum sfcgal_tesselate(PG_FUNCTION_ARGS)
{
GSERIALIZED *input, *output;
sfcgal_geometry_t *geom;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input);
geom = POSTGIS2SFCGALGeometry(input);
PG_FREE_IF_COPY(input, 0);
result = sfcgal_geometry_tesselate(geom);
sfcgal_geometry_delete(geom);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(ST_ConstrainedDelaunayTriangles);
Datum ST_ConstrainedDelaunayTriangles(PG_FUNCTION_ARGS)
{
GSERIALIZED *input, *output;
sfcgal_geometry_t *geom;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input);
geom = POSTGIS2SFCGALGeometry(input);
PG_FREE_IF_COPY(input, 0);
result = sfcgal_geometry_triangulate_2dz(geom);
sfcgal_geometry_delete(geom);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_force_lhr);
Datum sfcgal_force_lhr(PG_FUNCTION_ARGS)
{
GSERIALIZED *input, *output;
sfcgal_geometry_t *geom;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input);
geom = POSTGIS2SFCGALGeometry(input);
PG_FREE_IF_COPY(input, 0);
result = sfcgal_geometry_force_lhr(geom);
sfcgal_geometry_delete(geom);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_straight_skeleton);
Datum sfcgal_straight_skeleton(PG_FUNCTION_ARGS)
{
GSERIALIZED *input, *output;
sfcgal_geometry_t *geom;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input);
geom = POSTGIS2SFCGALGeometry(input);
PG_FREE_IF_COPY(input, 0);
result = sfcgal_geometry_straight_skeleton(geom);
sfcgal_geometry_delete(geom);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_approximate_medial_axis);
Datum sfcgal_approximate_medial_axis(PG_FUNCTION_ARGS)
{
GSERIALIZED *input, *output;
sfcgal_geometry_t *geom;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input);
geom = POSTGIS2SFCGALGeometry(input);
PG_FREE_IF_COPY(input, 0);
result = sfcgal_geometry_approximate_medial_axis(geom);
sfcgal_geometry_delete(geom);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_intersection3D);
Datum sfcgal_intersection3D(PG_FUNCTION_ARGS)
{
GSERIALIZED *input0, *input1, *output;
sfcgal_geometry_t *geom0, *geom1;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input0 = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input0);
input1 = PG_GETARG_GSERIALIZED_P(1);
geom0 = POSTGIS2SFCGALGeometry(input0);
PG_FREE_IF_COPY(input0, 0);
geom1 = POSTGIS2SFCGALGeometry(input1);
PG_FREE_IF_COPY(input1, 1);
result = sfcgal_geometry_intersection_3d(geom0, geom1);
sfcgal_geometry_delete(geom0);
sfcgal_geometry_delete(geom1);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_difference3D);
Datum sfcgal_difference3D(PG_FUNCTION_ARGS)
{
GSERIALIZED *input0, *input1, *output;
sfcgal_geometry_t *geom0, *geom1;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input0 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
srid = gserialized_get_srid(input0);
input1 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
geom0 = POSTGIS2SFCGALGeometry(input0);
PG_FREE_IF_COPY(input0, 0);
geom1 = POSTGIS2SFCGALGeometry(input1);
PG_FREE_IF_COPY(input1, 1);
result = sfcgal_geometry_difference_3d(geom0, geom1);
sfcgal_geometry_delete(geom0);
sfcgal_geometry_delete(geom1);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_union3D);
Datum sfcgal_union3D(PG_FUNCTION_ARGS)
{
GSERIALIZED *input0, *input1, *output;
sfcgal_geometry_t *geom0, *geom1;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input0 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
srid = gserialized_get_srid(input0);
input1 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
geom0 = POSTGIS2SFCGALGeometry(input0);
PG_FREE_IF_COPY(input0, 0);
geom1 = POSTGIS2SFCGALGeometry(input1);
PG_FREE_IF_COPY(input1, 1);
result = sfcgal_geometry_union_3d(geom0, geom1);
sfcgal_geometry_delete(geom0);
sfcgal_geometry_delete(geom1);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_volume);
Datum sfcgal_volume(PG_FUNCTION_ARGS)
{
GSERIALIZED *input;
sfcgal_geometry_t *geom;
double result;
sfcgal_postgis_init();
input = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
geom = POSTGIS2SFCGALGeometry(input);
result = sfcgal_geometry_volume(geom);
sfcgal_geometry_delete(geom);
PG_FREE_IF_COPY(input, 0);
PG_RETURN_FLOAT8(result);
}
PG_FUNCTION_INFO_V1(sfcgal_minkowski_sum);
Datum sfcgal_minkowski_sum(PG_FUNCTION_ARGS)
{
GSERIALIZED *input0, *input1, *output;
sfcgal_geometry_t *geom0, *geom1;
sfcgal_geometry_t *result;
srid_t srid;
sfcgal_postgis_init();
input0 = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input0);
input1 = PG_GETARG_GSERIALIZED_P(1);
geom0 = POSTGIS2SFCGALGeometry(input0);
PG_FREE_IF_COPY(input0, 0);
geom1 = POSTGIS2SFCGALGeometry(input1);
PG_FREE_IF_COPY(input1, 1);
result = sfcgal_geometry_minkowski_sum(geom0, geom1);
sfcgal_geometry_delete(geom0);
sfcgal_geometry_delete(geom1);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(sfcgal_extrude);
Datum sfcgal_extrude(PG_FUNCTION_ARGS)
{
GSERIALIZED *input, *output;
sfcgal_geometry_t *geom;
sfcgal_geometry_t *result;
double dx, dy, dz;
srid_t srid;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
srid = gserialized_get_srid(input);
geom = POSTGIS2SFCGALGeometry(input);
PG_FREE_IF_COPY(input, 0);
dx = PG_GETARG_FLOAT8(1);
dy = PG_GETARG_FLOAT8(2);
dz = PG_GETARG_FLOAT8(3);
result = sfcgal_geometry_extrude(geom, dx, dy, dz);
sfcgal_geometry_delete(geom);
output = SFCGALGeometry2POSTGIS(result, 0, srid);
sfcgal_geometry_delete(result);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(postgis_sfcgal_version);
Datum postgis_sfcgal_version(PG_FUNCTION_ARGS)
{
const char *ver = lwgeom_sfcgal_version();
text *result = cstring_to_text(ver);
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(sfcgal_is_solid);
Datum sfcgal_is_solid(PG_FUNCTION_ARGS)
{
int result;
GSERIALIZED *input = PG_GETARG_GSERIALIZED_P(0);
LWGEOM *lwgeom = lwgeom_from_gserialized(input);
PG_FREE_IF_COPY(input, 0);
if (!lwgeom)
elog(ERROR, "sfcgal_is_solid: Unable to deserialize input");
result = lwgeom_is_solid(lwgeom);
lwgeom_free(lwgeom);
PG_RETURN_BOOL(result);
}
PG_FUNCTION_INFO_V1(sfcgal_make_solid);
Datum sfcgal_make_solid(PG_FUNCTION_ARGS)
{
GSERIALIZED *output;
GSERIALIZED *input = PG_GETARG_GSERIALIZED_P(0);
LWGEOM *lwgeom = lwgeom_from_gserialized(input);
if (!lwgeom)
elog(ERROR, "sfcgal_make_solid: Unable to deserialize input");
FLAGS_SET_SOLID(lwgeom->flags, 1);
output = geometry_serialize(lwgeom);
lwgeom_free(lwgeom);
PG_FREE_IF_COPY(input, 0);
PG_RETURN_POINTER(output);
}
PG_FUNCTION_INFO_V1(postgis_sfcgal_noop);
Datum postgis_sfcgal_noop(PG_FUNCTION_ARGS)
{
GSERIALIZED *input, *output;
LWGEOM *geom, *result;
sfcgal_postgis_init();
input = PG_GETARG_GSERIALIZED_P(0);
geom = lwgeom_from_gserialized(input);
if (!geom)
elog(ERROR, "sfcgal_noop: Unable to deserialize input");
result = lwgeom_sfcgal_noop(geom);
lwgeom_free(geom);
if (!result)
elog(ERROR, "sfcgal_noop: Unable to deserialize lwgeom");
output = geometry_serialize(result);
PG_FREE_IF_COPY(input, 0);
PG_RETURN_POINTER(output);
}
|