PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 12497 of file rt_pg.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.

12498 {
12499  rt_pgraster *pgraster = NULL;
12500  rt_pgraster *pgrast = NULL;
12501  rt_raster raster = NULL;
12502  rt_raster rast = NULL;
12503 
12504  text *algtext = NULL;
12505  char *algchar = NULL;
12506  GDALResampleAlg alg = GRA_NearestNeighbour;
12507  double max_err = 0.125;
12508 
12509  int src_srid = SRID_UNKNOWN;
12510  char *src_srs = NULL;
12511  int dst_srid = SRID_UNKNOWN;
12512  char *dst_srs = NULL;
12513  int no_srid = 0;
12514 
12515  double scale[2] = {0};
12516  double *scale_x = NULL;
12517  double *scale_y = NULL;
12518 
12519  double gridw[2] = {0};
12520  double *grid_xw = NULL;
12521  double *grid_yw = NULL;
12522 
12523  double skew[2] = {0};
12524  double *skew_x = NULL;
12525  double *skew_y = NULL;
12526 
12527  int dim[2] = {0};
12528  int *dim_x = NULL;
12529  int *dim_y = NULL;
12530 
12531  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: Starting");
12532 
12533  /* pgraster is null, return null */
12534  if (PG_ARGISNULL(0))
12535  PG_RETURN_NULL();
12536  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
12537 
12538  /* raster */
12539  raster = rt_raster_deserialize(pgraster, FALSE);
12540  if (!raster) {
12541  PG_FREE_IF_COPY(pgraster, 0);
12542  elog(ERROR, "RASTER_GDALWarp: Could not deserialize raster");
12543  PG_RETURN_NULL();
12544  }
12545 
12546  /* resampling algorithm */
12547  if (!PG_ARGISNULL(1)) {
12548  algtext = PG_GETARG_TEXT_P(1);
12549  algchar = rtpg_trim(rtpg_strtoupper(text_to_cstring(algtext)));
12550  alg = rt_util_gdal_resample_alg(algchar);
12551  }
12552  POSTGIS_RT_DEBUGF(4, "Resampling algorithm: %d", alg);
12553 
12554  /* max error */
12555  if (!PG_ARGISNULL(2)) {
12556  max_err = PG_GETARG_FLOAT8(2);
12557  if (max_err < 0.) max_err = 0.;
12558  }
12559  POSTGIS_RT_DEBUGF(4, "max_err: %f", max_err);
12560 
12561  /* source SRID */
12562  src_srid = clamp_srid(rt_raster_get_srid(raster));
12563  POSTGIS_RT_DEBUGF(4, "source SRID: %d", src_srid);
12564 
12565  /* target SRID */
12566  if (!PG_ARGISNULL(3)) {
12567  dst_srid = clamp_srid(PG_GETARG_INT32(3));
12568  if (dst_srid == SRID_UNKNOWN) {
12569  rt_raster_destroy(raster);
12570  PG_FREE_IF_COPY(pgraster, 0);
12571  elog(ERROR, "RASTER_GDALWarp: %d is an invalid target SRID", dst_srid);
12572  PG_RETURN_NULL();
12573  }
12574  }
12575  else
12576  dst_srid = src_srid;
12577  POSTGIS_RT_DEBUGF(4, "destination SRID: %d", dst_srid);
12578 
12579  /* target SRID != src SRID, error */
12580  if (src_srid == SRID_UNKNOWN && dst_srid != src_srid) {
12581  rt_raster_destroy(raster);
12582  PG_FREE_IF_COPY(pgraster, 0);
12583  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid);
12584  PG_RETURN_NULL();
12585  }
12586  /* target SRID == src SRID, no reprojection */
12587  else if (dst_srid == src_srid) {
12588  no_srid = 1;
12589  }
12590 
12591  /* scale x */
12592  if (!PG_ARGISNULL(4)) {
12593  scale[0] = PG_GETARG_FLOAT8(4);
12594  if (FLT_NEQ(scale[0], 0)) scale_x = &scale[0];
12595  }
12596 
12597  /* scale y */
12598  if (!PG_ARGISNULL(5)) {
12599  scale[1] = PG_GETARG_FLOAT8(5);
12600  if (FLT_NEQ(scale[1], 0)) scale_y = &scale[1];
12601  }
12602 
12603  /* grid alignment x */
12604  if (!PG_ARGISNULL(6)) {
12605  gridw[0] = PG_GETARG_FLOAT8(6);
12606  grid_xw = &gridw[0];
12607  }
12608 
12609  /* grid alignment y */
12610  if (!PG_ARGISNULL(7)) {
12611  gridw[1] = PG_GETARG_FLOAT8(7);
12612  grid_yw = &gridw[1];
12613  }
12614 
12615  /* skew x */
12616  if (!PG_ARGISNULL(8)) {
12617  skew[0] = PG_GETARG_FLOAT8(8);
12618  if (FLT_NEQ(skew[0], 0)) skew_x = &skew[0];
12619  }
12620 
12621  /* skew y */
12622  if (!PG_ARGISNULL(9)) {
12623  skew[1] = PG_GETARG_FLOAT8(9);
12624  if (FLT_NEQ(skew[1], 0)) skew_y = &skew[1];
12625  }
12626 
12627  /* width */
12628  if (!PG_ARGISNULL(10)) {
12629  dim[0] = PG_GETARG_INT32(10);
12630  if (dim[0] < 0) dim[0] = 0;
12631  if (dim[0] > 0) dim_x = &dim[0];
12632  }
12633 
12634  /* height */
12635  if (!PG_ARGISNULL(11)) {
12636  dim[1] = PG_GETARG_INT32(11);
12637  if (dim[1] < 0) dim[1] = 0;
12638  if (dim[1] > 0) dim_y = &dim[1];
12639  }
12640 
12641  /* check that at least something is to be done */
12642  if (
12643  (dst_srid == SRID_UNKNOWN) &&
12644  (scale_x == NULL) &&
12645  (scale_y == NULL) &&
12646  (grid_xw == NULL) &&
12647  (grid_yw == NULL) &&
12648  (skew_x == NULL) &&
12649  (skew_y == NULL) &&
12650  (dim_x == NULL) &&
12651  (dim_y == NULL)
12652  ) {
12653  elog(NOTICE, "No resampling parameters provided. Returning original raster");
12654  rt_raster_destroy(raster);
12655  PG_RETURN_POINTER(pgraster);
12656  }
12657  /* both values of alignment must be provided if any one is provided */
12658  else if (
12659  (grid_xw != NULL && grid_yw == NULL) ||
12660  (grid_xw == NULL && grid_yw != NULL)
12661  ) {
12662  elog(NOTICE, "Values must be provided for both X and Y when specifying the alignment. Returning original raster");
12663  rt_raster_destroy(raster);
12664  PG_RETURN_POINTER(pgraster);
12665  }
12666  /* both values of scale must be provided if any one is provided */
12667  else if (
12668  (scale_x != NULL && scale_y == NULL) ||
12669  (scale_x == NULL && scale_y != NULL)
12670  ) {
12671  elog(NOTICE, "Values must be provided for both X and Y when specifying the scale. Returning original raster");
12672  rt_raster_destroy(raster);
12673  PG_RETURN_POINTER(pgraster);
12674  }
12675  /* scale and width/height provided */
12676  else if (
12677  (scale_x != NULL || scale_y != NULL) &&
12678  (dim_x != NULL || dim_y != NULL)
12679  ) {
12680  elog(NOTICE, "Scale X/Y and width/height are mutually exclusive. Only provide one. Returning original raster");
12681  rt_raster_destroy(raster);
12682  PG_RETURN_POINTER(pgraster);
12683  }
12684 
12685  /* get srses from srids */
12686  if (!no_srid) {
12687  /* source srs */
12688  src_srs = rtpg_getSR(src_srid);
12689  if (NULL == src_srs) {
12690  rt_raster_destroy(raster);
12691  PG_FREE_IF_COPY(pgraster, 0);
12692  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid);
12693  PG_RETURN_NULL();
12694  }
12695  POSTGIS_RT_DEBUGF(4, "src srs: %s", src_srs);
12696 
12697  dst_srs = rtpg_getSR(dst_srid);
12698  if (NULL == dst_srs) {
12699  if (!no_srid) pfree(src_srs);
12700  rt_raster_destroy(raster);
12701  PG_FREE_IF_COPY(pgraster, 0);
12702  elog(ERROR, "RASTER_GDALWarp: Target SRID (%d) is unknown", dst_srid);
12703  PG_RETURN_NULL();
12704  }
12705  POSTGIS_RT_DEBUGF(4, "dst srs: %s", dst_srs);
12706  }
12707 
12708  rast = rt_raster_gdal_warp(
12709  raster,
12710  src_srs, dst_srs,
12711  scale_x, scale_y,
12712  dim_x, dim_y,
12713  NULL, NULL,
12714  grid_xw, grid_yw,
12715  skew_x, skew_y,
12716  alg, max_err);
12717  rt_raster_destroy(raster);
12718  PG_FREE_IF_COPY(pgraster, 0);
12719  if (!no_srid) {
12720  pfree(src_srs);
12721  pfree(dst_srs);
12722  }
12723  if (!rast) {
12724  elog(ERROR, "RASTER_band: Could not create transformed raster");
12725  PG_RETURN_NULL();
12726  }
12727 
12728  /* add target SRID */
12729  rt_raster_set_srid(rast, dst_srid);
12730 
12731  pgrast = rt_raster_serialize(rast);
12732  rt_raster_destroy(rast);
12733 
12734  if (NULL == pgrast) PG_RETURN_NULL();
12735 
12736  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: done");
12737 
12738  SET_VARSIZE(pgrast, pgrast->size);
12739  PG_RETURN_POINTER(pgrast);
12740 }
static char * rtpg_strtoupper(char *str)
Definition: rt_pg.c:730
#define FLT_NEQ(x, y)
Definition: rt_api.h:2158
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:326
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_api.c:8158
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_api.c:9710
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
static char * rtpg_trim(const char *input)
Definition: rt_pg.c:856
static char * rtpg_getSR(int srid)
Definition: rt_pg.c:912
tuple rast
Definition: rtpixdump.py:62
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:123
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_api.c:5668
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_api.c:5661
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
Definition: rt_api.c:202
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rt_pg.h:58
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rt_pg.h:62
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_api.c:8350

Here is the call graph for this function: