PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_GDALWarp()

Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 448 of file rtpg_gdal.c.

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

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, SRID_UNKNOWN, and text_to_cstring().

Here is the call graph for this function: