949{
950 FuncCallContext *funcctx;
951 int call_cntr;
952 int max_calls;
953 int i = 0;
954 int j = 0;
955
956 struct tile_arg_t {
957
958 struct {
961 int32_t srid;
962 int width;
963 int height;
965
966 struct {
967 int width;
968 int height;
969
970 int nx;
971 int ny;
972 } tile;
973
974 int numbands;
975 int *nbands;
976
977 struct {
978 int pad;
979 double hasnodata;
980 double nodataval;
981 } pad;
982 };
983 struct tile_arg_t *arg1 = NULL;
984 struct tile_arg_t *arg2 = NULL;
985
986 if (SRF_IS_FIRSTCALL()) {
987 MemoryContext oldcontext;
989 int numbands;
990
991 ArrayType *array;
992 Oid etype;
993 Datum *e;
994 bool *nulls;
995
996 int16 typlen;
997 bool typbyval;
998 char typalign;
999
1001
1002
1003 funcctx = SRF_FIRSTCALL_INIT();
1004
1005
1006 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1007
1008
1009 if (PG_ARGISNULL(0)) {
1010 MemoryContextSwitchTo(oldcontext);
1011 SRF_RETURN_DONE(funcctx);
1012 }
1013
1014
1015 arg1 = palloc(sizeof(struct tile_arg_t));
1016 if (arg1 == NULL) {
1017 MemoryContextSwitchTo(oldcontext);
1018 elog(ERROR, "RASTER_tile: Could not allocate memory for arguments");
1019 SRF_RETURN_DONE(funcctx);
1020 }
1021
1022 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
1024 if (!arg1->raster.raster) {
1025 ereport(ERROR, (
1026 errcode(ERRCODE_OUT_OF_MEMORY),
1027 errmsg("Could not deserialize raster")
1028 ));
1029 pfree(arg1);
1030 PG_FREE_IF_COPY(pgraster, 0);
1031 MemoryContextSwitchTo(oldcontext);
1032 SRF_RETURN_DONE(funcctx);
1033 }
1034
1035
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049 if (PG_ARGISNULL(1)) {
1050 elog(NOTICE, "Width cannot be NULL. Returning NULL");
1052 pfree(arg1);
1053 PG_FREE_IF_COPY(pgraster, 0);
1054 MemoryContextSwitchTo(oldcontext);
1055 SRF_RETURN_DONE(funcctx);
1056 }
1057 arg1->tile.width = PG_GETARG_INT32(1);
1058 if (arg1->tile.width < 1) {
1059 elog(NOTICE, "Width must be greater than zero. Returning NULL");
1061 pfree(arg1);
1062 PG_FREE_IF_COPY(pgraster, 0);
1063 MemoryContextSwitchTo(oldcontext);
1064 SRF_RETURN_DONE(funcctx);
1065 }
1066
1067
1068 if (PG_ARGISNULL(2)) {
1069 elog(NOTICE, "Height cannot be NULL. Returning NULL");
1071 pfree(arg1);
1072 PG_FREE_IF_COPY(pgraster, 0);
1073 MemoryContextSwitchTo(oldcontext);
1074 SRF_RETURN_DONE(funcctx);
1075 }
1076 arg1->tile.height = PG_GETARG_INT32(2);
1077 if (arg1->tile.height < 1) {
1078 elog(NOTICE, "Height must be greater than zero. Returning NULL");
1080 pfree(arg1);
1081 PG_FREE_IF_COPY(pgraster, 0);
1082 MemoryContextSwitchTo(oldcontext);
1083 SRF_RETURN_DONE(funcctx);
1084 }
1085
1086
1087 if (numbands && !PG_ARGISNULL(3)) {
1088 array = PG_GETARG_ARRAYTYPE_P(3);
1089 etype = ARR_ELEMTYPE(array);
1090 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1091
1092 switch (etype) {
1093 case INT2OID:
1094 case INT4OID:
1095 break;
1096 default:
1098 pfree(arg1);
1099 PG_FREE_IF_COPY(pgraster, 0);
1100 MemoryContextSwitchTo(oldcontext);
1101 elog(ERROR, "RASTER_tile: Invalid data type for band indexes");
1102 SRF_RETURN_DONE(funcctx);
1103 break;
1104 }
1105
1106 deconstruct_array(array, etype, typlen, typbyval, typalign, &e, &nulls, &(arg1->numbands));
1107
1108 arg1->nbands = palloc(sizeof(int) * arg1->numbands);
1109 if (arg1->nbands == NULL) {
1111 pfree(arg1);
1112 PG_FREE_IF_COPY(pgraster, 0);
1113 MemoryContextSwitchTo(oldcontext);
1114 elog(ERROR, "RASTER_tile: Could not allocate memory for band indexes");
1115 SRF_RETURN_DONE(funcctx);
1116 }
1117
1118 for (i = 0, j = 0; i < arg1->numbands; i++) {
1119 if (nulls[i]) continue;
1120
1121 switch (etype) {
1122 case INT2OID:
1123 arg1->nbands[j] = DatumGetInt16(e[i]) - 1;
1124 break;
1125 case INT4OID:
1126 arg1->nbands[j] = DatumGetInt32(e[i]) - 1;
1127 break;
1128 }
1129
1130 j++;
1131 }
1132
1133 if (j < arg1->numbands) {
1134 arg1->nbands = repalloc(arg1->nbands, sizeof(int) * j);
1135 if (arg1->nbands == NULL) {
1137 pfree(arg1);
1138 PG_FREE_IF_COPY(pgraster, 0);
1139 MemoryContextSwitchTo(oldcontext);
1140 elog(ERROR, "RASTER_tile: Could not reallocate memory for band indexes");
1141 SRF_RETURN_DONE(funcctx);
1142 }
1143
1144 arg1->numbands = j;
1145 }
1146
1147
1148 for (i = 0; i < arg1->numbands; i++) {
1150 elog(NOTICE, "Band at index %d not found in raster", arg1->nbands[i] + 1);
1152 pfree(arg1->nbands);
1153 pfree(arg1);
1154 PG_FREE_IF_COPY(pgraster, 0);
1155 MemoryContextSwitchTo(oldcontext);
1156 SRF_RETURN_DONE(funcctx);
1157 }
1158 }
1159 }
1160 else {
1161 arg1->numbands = numbands;
1162
1163 if (numbands) {
1164 arg1->nbands = palloc(sizeof(int) * arg1->numbands);
1165
1166 if (arg1->nbands == NULL) {
1168 pfree(arg1);
1169 PG_FREE_IF_COPY(pgraster, 0);
1170 MemoryContextSwitchTo(oldcontext);
1171 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for pixel values");
1172 SRF_RETURN_DONE(funcctx);
1173 }
1174
1175 for (i = 0; i < arg1->numbands; i++) {
1176 arg1->nbands[i] = i;
1178 }
1179 }
1180 }
1181
1182
1183 if (!PG_ARGISNULL(4)) {
1184 arg1->pad.pad = PG_GETARG_BOOL(4) ? 1 : 0;
1185
1186 if (arg1->pad.pad && !PG_ARGISNULL(5)) {
1187 arg1->pad.hasnodata = 1;
1188 arg1->pad.nodataval = PG_GETARG_FLOAT8(5);
1189 }
1190 else {
1191 arg1->pad.hasnodata = 0;
1192 arg1->pad.nodataval = 0;
1193 }
1194 }
1195 else {
1196 arg1->pad.pad = 0;
1197 arg1->pad.hasnodata = 0;
1198 arg1->pad.nodataval = 0;
1199 }
1200
1201
1206
1207
1208 arg1->tile.nx = ceil(arg1->raster.width / (double) arg1->tile.width);
1209 arg1->tile.ny = ceil(arg1->raster.height / (double) arg1->tile.height);
1210 POSTGIS_RT_DEBUGF(4,
"# of tiles (x, y) = (%d, %d)", arg1->tile.nx, arg1->tile.ny);
1211
1212
1213 funcctx->user_fctx = arg1;
1214
1215
1216 funcctx->max_calls = (arg1->tile.nx * arg1->tile.ny);
1217
1218 MemoryContextSwitchTo(oldcontext);
1219 }
1220
1221
1222 funcctx = SRF_PERCALL_SETUP();
1223
1224 call_cntr = funcctx->call_cntr;
1225 max_calls = funcctx->max_calls;
1226 arg2 = funcctx->user_fctx;
1227
1228
1229 if (call_cntr < max_calls) {
1235 int hasnodata = 0;
1236 double nodataval = 0;
1237 int width = 0;
1238 int height = 0;
1239
1240 int k = 0;
1241 int tx = 0;
1242 int ty = 0;
1243 int rx = 0;
1244 int ry = 0;
1245 int ex = 0;
1246 int ey = 0;
1247 double ulx = 0;
1248 double uly = 0;
1249 uint16_t len = 0;
1250 void *vals = NULL;
1251 uint16_t nvals;
1252
1254
1255
1256
1257
1258
1259
1260
1261
1262 ty = call_cntr / arg2->tile.nx;
1263 tx = call_cntr % arg2->tile.nx;
1265
1266
1267 if (!arg2->pad.pad) {
1268 if (ty + 1 == arg2->tile.ny)
1269 ey = 1;
1270 if (tx + 1 == arg2->tile.nx)
1271 ex = 1;
1272 }
1273
1274
1275 rx = tx * arg2->tile.width;
1276 ry = ty * arg2->tile.height;
1278
1279
1280
1281 width = arg2->tile.width;
1282 height = arg2->tile.height;
1283
1284
1285 if (ex || ey) {
1286
1287 if (ex)
1288 width = arg2->raster.width - rx;
1289
1290 if (ey)
1291 height = arg2->raster.height - ry;
1292 }
1293
1294
1298
1299
1303 if (arg2->numbands) pfree(arg2->nbands);
1304 pfree(arg2);
1305 elog(ERROR, "RASTER_tile: Could not compute the coordinates of the upper-left corner of the output tile");
1306 SRF_RETURN_DONE(funcctx);
1307 }
1310
1311
1312 len = arg2->tile.width;
1313 if (rx + arg2->tile.width >= arg2->raster.width)
1314 len = arg2->raster.width - rx;
1316
1317
1318 for (i = 0; i < arg2->numbands; i++) {
1320
1322 if (_band == NULL) {
1323 int nband = arg2->nbands[i] + 1;
1326 pfree(arg2->nbands);
1327 pfree(arg2);
1328 elog(ERROR, "RASTER_tile: Could not get band %d from source raster", nband);
1329 SRF_RETURN_DONE(funcctx);
1330 }
1331
1334 if (hasnodata)
1336 else if (arg2->pad.pad && arg2->pad.hasnodata) {
1337 hasnodata = 1;
1338 nodataval = arg2->pad.nodataval;
1339 }
1340 else
1342
1343
1348 pfree(arg2->nbands);
1349 pfree(arg2);
1350 elog(ERROR, "RASTER_tile: Could not add new band to output tile");
1351 SRF_RETURN_DONE(funcctx);
1352 }
1354 if (band == NULL) {
1357 pfree(arg2->nbands);
1358 pfree(arg2);
1359 elog(ERROR, "RASTER_tile: Could not get newly added band from output tile");
1360 SRF_RETURN_DONE(funcctx);
1361 }
1362
1363
1366 continue;
1367 }
1368
1369
1370 for (j = 0; j < arg2->tile.height; j++) {
1371 k = ry + j;
1372
1373 if (k >= arg2->raster.height) {
1375 continue;
1376 }
1377
1382 pfree(arg2->nbands);
1383 pfree(arg2);
1384 elog(ERROR, "RASTER_tile: Could not get pixel line from source raster");
1385 SRF_RETURN_DONE(funcctx);
1386 }
1387
1391 pfree(arg2->nbands);
1392 pfree(arg2);
1393 elog(ERROR, "RASTER_tile: Could not set pixel line of output tile");
1394 SRF_RETURN_DONE(funcctx);
1395 }
1396 }
1397 }
1398
1399 else {
1400 uint8_t bandnum = 0;
1402
1404 width, height,
1405 pixtype,
1406 hasnodata, nodataval,
1408 );
1409
1410 if (band == NULL) {
1413 pfree(arg2->nbands);
1414 pfree(arg2);
1415 elog(ERROR, "RASTER_tile: Could not create new offline band for output tile");
1416 SRF_RETURN_DONE(funcctx);
1417 }
1418
1423 pfree(arg2->nbands);
1424 pfree(arg2);
1425 elog(ERROR, "RASTER_tile: Could not add new offline band to output tile");
1426 SRF_RETURN_DONE(funcctx);
1427 }
1428 }
1429 }
1430
1433 if (!pgtile) {
1435 if (arg2->numbands) pfree(arg2->nbands);
1436 pfree(arg2);
1437 SRF_RETURN_DONE(funcctx);
1438 }
1439
1440 SET_VARSIZE(pgtile, pgtile->
size);
1441 SRF_RETURN_NEXT(funcctx, PointerGetDatum(pgtile));
1442 }
1443
1444 else {
1446 if (arg2->numbands) pfree(arg2->nbands);
1447 pfree(arg2);
1448 SRF_RETURN_DONE(funcctx);
1449 }
1450}
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
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.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
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.
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
uint16_t rt_raster_get_height(rt_raster raster)
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.
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).
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
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).
uint16_t rt_raster_get_width(rt_raster raster)
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
int rt_band_is_offline(rt_band band)
Return non-zero if the given band data is on the filesystem.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
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.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
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,...)