986 FuncCallContext *funcctx;
1019 struct tile_arg_t *arg1 = NULL;
1020 struct tile_arg_t *arg2 = NULL;
1022 if (SRF_IS_FIRSTCALL()) {
1023 MemoryContext oldcontext;
1039 funcctx = SRF_FIRSTCALL_INIT();
1042 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1045 if (PG_ARGISNULL(0)) {
1046 MemoryContextSwitchTo(oldcontext);
1047 SRF_RETURN_DONE(funcctx);
1051 arg1 = palloc(
sizeof(
struct tile_arg_t));
1053 MemoryContextSwitchTo(oldcontext);
1054 elog(ERROR,
"RASTER_tile: Could not allocate memory for arguments");
1055 SRF_RETURN_DONE(funcctx);
1058 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
1060 if (!arg1->raster.raster) {
1062 errcode(ERRCODE_OUT_OF_MEMORY),
1063 errmsg(
"Could not deserialize raster")
1066 PG_FREE_IF_COPY(pgraster, 0);
1067 MemoryContextSwitchTo(oldcontext);
1068 SRF_RETURN_DONE(funcctx);
1085 if (PG_ARGISNULL(1)) {
1086 elog(NOTICE,
"Width cannot be NULL. Returning NULL");
1089 PG_FREE_IF_COPY(pgraster, 0);
1090 MemoryContextSwitchTo(oldcontext);
1091 SRF_RETURN_DONE(funcctx);
1093 arg1->tile.width = PG_GETARG_INT32(1);
1094 if (arg1->tile.width < 1) {
1095 elog(NOTICE,
"Width must be greater than zero. Returning NULL");
1098 PG_FREE_IF_COPY(pgraster, 0);
1099 MemoryContextSwitchTo(oldcontext);
1100 SRF_RETURN_DONE(funcctx);
1104 if (PG_ARGISNULL(2)) {
1105 elog(NOTICE,
"Height cannot be NULL. Returning NULL");
1108 PG_FREE_IF_COPY(pgraster, 0);
1109 MemoryContextSwitchTo(oldcontext);
1110 SRF_RETURN_DONE(funcctx);
1112 arg1->tile.height = PG_GETARG_INT32(2);
1113 if (arg1->tile.height < 1) {
1114 elog(NOTICE,
"Height must be greater than zero. Returning NULL");
1117 PG_FREE_IF_COPY(pgraster, 0);
1118 MemoryContextSwitchTo(oldcontext);
1119 SRF_RETURN_DONE(funcctx);
1123 if (numbands && !PG_ARGISNULL(3)) {
1124 array = PG_GETARG_ARRAYTYPE_P(3);
1125 etype = ARR_ELEMTYPE(array);
1126 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1135 PG_FREE_IF_COPY(pgraster, 0);
1136 MemoryContextSwitchTo(oldcontext);
1137 elog(ERROR,
"RASTER_tile: Invalid data type for band indexes");
1138 SRF_RETURN_DONE(funcctx);
1142 deconstruct_array(array, etype, typlen, typbyval, typalign, &e, &nulls, &(arg1->numbands));
1144 arg1->nbands = palloc(
sizeof(
int) * arg1->numbands);
1145 if (arg1->nbands == NULL) {
1148 PG_FREE_IF_COPY(pgraster, 0);
1149 MemoryContextSwitchTo(oldcontext);
1150 elog(ERROR,
"RASTER_tile: Could not allocate memory for band indexes");
1151 SRF_RETURN_DONE(funcctx);
1154 for (i = 0, j = 0; i < arg1->numbands; i++) {
1155 if (nulls[i])
continue;
1159 arg1->nbands[j] = DatumGetInt16(e[i]) - 1;
1162 arg1->nbands[j] = DatumGetInt32(e[i]) - 1;
1169 if (j < arg1->numbands) {
1170 arg1->nbands = repalloc(arg1->nbands,
sizeof(
int) * j);
1171 if (arg1->nbands == NULL) {
1174 PG_FREE_IF_COPY(pgraster, 0);
1175 MemoryContextSwitchTo(oldcontext);
1176 elog(ERROR,
"RASTER_tile: Could not reallocate memory for band indexes");
1177 SRF_RETURN_DONE(funcctx);
1184 for (i = 0; i < arg1->numbands; i++) {
1186 elog(NOTICE,
"Band at index %d not found in raster", arg1->nbands[i] + 1);
1188 pfree(arg1->nbands);
1190 PG_FREE_IF_COPY(pgraster, 0);
1191 MemoryContextSwitchTo(oldcontext);
1192 SRF_RETURN_DONE(funcctx);
1197 arg1->numbands = numbands;
1200 arg1->nbands = palloc(
sizeof(
int) * arg1->numbands);
1202 if (arg1->nbands == NULL) {
1205 PG_FREE_IF_COPY(pgraster, 0);
1206 MemoryContextSwitchTo(oldcontext);
1207 elog(ERROR,
"RASTER_dumpValues: Could not allocate memory for pixel values");
1208 SRF_RETURN_DONE(funcctx);
1211 for (i = 0; i < arg1->numbands; i++) {
1212 arg1->nbands[i] = i;
1219 if (!PG_ARGISNULL(4)) {
1220 arg1->pad.pad = PG_GETARG_BOOL(4) ? 1 : 0;
1222 if (arg1->pad.pad && !PG_ARGISNULL(5)) {
1223 arg1->pad.hasnodata = 1;
1224 arg1->pad.nodataval = PG_GETARG_FLOAT8(5);
1227 arg1->pad.hasnodata = 0;
1228 arg1->pad.nodataval = 0;
1233 arg1->pad.hasnodata = 0;
1234 arg1->pad.nodataval = 0;
1244 arg1->tile.nx = ceil(arg1->raster.width / (
double) arg1->tile.width);
1245 arg1->tile.ny = ceil(arg1->raster.height / (
double) arg1->tile.height);
1246 POSTGIS_RT_DEBUGF(4,
"# of tiles (x, y) = (%d, %d)", arg1->tile.nx, arg1->tile.ny);
1249 funcctx->user_fctx = arg1;
1252 funcctx->max_calls = (arg1->tile.nx * arg1->tile.ny);
1254 MemoryContextSwitchTo(oldcontext);
1258 funcctx = SRF_PERCALL_SETUP();
1260 call_cntr = funcctx->call_cntr;
1261 max_calls = funcctx->max_calls;
1262 arg2 = funcctx->user_fctx;
1265 if (call_cntr < max_calls) {
1272 double nodataval = 0;
1298 ty = call_cntr / arg2->tile.nx;
1299 tx = call_cntr % arg2->tile.nx;
1303 if (!arg2->pad.pad) {
1304 if (ty + 1 == arg2->tile.ny)
1306 if (tx + 1 == arg2->tile.nx)
1311 rx = tx * arg2->tile.width;
1312 ry = ty * arg2->tile.height;
1317 width = arg2->tile.width;
1318 height = arg2->tile.height;
1324 width = arg2->raster.width - rx;
1327 height = arg2->raster.height - ry;
1339 if (arg2->numbands) pfree(arg2->nbands);
1341 elog(ERROR,
"RASTER_tile: Could not compute the coordinates of the upper-left corner of the output tile");
1342 SRF_RETURN_DONE(funcctx);
1348 len = arg2->tile.width;
1349 if (rx + arg2->tile.width >= arg2->raster.width)
1350 len = arg2->raster.width - rx;
1354 for (i = 0; i < arg2->numbands; i++) {
1358 if (_band == NULL) {
1359 int nband = arg2->nbands[i] + 1;
1362 if (arg2->numbands) pfree(arg2->nbands);
1364 elog(ERROR,
"RASTER_tile: Could not get band %d from source raster", nband);
1365 SRF_RETURN_DONE(funcctx);
1372 else if (arg2->pad.pad && arg2->pad.hasnodata) {
1374 nodataval = arg2->pad.nodataval;
1384 pfree(arg2->nbands);
1386 elog(ERROR,
"RASTER_tile: Could not add new band to output tile");
1387 SRF_RETURN_DONE(funcctx);
1393 if (arg2->numbands) pfree(arg2->nbands);
1395 elog(ERROR,
"RASTER_tile: Could not get newly added band from output tile");
1396 SRF_RETURN_DONE(funcctx);
1406 for (j = 0; j < arg2->tile.height; j++) {
1409 if (k >= arg2->raster.height) {
1418 if (arg2->numbands) pfree(arg2->nbands);
1420 elog(ERROR,
"RASTER_tile: Could not get pixel line from source raster");
1421 SRF_RETURN_DONE(funcctx);
1427 if (arg2->numbands) pfree(arg2->nbands);
1429 elog(ERROR,
"RASTER_tile: Could not set pixel line of output tile");
1430 SRF_RETURN_DONE(funcctx);
1442 hasnodata, nodataval,
1449 if (arg2->numbands) pfree(arg2->nbands);
1451 elog(ERROR,
"RASTER_tile: Could not create new offline band for output tile");
1452 SRF_RETURN_DONE(funcctx);
1459 if (arg2->numbands) pfree(arg2->nbands);
1461 elog(ERROR,
"RASTER_tile: Could not add new offline band to output tile");
1462 SRF_RETURN_DONE(funcctx);
1471 if (arg2->numbands) pfree(arg2->nbands);
1473 SRF_RETURN_DONE(funcctx);
1476 SET_VARSIZE(pgtile, pgtile->
size);
1477 SRF_RETURN_NEXT(funcctx, PointerGetDatum(pgtile));
1482 if (arg2->numbands) pfree(arg2->nbands);
1484 SRF_RETURN_DONE(funcctx);
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
int rt_raster_get_num_bands(rt_raster raster)
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_band_is_offline(rt_band band)
Return non-zero if the given band data is on the filesystem.
void rt_band_destroy(rt_band band)
Destroy a raster band.
#define POSTGIS_RT_DEBUGF(level, msg,...)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
const char * rt_band_get_ext_path(rt_band band)
Return band's external path (only valid when rt_band_is_offline returns non-zero).
rt_errorstate rt_raster_cell_to_geopoint(rt_raster raster, double xr, double yr, double *xw, double *yw, double *gt)
Convert an xr, yr raster point to an xw, yw point on map.
int rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype, double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
Generate a new inline band and add it to a raster.
rt_errorstate rt_band_get_pixel_line(rt_band band, int x, int y, uint16_t len, void **vals, uint16_t *nvals)
Get values of multiple pixels.
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
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.
rt_errorstate rt_band_get_ext_band_num(rt_band band, uint8_t *bandnum)
Return bands' external band number (only valid when rt_band_is_offline returns non-zero).
uint16_t rt_raster_get_width(rt_raster raster)
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
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.
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.