67 #include "executor/spi.h"
69 #include "utils/lsyscache.h"
70 #include "utils/array.h"
71 #include "catalog/pg_type.h"
73 #if PG_VERSION_NUM >= 80300
74 #include "access/tupmacs.h"
82 #define LOG_NPASS 200000
85 #ifdef PG_MODULE_MAGIC
91 Datum pgh3c_version(PG_FUNCTION_ARGS);
92 Datum pgh3c_ang2ipix(PG_FUNCTION_ARGS);
93 Datum pgh3c_ang2ipix_default(PG_FUNCTION_ARGS);
94 Datum pgh3c_ipix2ang(PG_FUNCTION_ARGS);
95 Datum pgh3c_ipix2ang_default(PG_FUNCTION_ARGS);
96 Datum pgh3c_disk_ipix(PG_FUNCTION_ARGS);
97 Datum pgh3c_disk_ipix_default(PG_FUNCTION_ARGS);
98 Datum pgh3c_radial_query_it(PG_FUNCTION_ARGS);
99 Datum pgh3c_radial_query_it_default(PG_FUNCTION_ARGS);
100 Datum pgh3c_dist(PG_FUNCTION_ARGS);
101 Datum pgh3c_sindist(PG_FUNCTION_ARGS);
102 Datum pgh3c_poly_query_it(PG_FUNCTION_ARGS);
103 Datum pgh3c_poly_query_it_default(PG_FUNCTION_ARGS);
104 Datum pgh3c_in_poly_convex(PG_FUNCTION_ARGS);
105 Datum pgh3c_in_poly(PG_FUNCTION_ARGS);
107 Datum pgh3c_poly_center(PG_FUNCTION_ARGS);
108 Datum pgh3c_circle_intersect_poly(PG_FUNCTION_ARGS);
109 Datum pgh3c_poly_intersect_poly(PG_FUNCTION_ARGS);
112 Datum pgh3c_box_to_poly(PG_FUNCTION_ARGS);
114 Datum pgh3c_nearby_it(PG_FUNCTION_ARGS);
115 Datum pgh3c_nearby_it_default(PG_FUNCTION_ARGS);
116 Datum pgh3c_dist_ipix(PG_FUNCTION_ARGS);
117 Datum pgh3c_in_ellipse(PG_FUNCTION_ARGS);
118 Datum pgh3c_ellipse_query_it(PG_FUNCTION_ARGS);
120 Datum pgh3c_circle_area(PG_FUNCTION_ARGS);
121 Datum pgh3c_poly_area(PG_FUNCTION_ARGS);
122 Datum pgh3c_ipix2default_it(PG_FUNCTION_ARGS);
128 PG_FUNCTION_INFO_V1(pgh3c_version);
129 Datum pgh3c_version(PG_FUNCTION_ARGS)
132 PG_RETURN_FLOAT8(VERSION);
146 PG_FUNCTION_INFO_V1(pgh3c_ang2ipix);
147 Datum pgh3c_ang2ipix(PG_FUNCTION_ARGS)
149 h3c_coord_t ra = PG_GETARG_FLOAT8(0);
150 h3c_coord_t dec = PG_GETARG_FLOAT8(1);
151 int nside = PG_GETARG_UINT32(2);
153 static int invocation;
154 static h3c_coord_t ra_buf, dec_buf;
155 static h3c_ipix_t ipix_buf;
156 static int nside_buf;
164 if ((ra == ra_buf) && (dec == dec_buf) && nside == nside_buf)
166 PG_RETURN_INT64(ipix_buf);
170 h3c_ang2pix_nest(nside,
173 (h3c_ipix_t *) &ipix);
180 PG_RETURN_INT64(ipix);
192 PG_FUNCTION_INFO_V1(pgh3c_ang2ipix_default);
193 Datum pgh3c_ang2ipix_default(PG_FUNCTION_ARGS)
195 h3c_coord_t ra = PG_GETARG_FLOAT8(0);
196 h3c_coord_t dec = PG_GETARG_FLOAT8(1);
198 static int invocation;
199 static h3c_coord_t ra_buf, dec_buf;
200 static h3c_ipix_t ipix_buf;
208 if ((ra == ra_buf) && (dec == dec_buf))
210 PG_RETURN_INT64(ipix_buf);
214 h3c_ang2pix_nest(H3C_DEFAULT_NSIDE,
217 (h3c_ipix_t *) &ipix);
223 PG_RETURN_INT64(ipix);
233 PG_FUNCTION_INFO_V1(pgh3c_ipix2ang);
234 Datum pgh3c_ipix2ang(PG_FUNCTION_ARGS)
237 h3c_coord_t theta, phi;
244 ipix = PG_GETARG_INT64(0);
245 h3c_pix2ang_nest(PG_GETARG_INT32(1),
254 data = ( Datum *) palloc(
sizeof(Datum)*2);
255 data[0] = Float8GetDatum (phi);
256 data[1] = Float8GetDatum (theta);
258 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
261 result = construct_array(data, 2, FLOAT8OID, typlen, typbyval, typalign);
263 PG_RETURN_ARRAYTYPE_P(result);
274 PG_FUNCTION_INFO_V1(pgh3c_ipix2ang_default);
275 Datum pgh3c_ipix2ang_default(PG_FUNCTION_ARGS)
278 h3c_coord_t theta, phi;
285 ipix = PG_GETARG_INT64(0);
286 h3c_pix2ang_nest(H3C_DEFAULT_NSIDE,
294 data = ( Datum *) palloc(
sizeof(Datum)*2);
295 data[0] = Float8GetDatum (phi);
296 data[1] = Float8GetDatum (theta);
298 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
301 result = construct_array(data, 2, FLOAT8OID, typlen, typbyval, typalign);
303 PG_RETURN_ARRAYTYPE_P(result);
315 PG_FUNCTION_INFO_V1(pgh3c_disk_ipix);
316 Datum pgh3c_disk_ipix(PG_FUNCTION_ARGS)
318 FuncCallContext *funcctx;
319 int call_cntr, count;
320 static h3c_ipix_t *ipix;
322 if (SRF_IS_FIRSTCALL()) {
323 MemoryContext oldcontext;
328 funcctx = SRF_FIRSTCALL_INIT();
333 MemoryContextSwitchTo(funcctx->
334 multi_call_memory_ctx);
336 MemoryContextSwitchTo(oldcontext);
346 funcctx = SRF_PERCALL_SETUP();
348 call_cntr = funcctx->call_cntr;
350 h3c_log(H3C_DEBUG,
"pgh3c_disk_ipix end");
351 if (ipix && ipix[call_cntr] > -1) {
352 if (ipix[call_cntr] < 0) ipix[call_cntr] = ipix[call_cntr]*-1;
353 SRF_RETURN_NEXT(funcctx,Int64GetDatum(ipix[call_cntr]));
357 SRF_RETURN_DONE(funcctx);
373 PG_FUNCTION_INFO_V1(pgh3c_disk_ipix_default);
374 Datum pgh3c_disk_ipix_default(PG_FUNCTION_ARGS)
376 FuncCallContext *funcctx;
377 int call_cntr, count;
378 static h3c_ipix_t *ipix;
380 if (SRF_IS_FIRSTCALL()) {
381 MemoryContext oldcontext;
386 funcctx = SRF_FIRSTCALL_INIT();
391 MemoryContextSwitchTo(funcctx->
392 multi_call_memory_ctx);
394 MemoryContextSwitchTo(oldcontext);
404 funcctx = SRF_PERCALL_SETUP();
406 call_cntr = funcctx->call_cntr;
408 h3c_log(H3C_DEBUG,
"pgh3c_disk_ipix_default end");
409 if (ipix && ipix[call_cntr] > -1) {
410 SRF_RETURN_NEXT(funcctx,Int64GetDatum(ipix[call_cntr]));
414 SRF_RETURN_DONE(funcctx);
436 PG_FUNCTION_INFO_V1(pgh3c_radial_query_it);
437 Datum pgh3c_radial_query_it(PG_FUNCTION_ARGS)
439 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
440 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
441 static int invocation=0;
442 static int nside_buf;
443 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
444 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
445 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
446 int iteration = PG_GETARG_INT32(3);
447 int nside = PG_GETARG_INT32(4);
461 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
462 (radius == radius_buf) && (nside == nside_buf))
464 if (iteration<max_ipix) PG_RETURN_INT64(ipix[iteration]);
470 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
475 H3C_DEFAULT_MEM_BLOCK) ;
479 dec_cen_buf = dec_cen;
483 h3c_log(H3C_DEBUG,
"pgh3c_radial_query_it end");
484 PG_RETURN_INT64(ipix[iteration]);
502 PG_FUNCTION_INFO_V1(pgh3c_radial_query_it_default);
503 Datum pgh3c_radial_query_it_default(PG_FUNCTION_ARGS)
505 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
506 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
507 static int invocation=0;
508 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
509 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
510 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
511 int iteration = PG_GETARG_INT32(3);
525 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
526 (radius == radius_buf))
528 if (iteration<max_ipix) PG_RETURN_INT64(ipix[iteration]);
534 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
539 H3C_DEFAULT_MEM_BLOCK) ;
543 dec_cen_buf = dec_cen;
546 h3c_log(H3C_DEBUG,
"pgh3c_radial_query_it_default end");
547 PG_RETURN_INT64(ipix[iteration]);
569 PG_FUNCTION_INFO_V1(pgh3c_nearby_it);
570 Datum pgh3c_nearby_it(PG_FUNCTION_ARGS)
572 static h3c_ipix_t ipix[H3C_DEFAULT_NEARBY_MEM_BLOCK];
573 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
574 static int invocation = 0;
575 static int nside_buf;
577 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
578 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
579 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
580 int iteration = PG_GETARG_INT32(3);
581 int nside = PG_GETARG_INT32(4);
582 static int npass = 0;
591 h3c_log(H3C_INFO,
"initialize pgh3c_nearby_it");
595 if (((npass++) % LOG_NPASS)==0)
h3c_log(H3C_INFO,
"pgh3c_nearby_it_default npass %d", npass);
596 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
597 (radius == radius_buf) && (nside == nside_buf))
599 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
605 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
610 H3C_DEFAULT_NEARBY_MEM_BLOCK) ;
611 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_nearby_it_default nothing found");
615 dec_cen_buf = dec_cen;
619 h3c_log(H3C_DEBUG,
"pgh3c_nearby_it_default end");
620 PG_RETURN_INT64(ipix[iteration]);
637 PG_FUNCTION_INFO_V1(pgh3c_nearby_it_default);
638 Datum pgh3c_nearby_it_default(PG_FUNCTION_ARGS)
640 static h3c_ipix_t ipix[H3C_DEFAULT_NEARBY_MEM_BLOCK];
641 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
642 static int invocation=0;
643 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
644 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
645 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
646 int iteration = PG_GETARG_INT32(3);
648 static int npass = 0;
657 h3c_log(H3C_INFO,
"initialize pgh3c_nearby_it_default");
661 if (((npass++) % LOG_NPASS)==0)
h3c_log(H3C_INFO,
"pgh3c_nearby_it_default npass %d", npass);
662 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
664 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
670 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
675 H3C_DEFAULT_NEARBY_MEM_BLOCK) ;
676 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_nearby_it_default nothing found");
680 dec_cen_buf = dec_cen;
683 h3c_log(H3C_DEBUG,
"pgh3c_nearby_it_default end");
684 PG_RETURN_INT64(ipix[iteration]);
696 PG_FUNCTION_INFO_V1(pgh3c_dist);
697 Datum pgh3c_dist(PG_FUNCTION_ARGS)
699 h3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
700 h3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
701 h3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
702 h3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
703 PG_RETURN_FLOAT8(
h3c_dist(ra1, dec1, ra2, dec2));
716 PG_FUNCTION_INFO_V1(pgh3c_sindist);
717 Datum pgh3c_sindist(PG_FUNCTION_ARGS)
719 h3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
720 h3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
721 h3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
722 h3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
723 PG_RETURN_FLOAT8(
h3c_sindist(ra1, dec1, ra2, dec2));
737 static int h3c_array_push(
int poly_nitems,
745 Oid element_type=FLOAT8OID;
748 h3c_coord_t ra_cur, dec_cur;
750 #if PG_VERSION_NUM >= 80300
754 bitmap = ARR_NULLBITMAP(poly_arr);
758 p = ARR_DATA_PTR(poly_arr);
759 get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
761 for (i = 0; i < poly_nitems; i++)
763 #if PG_VERSION_NUM >= 80300
764 if (bitmap && (*bitmap & bitmask) == 0)
767 "null array element not allowed in this context");
770 ra_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
771 p = att_addlength_pointer(p, typlen, PointerGetDatum(p));
772 p = (
char *) att_align_nominal(p, typalign);
776 if (bitmask == 0x100)
783 if (in_ra[i] != ra_cur) same = 0;
786 if (bitmap && (*bitmap & bitmask) == 0)
789 "null array element not allowed in this context");
792 dec_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
793 p = att_addlength_pointer(p, typlen, PointerGetDatum(p));
794 p = (
char *) att_align_nominal(p, typalign);
799 if (bitmask == 0x100)
806 if (in_dec[i] != dec_cur) same = 0;
810 ra_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
811 if (in_ra[i] != ra_cur) same = 0;
814 p = att_addlength(p, typlen, PointerGetDatum(p));
815 p = (
char *) att_align(p, typalign);
816 dec_cur = DatumGetFloat8(fetch_att(p, typbyval, typlen));
817 if (in_dec[i] != dec_cur) same = 0;
820 p = att_addlength(p, typlen, PointerGetDatum(p));
821 p = (
char *) att_align(p, typalign);
843 PG_FUNCTION_INFO_V1(pgh3c_poly_query_it);
844 Datum pgh3c_poly_query_it(PG_FUNCTION_ARGS)
846 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
847 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
849 static int nside_buf;
851 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
852 int iteration = PG_GETARG_UINT32(1);
853 int nside = PG_GETARG_UINT32(2);
855 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
862 if (poly_nitems % 2 != 0)
864 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
866 else if (poly_nitems <= 4)
868 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
873 same = h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
874 if (same && (nside == nside_buf)) {
875 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
880 max_ipix = h3c_polygon_ipix_it(in_ra,
885 H3C_DEFAULT_MEM_BLOCK);
886 if (max_ipix < 1)
h3c_log(H3C_ERROR,
"pgh3c_poly_query_it nothing found");
889 h3c_log(H3C_DEBUG,
"pgh3c_poly_query_it end");
890 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
906 PG_FUNCTION_INFO_V1(pgh3c_poly_query_it_default);
907 Datum pgh3c_poly_query_it_default(PG_FUNCTION_ARGS)
909 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
910 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
913 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
914 int iteration = PG_GETARG_UINT32(1);
916 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
923 if (poly_nitems % 2 != 0)
925 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
927 else if (poly_nitems <= 4)
929 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
934 same = h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
936 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
941 max_ipix = h3c_polygon_ipix_it(in_ra,
946 H3C_DEFAULT_MEM_BLOCK);
947 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_poly_query_it_default nothing found");
949 h3c_log(H3C_DEBUG,
"pgh3c_poly_query_it_default end");
950 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
963 PG_FUNCTION_INFO_V1(pgh3c_box_to_poly);
964 Datum pgh3c_box_to_poly(PG_FUNCTION_ARGS)
966 static ArrayType *result;
972 static h3c_coord_t in_ra[4];
973 static h3c_coord_t in_dec[4];
974 static h3c_coord_t ra_cen_buf, dec_cen_buf;
975 static h3c_coord_t width_buf, height_buf;
977 if (PG_GETARG_FLOAT8(0) == ra_cen_buf &&
978 PG_GETARG_FLOAT8(1) == dec_cen_buf &&
979 PG_GETARG_FLOAT8(2) == width_buf &&
980 PG_GETARG_FLOAT8(3) == height_buf) {
984 ra_cen_buf = PG_GETARG_FLOAT8(0);
985 dec_cen_buf = PG_GETARG_FLOAT8(1);
986 width_buf = PG_GETARG_FLOAT8(2);
987 height_buf = PG_GETARG_FLOAT8(3);
989 h3c_box_2_polygon(PG_GETARG_FLOAT8(0),
996 data = ( Datum *) palloc(
sizeof(Datum)*8);
997 data[0] = Float8GetDatum (in_ra[0]);
998 data[1] = Float8GetDatum (in_dec[0]);
999 data[2] = Float8GetDatum (in_ra[1]);
1000 data[3] = Float8GetDatum (in_dec[1]);
1001 data[4] = Float8GetDatum (in_ra[2]);
1002 data[5] = Float8GetDatum (in_dec[2]);
1003 data[6] = Float8GetDatum (in_ra[3]);
1004 data[7] = Float8GetDatum (in_dec[3]);
1007 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
1010 result = construct_array(data, 8, FLOAT8OID, typlen, typbyval, typalign);
1012 PG_RETURN_ARRAYTYPE_P(result);
1033 PG_FUNCTION_INFO_V1(pgh3c_box_query_it);
1034 Datum pgh3c_box_query_it(PG_FUNCTION_ARGS)
1036 static int invocation=0;
1037 static h3c_coord_t ra_cen_buf,dec_cen_buf, height_buf, width_buf;
1038 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
1039 static int max_ipix;
1040 static int nside_buf;
1041 h3c_coord_t in_ra[4], in_dec[4];
1043 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1044 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1045 h3c_coord_t width = PG_GETARG_FLOAT8(2);
1046 h3c_coord_t height = PG_GETARG_FLOAT8(3);
1048 int iteration = PG_GETARG_UINT32(4);
1049 int nside = PG_GETARG_UINT32(5);
1052 if (invocation == 0)
1059 h3c_log(H3C_INFO,
"initialize pgh3c_box_query_it");
1063 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf)
1064 && (height == height_buf) && (width == width_buf)
1065 && nside == nside_buf)
1067 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
1068 PG_RETURN_INT64(-1);
1072 _box_2_polygon(ra_cen, dec_cen, width, height, in_ra, in_de);
1075 max_ipix = h3c_polygon_ipix_it(in_ra,
1080 H3C_DEFAULT_MEM_BLOCK);
1081 if (max_ipix < 1)
h3c_log(H3C_ERROR,
"pgh3c_box_query_it nothing found");
1085 ra_cen_buf = ra_cen;
1086 dec_cen_buf = dec_cen;
1088 height_buf = height;
1090 h3c_log(H3C_DEBUG,
"pgh3c_box_query_it end");
1091 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
1092 PG_RETURN_INT64(-1);
1110 PG_FUNCTION_INFO_V1(pgh3c_box_query_it_default);
1111 Datum pgh3c_box_query_it_default(PG_FUNCTION_ARGS)
1113 static int invocation=0;
1114 static h3c_coord_t ra_cen_buf,dec_cen_buf, height_buf, width_buf;
1115 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
1116 static int max_ipix;
1117 h3c_coord_t in_ra[4], in_dec[4];
1119 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1120 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1121 h3c_coord_t width = PG_GETARG_FLOAT8(2);
1122 h3c_coord_t height = PG_GETARG_FLOAT8(3);
1124 int iteration = PG_GETARG_UINT32(4);
1126 if (invocation == 0)
1133 h3c_log(H3C_INFO,
"initialize pgh3c_box_query_it");
1137 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf)
1138 && (height == height_buf) && (width == width_buf))
1140 if (iteration < max_ipix) PG_RETURN_INT64(ipix[iteration]);
1141 PG_RETURN_INT64(-1);
1145 _box_2_polygon(ra_cen, dec_cen, width, height, in_ra, in_de);
1148 h3c_log(H3C_DEBUG,
"pgh3c_box_query_it: update to polygon h3c_poly_query: (%f,%f),(%f,%f), (%f,%f), (%f,%f)",
1149 in_ra[0], in_dec[0], in_ra[1], in_dec[1], in_ra[2], in_dec[2], in_ra[3], in_dec[3]);
1152 max_ipix = h3c_polygon_ipix_it(in_ra,
1157 H3C_DEFAULT_MEM_BLOCK);
1158 if (max_ipix < 1)
h3c_log(H3C_ERROR,
"pgh3c_box_query_it nothing found");
1161 ra_cen_buf = ra_cen;
1162 dec_cen_buf = dec_cen;
1164 height_buf = height;
1166 h3c_log(H3C_DEBUG,
"pgh3c_box_query_it end");
1167 if (max_ipix > 0) PG_RETURN_INT64(ipix[0]);
1168 PG_RETURN_INT64(-1);
1188 PG_FUNCTION_INFO_V1(pgh3c_in_poly_convex);
1189 Datum pgh3c_in_poly_convex(PG_FUNCTION_ARGS)
1191 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1193 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(2);
1194 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1195 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1197 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1206 if (poly_nitems % 2 != 0)
1208 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1210 else if (poly_nitems <= 4)
1212 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1217 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1219 h3c_log(H3C_ERROR,
"The polygon is not convex");
1223 ra_cen, dec_cen)) != H3C_DISJUNCT;
1226 PG_RETURN_BOOL((result));
1242 PG_FUNCTION_INFO_V1(pgh3c_in_poly);
1243 Datum pgh3c_in_poly(PG_FUNCTION_ARGS)
1245 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1247 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(2);
1248 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1249 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1251 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1260 if (poly_nitems % 2 != 0)
1262 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1264 else if (poly_nitems <= 4)
1266 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1271 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1273 result = (h3c_check_sphere_point_in_poly(poly_nitems, in_ra, in_dec,
1274 ra_cen, dec_cen)) != H3C_DISJUNCT;
1277 PG_RETURN_BOOL((result));
1290 PG_FUNCTION_INFO_V1(pgh3c_in_box);
1291 Datum pgh3c_in_box(PG_FUNCTION_ARGS)
1293 h3c_coord_t in_ra[4], in_dec[4];
1294 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1295 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1296 h3c_coord_t width = PG_GETARG_FLOAT8(2);
1297 h3c_coord_t height = PG_GETARG_FLOAT8(3);
1299 _box_2_polygon(ra_cen, dec_cen, width, height, in_ra, in_dec);
1301 result = (h3c_check_sphere_point_in_poly(poly_nitems, in_ra, in_dec,
1302 ra_cen, dec_cen)) != H3C_DISJUNCT;
1305 PG_RETURN_BOOL((result));
1315 PG_FUNCTION_INFO_V1(pgh3c_poly_center);
1316 Datum pgh3c_poly_center(PG_FUNCTION_ARGS)
1318 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1319 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
1320 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1321 h3c_coord_t radec[2];
1328 if (poly_nitems % 2 != 0)
1330 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1332 else if (poly_nitems <= 4)
1334 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1339 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1341 h3c_log(H3C_ERROR,
"The polygon is not convex");
1346 data = ( Datum *) palloc(
sizeof(Datum)*2);
1347 data[0] = Float8GetDatum (radec[0]);
1348 data[1] = Float8GetDatum (radec[1]);
1350 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
1353 result = construct_array(data, 2, FLOAT8OID, typlen, typbyval, typalign);
1355 PG_RETURN_ARRAYTYPE_P(result);
1366 PG_FUNCTION_INFO_V1(pgh3c_circle_intersect_poly);
1367 Datum pgh3c_circle_intersect_poly(PG_FUNCTION_ARGS)
1369 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1372 ArrayType *poly_arr;
1374 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1375 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1376 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
1377 poly_arr = PG_GETARG_ARRAYTYPE_P(3);
1379 poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1380 if (poly_nitems % 2 != 0)
1382 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1384 else if (poly_nitems <= 4)
1386 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1390 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1392 h3c_log(H3C_ERROR,
"The polygon is not convex");
1396 ra_cen, dec_cen, radius)) != H3C_DISJUNCT;
1398 PG_RETURN_BOOL((result));
1408 PG_FUNCTION_INFO_V1(pgh3c_poly_intersect_poly);
1409 Datum pgh3c_poly_intersect_poly(PG_FUNCTION_ARGS)
1411 static h3c_coord_t in_ra1[H3C_MAX_N_POLY_VERTEX], in_dec1[H3C_MAX_N_POLY_VERTEX];
1412 static h3c_coord_t in_ra2[H3C_MAX_N_POLY_VERTEX], in_dec2[H3C_MAX_N_POLY_VERTEX];
1414 int poly_nitems1, poly_nitems2;
1415 ArrayType *poly_arr1, *poly_arr2;
1417 poly_arr1 = PG_GETARG_ARRAYTYPE_P(0);
1418 poly_arr2 = PG_GETARG_ARRAYTYPE_P(1);
1420 poly_nitems1 = ArrayGetNItems(ARR_NDIM(poly_arr1), ARR_DIMS(poly_arr1));
1421 if (poly_nitems1 % 2 != 0)
1423 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1425 else if (poly_nitems1 <= 4)
1427 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1431 h3c_array_push(poly_nitems1, in_ra1, in_dec1, poly_arr1);
1433 h3c_log(H3C_ERROR,
"The polygon1 is not convex");
1436 poly_nitems2 = ArrayGetNItems(ARR_NDIM(poly_arr2), ARR_DIMS(poly_arr2));
1437 if (poly_nitems2 % 2 != 0)
1439 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1441 else if (poly_nitems2 <= 4)
1443 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1447 h3c_array_push(poly_nitems2, in_ra2, in_dec2, poly_arr2);
1449 h3c_log(H3C_ERROR,
"The polygon2 is not convex");
1453 poly_nitems2, in_ra2, in_dec2)) != H3C_DISJUNCT;
1455 PG_RETURN_BOOL((result));
1465 PG_FUNCTION_INFO_V1(pgh3c_dist_ipix);
1466 Datum pgh3c_dist_ipix(PG_FUNCTION_ARGS)
1468 h3c_coord_t coo1[2], coo2[2], d;
1469 h3c_ipix_t ipix1 = PG_GETARG_INT64(0);
1470 h3c_ipix_t ipix2 = PG_GETARG_INT64(1);
1472 h3c_pix2ang_nest(H3C_DEFAULT_NSIDE,
1479 h3c_pix2ang_nest(H3C_DEFAULT_NSIDE,
1486 d =
h3c_dist(coo1[0], coo1[1], coo2[0], coo2[2]);
1487 PG_RETURN_FLOAT8(d);
1502 PG_FUNCTION_INFO_V1(pgh3c_in_ellipse);
1503 Datum pgh3c_in_ellipse(PG_FUNCTION_ARGS)
1506 h3c_coord_t ra = PG_GETARG_FLOAT8(0);
1507 h3c_coord_t dec = PG_GETARG_FLOAT8(1);
1508 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(2);
1509 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(3);
1510 h3c_coord_t radius = PG_GETARG_FLOAT8(4);
1511 h3c_coord_t axis_ratio = PG_GETARG_FLOAT8(5);
1512 h3c_coord_t PA = PG_GETARG_FLOAT8(6);
1513 h3c_coord_t e = sqrt(1 - axis_ratio * axis_ratio);
1514 bool result =
h3c_in_ellipse(ra_cen, dec_cen, ra,dec, radius, e, PA);
1515 PG_RETURN_BOOL(result);
1528 PG_FUNCTION_INFO_V1(pgh3c_ellipse_query_it);
1529 Datum pgh3c_ellipse_query_it(PG_FUNCTION_ARGS)
1531 static h3c_ipix_t ipix[H3C_DEFAULT_MEM_BLOCK];
1532 static h3c_coord_t ra_cen_buf,dec_cen_buf, radius_buf;
1533 static int invocation=0;
1534 static int nside_buf, axis_ratio_buf;
1535 h3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
1536 h3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
1537 h3c_coord_t radius = PG_GETARG_FLOAT8(2);
1538 h3c_coord_t axis_ratio = PG_GETARG_FLOAT8(3);
1539 int iteration = PG_GETARG_INT32(4);
1540 int nside = PG_GETARG_INT32(5);
1541 static int max_ipix;
1543 if (invocation == 0)
1553 if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
1554 (radius == radius_buf) && (nside == nside_buf) &&
1555 (axis_ratio == axis_ratio_buf))
1557 if (iteration<max_ipix) PG_RETURN_INT64(ipix[iteration]);
1558 PG_RETURN_INT64(-1);
1563 max_ipix = h3c_disk_ipix_it(PG_GETARG_FLOAT8(0),
1564 PG_GETARG_FLOAT8(1),
1565 PG_GETARG_FLOAT8(2)/2.,
1568 H3C_DEFAULT_MEM_BLOCK) ;
1569 if (max_ipix < 0)
h3c_log(H3C_ERROR,
"pgh3c_ellipse_query_it nothing found");
1572 ra_cen_buf = ra_cen;
1573 dec_cen_buf = dec_cen;
1574 radius_buf = radius;
1576 axis_ratio_buf = axis_ratio;
1578 h3c_log(H3C_DEBUG,
"pgh3c_ellipse_query_it end");
1579 PG_RETURN_INT64(ipix[iteration]);
1588 PG_FUNCTION_INFO_V1(pgh3c_circle_area);
1589 Datum pgh3c_circle_area(PG_FUNCTION_ARGS)
1591 h3c_coord_t radius = PG_GETARG_FLOAT8(0);
1592 h3c_coord_t val = sin(radius*H3C_DEGRA/2.);
1593 PG_RETURN_FLOAT8(4*H3C_PI*val*val);
1602 PG_FUNCTION_INFO_V1(pgh3c_poly_area);
1603 Datum pgh3c_poly_area(PG_FUNCTION_ARGS)
1606 static h3c_coord_t in_ra[H3C_MAX_N_POLY_VERTEX], in_dec[H3C_MAX_N_POLY_VERTEX];
1607 ArrayType *poly_arr = PG_GETARG_ARRAYTYPE_P(0);
1608 int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
1614 if (poly_nitems % 2 != 0)
1616 h3c_log(H3C_ERROR,
"Invalid array argument! The array should contain even number of arguments");
1618 else if (poly_nitems <= 4)
1620 h3c_log(H3C_ERROR,
"Invalid polygon! Less than 3 vertexes");
1624 h3c_array_push(poly_nitems, in_ra, in_dec, poly_arr);
1628 h3c_log(H3C_ERROR,
"The polygon is not convex");
1632 h3c_log(H3C_DEBUG,
"pgh3c_poly_area end");
1633 PG_RETURN_FLOAT8(area);
1644 PG_FUNCTION_INFO_V1(pgh3c_ipix2default_it);
1645 Datum pgh3c_ipix2default_it(PG_FUNCTION_ARGS)
1647 static h3c_ipix_t m_ipix[2];
1648 static h3c_ipix_t ipix_buf, nside_buf;
1649 static int invocation = 0;
1650 h3c_ipix_t ipix = PG_GETARG_INT32(0);
1651 int nside = PG_GETARG_INT32(1);
1652 int iteration = PG_GETARG_INT32(2);
1653 int order, order_default;
1655 if (invocation == 0)
1665 if ((ipix == ipix_buf) && (nside == nside_buf))
1667 if (iteration < 2) PG_RETURN_INT64(m_ipix[iteration]);
1668 PG_RETURN_INT64(-1);
1672 order_default =
h3c_order(H3C_DEFAULT_NSIDE);
1674 if (order == order_default) {
1678 else if (order < order_default) {
1679 m_ipix[0] = ipix<<(order_default-order)*2;
1680 m_ipix[1] = (ipix+1)<<(order_default-order)*2;
1683 m_ipix[0] = ipix>>(order_default-order)*2;
1684 m_ipix[1] = (ipix+1)>>(order_default-order)*2;
1691 h3c_log(H3C_DEBUG,
"pgh3c_ipix2default_it");
1692 PG_RETURN_INT64(m_ipix[iteration]);
1700 PG_FUNCTION_INFO_V1(pgh3c_nside_config);
1701 Datum pgh3c_nside_config(PG_FUNCTION_ARGS)
1703 PG_RETURN_INT64(H3C_DEFAULT_NSIDE);