1458{
1463
1465 ArrayType *array;
1466 Oid etype;
1467 Datum *e;
1468 bool *nulls;
1469 int16 typlen;
1470 bool typbyval;
1471 char typalign;
1472
1473 uint32_t numBands;
1474 uint32_t *bandNums;
1475 uint32 idx = 0;
1476 int n;
1477 int i = 0;
1478 int j = 0;
1479
1480 if (PG_ARGISNULL(0))
1481 PG_RETURN_NULL();
1482 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1483
1485 if (!raster) {
1486 PG_FREE_IF_COPY(pgraster, 0);
1487 elog(ERROR, "RASTER_band: Could not deserialize raster");
1488 PG_RETURN_NULL();
1489 }
1490
1491
1492 if (PG_ARGISNULL(1)) {
1493 elog(NOTICE, "Band number(s) not provided. Returning original raster");
1495 }
1496 if (!skip) {
1498
1499 array = PG_GETARG_ARRAYTYPE_P(1);
1500 etype = ARR_ELEMTYPE(array);
1501 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1502
1503 switch (etype) {
1504 case INT2OID:
1505 case INT4OID:
1506 break;
1507 default:
1509 PG_FREE_IF_COPY(pgraster, 0);
1510 elog(ERROR, "RASTER_band: Invalid data type for band number(s)");
1511 PG_RETURN_NULL();
1512 break;
1513 }
1514
1515 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1516 &nulls, &n);
1517
1518 bandNums = palloc(sizeof(uint32_t) * n);
1519 for (i = 0, j = 0; i < n; i++) {
1520 if (nulls[i]) continue;
1521
1522 switch (etype) {
1523 case INT2OID:
1524 idx = (uint32_t) DatumGetInt16(e[i]);
1525 break;
1526 case INT4OID:
1527 idx = (uint32_t) DatumGetInt32(e[i]);
1528 break;
1529 }
1530
1532 if (idx > numBands || idx < 1) {
1533 elog(NOTICE, "Invalid band index (must use 1-based). Returning original raster");
1535 break;
1536 }
1537
1538 bandNums[j] = idx - 1;
1540 j++;
1541 }
1542
1543 if (skip || j < 1) {
1544 pfree(bandNums);
1546 }
1547 }
1548
1549 if (!skip) {
1551 pfree(bandNums);
1553 PG_FREE_IF_COPY(pgraster, 0);
1554 if (!rast) {
1555 elog(ERROR, "RASTER_band: Could not create new raster");
1556 PG_RETURN_NULL();
1557 }
1558
1561
1562 if (!pgrast)
1563 PG_RETURN_NULL();
1564
1565 SET_VARSIZE(pgrast, pgrast->
size);
1566 PG_RETURN_POINTER(pgrast);
1567 }
1568
1569 PG_RETURN_POINTER(pgraster);
1570}
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
uint16_t rt_raster_get_num_bands(rt_raster raster)
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.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
#define POSTGIS_RT_DEBUGF(level, msg,...)