449{
454
455 text *algtext = NULL;
456 char *algchar = NULL;
457 GDALResampleAlg alg = GRA_NearestNeighbour;
458 double max_err = 0.125;
459
461 char *src_srs = NULL;
463 char *dst_srs = NULL;
464 int no_srid = 0;
465
466 double scale[2] = {0};
467 double *scale_x = NULL;
468 double *scale_y = NULL;
469
470 double gridw[2] = {0};
471 double *grid_xw = NULL;
472 double *grid_yw = NULL;
473
474 double skew[2] = {0};
475 double *skew_x = NULL;
476 double *skew_y = NULL;
477
478 int dim[2] = {0};
479 int *dim_x = NULL;
480 int *dim_y = NULL;
481
483
484
485 if (PG_ARGISNULL(0))
486 PG_RETURN_NULL();
487 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
488
489
491 if (!raster) {
492 PG_FREE_IF_COPY(pgraster, 0);
493 elog(ERROR, "RASTER_GDALWarp: Could not deserialize raster");
494 PG_RETURN_NULL();
495 }
496
497
498 if (!PG_ARGISNULL(1)) {
499 algtext = PG_GETARG_TEXT_P(1);
502 }
504
505
506 if (!PG_ARGISNULL(2)) {
507 max_err = PG_GETARG_FLOAT8(2);
508 if (max_err < 0.) max_err = 0.;
509 }
511
512
515
516
517 if (!PG_ARGISNULL(3)) {
521 PG_FREE_IF_COPY(pgraster, 0);
522 elog(ERROR, "RASTER_GDALWarp: %d is an invalid target SRID", dst_srid);
523 PG_RETURN_NULL();
524 }
525 }
526 else
527 dst_srid = src_srid;
529
530
533 PG_FREE_IF_COPY(pgraster, 0);
534 elog(ERROR, "RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid);
535 PG_RETURN_NULL();
536 }
537
538 else if (dst_srid == src_srid) {
539 no_srid = 1;
540 }
541
542
543 if (!PG_ARGISNULL(4)) {
544 scale[0] = PG_GETARG_FLOAT8(4);
546 scale_x = &scale[0];
547 }
548
549
550 if (!PG_ARGISNULL(5)) {
551 scale[1] = PG_GETARG_FLOAT8(5);
553 scale_y = &scale[1];
554 }
555
556
557 if (!PG_ARGISNULL(6)) {
558 gridw[0] = PG_GETARG_FLOAT8(6);
559 grid_xw = &gridw[0];
560 }
561
562
563 if (!PG_ARGISNULL(7)) {
564 gridw[1] = PG_GETARG_FLOAT8(7);
565 grid_yw = &gridw[1];
566 }
567
568
569 if (!PG_ARGISNULL(8)) {
570 skew[0] = PG_GETARG_FLOAT8(8);
572 skew_x = &skew[0];
573 }
574
575
576 if (!PG_ARGISNULL(9)) {
577 skew[1] = PG_GETARG_FLOAT8(9);
579 skew_y = &skew[1];
580 }
581
582
583 if (!PG_ARGISNULL(10)) {
584 dim[0] = PG_GETARG_INT32(10);
585 if (dim[0] < 0) dim[0] = 0;
586 if (dim[0] > 0) dim_x = &dim[0];
587 }
588
589
590 if (!PG_ARGISNULL(11)) {
591 dim[1] = PG_GETARG_INT32(11);
592 if (dim[1] < 0) dim[1] = 0;
593 if (dim[1] > 0) dim_y = &dim[1];
594 }
595
596
597 if (
599 (scale_x == NULL) && (scale_y == NULL) &&
600 (grid_xw == NULL) && (grid_yw == NULL) &&
601 (skew_x == NULL) && (skew_y == NULL) &&
602 (dim_x == NULL) && (dim_y == NULL)
603 ) {
604 elog(NOTICE, "No resampling parameters provided. Returning original raster");
606 PG_RETURN_POINTER(pgraster);
607 }
608
609 else if (
610 (grid_xw != NULL && grid_yw == NULL) ||
611 (grid_xw == NULL && grid_yw != NULL)
612 ) {
613 elog(NOTICE, "Values must be provided for both X and Y when specifying the alignment. Returning original raster");
615 PG_RETURN_POINTER(pgraster);
616 }
617
618 else if (
619 (scale_x != NULL && scale_y == NULL) ||
620 (scale_x == NULL && scale_y != NULL)
621 ) {
622 elog(NOTICE, "Values must be provided for both X and Y when specifying the scale. Returning original raster");
624 PG_RETURN_POINTER(pgraster);
625 }
626
627 else if (
628 (scale_x != NULL || scale_y != NULL) &&
629 (dim_x != NULL || dim_y != NULL)
630 ) {
631 elog(NOTICE, "Scale X/Y and width/height are mutually exclusive. Only provide one. Returning original raster");
633 PG_RETURN_POINTER(pgraster);
634 }
635
636
637 if (!no_srid) {
638
640 if (NULL == src_srs) {
642 PG_FREE_IF_COPY(pgraster, 0);
643 elog(ERROR, "RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid);
644 PG_RETURN_NULL();
645 }
647
649 if (NULL == dst_srs) {
650 pfree(src_srs);
652 PG_FREE_IF_COPY(pgraster, 0);
653 elog(ERROR, "RASTER_GDALWarp: Target SRID (%d) is unknown", dst_srid);
654 PG_RETURN_NULL();
655 }
657 }
658
660 raster,
661 src_srs, dst_srs,
662 scale_x, scale_y,
663 dim_x, dim_y,
664 NULL, NULL,
665 grid_xw, grid_yw,
666 skew_x, skew_y,
667 alg, max_err);
669 PG_FREE_IF_COPY(pgraster, 0);
670 if (!no_srid) {
671 pfree(src_srs);
672 pfree(dst_srs);
673 }
674 if (!rast) {
675 elog(ERROR, "RASTER_band: Could not create transformed raster");
676 PG_RETURN_NULL();
677 }
678
679
681
684
685 if (NULL == pgrast) PG_RETURN_NULL();
686
688
689 SET_VARSIZE(pgrast, pgrast->
size);
690 PG_RETURN_POINTER(pgrast);
691}
#define SRID_UNKNOWN
Unknown SRID value.
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
rt_raster rt_raster_gdal_warp(rt_raster raster, const char *src_srs, const char *dst_srs, double *scale_x, double *scale_y, int *width, int *height, double *ul_xw, double *ul_yw, double *grid_xw, double *grid_yw, double *skew_x, double *skew_y, GDALResampleAlg resample_alg, double max_err)
Return a warped raster using GDAL Warp API.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
char * text_to_cstring(const text *textptr)
char * rtpg_getSR(int32_t srid)
char * rtpg_trim(const char *input)
char * rtpg_strtoupper(char *str)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)