PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ RASTER_band()

Datum RASTER_band ( PG_FUNCTION_ARGS  )

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

1447 {
1448  rt_pgraster *pgraster;
1449  rt_pgraster *pgrast;
1450  rt_raster raster;
1451  rt_raster rast;
1452 
1453  bool skip = FALSE;
1454  ArrayType *array;
1455  Oid etype;
1456  Datum *e;
1457  bool *nulls;
1458  int16 typlen;
1459  bool typbyval;
1460  char typalign;
1461 
1462  uint32_t numBands;
1463  uint32_t *bandNums;
1464  uint32 idx = 0;
1465  int n;
1466  int i = 0;
1467  int j = 0;
1468 
1469  if (PG_ARGISNULL(0))
1470  PG_RETURN_NULL();
1471  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1472 
1473  raster = rt_raster_deserialize(pgraster, FALSE);
1474  if (!raster) {
1475  PG_FREE_IF_COPY(pgraster, 0);
1476  elog(ERROR, "RASTER_band: Could not deserialize raster");
1477  PG_RETURN_NULL();
1478  }
1479 
1480  /* process bandNums */
1481  if (PG_ARGISNULL(1)) {
1482  elog(NOTICE, "Band number(s) not provided. Returning original raster");
1483  skip = TRUE;
1484  }
1485  do {
1486  if (skip) break;
1487 
1488  numBands = rt_raster_get_num_bands(raster);
1489 
1490  array = PG_GETARG_ARRAYTYPE_P(1);
1491  etype = ARR_ELEMTYPE(array);
1492  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1493 
1494  switch (etype) {
1495  case INT2OID:
1496  case INT4OID:
1497  break;
1498  default:
1499  rt_raster_destroy(raster);
1500  PG_FREE_IF_COPY(pgraster, 0);
1501  elog(ERROR, "RASTER_band: Invalid data type for band number(s)");
1502  PG_RETURN_NULL();
1503  break;
1504  }
1505 
1506  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1507  &nulls, &n);
1508 
1509  bandNums = palloc(sizeof(uint32_t) * n);
1510  for (i = 0, j = 0; i < n; i++) {
1511  if (nulls[i]) continue;
1512 
1513  switch (etype) {
1514  case INT2OID:
1515  idx = (uint32_t) DatumGetInt16(e[i]);
1516  break;
1517  case INT4OID:
1518  idx = (uint32_t) DatumGetInt32(e[i]);
1519  break;
1520  }
1521 
1522  POSTGIS_RT_DEBUGF(3, "band idx (before): %d", idx);
1523  if (idx > numBands || idx < 1) {
1524  elog(NOTICE, "Invalid band index (must use 1-based). Returning original raster");
1525  skip = TRUE;
1526  break;
1527  }
1528 
1529  bandNums[j] = idx - 1;
1530  POSTGIS_RT_DEBUGF(3, "bandNums[%d] = %d", j, bandNums[j]);
1531  j++;
1532  }
1533 
1534  if (skip || j < 1) {
1535  pfree(bandNums);
1536  skip = TRUE;
1537  }
1538  }
1539  while (0);
1540 
1541  if (!skip) {
1542  rast = rt_raster_from_band(raster, bandNums, j);
1543  pfree(bandNums);
1544  rt_raster_destroy(raster);
1545  PG_FREE_IF_COPY(pgraster, 0);
1546  if (!rast) {
1547  elog(ERROR, "RASTER_band: Could not create new raster");
1548  PG_RETURN_NULL();
1549  }
1550 
1551  pgrast = rt_raster_serialize(rast);
1552  rt_raster_destroy(rast);
1553 
1554  if (!pgrast)
1555  PG_RETURN_NULL();
1556 
1557  SET_VARSIZE(pgrast, pgrast->size);
1558  PG_RETURN_POINTER(pgrast);
1559  }
1560 
1561  PG_RETURN_POINTER(pgraster);
1562 }
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
uint16_t 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:2230
#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: