555 int appendband =
FALSE;
556 char *outdbfile = NULL;
557 int *srcnband = NULL;
559 int allbands =
FALSE;
560 int hasnodata =
FALSE;
561 double nodataval = 0.;
564 char *authname = NULL;
565 char *authcode = NULL;
571 GDALRasterBandH hbandOut;
572 GDALDataType gdpixtype;
576 double ogt[6] = {0.};
582 if (!PG_ARGISNULL(0)) {
583 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
588 PG_FREE_IF_COPY(pgraster, 0);
589 elog(ERROR,
"RASTER_addBandOutDB: Could not deserialize destination raster");
597 if (!PG_ARGISNULL(1))
598 dstnband = PG_GETARG_INT32(1);
603 if (PG_ARGISNULL(2)) {
604 elog(NOTICE,
"Out-db raster file not provided. Returning original raster");
605 if (pgraster != NULL) {
607 PG_RETURN_POINTER(pgraster);
613 outdbfile = text_to_cstring(PG_GETARG_TEXT_P(2));
614 if (!strlen(outdbfile)) {
615 elog(NOTICE,
"Out-db raster file not provided. Returning original raster");
616 if (pgraster != NULL) {
618 PG_RETURN_POINTER(pgraster);
626 if (!PG_ARGISNULL(3)) {
638 array = PG_GETARG_ARRAYTYPE_P(3);
639 etype = ARR_ELEMTYPE(array);
640 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
647 if (pgraster != NULL) {
649 PG_FREE_IF_COPY(pgraster, 0);
651 elog(ERROR,
"RASTER_addBandOutDB: Invalid data type for band indexes");
656 deconstruct_array(array, etype, typlen, typbyval, typalign, &e, &nulls, &numsrcnband);
658 srcnband = palloc(
sizeof(
int) * numsrcnband);
659 if (srcnband == NULL) {
660 if (pgraster != NULL) {
662 PG_FREE_IF_COPY(pgraster, 0);
664 elog(ERROR,
"RASTER_addBandOutDB: Could not allocate memory for band indexes");
668 for (i = 0, j = 0; i < numsrcnband; i++) {
669 if (nulls[i])
continue;
673 srcnband[j] = DatumGetInt16(e[i]);
676 srcnband[j] = DatumGetInt32(e[i]);
682 if (j < numsrcnband) {
683 srcnband = repalloc(srcnband,
sizeof(
int) * j);
684 if (srcnband == NULL) {
685 if (pgraster != NULL) {
687 PG_FREE_IF_COPY(pgraster, 0);
689 elog(ERROR,
"RASTER_addBandOutDB: Could not reallocate memory for band indexes");
700 if (!PG_ARGISNULL(4)) {
702 nodataval = PG_GETARG_FLOAT8(4);
710 if (raster != NULL) {
714 elog(NOTICE,
"Invalid band index %d for adding bands. Using band index 1", dstnband);
717 else if (numbands > 0 && dstnband > numbands) {
718 elog(NOTICE,
"Invalid band index %d for adding bands. Using band index %d", dstnband, numbands);
719 dstnband = numbands + 1;
723 dstnband = numbands + 1;
729 if (hdsOut == NULL) {
730 if (pgraster != NULL) {
732 PG_FREE_IF_COPY(pgraster, 0);
734 elog(ERROR,
"RASTER_addBandOutDB: Could not open out-db file with GDAL");
739 if (GDALGetGeoTransform(hdsOut, ogt) != CE_None) {
749 if (raster == NULL) {
750 raster =
rt_raster_new(GDALGetRasterXSize(hdsOut), GDALGetRasterYSize(hdsOut));
752 elog(ERROR,
"RASTER_addBandOutDB: Could not create new raster");
761 strcmp(authname,
"EPSG") == 0 &&
767 elog(INFO,
"Unknown SRS auth name and code from out-db file. Defaulting SRID of new raster to %d",
SRID_UNKNOWN);
770 elog(INFO,
"Could not get SRS auth name and code from out-db file. Defaulting SRID of new raster to %d",
SRID_UNKNOWN);
788 if (pgraster != NULL)
789 PG_FREE_IF_COPY(pgraster, 0);
790 elog(ERROR,
"RASTER_addBandOutDB: Could not test alignment of out-db file");
794 elog(WARNING,
"The in-db representation of the out-db raster is not aligned. Band data may be incorrect");
796 numbands = GDALGetRasterCount(hdsOut);
800 numsrcnband = numbands;
801 srcnband = palloc(
sizeof(
int) * numsrcnband);
802 if (srcnband == NULL) {
806 if (pgraster != NULL)
807 PG_FREE_IF_COPY(pgraster, 0);
808 elog(ERROR,
"RASTER_addBandOutDB: Could not allocate memory for band indexes");
812 for (i = 0, j = 1; i < numsrcnband; i++, j++)
817 for (i = 0, j = dstnband - 1; i < numsrcnband; i++, j++) {
819 if (srcnband[i] < 1 || srcnband[i] > numbands) {
820 elog(NOTICE,
"Out-db file does not have a band at index %d. Returning original raster", srcnband[i]);
824 if (pgraster != NULL)
825 PG_RETURN_POINTER(pgraster);
832 hbandOut = GDALGetRasterBand(hdsOut, srcnband[i]);
833 if (NULL == hbandOut) {
837 if (pgraster != NULL)
838 PG_FREE_IF_COPY(pgraster, 0);
839 elog(ERROR,
"RASTER_addBandOutDB: Could not get band %d from GDAL dataset", srcnband[i]);
844 gdpixtype = GDALGetRasterDataType(hbandOut);
847 elog(NOTICE,
"Pixel type %s of band %d from GDAL dataset is not supported. Returning original raster", GDALGetDataTypeName(gdpixtype), srcnband[i]);
851 if (pgraster != NULL)
852 PG_RETURN_POINTER(pgraster);
859 nodataval = GDALGetRasterNoDataValue(hbandOut, &hasnodata);
865 hasnodata, nodataval,
866 srcnband[i] - 1, outdbfile
872 if (pgraster != NULL)
873 PG_FREE_IF_COPY(pgraster, 0);
874 elog(ERROR,
"RASTER_addBandOutDB: Could not create new out-db band");
882 if (pgraster != NULL)
883 PG_FREE_IF_COPY(pgraster, 0);
884 elog(ERROR,
"RASTER_addBandOutDB: Could not add new out-db band to raster");
891 if (pgraster != NULL)
892 PG_FREE_IF_COPY(pgraster, 0);
896 SET_VARSIZE(pgrtn, pgrtn->
size);
897 PG_RETURN_POINTER(pgrtn);
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
int rt_util_gdal_register_all(int force_register_all)
int rt_raster_get_num_bands(rt_raster raster)
rt_errorstate rt_raster_same_alignment(rt_raster rast1, rt_raster rast2, int *aligned, char **reason)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
#define SRID_UNKNOWN
Unknown SRID value.
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
uint16_t rt_raster_get_width(rt_raster raster)
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
uint16_t rt_raster_get_height(rt_raster raster)
#define POSTGIS_RT_DEBUG(level, msg)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
rt_band rt_band_new_offline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t bandNum, const char *path)
Create an out-db rt_band.