PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ RASTER_GDALWarp()

Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 447 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, SRID_UNKNOWN, and text_to_cstring().

Referenced by RASTER_getGDALDrivers().

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