PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_band()

Datum RASTER_band ( PG_FUNCTION_ARGS  )

Definition at line 1493 of file rtpg_create.c.

References FALSE, POSTGIS_RT_DEBUGF, rtpixdump::rast, rtrowdump::raster, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_from_band(), rt_raster_get_num_bands(), rt_raster_serialize(), rt_raster_serialized_t::size, and TRUE.

Referenced by RASTER_tile().

1494 {
1495  rt_pgraster *pgraster;
1496  rt_pgraster *pgrast;
1497  rt_raster raster;
1498  rt_raster rast;
1499 
1500  bool skip = FALSE;
1501  ArrayType *array;
1502  Oid etype;
1503  Datum *e;
1504  bool *nulls;
1505  int16 typlen;
1506  bool typbyval;
1507  char typalign;
1508 
1509  uint32_t numBands;
1510  uint32_t *bandNums;
1511  uint32 idx = 0;
1512  int n;
1513  int i = 0;
1514  int j = 0;
1515 
1516  if (PG_ARGISNULL(0))
1517  PG_RETURN_NULL();
1518  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1519 
1520  raster = rt_raster_deserialize(pgraster, FALSE);
1521  if (!raster) {
1522  PG_FREE_IF_COPY(pgraster, 0);
1523  elog(ERROR, "RASTER_band: Could not deserialize raster");
1524  PG_RETURN_NULL();
1525  }
1526 
1527  /* process bandNums */
1528  if (PG_ARGISNULL(1)) {
1529  elog(NOTICE, "Band number(s) not provided. Returning original raster");
1530  skip = TRUE;
1531  }
1532  do {
1533  if (skip) break;
1534 
1535  numBands = rt_raster_get_num_bands(raster);
1536 
1537  array = PG_GETARG_ARRAYTYPE_P(1);
1538  etype = ARR_ELEMTYPE(array);
1539  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1540 
1541  switch (etype) {
1542  case INT2OID:
1543  case INT4OID:
1544  break;
1545  default:
1546  rt_raster_destroy(raster);
1547  PG_FREE_IF_COPY(pgraster, 0);
1548  elog(ERROR, "RASTER_band: Invalid data type for band number(s)");
1549  PG_RETURN_NULL();
1550  break;
1551  }
1552 
1553  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1554  &nulls, &n);
1555 
1556  bandNums = palloc(sizeof(uint32_t) * n);
1557  for (i = 0, j = 0; i < n; i++) {
1558  if (nulls[i]) continue;
1559 
1560  switch (etype) {
1561  case INT2OID:
1562  idx = (uint32_t) DatumGetInt16(e[i]);
1563  break;
1564  case INT4OID:
1565  idx = (uint32_t) DatumGetInt32(e[i]);
1566  break;
1567  }
1568 
1569  POSTGIS_RT_DEBUGF(3, "band idx (before): %d", idx);
1570  if (idx > numBands || idx < 1) {
1571  elog(NOTICE, "Invalid band index (must use 1-based). Returning original raster");
1572  skip = TRUE;
1573  break;
1574  }
1575 
1576  bandNums[j] = idx - 1;
1577  POSTGIS_RT_DEBUGF(3, "bandNums[%d] = %d", j, bandNums[j]);
1578  j++;
1579  }
1580 
1581  if (skip || j < 1) {
1582  pfree(bandNums);
1583  skip = TRUE;
1584  }
1585  }
1586  while (0);
1587 
1588  if (!skip) {
1589  rast = rt_raster_from_band(raster, bandNums, j);
1590  pfree(bandNums);
1591  rt_raster_destroy(raster);
1592  PG_FREE_IF_COPY(pgraster, 0);
1593  if (!rast) {
1594  elog(ERROR, "RASTER_band: Could not create new raster");
1595  PG_RETURN_NULL();
1596  }
1597 
1598  pgrast = rt_raster_serialize(rast);
1599  rt_raster_destroy(rast);
1600 
1601  if (!pgrast)
1602  PG_RETURN_NULL();
1603 
1604  SET_VARSIZE(pgrast, pgrast->size);
1605  PG_RETURN_POINTER(pgrast);
1606  }
1607 
1608  PG_RETURN_POINTER(pgraster);
1609 }
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
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_from_band(rt_raster raster, uint32_t *bandNums, int count)
Construct a new rt_raster from an existing rt_raster and an array of band numbers.
Definition: rt_raster.c:1439
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
unsigned int uint32_t
Definition: uthash.h:78
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
#define TRUE
Definition: dbfopen.c:169
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
Here is the call graph for this function:
Here is the caller graph for this function: