PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ RASTER_GDALWarp()

Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 450 of file rtpg_gdal.c.

References clamp_srid(), FALSE, FLT_NEQ, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtpixdump::rast, rtrowdump::raster, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_gdal_warp(), rt_raster_get_srid(), rt_raster_serialize(), rt_raster_set_srid(), rt_util_gdal_resample_alg(), rtpg_getSR(), rtpg_strtoupper(), rtpg_trim(), rt_raster_serialized_t::size, and SRID_UNKNOWN.

Referenced by RASTER_getGDALDrivers().

451 {
452  rt_pgraster *pgraster = NULL;
453  rt_pgraster *pgrast = NULL;
454  rt_raster raster = NULL;
455  rt_raster rast = NULL;
456 
457  text *algtext = NULL;
458  char *algchar = NULL;
459  GDALResampleAlg alg = GRA_NearestNeighbour;
460  double max_err = 0.125;
461 
462  int src_srid = SRID_UNKNOWN;
463  char *src_srs = NULL;
464  int dst_srid = SRID_UNKNOWN;
465  char *dst_srs = NULL;
466  int no_srid = 0;
467 
468  double scale[2] = {0};
469  double *scale_x = NULL;
470  double *scale_y = NULL;
471 
472  double gridw[2] = {0};
473  double *grid_xw = NULL;
474  double *grid_yw = NULL;
475 
476  double skew[2] = {0};
477  double *skew_x = NULL;
478  double *skew_y = NULL;
479 
480  int dim[2] = {0};
481  int *dim_x = NULL;
482  int *dim_y = NULL;
483 
484  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: Starting");
485 
486  /* pgraster is null, return null */
487  if (PG_ARGISNULL(0))
488  PG_RETURN_NULL();
489  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
490 
491  /* raster */
492  raster = rt_raster_deserialize(pgraster, FALSE);
493  if (!raster) {
494  PG_FREE_IF_COPY(pgraster, 0);
495  elog(ERROR, "RASTER_GDALWarp: Could not deserialize raster");
496  PG_RETURN_NULL();
497  }
498 
499  /* resampling algorithm */
500  if (!PG_ARGISNULL(1)) {
501  algtext = PG_GETARG_TEXT_P(1);
502  algchar = rtpg_trim(rtpg_strtoupper(text_to_cstring(algtext)));
503  alg = rt_util_gdal_resample_alg(algchar);
504  }
505  POSTGIS_RT_DEBUGF(4, "Resampling algorithm: %d", alg);
506 
507  /* max error */
508  if (!PG_ARGISNULL(2)) {
509  max_err = PG_GETARG_FLOAT8(2);
510  if (max_err < 0.) max_err = 0.;
511  }
512  POSTGIS_RT_DEBUGF(4, "max_err: %f", max_err);
513 
514  /* source SRID */
515  src_srid = clamp_srid(rt_raster_get_srid(raster));
516  POSTGIS_RT_DEBUGF(4, "source SRID: %d", src_srid);
517 
518  /* target SRID */
519  if (!PG_ARGISNULL(3)) {
520  dst_srid = clamp_srid(PG_GETARG_INT32(3));
521  if (dst_srid == SRID_UNKNOWN) {
522  rt_raster_destroy(raster);
523  PG_FREE_IF_COPY(pgraster, 0);
524  elog(ERROR, "RASTER_GDALWarp: %d is an invalid target SRID", dst_srid);
525  PG_RETURN_NULL();
526  }
527  }
528  else
529  dst_srid = src_srid;
530  POSTGIS_RT_DEBUGF(4, "destination SRID: %d", dst_srid);
531 
532  /* target SRID != src SRID, error */
533  if (src_srid == SRID_UNKNOWN && dst_srid != src_srid) {
534  rt_raster_destroy(raster);
535  PG_FREE_IF_COPY(pgraster, 0);
536  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid);
537  PG_RETURN_NULL();
538  }
539  /* target SRID == src SRID, no reprojection */
540  else if (dst_srid == src_srid) {
541  no_srid = 1;
542  }
543 
544  /* scale x */
545  if (!PG_ARGISNULL(4)) {
546  scale[0] = PG_GETARG_FLOAT8(4);
547  if (FLT_NEQ(scale[0], 0)) scale_x = &scale[0];
548  }
549 
550  /* scale y */
551  if (!PG_ARGISNULL(5)) {
552  scale[1] = PG_GETARG_FLOAT8(5);
553  if (FLT_NEQ(scale[1], 0)) scale_y = &scale[1];
554  }
555 
556  /* grid alignment x */
557  if (!PG_ARGISNULL(6)) {
558  gridw[0] = PG_GETARG_FLOAT8(6);
559  grid_xw = &gridw[0];
560  }
561 
562  /* grid alignment y */
563  if (!PG_ARGISNULL(7)) {
564  gridw[1] = PG_GETARG_FLOAT8(7);
565  grid_yw = &gridw[1];
566  }
567 
568  /* skew x */
569  if (!PG_ARGISNULL(8)) {
570  skew[0] = PG_GETARG_FLOAT8(8);
571  if (FLT_NEQ(skew[0], 0)) skew_x = &skew[0];
572  }
573 
574  /* skew y */
575  if (!PG_ARGISNULL(9)) {
576  skew[1] = PG_GETARG_FLOAT8(9);
577  if (FLT_NEQ(skew[1], 0)) skew_y = &skew[1];
578  }
579 
580  /* width */
581  if (!PG_ARGISNULL(10)) {
582  dim[0] = PG_GETARG_INT32(10);
583  if (dim[0] < 0) dim[0] = 0;
584  if (dim[0] > 0) dim_x = &dim[0];
585  }
586 
587  /* height */
588  if (!PG_ARGISNULL(11)) {
589  dim[1] = PG_GETARG_INT32(11);
590  if (dim[1] < 0) dim[1] = 0;
591  if (dim[1] > 0) dim_y = &dim[1];
592  }
593 
594  /* check that at least something is to be done */
595  if (
596  (dst_srid == SRID_UNKNOWN) &&
597  (scale_x == NULL) && (scale_y == NULL) &&
598  (grid_xw == NULL) && (grid_yw == NULL) &&
599  (skew_x == NULL) && (skew_y == NULL) &&
600  (dim_x == NULL) && (dim_y == NULL)
601  ) {
602  elog(NOTICE, "No resampling parameters provided. Returning original raster");
603  rt_raster_destroy(raster);
604  PG_RETURN_POINTER(pgraster);
605  }
606  /* both values of alignment must be provided if any one is provided */
607  else if (
608  (grid_xw != NULL && grid_yw == NULL) ||
609  (grid_xw == NULL && grid_yw != NULL)
610  ) {
611  elog(NOTICE, "Values must be provided for both X and Y when specifying the alignment. Returning original raster");
612  rt_raster_destroy(raster);
613  PG_RETURN_POINTER(pgraster);
614  }
615  /* both values of scale must be provided if any one is provided */
616  else if (
617  (scale_x != NULL && scale_y == NULL) ||
618  (scale_x == NULL && scale_y != NULL)
619  ) {
620  elog(NOTICE, "Values must be provided for both X and Y when specifying the scale. Returning original raster");
621  rt_raster_destroy(raster);
622  PG_RETURN_POINTER(pgraster);
623  }
624  /* scale and width/height provided */
625  else if (
626  (scale_x != NULL || scale_y != NULL) &&
627  (dim_x != NULL || dim_y != NULL)
628  ) {
629  elog(NOTICE, "Scale X/Y and width/height are mutually exclusive. Only provide one. Returning original raster");
630  rt_raster_destroy(raster);
631  PG_RETURN_POINTER(pgraster);
632  }
633 
634  /* get srses from srids */
635  if (!no_srid) {
636  /* source srs */
637  src_srs = rtpg_getSR(src_srid);
638  if (NULL == src_srs) {
639  rt_raster_destroy(raster);
640  PG_FREE_IF_COPY(pgraster, 0);
641  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid);
642  PG_RETURN_NULL();
643  }
644  POSTGIS_RT_DEBUGF(4, "src srs: %s", src_srs);
645 
646  dst_srs = rtpg_getSR(dst_srid);
647  if (NULL == dst_srs) {
648  if (!no_srid) pfree(src_srs);
649  rt_raster_destroy(raster);
650  PG_FREE_IF_COPY(pgraster, 0);
651  elog(ERROR, "RASTER_GDALWarp: Target SRID (%d) is unknown", dst_srid);
652  PG_RETURN_NULL();
653  }
654  POSTGIS_RT_DEBUGF(4, "dst srs: %s", dst_srs);
655  }
656 
657  rast = rt_raster_gdal_warp(
658  raster,
659  src_srs, dst_srs,
660  scale_x, scale_y,
661  dim_x, dim_y,
662  NULL, NULL,
663  grid_xw, grid_yw,
664  skew_x, skew_y,
665  alg, max_err);
666  rt_raster_destroy(raster);
667  PG_FREE_IF_COPY(pgraster, 0);
668  if (!no_srid) {
669  pfree(src_srs);
670  pfree(dst_srs);
671  }
672  if (!rast) {
673  elog(ERROR, "RASTER_band: Could not create transformed raster");
674  PG_RETURN_NULL();
675  }
676 
677  /* add target SRID */
678  rt_raster_set_srid(rast, dst_srid);
679 
680  pgrast = rt_raster_serialize(rast);
681  rt_raster_destroy(rast);
682 
683  if (NULL == pgrast) PG_RETURN_NULL();
684 
685  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: done");
686 
687  SET_VARSIZE(pgrast, pgrast->size);
688  PG_RETURN_POINTER(pgrast);
689 }
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
Definition: lwutil.c:378
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
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.
Definition: rt_warp.c:178
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:57
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
#define FLT_NEQ(x, y)
Definition: librtcore.h:2196
char * rtpg_trim(const char *input)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster&#39;s SRID.
Definition: rt_raster.c:363
int32_t rt_raster_get_srid(rt_raster raster)
Get raster&#39;s SRID.
Definition: rt_raster.c:356
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2213
char * rtpg_getSR(int srid)
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:53
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
Definition: rt_util.c:91
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
char * rtpg_strtoupper(char *str)
Here is the call graph for this function:
Here is the caller graph for this function: