PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_GDALWarp()

Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 451 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().

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