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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
|
/*
Copyright (C) 2004-2023 Sergey Koposov
Email: skoposov AT ed DOT ac DOT uk
This file is part of Q3C.
Q3C 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.
Q3C 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 Q3C; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
/* Postgres stuff */
#include "postgres.h"
#include "executor/spi.h"
#include "utils/lsyscache.h"
/* I included that just to remove the gcc warning
* q3c.c:128: warning: implicit declaration of function `get_typlenbyvalalign'
*/
#include "utils/array.h"
#include "utils/geo_decls.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#if PG_VERSION_NUM >= 90300
#include "access/tupmacs.h"
#endif
//#include "nodes/relation.h"
#include "utils/selfuncs.h"
/* For PostgreSQL versions >= 8.2 */
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/* End of Postgres stuff */
#include "common.h"
extern Node *estimate_expression_value(PlannerInfo *root, Node *node);
/* Postgres functions */
Datum pgq3c_ang2ipix(PG_FUNCTION_ARGS);
Datum pgq3c_ang2ipix_real(PG_FUNCTION_ARGS);
Datum pgq3c_ipix2ang(PG_FUNCTION_ARGS);
Datum pgq3c_pixarea(PG_FUNCTION_ARGS);
Datum pgq3c_dist(PG_FUNCTION_ARGS);
Datum pgq3c_dist_pm(PG_FUNCTION_ARGS);
Datum pgq3c_sindist(PG_FUNCTION_ARGS);
Datum pgq3c_sindist_pm(PG_FUNCTION_ARGS);
Datum q3c_strquery(PG_FUNCTION_ARGS);
Datum pgq3c_nearby_it(PG_FUNCTION_ARGS);
Datum pgq3c_nearby_pm_it(PG_FUNCTION_ARGS);
Datum pgq3c_ellipse_nearby_it(PG_FUNCTION_ARGS);
Datum pgq3c_radial_array(PG_FUNCTION_ARGS);
Datum pgq3c_radial_query_it(PG_FUNCTION_ARGS);
Datum pgq3c_ellipse_query_it(PG_FUNCTION_ARGS);
Datum pgq3c_poly_query_it(PG_FUNCTION_ARGS);
Datum pgq3c_poly_query1_it(PG_FUNCTION_ARGS);
Datum pgq3c_in_ellipse(PG_FUNCTION_ARGS);
Datum pgq3c_in_poly(PG_FUNCTION_ARGS);
Datum pgq3c_in_poly1(PG_FUNCTION_ARGS);
Datum pgq3c_get_version(PG_FUNCTION_ARGS);
Datum pgq3c_sel(PG_FUNCTION_ARGS);
Datum pgq3c_seljoin(PG_FUNCTION_ARGS);
Datum pgq3c_seloper(PG_FUNCTION_ARGS);
/* Dummy function that implements the selectivity operator */
PG_FUNCTION_INFO_V1(pgq3c_seloper);
Datum pgq3c_seloper(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(true);
}
/* The actual selectivity function, it returns the ratio of the
* search circle to the whole sky area
*/
PG_FUNCTION_INFO_V1(pgq3c_sel);
Datum pgq3c_sel(PG_FUNCTION_ARGS)
{
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
List *args = (List *) PG_GETARG_POINTER(2);
int varRelid = PG_GETARG_INT32(3);
Node *left;
Node *other;
VariableStatData vardata;
Datum radDatum;
bool isnull;
double rad;
double ratio;
/* this needs more protections against crazy inputs */
if (list_length(args) != 2) { elog(ERROR, "Wrong inputs to selectivity function");}
left = (Node *) linitial(args);
examine_variable(root, left, varRelid, &vardata);
other = estimate_expression_value(root, vardata.var);
radDatum = ((Const *) other)->constvalue;
isnull = ((Const *) other)->constisnull;
/* We shouldn't be really getting null inputs here */
if (!isnull)
{
rad = DatumGetFloat8(radDatum);
}
else
{
rad = 0;
}
ratio = 3.14 * rad * rad / 41252.; /* pi*r^2/whole_sky_area */
/* clamp at 0, 1*/
CLAMP_PROBABILITY(ratio);
//elog(WARNING, "HERE0.... %e", ratio);
PG_RETURN_FLOAT8(ratio);
}
PG_FUNCTION_INFO_V1(pgq3c_seljoin);
Datum pgq3c_seljoin(PG_FUNCTION_ARGS)
{
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
List *args = (List *) PG_GETARG_POINTER(2);
int varRelid = 0;
/* Because there is no varrelid in the join selectivity call
* I just set it to zero */
Node *left;
Node *other;
VariableStatData vardata;
Datum radDatum;
bool isnull;
double rad;
double ratio;
/* this needs more protections against crazy inputs */
if (list_length(args) != 2) { elog(ERROR, "Wrong inputs to selectivity function");}
left = (Node *) linitial(args);
examine_variable(root, left, varRelid, &vardata);
other = estimate_expression_value(root, vardata.var);
radDatum = ((Const *) other)->constvalue;
isnull = ((Const *) other)->constisnull;
/* We shouldn't be really getting null inputs here */
if (!isnull)
{
rad = DatumGetFloat8(radDatum);
}
else
{
rad = 0;
}
ratio = 3.14 * rad * rad / 41252.; /* pi*r^2/whole_sky_area */
/* clamp at 0, 1*/
CLAMP_PROBABILITY(ratio);
PG_RETURN_FLOAT8(ratio);
}
static int convert_pgarray2poly(ArrayType *poly_arr, q3c_coord_t *in_ra, q3c_coord_t *in_dec, int *nvert);
PG_FUNCTION_INFO_V1(pgq3c_get_version);
Datum pgq3c_get_version(PG_FUNCTION_ARGS)
{
char VERSION_MAX_BYTES = 100;
char *buf = palloc(VERSION_MAX_BYTES);
q3c_get_version(buf, VERSION_MAX_BYTES);
PG_RETURN_CSTRING(buf);
}
PG_FUNCTION_INFO_V1(pgq3c_ang2ipix);
Datum pgq3c_ang2ipix(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra = PG_GETARG_FLOAT8(0);
q3c_coord_t dec = PG_GETARG_FLOAT8(1);
q3c_ipix_t ipix;
static int invocation;
static q3c_coord_t ra_buf, dec_buf;
static q3c_ipix_t ipix_buf;
if (invocation == 0)
{
}
else
{
if ((ra == ra_buf) && (dec == dec_buf))
{
PG_RETURN_INT64(ipix_buf);
}
}
if ((!isfinite(ra)) || (!isfinite(dec)))
{
PG_RETURN_NULL();
}
q3c_ang2ipix(&hprm, ra, dec, &ipix);
ra_buf = ra;
dec_buf = dec;
ipix_buf = ipix;
invocation = 1;
PG_RETURN_INT64(ipix);
}
PG_FUNCTION_INFO_V1(pgq3c_ang2ipix_real);
Datum pgq3c_ang2ipix_real(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra = PG_GETARG_FLOAT4(0);
q3c_coord_t dec = PG_GETARG_FLOAT4(1);
q3c_ipix_t ipix;
static int invocation;
static q3c_coord_t ra_buf, dec_buf;
static q3c_ipix_t ipix_buf;
if (invocation == 0)
{
}
else
{
if ((ra == ra_buf) && (dec == dec_buf))
{
PG_RETURN_INT64(ipix_buf);
}
}
if ((!isfinite(ra)) || (!isfinite(dec)))
{
PG_RETURN_NULL();
}
q3c_ang2ipix(&hprm, ra, dec, &ipix);
ra_buf = ra;
dec_buf = dec;
ipix_buf = ipix;
invocation = 1;
PG_RETURN_INT64(ipix);
}
PG_FUNCTION_INFO_V1(pgq3c_ipix2ang);
Datum pgq3c_ipix2ang(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_ipix_t ipix;
q3c_coord_t ra, dec;
Datum *data;
int16 typlen;
bool typbyval;
char typalign;
ArrayType *result;
ipix = PG_GETARG_INT64(0);
if ((ipix < 0) || (ipix > Q3C_MAX_IPIX))
{
elog(ERROR, "Invalid ipix value");
}
q3c_ipix2ang(&hprm, ipix, &ra, &dec);
data = ( Datum *) palloc(sizeof(Datum) * 2);
data[0] = Float8GetDatum (ra);
data[1] = Float8GetDatum (dec);
/* get required info about the element type */
get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
/* now build the array */
result = construct_array(data, 2, FLOAT8OID, typlen, typbyval, typalign);
PG_RETURN_ARRAYTYPE_P(result);
}
PG_FUNCTION_INFO_V1(pgq3c_pixarea);
Datum pgq3c_pixarea(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_ipix_t ipix;
q3c_coord_t res;
int depth;
ipix = PG_GETARG_INT64(0);
depth = PG_GETARG_INT32(1);
if (depth <= 0)
{
elog(ERROR, "Invalid depth. It should be greater than 0.");
}
if (depth > 30)
{
elog(ERROR, "Invalid depth. It should be less than 31.");
}
if (ipix < 0)
{
elog(ERROR, "Invalid ipix value");
}
if (ipix > Q3C_MAX_IPIX)
{
elog(ERROR, "Invalid ipix value");
}
res = q3c_pixarea(&hprm, ipix, depth);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_dist);
Datum pgq3c_dist(PG_FUNCTION_ARGS)
{
q3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
q3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
q3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
q3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
q3c_coord_t res;
res = q3c_dist(ra1, dec1, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_sindist);
Datum pgq3c_sindist(PG_FUNCTION_ARGS)
{
q3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
q3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
q3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
q3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
q3c_coord_t res;
res = q3c_sindist(ra1, dec1, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_sindist_pm);
Datum pgq3c_sindist_pm(PG_FUNCTION_ARGS)
{
q3c_coord_t pmra1, pmdec1, epoch1, epoch2;
q3c_coord_t ra1, dec1, ra2, dec2, ra1_shift, dec1_shift, cdec;
bool pm_enabled = true, cosdec;
q3c_coord_t res;
const int ra_arg_pos = 0, dec_arg_pos = 1, pmra_arg_pos = 2, pmdec_arg_pos = 3,
cosdec_arg_pos = 4, epoch_arg_pos = 5, ra2_arg_pos = 6, dec2_arg_pos = 7,
epoch2_arg_pos = 8;
if (PG_ARGISNULL(ra_arg_pos) || PG_ARGISNULL(dec_arg_pos) ||
PG_ARGISNULL(ra2_arg_pos) || PG_ARGISNULL(dec2_arg_pos))
{
PG_RETURN_NULL();
}
ra1 = PG_GETARG_FLOAT8(ra_arg_pos);
dec1 = PG_GETARG_FLOAT8(dec_arg_pos);
if ((!PG_ARGISNULL(pmra_arg_pos)) &&
(!PG_ARGISNULL(pmdec_arg_pos)) &&
(!PG_ARGISNULL(epoch_arg_pos)) && (!PG_ARGISNULL(epoch2_arg_pos)))
{
pmra1 = PG_GETARG_FLOAT8(pmra_arg_pos);
pmdec1 = PG_GETARG_FLOAT8(pmdec_arg_pos);
epoch1 = PG_GETARG_FLOAT8(epoch_arg_pos);
epoch2 = PG_GETARG_FLOAT8(epoch2_arg_pos);
}
else
{
pm_enabled = false;
pmra1 = 0;
pmdec1 = 0;
epoch1 = 0;
epoch2 = 0;
}
cosdec = PG_GETARG_INT32(cosdec_arg_pos) != 0;
ra2 = PG_GETARG_FLOAT8(ra2_arg_pos);
dec2 = PG_GETARG_FLOAT8(dec2_arg_pos);
if (pm_enabled)
{
if (cosdec)
{
cdec = cos(dec1 * Q3C_DEGRA);
}
else
{
cdec = 1;
}
ra1_shift = ra1 + pmra1 * (epoch2 - epoch1) / cdec / 3600000;
dec1_shift = dec1 + pmdec1 * (epoch2 - epoch1) / 3600000;
}
else
{
ra1_shift = ra1;
dec1_shift = dec1;
}
res = q3c_sindist(ra1_shift, dec1_shift, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_dist_pm);
Datum pgq3c_dist_pm(PG_FUNCTION_ARGS)
{
q3c_coord_t pmra1, pmdec1, epoch1, epoch2;
q3c_coord_t ra1, dec1, ra2, dec2, ra1_shift, dec1_shift, cdec = 1;
bool pm_enabled = true, cosdec;
q3c_coord_t res;
const int ra_arg_pos = 0, dec_arg_pos = 1, pmra_arg_pos = 2, pmdec_arg_pos = 3,
cosdec_arg_pos = 4, epoch_arg_pos = 5, ra2_arg_pos = 6, dec2_arg_pos = 7,
epoch2_arg_pos = 8;
if (PG_ARGISNULL(ra_arg_pos) || PG_ARGISNULL(dec_arg_pos) ||
PG_ARGISNULL(ra2_arg_pos) || PG_ARGISNULL(dec2_arg_pos))
{
PG_RETURN_NULL();
}
ra1 = PG_GETARG_FLOAT8(ra_arg_pos);
dec1 = PG_GETARG_FLOAT8(dec_arg_pos);
if ((!PG_ARGISNULL(pmra_arg_pos)) && (!PG_ARGISNULL(pmdec_arg_pos)) &&
(!PG_ARGISNULL(epoch_arg_pos)) && (!PG_ARGISNULL(epoch2_arg_pos)))
{
pmra1 = PG_GETARG_FLOAT8(pmra_arg_pos);
pmdec1 = PG_GETARG_FLOAT8(pmdec_arg_pos);
epoch1 = PG_GETARG_FLOAT8(epoch_arg_pos);
epoch2 = PG_GETARG_FLOAT8(epoch2_arg_pos);
}
else
{
pm_enabled = false;
pmra1 = 0;
pmdec1 = 0;
epoch1 = 0;
epoch2 = 0;
}
cosdec = PG_GETARG_INT32(cosdec_arg_pos) != 0;
ra2 = PG_GETARG_FLOAT8(ra2_arg_pos);
dec2 = PG_GETARG_FLOAT8(dec2_arg_pos);
if (pm_enabled)
{
if (cosdec)
{
cdec = cos(dec1 * Q3C_DEGRA);
}
else
{
cdec = 1;
}
ra1_shift = ra1 + pmra1 * (epoch2 - epoch1) / cdec / 3600000;
dec1_shift = dec1 + pmdec1 * (epoch2 - epoch1) / 3600000;
}
else
{
ra1_shift = ra1;
dec1_shift = dec1;
}
res = q3c_dist(ra1_shift, dec1_shift, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_nearby_it);
Datum pgq3c_nearby_it(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix_array[8];
static q3c_ipix_t ipix_array_buf[8];
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static int invocation;
int i;
extern struct q3c_prm hprm;
q3c_circle_region circle;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0); // ra_cen
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1); // dec_cen
q3c_coord_t radius = PG_GETARG_FLOAT8(2); // error radius
int iteration = PG_GETARG_INT32(3); // iteration
if ( (!isfinite(ra_cen)) || (!isfinite(dec_cen)) )
{
elog(ERROR, "The values of ra,dec are infinites or NaNs");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
{
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
}
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90) {dec_cen = q3c_fmod(dec_cen,90);}
circle.ra = ra_cen;
circle.dec = dec_cen;
circle.rad = radius;
q3c_get_nearby(&hprm, Q3C_CIRCLE, &circle, ipix_array);
for(i = 0; i < 8; i++)
{
ipix_array_buf[i] = ipix_array[i];
}
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
invocation = 1;
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
PG_FUNCTION_INFO_V1(pgq3c_nearby_pm_it);
Datum pgq3c_nearby_pm_it(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix_array[8];
static q3c_ipix_t ipix_array_buf[8];
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static q3c_coord_t pmra_buf, pmdec_buf, max_epoch_delta_buf;
static int invocation;
int i;
extern struct q3c_prm hprm;
q3c_circle_region circle;
q3c_coord_t new_radius;
q3c_coord_t ra_cen, dec_cen, pmra, pmdec;
q3c_coord_t max_epoch_delta = 0, radius = 0;
bool pm_enabled = true, cosdec;
int iteration;
const int ra_arg_pos = 0, dec_arg_pos = 1, pmra_arg_pos = 2, pmdec_arg_pos = 3,
cosdec_arg_pos = 4, maxepochdelta_arg_pos = 5, radius_arg_pos = 6, iteration_arg_pos = 7;
if (PG_ARGISNULL(ra_arg_pos) || PG_ARGISNULL(dec_arg_pos) || PG_ARGISNULL(radius_arg_pos))
{
elog(ERROR, "Right Ascensions and raddii must be not null");
}
ra_cen = PG_GETARG_FLOAT8(ra_arg_pos); // ra_cen
dec_cen = PG_GETARG_FLOAT8(dec_arg_pos); // dec_cen
if ((!PG_ARGISNULL(pmra_arg_pos)) && (!PG_ARGISNULL(pmdec_arg_pos)) &&
(!PG_ARGISNULL(maxepochdelta_arg_pos)))
{
pmra = PG_GETARG_FLOAT8(pmra_arg_pos);
pmdec = PG_GETARG_FLOAT8(pmdec_arg_pos);
max_epoch_delta = PG_GETARG_FLOAT8(maxepochdelta_arg_pos);
}
else
{
pm_enabled = false;
pmra = 0;
pmdec = 0;
max_epoch_delta = 0;
}
cosdec = PG_GETARG_INT32(cosdec_arg_pos) != 0;
radius = PG_GETARG_FLOAT8(radius_arg_pos); // error radius
iteration = PG_GETARG_INT32(iteration_arg_pos); // iteration
if ( (!isfinite(ra_cen)) || (!isfinite(dec_cen)) )
{
elog(ERROR, "The values of ra,dec are infinites or NaNs");
}
if ( (!isfinite(pmra)) || (!isfinite(pmdec)) ||
(!isfinite(max_epoch_delta)) )
{
pmra = 0;
pmdec = 0;
max_epoch_delta = 0;
}
if (max_epoch_delta < 0)
{
elog(ERROR, "The maximum epoch difference must be >=0 ");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
(radius == radius_buf) && (pmra == pmra_buf) &&
(pmdec == pmdec_buf) && (max_epoch_delta == max_epoch_delta_buf))
{
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
}
if (pm_enabled)
{
q3c_coord_t pmra1;
if (cosdec) { pmra1 = pmra; } else { pmra1 = pmra * cos(Q3C_DEGRA * dec_cen);}
new_radius = q3c_sqrt(pmra1 * pmra1 + pmdec * pmdec) / 3600000 * max_epoch_delta + radius;
}
else
{
new_radius = radius;
}
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90) {dec_cen = q3c_fmod(dec_cen,90);}
circle.ra = ra_cen;
circle.dec = dec_cen;
circle.rad = new_radius;
q3c_get_nearby(&hprm, Q3C_CIRCLE, &circle, ipix_array);
for(i = 0; i < 8; i++)
{
ipix_array_buf[i] = ipix_array[i];
}
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
max_epoch_delta_buf = max_epoch_delta;
pmra_buf = pmra;
pmdec_buf = pmdec;
invocation = 1;
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
PG_FUNCTION_INFO_V1(pgq3c_ellipse_nearby_it);
Datum pgq3c_ellipse_nearby_it(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix_array[8];
static q3c_ipix_t ipix_array_buf[8];
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf, axis_ratio_buf, PA_buf;
static int invocation;
int i;
q3c_ellipse_region ellipse;
extern struct q3c_prm hprm;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0); /* ra_cen */
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1); /* dec_cen */
q3c_coord_t radius = PG_GETARG_FLOAT8(2); /* error radius */
q3c_coord_t axis_ratio = PG_GETARG_FLOAT8(3); /* axis_ratio */
q3c_coord_t PA = PG_GETARG_FLOAT8(4); /* PA */
int iteration = PG_GETARG_INT32(5); /* iteration */
if ( (!isfinite(ra_cen)) || (!isfinite(dec_cen)) || (!isfinite(radius)) )
{
elog(ERROR, "The values of ra,dec,radius are infinites or NaNs");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
(radius == radius_buf) && (PA == PA_buf) &&
(axis_ratio == axis_ratio_buf))
{
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
}
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90) {dec_cen = q3c_fmod(dec_cen,90);}
ellipse.ra = ra_cen;
ellipse.dec = dec_cen;
ellipse.rad = radius;
ellipse.e = q3c_sqrt ( 1 - axis_ratio * axis_ratio );
ellipse.PA = PA;
q3c_get_nearby(&hprm, Q3C_ELLIPSE, &ellipse, ipix_array);
for(i = 0; i < 8; i++)
{
ipix_array_buf[i] = ipix_array[i];
}
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
axis_ratio_buf = axis_ratio;
PA_buf = PA;
invocation = 1;
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
PG_FUNCTION_INFO_V1(pgq3c_radial_query_it);
Datum pgq3c_radial_query_it(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
q3c_coord_t radius = PG_GETARG_FLOAT8(2); /* error radius */
int iteration = PG_GETARG_INT32(3); /* iteration */
int full_flag = PG_GETARG_INT32(4); /* full_flag */
/* 1 means full, 0 means partial */
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static q3c_ipix_t partials[2 * Q3C_NPARTIALS];
static q3c_ipix_t fulls[2 * Q3C_NFULLS];
/* !!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
* Here the Q3C_NPARTIALS and Q3C_NFULLS is the number of pairs !!! of ranges
* So we should have the array with the size twice bigger
*/
static int invocation;
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90)
{
elog(ERROR, "The absolute value of declination > 90!");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
{
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
}
q3c_radial_query(&hprm, ra_cen, dec_cen, radius, fulls, partials);
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
invocation = 1;
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
PG_FUNCTION_INFO_V1(pgq3c_ellipse_query_it);
Datum pgq3c_ellipse_query_it(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
q3c_coord_t radius = PG_GETARG_FLOAT8(2); /* Major axis */
q3c_coord_t axis_ratio = PG_GETARG_FLOAT8(3); /* Axis ratio */
q3c_coord_t PA = PG_GETARG_FLOAT8(4); /* PA */
int iteration = PG_GETARG_INT32(5); /* iteration */
int full_flag = PG_GETARG_INT32(6); /* full_flag */
q3c_coord_t ell = q3c_sqrt ( 1 - axis_ratio * axis_ratio );
/* 1 means full, 0 means partial */
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static q3c_ipix_t partials[2 * Q3C_NPARTIALS];
static q3c_ipix_t fulls[2 * Q3C_NFULLS];
/* !!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
* Here the Q3C_NPARTIALS and Q3C_NFULLS is the number of pairs !!! of ranges
* So we should have the array with the size twice bigger
*/
static int invocation;
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90)
{
elog(ERROR, "The absolute value of declination > 90!");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
{
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
}
q3c_ellipse_query(&hprm, ra_cen, dec_cen, radius, ell, PA, fulls,
partials);
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
invocation = 1;
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
static q3c_coord_t read_from_array(char **p, bits8 *bitmap, int *bitmask, bool typbyval,
char typalign, int16 typlen)
{
q3c_coord_t val;
/* Taken from /pgsql/src/backend/utils/adt/arrayfuncs.c
* function deconstruct_array
*/
if (bitmap && (*bitmap & *bitmask) == 0)
{
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("null array element not allowed in this context")));
}
val = DatumGetFloat8(fetch_att(*p, typbyval, typlen));
*p = att_addlength_pointer(*p, typlen, PointerGetDatum(p));
*p = (char *) att_align_nominal(*p, typalign);
if (bitmap)
{
*bitmask <<= 1;
if (*bitmask == 0x100)
{
bitmap++;
*bitmask = 1;
}
}
return val;
}
/* Convert the PG array in two c arrays of ra,dec */
static int convert_pgarray2poly(ArrayType *poly_arr, q3c_coord_t *in_ra, q3c_coord_t *in_dec, int *nvert)
{
int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
Oid element_type = FLOAT8OID;
int identical = 1;
int16 typlen;
bool typbyval;
char typalign;
int i;
q3c_coord_t ra_cur, dec_cur;
char *p;
bits8 *bitmap;
int bitmask;
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
/* Taken from /pgsql/src/backend/utils/adt/arrayfuncs.c
* function deconstruct_array
*/
if ((poly_nitems % 2) != 0)
{
elog(ERROR, "Invalid array argument!\nThe array should contain even number of elements");
}
else if (poly_nitems <= 4)
{
elog(ERROR, "Invalid polygon! The polygon must have more than two vertices");
}
else if (poly_nitems > (2 * Q3C_MAX_N_POLY_VERTEX))
{
elog(ERROR,"Polygons with more than 100 vertices are not supported");
}
p = ARR_DATA_PTR(poly_arr);
poly_nitems /= 2;
*nvert = poly_nitems;
identical = 1;
bitmap = ARR_NULLBITMAP(poly_arr);
bitmask = 1;
for (i = 0; i < poly_nitems; i++)
{
ra_cur = read_from_array(&p, bitmap, &bitmask, typbyval, typalign, typlen);
dec_cur = read_from_array(&p, bitmap, &bitmask, typbyval, typalign, typlen);
if ((in_ra[i] != ra_cur) || (in_dec[i] != dec_cur))
{
identical = 0;
}
in_ra[i] = ra_cur;
in_dec[i] = dec_cur;
}
return identical;
}
/* Convert Postgresql polygon in two c arrays */
static int convert_pgpoly2poly(POLYGON *poly, q3c_coord_t *ra, q3c_coord_t *dec, int *n)
{
int i, npts = poly->npts;
q3c_coord_t newx, newy;
int identical = 1;
*n = npts;
if (npts < 3)
{
elog(ERROR, "Invalid polygon! The polygon must have more than two vertices");
}
else if (npts > Q3C_MAX_N_POLY_VERTEX)
{
elog(ERROR,"Polygons with more than 100 vertices are not supported");
}
for(i = 0; i < npts; i++)
{
newx = poly->p[i].x;
newy = poly->p[i].y;
if ((newx != ra[i]) || (newy != dec[i])) {identical = 0;}
ra[i] = newx;
dec[i] = newy;
}
return identical;
}
typedef struct q3c_poly_info_type {
/* !!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
* Here the Q3C_NPARTIALS and Q3C_NFULLS is the number of pairs !!! of ranges
* So we should have the array with the size twice bigger
*/
int ready;
q3c_ipix_t partials[2 * Q3C_NPARTIALS]; /* array of ipixes partially covered */
q3c_ipix_t fulls[2 * Q3C_NFULLS]; /* array of ipixes fully covered */
q3c_coord_t ra[Q3C_MAX_N_POLY_VERTEX],
dec[Q3C_MAX_N_POLY_VERTEX], x[Q3C_MAX_N_POLY_VERTEX],y[Q3C_MAX_N_POLY_VERTEX],
ax[Q3C_MAX_N_POLY_VERTEX], ay[Q3C_MAX_N_POLY_VERTEX];
q3c_coord_t xpj[3][Q3C_MAX_N_POLY_VERTEX], ypj[3][Q3C_MAX_N_POLY_VERTEX],
axpj[3][Q3C_MAX_N_POLY_VERTEX], aypj[3][Q3C_MAX_N_POLY_VERTEX];
// arrays storing the ra,dec ,projected x,y
char faces[6];
char multi_flag;
/* IF YOU MAKE CHANGES MAKE SURE YOU CHANGE THE COPY() FUNCTION */
} q3c_poly_info_type;
static void copy_q3c_poly_info_type(q3c_poly_info_type *a, q3c_poly_info_type *b)
{
int i,j;
for (i = 0; i < (2 * Q3C_NPARTIALS); i++)
{
b->partials[i] = a->partials[i];
}
for (i = 0; i < (2 * Q3C_NPARTIALS); i++)
{
b->fulls[i] = a->fulls[i];
}
for (i = 0; i < Q3C_MAX_N_POLY_VERTEX; i++)
{
b->ra[i] = a->ra[i];
b->dec[i] = a->dec[i];
b->x[i] = a->x[i];
b->y[i] = a->y[i];
b->ax[i] = a->ax[i];
b->ay[i] = a->ay[i];
for (j = 0; j < 3; j++)
{
b->axpj[j][i] = a->axpj[j][i];
b->aypj[j][i] = a->aypj[j][i];
b->xpj[j][i] = a->xpj[j][i];
b->ypj[j][i] = a->ypj[j][i];
}
}
for (i = 0; i < 6; i++)
{
b->faces[i] = a->faces[i];
}
b->multi_flag = a->multi_flag;
}
/* Cache logic here is the following
when the function is called for the first time with iteration =0
I compute everything allocate memory and store computations in the static variable
and locally allocated q3c_poly_info_table
when the function is called for the first time and iteration !=0
I allocate new memory, copy stuff from static variable into locally allocated stuff
I make no checks of the data
If the function is called for the second time (i.e. fn_extra is not null)
I blindly assume everything is EXACTLY the same and do not recompute anything
as the q3c_poly_query_it() is the internal function and is ONLY supposed
to be called with the constant polygon
*/
PG_FUNCTION_INFO_V1(pgq3c_poly_query_it);
Datum pgq3c_poly_query_it(PG_FUNCTION_ARGS)
{
ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
int iteration = PG_GETARG_INT32(1); /* iteration */
int full_flag = PG_GETARG_INT32(2); /* full_flag */
/* 1 means full, 0 means partial*/
extern struct q3c_prm hprm;
char too_large = 0;
q3c_poly_info_type *qpit;
q3c_poly qp;
static int good_cache;
int first_call;
int identical = 0;
static q3c_poly_info_type lqpit;
if (fcinfo->flinfo->fn_extra == 0)
{
// allocate memory where we are going to store converted info
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, sizeof(q3c_poly_info_type));
first_call = 1;
}
else
{
first_call = 0;
}
qpit = (q3c_poly_info_type*) (fcinfo->flinfo->fn_extra);
/* if second call it is easy */
if (!first_call)
{
if (full_flag)
{
PG_RETURN_INT64(qpit->fulls[iteration]);
}
else
{
PG_RETURN_INT64(qpit->partials[iteration]);
}
}
if (iteration > 0)
{
copy_q3c_poly_info_type(&lqpit, qpit);
}
qp.ra = qpit->ra;
qp.dec = qpit->dec;
qp.x = qpit->x;
qp.y = qpit->y;
qp.ax = qpit->ax;
qp.ay = qpit->ay;
identical = convert_pgarray2poly(poly_arr, qp.ra, qp.dec, &(qp.n));
/* We fill the arrays and check if it matches what we had before */
if (!identical || !good_cache)
{
q3c_poly_query(&hprm, &qp, qpit->fulls, qpit->partials, &too_large);
if (too_large)
{
elog(ERROR, "The polygon is too large. Polygons having diameter >~23 degrees are unsupported");
}
}
if (iteration == 0)
{
good_cache = 0;
copy_q3c_poly_info_type(qpit, &lqpit);
good_cache = 1;
}
if (full_flag)
{
PG_RETURN_INT64(qpit->fulls[iteration]);
}
else
{
PG_RETURN_INT64(qpit->partials[iteration]);
}
}
PG_FUNCTION_INFO_V1(pgq3c_poly_query1_it);
Datum pgq3c_poly_query1_it(PG_FUNCTION_ARGS)
{
POLYGON *poly_arr = PG_GETARG_POLYGON_P(0);
int iteration = PG_GETARG_INT32(1); /* iteration */
int full_flag = PG_GETARG_INT32(2); /* full_flag */
/* 1 means full, 0 means partial*/
extern struct q3c_prm hprm;
char too_large = 0;
q3c_poly_info_type *qpit;
q3c_poly qp;
static int good_cache;
int first_call;
int identical = 0;
static q3c_poly_info_type lqpit;
if (fcinfo->flinfo->fn_extra == 0)
{
// allocate memory where we are going to store converted info
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, sizeof(q3c_poly_info_type));
first_call = 1;
}
else
{
first_call = 0;
}
qpit = (q3c_poly_info_type*) (fcinfo->flinfo->fn_extra);
/* if second call it is easy */
if (!first_call)
{
if (full_flag)
{
PG_RETURN_INT64(qpit->fulls[iteration]);
}
else
{
PG_RETURN_INT64(qpit->partials[iteration]);
}
}
if (iteration > 0)
{
copy_q3c_poly_info_type(&lqpit, qpit);
}
qp.ra = qpit->ra;
qp.dec = qpit->dec;
qp.x = qpit->x;
qp.y = qpit->y;
qp.ax = qpit->ax;
qp.ay = qpit->ay;
identical = convert_pgpoly2poly(poly_arr, qp.ra, qp.dec, &(qp.n));
/* We fill the arrays and check if it matches what we had before */
if (!identical || !good_cache)
{
q3c_poly_query(&hprm, &qp, qpit->fulls, qpit->partials, &too_large);
if (too_large)
{
elog(ERROR, "The polygon is too large. Polygons having diameter >~23 degrees are unsupported");
}
}
if (iteration == 0)
{
good_cache = 0;
copy_q3c_poly_info_type(qpit, &lqpit);
good_cache = 1;
}
if (full_flag)
{
PG_RETURN_INT64(qpit->fulls[iteration]);
}
else
{
PG_RETURN_INT64(qpit->partials[iteration]);
}
}
PG_FUNCTION_INFO_V1(pgq3c_in_ellipse);
Datum pgq3c_in_ellipse(PG_FUNCTION_ARGS)
{
q3c_coord_t ra = PG_GETARG_FLOAT8(0); // ra_cen
q3c_coord_t dec = PG_GETARG_FLOAT8(1); // dec_cen
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(2); // ra_cen
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(3); // dec_cen
q3c_coord_t radius = PG_GETARG_FLOAT8(4); // error radius
q3c_coord_t axis_ratio = PG_GETARG_FLOAT8(5); // axis_ratio
q3c_coord_t PA = PG_GETARG_FLOAT8(6); // PA
q3c_coord_t e = q3c_sqrt(1 - axis_ratio * axis_ratio);
bool result = q3c_in_ellipse(ra_cen, dec_cen, ra,dec, radius, e, PA);
PG_RETURN_BOOL(result);
}
/* The caching assumptions for this code are
For the following calls of the function, if the polygon is
exactly the same in the I will not reproject
*/
PG_FUNCTION_INFO_V1(pgq3c_in_poly);
Datum pgq3c_in_poly(PG_FUNCTION_ARGS)
{
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0); // ra_cen
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1); // dec_cen
ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(2); // ra_cen
extern struct q3c_prm hprm;
char too_large = 0;
int nvert;
bool result;
int identical;
q3c_poly_info_type *qpit;
if (fcinfo->flinfo->fn_extra == 0)
{
// allocate memory where we are going to store converted info
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, sizeof(q3c_poly_info_type));
((q3c_poly_info_type*) (fcinfo->flinfo->fn_extra))->ready = 0;
}
qpit = (q3c_poly_info_type*) (fcinfo->flinfo->fn_extra);
identical = convert_pgarray2poly(poly_arr, qpit->ra, qpit->dec, &nvert) && qpit->ready;
result = q3c_check_sphere_point_in_poly(&hprm, nvert, qpit->ra, qpit->dec,
ra_cen, dec_cen, &too_large, identical,
qpit->xpj, qpit->ypj,
qpit->axpj, qpit->aypj,
qpit->faces, &(qpit->multi_flag)
) != Q3C_DISJUNCT;
if (too_large)
{
elog(ERROR, "The polygon is too large. Polygons having diameter >~23 degrees are unsupported");
}
qpit->ready = 1;
PG_RETURN_BOOL(result);
}
PG_FUNCTION_INFO_V1(pgq3c_in_poly1);
Datum pgq3c_in_poly1(PG_FUNCTION_ARGS)
{
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0); // ra_cen
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1); // dec_cen
POLYGON *poly = PG_GETARG_POLYGON_P(2); // ra_cen
extern struct q3c_prm hprm;
char too_large = 0;
int nvert;
bool result;
int identical;
q3c_poly_info_type *qpit;
if (fcinfo->flinfo->fn_extra == 0)
{
// allocate memory where we are going to store converted info
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, sizeof(q3c_poly_info_type));
((q3c_poly_info_type*) (fcinfo->flinfo->fn_extra))->ready = 0;
}
qpit = (q3c_poly_info_type*) (fcinfo->flinfo->fn_extra);
identical = convert_pgpoly2poly(poly, qpit->ra, qpit->dec, &nvert) && qpit->ready;
result = q3c_check_sphere_point_in_poly(&hprm, nvert, qpit->ra, qpit->dec,
ra_cen, dec_cen, &too_large, identical,
qpit->xpj, qpit->ypj,
qpit->axpj, qpit->aypj,
qpit->faces, &(qpit->multi_flag)
) != Q3C_DISJUNCT;
qpit->ready = 1;
if (too_large)
{
elog(ERROR, "The polygon is too large. Polygons having diameter >~23 degrees are unsupported");
}
PG_RETURN_BOOL(result);
}
|