345{
349
352
353 int srcnband = 1;
354 bool appendband =
FALSE;
355 int dstnband = 1;
356 int srcnumbands = 0;
357 int dstnumbands = 0;
358
359 ArrayType *array;
360 Oid etype;
361 Datum *e;
362 bool *nulls;
363 int16 typlen;
364 bool typbyval;
365 char typalign;
366 int n = 0;
367
368 int rtn = 0;
369 int i = 0;
370
371
372 if (!PG_ARGISNULL(0)) {
373 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
374
375
377 if (!raster) {
378 PG_FREE_IF_COPY(pgraster, 0);
379 elog(ERROR, "RASTER_addBandRasterArray: Could not deserialize destination raster");
380 PG_RETURN_NULL();
381 }
382
384 }
385
386 if (PG_ARGISNULL(1))
387 {
388 if (raster != NULL)
389 {
391 PG_RETURN_POINTER(pgraster);
392 }
393 else
394 PG_RETURN_NULL();
395 }
396
397
398 if (!PG_ARGISNULL(2))
399 srcnband = PG_GETARG_INT32(2);
400 if (srcnband < 1) {
401 elog(NOTICE, "Invalid band index for source rasters (must be 1-based). Returning original raster");
402 if (raster != NULL) {
404 PG_RETURN_POINTER(pgraster);
405 }
406 else
407 PG_RETURN_NULL();
408 }
410
411
412 if (!PG_ARGISNULL(3)) {
413 dstnband = PG_GETARG_INT32(3);
415
416 if (dstnband < 1) {
417 elog(NOTICE, "Invalid band index for destination raster (must be 1-based). Returning original raster");
418 if (raster != NULL) {
420 PG_RETURN_POINTER(pgraster);
421 }
422 else
423 PG_RETURN_NULL();
424 }
425 }
426 else
428
429
430 if (raster != NULL) {
432
433 if (dstnumbands < 1) {
435 dstnband = 1;
436 }
437 else if (appendband)
438 dstnband = dstnumbands + 1;
439 else if (dstnband > dstnumbands) {
440 elog(NOTICE, "Band index provided for destination raster is greater than the number of bands in the raster. Bands will be appended");
442 dstnband = dstnumbands + 1;
443 }
444 }
447
448
450 array = PG_GETARG_ARRAYTYPE_P(1);
451 etype = ARR_ELEMTYPE(array);
452 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
453
454 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
455 &nulls, &n);
456
457
458 srcnband--;
459 dstnband--;
461
462
463 for (i = 0; i < n; i++) {
464 if (nulls[i]) continue;
465 src = NULL;
466
469 if (src == NULL) {
470 pfree(nulls);
471 pfree(e);
472 if (raster != NULL)
474 if (pgraster != NULL)
475 PG_FREE_IF_COPY(pgraster, 0);
476 elog(ERROR, "RASTER_addBandRasterArray: Could not deserialize source raster at index %d", i + 1);
477 PG_RETURN_NULL();
478 }
479
482
483
484 if (srcnband > srcnumbands - 1) {
485 elog(NOTICE, "Invalid band index for source raster at index %d. Returning original raster", i + 1);
486 pfree(nulls);
487 pfree(e);
489 if (raster != NULL) {
491 PG_RETURN_POINTER(pgraster);
492 }
493 else
494 PG_RETURN_NULL();
495 }
496
497
498 if (raster == NULL) {
499 uint32_t srcnbands[1] = {srcnband};
500
502
505 if (raster == NULL) {
506 pfree(nulls);
507 pfree(e);
508 if (pgraster != NULL)
509 PG_FREE_IF_COPY(pgraster, 0);
510 elog(ERROR, "RASTER_addBandRasterArray: Could not create raster from source raster at index %d", i + 1);
511 PG_RETURN_NULL();
512 }
513 }
514
515 else {
517 raster, src,
518 srcnband, dstnband
519 );
521
523 elog(NOTICE, "Could not add band from source raster at index %d to destination raster. Returning original raster", i + 1);
525 pfree(nulls);
526 pfree(e);
527 if (pgraster != NULL)
528 PG_RETURN_POINTER(pgraster);
529 else
530 PG_RETURN_NULL();
531 }
532 }
533
534 dstnband++;
535 dstnumbands++;
536 }
537
538 if (raster != NULL) {
541 if (pgraster != NULL)
542 PG_FREE_IF_COPY(pgraster, 0);
543 if (!pgrtn)
544 PG_RETURN_NULL();
545
546 SET_VARSIZE(pgrtn, pgrtn->
size);
547 PG_RETURN_POINTER(pgrtn);
548 }
549
550 PG_RETURN_NULL();
551}
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.
int rt_raster_copy_band(rt_raster torast, rt_raster fromrast, int fromindex, int toindex)
Copy one band from one raster to another.
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_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)