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

Definition at line 6619 of file rt_pg.c.

References FALSE, rt_raster_copy_band(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), rt_raster_serialize(), and rt_raster_serialized_t::size.

6620 {
6621  rt_pgraster *pgto = NULL;
6622  rt_pgraster *pgfrom = NULL;
6623  rt_pgraster *pgrtn = NULL;
6624  rt_raster torast = NULL;
6625  rt_raster fromrast = NULL;
6626  int toindex = 0;
6627  int fromband = 0;
6628  int oldtorastnumbands = 0;
6629  int newtorastnumbands = 0;
6630  int newbandindex = 0;
6631 
6632  /* Deserialize torast */
6633  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
6634  pgto = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
6635 
6636  torast = rt_raster_deserialize(pgto, FALSE);
6637  if (!torast) {
6638  PG_FREE_IF_COPY(pgto, 0);
6639  elog(ERROR, "RASTER_copyBand: Could not deserialize first raster");
6640  PG_RETURN_NULL();
6641  }
6642 
6643  /* Deserialize fromrast */
6644  if (!PG_ARGISNULL(1)) {
6645  pgfrom = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
6646 
6647  fromrast = rt_raster_deserialize(pgfrom, FALSE);
6648  if (!fromrast) {
6649  rt_raster_destroy(torast);
6650  PG_FREE_IF_COPY(pgfrom, 1);
6651  PG_FREE_IF_COPY(pgto, 0);
6652  elog(ERROR, "RASTER_copyBand: Could not deserialize second raster");
6653  PG_RETURN_NULL();
6654  }
6655 
6656  oldtorastnumbands = rt_raster_get_num_bands(torast);
6657 
6658  if (PG_ARGISNULL(2))
6659  fromband = 1;
6660  else
6661  fromband = PG_GETARG_INT32(2);
6662 
6663  if (PG_ARGISNULL(3))
6664  toindex = oldtorastnumbands + 1;
6665  else
6666  toindex = PG_GETARG_INT32(3);
6667 
6668  /* Copy band fromrast torast */
6669  newbandindex = rt_raster_copy_band(
6670  torast, fromrast,
6671  fromband - 1, toindex - 1
6672  );
6673 
6674  newtorastnumbands = rt_raster_get_num_bands(torast);
6675  if (newtorastnumbands == oldtorastnumbands || newbandindex == -1) {
6676  elog(NOTICE, "RASTER_copyBand: Could not add band to raster. "
6677  "Returning original raster."
6678  );
6679  }
6680 
6681  rt_raster_destroy(fromrast);
6682  PG_FREE_IF_COPY(pgfrom, 1);
6683  }
6684 
6685  /* Serialize and return torast */
6686  pgrtn = rt_raster_serialize(torast);
6687  rt_raster_destroy(torast);
6688  PG_FREE_IF_COPY(pgto, 0);
6689  if (!pgrtn) PG_RETURN_NULL();
6690 
6691  SET_VARSIZE(pgrtn, pgrtn->size);
6692  PG_RETURN_POINTER(pgrtn);
6693 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_api.c:5677
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_api.c:8158
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
int rt_raster_copy_band(rt_raster torast, rt_raster fromrast, int fromindex, int toindex)
Copy one band from one raster to another.
Definition: rt_api.c:8582
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
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: