65 #include "executor/spi.h"
67 #include "utils/lsyscache.h"
68 #include "utils/array.h"
69 #include "catalog/pg_type.h"
71 #if PG_VERSION_NUM >= 80300
72 #include "access/tupmacs.h"
80 #define LOG_NPASS 200000
83 #ifdef PG_MODULE_MAGIC
89 Datum pgh3c_version(PG_FUNCTION_ARGS);
90 Datum pgh3c_ang2ipix(PG_FUNCTION_ARGS);
91 Datum pgh3c_ang2ipix_default(PG_FUNCTION_ARGS);
92 Datum pgh3c_ipix2ang(PG_FUNCTION_ARGS);
93 Datum pgh3c_ipix2ang_default(PG_FUNCTION_ARGS);
94 Datum pgh3c_disk_ipix(PG_FUNCTION_ARGS);
95 Datum pgh3c_disk_ipix_default(PG_FUNCTION_ARGS);
96 Datum pgh3c_radial_query_it(PG_FUNCTION_ARGS);
97 Datum pgh3c_radial_query_it_default(PG_FUNCTION_ARGS);
98 Datum pgh3c_dist(PG_FUNCTION_ARGS);
99 Datum pgh3c_sindist(PG_FUNCTION_ARGS);
100 Datum pgh3c_poly_query_it(PG_FUNCTION_ARGS);
101 Datum pgh3c_poly_query_it_default(PG_FUNCTION_ARGS);
102 Datum pgh3c_in_poly_convex(PG_FUNCTION_ARGS);
103 Datum pgh3c_in_poly(PG_FUNCTION_ARGS);
105 Datum pgh3c_poly_center(PG_FUNCTION_ARGS);
106 Datum pgh3c_circle_intersect_poly(PG_FUNCTION_ARGS);
107 Datum pgh3c_poly_intersect_poly(PG_FUNCTION_ARGS);
110 Datum pgh3c_box_to_poly(PG_FUNCTION_ARGS);
112 Datum pgh3c_nearby_it(PG_FUNCTION_ARGS);
113 Datum pgh3c_nearby_it_default(PG_FUNCTION_ARGS);
114 Datum pgh3c_dist_ipix(PG_FUNCTION_ARGS);
115 Datum pgh3c_in_ellipse(PG_FUNCTION_ARGS);
116 Datum pgh3c_ellipse_query_it(PG_FUNCTION_ARGS);
118 Datum pgh3c_circle_area(PG_FUNCTION_ARGS);
119 Datum pgh3c_poly_area(PG_FUNCTION_ARGS);
120 Datum pgh3c_ipix2default_it(PG_FUNCTION_ARGS);
126 PG_FUNCTION_INFO_V1(pgh3c_version);
127 Datum pgh3c_version(PG_FUNCTION_ARGS)
130 PG_RETURN_FLOAT8(VERSION);
144 PG_FUNCTION_INFO_V1(pgh3c_ang2ipix);
145 Datum pgh3c_ang2ipix(PG_FUNCTION_ARGS)
147 h3c_coord_t ra = PG_GETARG_FLOAT8(0);
148 h3c_coord_t dec = PG_GETARG_FLOAT8(1);
149 int nside = PG_GETARG_UINT32(2);
151 static int invocation;
152 static h3c_coord_t ra_buf, dec_buf;
153 static h3c_ipix_t ipix_buf;
154 static int nside_buf;
162 if ((ra == ra_buf) && (dec == dec_buf) && nside == nside_buf)
164 PG_RETURN_INT64(ipix_buf);
168 h3c_ang2pix_nest(nside,
171 (h3c_ipix_t *) &ipix);
178 PG_RETURN_INT64(ipix);
190 PG_FUNCTION_INFO_V1(pgh3c_ang2ipix_default);
191 Datum pgh3c_ang2ipix_default(PG_FUNCTION_ARGS)
193 h3c_coord_t ra = PG_GETARG_FLOAT8(0);
194 h3c_coord_t dec = PG_GETARG_FLOAT8(1);
196 static int invocation;
197 static h3c_coord_t ra_buf, dec_buf;
198 static h3c_ipix_t ipix_buf;
206 if ((ra == ra_buf) && (dec == dec_buf))
208 PG_RETURN_INT64(ipix_buf);
212 h3c_ang2pix_nest(H3C_DEFAULT_NSIDE,
215 (h3c_ipix_t *) &ipix);
221 PG_RETURN_INT64(ipix);
231 PG_FUNCTION_INFO_V1(pgh3c_ipix2ang);
232 Datum pgh3c_ipix2ang(PG_FUNCTION_ARGS)
235 h3c_coord_t theta, phi;
242 ipix = PG_GETARG_INT64(0);
243 h3c_pix2ang_nest(PG_GETARG_INT32(1),
252 data = ( Datum *) palloc(
sizeof(Datum)*2);
253 data[0] = Float8GetDatum (phi);
254 data[1] = Float8GetDatum (theta);
256 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
259 result = construct_array(data, 2, FLOAT8OID, typlen, typbyval, typalign);
261 PG_RETURN_ARRAYTYPE_P(result);
272 PG_FUNCTION_INFO_V1(pgh3c_ipix2ang_default);
273 Datum pgh3c_ipix2ang_default(PG_FUNCTION_ARGS)
276 h3c_coord_t theta, phi;
283 ipix = PG_GETARG_INT64(0);
284 h3c_pix2ang_nest(H3C_DEFAULT_NSIDE,
292 data = ( Datum *) palloc(
sizeof(Datum)*2);
293 data[0] = Float8GetDatum (phi);
294 data[1] = Float8GetDatum (theta);
296 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
299 result = construct_array(data, 2, FLOAT8OID, typlen, typbyval, typalign);
301 PG_RETURN_ARRAYTYPE_P(result);
313 PG_FUNCTION_INFO_V1(pgh3c_disk_ipix);
314 Datum pgh3c_disk_ipix(PG_FUNCTION_ARGS)
316 FuncCallContext *funcctx;
317 int call_cntr, count;
318 static h3c_ipix_t *ipix;
320 if (SRF_IS_FIRSTCALL()) {
321 MemoryContext oldcontext;
326 funcctx = SRF_FIRSTCALL_INIT();
331 MemoryContextSwitchTo(funcctx->
332 multi_call_memory_ctx);
334 MemoryContextSwitchTo(oldcontext);
344 funcctx = SRF_PERCALL_SETUP();
346 call_cntr = funcctx->call_cntr;
348 h3c_log(H3C_DEBUG,
"pgh3c_disk_ipix end");
349 if (ipix && ipix[call_cntr] > -1) {
350 if (ipix[call_cntr] < 0) ipix[call_cntr] = ipix[call_cntr]*-1;
351 SRF_RETURN_NEXT(funcctx,Int64GetDatum(ipix[call_cntr]));
355 SRF_RETURN_DONE(funcctx);
371 PG_FUNCTION_INFO_V1(pgh3c_disk_ipix_default);
372 Datum pgh3c_disk_ipix_default(PG_FUNCTION_ARGS)
374 FuncCallContext *funcctx;
375 int call_cntr, count;
376 static h3c_ipix_t *ipix;
378 if (SRF_IS_FIRSTCALL()) {
379 MemoryContext oldcontext;
384 funcctx = SRF_FIRSTCALL_INIT();
389 MemoryContextSwitchTo(funcctx->
390 multi_call_memory_ctx);
392 MemoryContextSwitchTo(oldcontext);
402 funcctx = SRF_PERCALL_SETUP();
404 call_cntr = funcctx->call_cntr;
406 h3c_log(H3C_DEBUG,
"pgh3c_disk_ipix_default end");
407 if (ipix && ipix[call_cntr] > -1) {
408 SRF_RETURN_NEXT(funcctx,Int64GetDatum(ipix[call_cntr]));
412 SRF_RETURN_DONE(funcctx);
434 PG_FUNCTION_INFO_V1(pgh3c_radial_query_it);
435 Datum pgh3c_radial_query_it(PG_FUNCTION_ARGS)
437 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
438 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
439 static int invocation=0;
440 static int nside_buf;
441 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
442 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
443 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
444 int iteration = PG_GETARG_INT32(3);
445 int nside = PG_GETARG_INT32(4);
459 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
460 (radius == radius_buf) && (nside == nside_buf))
462 if (iteration<max_ipix) PG_RETURN_INT64(ipix[iteration]);
468 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
473 H3C_DEFAULT_MEM_BLOCK) ;
477 dec_cen_buf = dec_cen;
481 h3c_log(H3C_DEBUG,
"pgh3c_radial_query_it end");
482 PG_RETURN_INT64(ipix[iteration]);
500 PG_FUNCTION_INFO_V1(pgh3c_radial_query_it_default);
501 Datum pgh3c_radial_query_it_default(PG_FUNCTION_ARGS)
503 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
504 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
505 static int invocation=0;
506 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
507 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
508 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
509 int iteration = PG_GETARG_INT32(3);
523 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
524 (radius == radius_buf))
526 if (iteration<max_ipix) PG_RETURN_INT64(ipix[iteration]);
532 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
537 H3C_DEFAULT_MEM_BLOCK) ;
541 dec_cen_buf = dec_cen;
544 h3c_log(H3C_DEBUG,
"pgh3c_radial_query_it_default end");
545 PG_RETURN_INT64(ipix[iteration]);
567 PG_FUNCTION_INFO_V1(pgh3c_nearby_it);
568 Datum pgh3c_nearby_it(PG_FUNCTION_ARGS)
570 static h3c_ipix_t ipix[H3C_DEFAULT_NEARBY_MEM_BLOCK];
571 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
572 static int invocation = 0;
573 static int nside_buf;
575 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
576 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
577 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
578 int iteration = PG_GETARG_INT32(3);
579 int nside = PG_GETARG_INT32(4);
580 static int npass = 0;
589 h3c_log(H3C_INFO,
"initialize pgh3c_nearby_it");
593 if (((npass++) % LOG_NPASS)==0)
h3c_log(H3C_INFO,
"pgh3c_nearby_it_default npass %d", npass);
594 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
595 (radius == radius_buf) && (nside == nside_buf))
597 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
603 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
608 H3C_DEFAULT_NEARBY_MEM_BLOCK) ;
609 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_nearby_it_default nothing found");
613 dec_cen_buf = dec_cen;
617 h3c_log(H3C_DEBUG,
"pgh3c_nearby_it_default end");
618 PG_RETURN_INT64(ipix[iteration]);
635 PG_FUNCTION_INFO_V1(pgh3c_nearby_it_default);
636 Datum pgh3c_nearby_it_default(PG_FUNCTION_ARGS)
638 static h3c_ipix_t ipix[H3C_DEFAULT_NEARBY_MEM_BLOCK];
639 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
640 static int invocation=0;
641 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
642 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
643 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
644 int iteration = PG_GETARG_INT32(3);
646 static int npass = 0;
655 h3c_log(H3C_INFO,
"initialize pgh3c_nearby_it_default");
659 if (((npass++) % LOG_NPASS)==0)
h3c_log(H3C_INFO,
"pgh3c_nearby_it_default npass %d", npass);
660 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
662 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
668 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
673 H3C_DEFAULT_NEARBY_MEM_BLOCK) ;
674 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_nearby_it_default nothing found");
678 dec_cen_buf = dec_cen;
681 h3c_log(H3C_DEBUG,
"pgh3c_nearby_it_default end");
682 PG_RETURN_INT64(ipix[iteration]);
694 PG_FUNCTION_INFO_V1(pgh3c_dist);
695 Datum pgh3c_dist(PG_FUNCTION_ARGS)
697 h3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
698 h3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
699 h3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
700 h3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
701 PG_RETURN_FLOAT8(
h3c_dist(ra1, dec1, ra2, dec2));
714 PG_FUNCTION_INFO_V1(pgh3c_sindist);
715 Datum pgh3c_sindist(PG_FUNCTION_ARGS)
717 h3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
718 h3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
719 h3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
720 h3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
721 PG_RETURN_FLOAT8(
h3c_sindist(ra1, dec1, ra2, dec2));
735 static int h3c_array_push(
int poly_nitems,
743 Oid element_type=FLOAT8OID;
746 h3c_coord_t ra_cur, dec_cur;
748 #if PG_VERSION_NUM >= 80300
752 bitmap = ARR_NULLBITMAP(poly_arr);
756 p = ARR_DATA_PTR(poly_arr);
757 get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
759 for (i = 0; i < poly_nitems; i++)
761 #if PG_VERSION_NUM >= 80300
762 if (bitmap && (*bitmap & bitmask) == 0)
765 "null array element not allowed in this context");
768 ra_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
769 p = att_addlength_pointer(p, typlen, PointerGetDatum(p));
770 p = (
char *) att_align_nominal(p, typalign);
774 if (bitmask == 0x100)
781 if (in_ra[i] != ra_cur) same = 0;
784 if (bitmap && (*bitmap & bitmask) == 0)
787 "null array element not allowed in this context");
790 dec_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
791 p = att_addlength_pointer(p, typlen, PointerGetDatum(p));
792 p = (
char *) att_align_nominal(p, typalign);
797 if (bitmask == 0x100)
804 if (in_dec[i] != dec_cur) same = 0;
808 ra_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
809 if (in_ra[i] != ra_cur) same = 0;
812 p = att_addlength(p, typlen, PointerGetDatum(p));
813 p = (
char *) att_align(p, typalign);
814 dec_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
815 if (in_dec[i] != dec_cur) same = 0;
818 p = att_addlength(p, typlen, PointerGetDatum(p));
819 p = (
char *) att_align(p, typalign);
841 PG_FUNCTION_INFO_V1(pgh3c_poly_query_it);
842 Datum pgh3c_poly_query_it(PG_FUNCTION_ARGS)
844 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
845 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
847 static int nside_buf;
849 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
850 int iteration = PG_GETARG_UINT32(1);
851 int nside = PG_GETARG_UINT32(2);
853 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
860 if (poly_nitems % 2 != 0)
862 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
864 else if (poly_nitems <= 4)
866 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
871 same = h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
872 if (same && (nside == nside_buf)) {
873 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
878 max_ipix = h3c_polygon_ipix_it(in_ra,
883 H3C_DEFAULT_MEM_BLOCK);
884 if (max_ipix < 1)
h3c_log(H3C_ERROR,
"pgh3c_poly_query_it nothing found");
887 h3c_log(H3C_DEBUG,
"pgh3c_poly_query_it end");
888 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
904 PG_FUNCTION_INFO_V1(pgh3c_poly_query_it_default);
905 Datum pgh3c_poly_query_it_default(PG_FUNCTION_ARGS)
907 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
908 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
911 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
912 int iteration = PG_GETARG_UINT32(1);
914 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
921 if (poly_nitems % 2 != 0)
923 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
925 else if (poly_nitems <= 4)
927 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
932 same = h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
934 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
939 max_ipix = h3c_polygon_ipix_it(in_ra,
944 H3C_DEFAULT_MEM_BLOCK);
945 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_poly_query_it_default nothing found");
947 h3c_log(H3C_DEBUG,
"pgh3c_poly_query_it_default end");
948 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
961 PG_FUNCTION_INFO_V1(pgh3c_box_to_poly);
962 Datum pgh3c_box_to_poly(PG_FUNCTION_ARGS)
964 static ArrayType *result;
970 static h3c_coord_t in_ra[4];
971 static h3c_coord_t in_dec[4];
972 static h3c_coord_t ra_cen_buf, dec_cen_buf;
973 static h3c_coord_t width_buf, height_buf;
975 if (PG_GETARG_FLOAT8(0) == ra_cen_buf &&
976 PG_GETARG_FLOAT8(1) == dec_cen_buf &&
977 PG_GETARG_FLOAT8(2) == width_buf &&
978 PG_GETARG_FLOAT8(3) == height_buf) {
982 ra_cen_buf = PG_GETARG_FLOAT8(0);
983 dec_cen_buf = PG_GETARG_FLOAT8(1);
984 width_buf = PG_GETARG_FLOAT8(2);
985 height_buf = PG_GETARG_FLOAT8(3);
987 h3c_box_2_polygon(PG_GETARG_FLOAT8(0),
994 data = ( Datum *) palloc(
sizeof(Datum)*8);
995 data[0] = Float8GetDatum (in_ra[0]);
996 data[1] = Float8GetDatum (in_dec[0]);
997 data[2] = Float8GetDatum (in_ra[1]);
998 data[3] = Float8GetDatum (in_dec[1]);
999 data[4] = Float8GetDatum (in_ra[2]);
1000 data[5] = Float8GetDatum (in_dec[2]);
1001 data[6] = Float8GetDatum (in_ra[3]);
1002 data[7] = Float8GetDatum (in_dec[3]);
1005 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
1008 result = construct_array(data, 8, FLOAT8OID, typlen, typbyval, typalign);
1010 PG_RETURN_ARRAYTYPE_P(result);
1031 PG_FUNCTION_INFO_V1(pgh3c_box_query_it);
1032 Datum pgh3c_box_query_it(PG_FUNCTION_ARGS)
1034 static int invocation=0;
1035 static h3c_coord_t ra_cen_buf,dec_cen_buf, height_buf, width_buf;
1036 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
1037 static int max_ipix;
1038 static int nside_buf;
1039 h3c_coord_t in_ra[4], in_dec[4];
1041 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1042 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1043 h3c_coord_t width = PG_GETARG_FLOAT8(2);
1044 h3c_coord_t height = PG_GETARG_FLOAT8(3);
1046 int iteration = PG_GETARG_UINT32(4);
1047 int nside = PG_GETARG_UINT32(5);
1050 if (invocation == 0)
1057 h3c_log(H3C_INFO,
"initialize pgh3c_box_query_it");
1061 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf)
1062 && (height == height_buf) && (width == width_buf)
1063 && nside == nside_buf)
1065 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
1066 PG_RETURN_INT64(-1);
1070 _box_2_polygon(ra_cen, dec_cen, width, height, in_ra, in_de);
1073 max_ipix = h3c_polygon_ipix_it(in_ra,
1078 H3C_DEFAULT_MEM_BLOCK);
1079 if (max_ipix < 1)
h3c_log(H3C_ERROR,
"pgh3c_box_query_it nothing found");
1083 ra_cen_buf = ra_cen;
1084 dec_cen_buf = dec_cen;
1086 height_buf = height;
1088 h3c_log(H3C_DEBUG,
"pgh3c_box_query_it end");
1089 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
1090 PG_RETURN_INT64(-1);
1108 PG_FUNCTION_INFO_V1(pgh3c_box_query_it_default);
1109 Datum pgh3c_box_query_it_default(PG_FUNCTION_ARGS)
1111 static int invocation=0;
1112 static h3c_coord_t ra_cen_buf,dec_cen_buf, height_buf, width_buf;
1113 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
1114 static int max_ipix;
1115 h3c_coord_t in_ra[4], in_dec[4];
1117 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1118 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1119 h3c_coord_t width = PG_GETARG_FLOAT8(2);
1120 h3c_coord_t height = PG_GETARG_FLOAT8(3);
1122 int iteration = PG_GETARG_UINT32(4);
1124 if (invocation == 0)
1131 h3c_log(H3C_INFO,
"initialize pgh3c_box_query_it");
1135 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf)
1136 && (height == height_buf) && (width == width_buf))
1138 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
1139 PG_RETURN_INT64(-1);
1143 _box_2_polygon(ra_cen, dec_cen, width, height, in_ra, in_de);
1146 h3c_log(H3C_DEBUG,
"pgh3c_box_query_it: update to polygon h3c_poly_query: (%f,%f),(%f,%f), (%f,%f), (%f,%f)",
1147 in_ra[0], in_dec[0], in_ra[1], in_dec[1], in_ra[2], in_dec[2], in_ra[3], in_dec[3]);
1150 max_ipix = h3c_polygon_ipix_it(in_ra,
1155 H3C_DEFAULT_MEM_BLOCK);
1156 if (max_ipix < 1)
h3c_log(H3C_ERROR,
"pgh3c_box_query_it nothing found");
1159 ra_cen_buf = ra_cen;
1160 dec_cen_buf = dec_cen;
1162 height_buf = height;
1164 h3c_log(H3C_DEBUG,
"pgh3c_box_query_it end");
1165 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
1166 PG_RETURN_INT64(-1);
1186 PG_FUNCTION_INFO_V1(pgh3c_in_poly_convex);
1187 Datum pgh3c_in_poly_convex(PG_FUNCTION_ARGS)
1189 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1191 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(2);
1192 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1193 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1195 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1204 if (poly_nitems % 2 != 0)
1206 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1208 else if (poly_nitems <= 4)
1210 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1215 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1217 h3c_log(H3C_ERROR,
"The polygon is not convex");
1221 ra_cen, dec_cen)) != H3C_DISJUNCT;
1224 PG_RETURN_BOOL((result));
1240 PG_FUNCTION_INFO_V1(pgh3c_in_poly);
1241 Datum pgh3c_in_poly(PG_FUNCTION_ARGS)
1243 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1245 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(2);
1246 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1247 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1249 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1258 if (poly_nitems % 2 != 0)
1260 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1262 else if (poly_nitems <= 4)
1264 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1269 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1271 result = (h3c_check_sphere_point_in_poly(poly_nitems, in_ra, in_dec,
1272 ra_cen, dec_cen)) != H3C_DISJUNCT;
1275 PG_RETURN_BOOL((result));
1288 PG_FUNCTION_INFO_V1(pgh3c_in_box);
1289 Datum pgh3c_in_box(PG_FUNCTION_ARGS)
1291 h3c_coord_t in_ra[4], in_dec[4];
1292 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1293 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1294 h3c_coord_t width = PG_GETARG_FLOAT8(2);
1295 h3c_coord_t height = PG_GETARG_FLOAT8(3);
1297 _box_2_polygon(ra_cen, dec_cen, width, height, in_ra, in_dec);
1299 result = (h3c_check_sphere_point_in_poly(poly_nitems, in_ra, in_dec,
1300 ra_cen, dec_cen)) != H3C_DISJUNCT;
1303 PG_RETURN_BOOL((result));
1313 PG_FUNCTION_INFO_V1(pgh3c_poly_center);
1314 Datum pgh3c_poly_center(PG_FUNCTION_ARGS)
1316 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1317 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
1318 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1319 h3c_coord_t radec[2];
1326 if (poly_nitems % 2 != 0)
1328 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1330 else if (poly_nitems <= 4)
1332 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1337 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1339 h3c_log(H3C_ERROR,
"The polygon is not convex");
1344 data = ( Datum *) palloc(
sizeof(Datum)*2);
1345 data[0] = Float8GetDatum (radec[0]);
1346 data[1] = Float8GetDatum (radec[1]);
1348 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
1351 result = construct_array(data, 2, FLOAT8OID, typlen, typbyval, typalign);
1353 PG_RETURN_ARRAYTYPE_P(result);
1364 PG_FUNCTION_INFO_V1(pgh3c_circle_intersect_poly);
1365 Datum pgh3c_circle_intersect_poly(PG_FUNCTION_ARGS)
1367 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1370 ArrayType *poly_arr;
1372 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1373 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1374 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
1375 poly_arr = PG_GETARG_ARRAYTYPE_P(3);
1377 poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1378 if (poly_nitems % 2 != 0)
1380 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1382 else if (poly_nitems <= 4)
1384 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1388 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1390 h3c_log(H3C_ERROR,
"The polygon is not convex");
1394 ra_cen, dec_cen, radius)) != H3C_DISJUNCT;
1396 PG_RETURN_BOOL((result));
1406 PG_FUNCTION_INFO_V1(pgh3c_poly_intersect_poly);
1407 Datum pgh3c_poly_intersect_poly(PG_FUNCTION_ARGS)
1409 static h3c_coord_t in_ra1[H3C_MAX_N_POLY_VERTEX], in_dec1[H3C_MAX_N_POLY_VERTEX];
1410 static h3c_coord_t in_ra2[H3C_MAX_N_POLY_VERTEX], in_dec2[H3C_MAX_N_POLY_VERTEX];
1412 int poly_nitems1, poly_nitems2;
1413 ArrayType *poly_arr1, *poly_arr2;
1415 poly_arr1 = PG_GETARG_ARRAYTYPE_P(0);
1416 poly_arr2 = PG_GETARG_ARRAYTYPE_P(1);
1418 poly_nitems1 = ArrayGetNItems(ARR_NDIM(poly_arr1), ARR_DIMS(poly_arr1));
1419 if (poly_nitems1 % 2 != 0)
1421 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1423 else if (poly_nitems1 <= 4)
1425 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1429 h3c_array_push(poly_nitems1, in_ra1, in_dec1, poly_arr1);
1431 h3c_log(H3C_ERROR,
"The polygon1 is not convex");
1434 poly_nitems2 = ArrayGetNItems(ARR_NDIM(poly_arr2), ARR_DIMS(poly_arr2));
1435 if (poly_nitems2 % 2 != 0)
1437 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1439 else if (poly_nitems2 <= 4)
1441 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1445 h3c_array_push(poly_nitems2, in_ra2, in_dec2, poly_arr2);
1447 h3c_log(H3C_ERROR,
"The polygon2 is not convex");
1451 poly_nitems2, in_ra2, in_dec2)) != H3C_DISJUNCT;
1453 PG_RETURN_BOOL((result));
1463 PG_FUNCTION_INFO_V1(pgh3c_dist_ipix);
1464 Datum pgh3c_dist_ipix(PG_FUNCTION_ARGS)
1466 h3c_coord_t coo1[2], coo2[2], d;
1467 h3c_ipix_t ipix1 = PG_GETARG_INT64(0);
1468 h3c_ipix_t ipix2 = PG_GETARG_INT64(1);
1470 h3c_pix2ang_nest(H3C_DEFAULT_NSIDE,
1477 h3c_pix2ang_nest(H3C_DEFAULT_NSIDE,
1484 d =
h3c_dist(coo1[0], coo1[1], coo2[0], coo2[2]);
1485 PG_RETURN_FLOAT8(d);
1500 PG_FUNCTION_INFO_V1(pgh3c_in_ellipse);
1501 Datum pgh3c_in_ellipse(PG_FUNCTION_ARGS)
1504 h3c_coord_t ra = PG_GETARG_FLOAT8(0);
1505 h3c_coord_t dec = PG_GETARG_FLOAT8(1);
1506 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(2);
1507 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(3);
1508 h3c_coord_t radius = PG_GETARG_FLOAT8(4);
1509 h3c_coord_t axis_ratio = PG_GETARG_FLOAT8(5);
1510 h3c_coord_t PA = PG_GETARG_FLOAT8(6);
1511 h3c_coord_t e = sqrt(1 - axis_ratio * axis_ratio);
1512 bool result =
h3c_in_ellipse(ra_cen, dec_cen, ra,dec, radius, e, PA);
1513 PG_RETURN_BOOL(result);
1526 PG_FUNCTION_INFO_V1(pgh3c_ellipse_query_it);
1527 Datum pgh3c_ellipse_query_it(PG_FUNCTION_ARGS)
1529 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
1530 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
1531 static int invocation=0;
1532 static int nside_buf, axis_ratio_buf;
1533 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1534 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1535 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
1536 h3c_coord_t axis_ratio = PG_GETARG_FLOAT8(3);
1537 int iteration = PG_GETARG_INT32(4);
1538 int nside = PG_GETARG_INT32(5);
1539 static int max_ipix;
1541 if (invocation == 0)
1551 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
1552 (radius == radius_buf) && (nside == nside_buf) &&
1553 (axis_ratio == axis_ratio_buf))
1555 if (iteration<max_ipix) PG_RETURN_INT64(ipix[iteration]);
1556 PG_RETURN_INT64(-1);
1561 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
1562 PG_GETARG_FLOAT8(1),
1563 PG_GETARG_FLOAT8(2)/2.,
1566 H3C_DEFAULT_MEM_BLOCK) ;
1567 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_ellipse_query_it nothing found");
1570 ra_cen_buf = ra_cen;
1571 dec_cen_buf = dec_cen;
1572 radius_buf = radius;
1574 axis_ratio_buf = axis_ratio;
1576 h3c_log(H3C_DEBUG,
"pgh3c_ellipse_query_it end");
1577 PG_RETURN_INT64(ipix[iteration]);
1586 PG_FUNCTION_INFO_V1(pgh3c_circle_area);
1587 Datum pgh3c_circle_area(PG_FUNCTION_ARGS)
1589 h3c_coord_t radius = PG_GETARG_FLOAT8(0);
1590 h3c_coord_t val = sin(radius*H3C_DEGRA/2.);
1591 PG_RETURN_FLOAT8(4*H3C_PI*val*val);
1600 PG_FUNCTION_INFO_V1(pgh3c_poly_area);
1601 Datum pgh3c_poly_area(PG_FUNCTION_ARGS)
1604 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1605 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
1606 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1612 if (poly_nitems % 2 != 0)
1614 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1616 else if (poly_nitems <= 4)
1618 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1622 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1626 h3c_log(H3C_ERROR,
"The polygon is not convex");
1630 h3c_log(H3C_DEBUG,
"pgh3c_poly_area end");
1631 PG_RETURN_FLOAT8(area);
1642 PG_FUNCTION_INFO_V1(pgh3c_ipix2default_it);
1643 Datum pgh3c_ipix2default_it(PG_FUNCTION_ARGS)
1645 static h3c_ipix_t m_ipix[2];
1646 static h3c_ipix_t ipix_buf, nside_buf;
1647 static int invocation = 0;
1648 h3c_ipix_t ipix = PG_GETARG_INT32(0);
1649 int nside = PG_GETARG_INT32(1);
1650 int iteration = PG_GETARG_INT32(2);
1651 int order, order_default;
1653 if (invocation == 0)
1663 if ((ipix == ipix_buf) && (nside == nside_buf))
1665 if (iteration < 2) PG_RETURN_INT64(m_ipix[iteration]);
1666 PG_RETURN_INT64(-1);
1670 order_default =
h3c_order(H3C_DEFAULT_NSIDE);
1672 if (order == order_default) {
1676 else if (order < order_default) {
1677 m_ipix[0] = ipix<<(order_default-order)*2;
1678 m_ipix[1] = (ipix+1)<<(order_default-order)*2;
1681 m_ipix[0] = ipix>>(order_default-order)*2;
1682 m_ipix[1] = (ipix+1)>>(order_default-order)*2;
1689 h3c_log(H3C_DEBUG,
"pgh3c_ipix2default_it");
1690 PG_RETURN_INT64(m_ipix[iteration]);
1698 PG_FUNCTION_INFO_V1(pgh3c_nside_config);
1699 Datum pgh3c_nside_config(PG_FUNCTION_ARGS)
1701 PG_RETURN_INT64(H3C_DEFAULT_NSIDE);