PostGIS  2.5.1dev-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  if (!skip) {
1486  numBands = rt_raster_get_num_bands(raster);
1487 
1488  array = PG_GETARG_ARRAYTYPE_P(1);
1489  etype = ARR_ELEMTYPE(array);
1490  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1491 
1492  switch (etype) {
1493  case INT2OID:
1494  case INT4OID:
1495  break;
1496  default:
1497  rt_raster_destroy(raster);
1498  PG_FREE_IF_COPY(pgraster, 0);
1499  elog(ERROR, "RASTER_band: Invalid data type for band number(s)");
1500  PG_RETURN_NULL();
1501  break;
1502  }
1503 
1504  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1505  &nulls, &n);
1506 
1507  bandNums = palloc(sizeof(uint32_t) * n);
1508  for (i = 0, j = 0; i < n; i++) {
1509  if (nulls[i]) continue;
1510 
1511  switch (etype) {
1512  case INT2OID:
1513  idx = (uint32_t) DatumGetInt16(e[i]);
1514  break;
1515  case INT4OID:
1516  idx = (uint32_t) DatumGetInt32(e[i]);
1517  break;
1518  }
1519 
1520  POSTGIS_RT_DEBUGF(3, "band idx (before): %d", idx);
1521  if (idx > numBands || idx < 1) {
1522  elog(NOTICE, "Invalid band index (must use 1-based). Returning original raster");
1523  skip = TRUE;
1524  break;
1525  }
1526 
1527  bandNums[j] = idx - 1;
1528  POSTGIS_RT_DEBUGF(3, "bandNums[%d] = %d", j, bandNums[j]);
1529  j++;
1530  }
1531 
1532  if (skip || j < 1) {
1533  pfree(bandNums);
1534  skip = TRUE;
1535  }
1536  }
1537 
1538  if (!skip) {
1539  rast = rt_raster_from_band(raster, bandNums, j);
1540  pfree(bandNums);
1541  rt_raster_destroy(raster);
1542  PG_FREE_IF_COPY(pgraster, 0);
1543  if (!rast) {
1544  elog(ERROR, "RASTER_band: Could not create new raster");
1545  PG_RETURN_NULL();
1546  }
1547 
1548  pgrast = rt_raster_serialize(rast);
1549  rt_raster_destroy(rast);
1550 
1551  if (!pgrast)
1552  PG_RETURN_NULL();
1553 
1554  SET_VARSIZE(pgrast, pgrast->size);
1555  PG_RETURN_POINTER(pgrast);
1556  }
1557 
1558  PG_RETURN_POINTER(pgraster);
1559 }
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:1435
#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:2250
#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:725
Here is the call graph for this function:
Here is the caller graph for this function: