1340 {
1341 GDALDatasetH hdsSrc;
1342 VRTDatasetH hdsOv;
1343 VRTSourcedRasterBandH hbandOv;
1344 double gtOv[6] = {0.};
1345 int dimOv[2] = {0};
1346
1347 uint32_t j = 0;
1348 int factor;
1349 const char *ovtable = NULL;
1350
1351 VRTDatasetH hdsDst;
1352 VRTSourcedRasterBandH hbandDst;
1353 int tile_size[2] = {0};
1354 int _tile_size[2] = {0};
1355 int ntiles[2] = {1, 1};
1356 int xtile = 0;
1357 int ytile = 0;
1358 double gt[6] = {0.};
1359
1361 char *hex;
1362 uint32_t hexlen = 0;
1363
1364 hdsSrc = GDALOpenShared(config->
rt_file[idx], GA_ReadOnly);
1365 if (hdsSrc == NULL) {
1366 rterror(
_(
"build_overview: Could not open raster: %s"), config->
rt_file[idx]);
1367 return 0;
1368 }
1369
1370
1371 memcpy(gtOv, info->
gt,
sizeof(
double) * 6);
1372
1374 rterror(
_(
"build_overview: Invalid overview index: %d"), ovx);
1375 return 0;
1376 }
1379
1380
1381 if (factor < MINOVFACTOR || factor >
MAXOVFACTOR) {
1383 return 0;
1384 }
1385
1386 dimOv[0] = (int) (info->
dim[0] + (factor / 2)) / factor;
1387 dimOv[1] = (int) (info->
dim[1] + (factor / 2)) / factor;
1388
1389
1390 hdsOv = VRTCreate(dimOv[0], dimOv[1]);
1391
1392
1393
1394 GDALSetProjection(hdsOv, info->
srs);
1395
1396
1397 gtOv[1] *= factor;
1398 gtOv[5] *= factor;
1399
1400 GDALSetGeoTransform(hdsOv, gtOv);
1401
1402
1405 hbandOv = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsOv, j + 1);
1406
1408 GDALSetRasterNoDataValue(hbandOv, info->
nodataval[j]);
1409
1410 VRTAddSimpleSource(
1411 hbandOv, GDALGetRasterBand(hdsSrc, info->
nband[j]),
1412 0, 0,
1413 info->
dim[0], info->
dim[1],
1414 0, 0,
1415 dimOv[0], dimOv[1],
1416 "near", VRT_NODATA_UNSET
1417 );
1418 }
1419
1420
1421 VRTFlushCache(hdsOv);
1422
1423
1425 tile_size[0] = dimOv[0];
1426 else
1429 tile_size[1] = dimOv[1];
1430 else
1432
1433
1434 if (
1435 tile_size[0] != dimOv[0] &&
1436 tile_size[1] != dimOv[1]
1437 ) {
1438 ntiles[0] = (dimOv[0] + tile_size[0] - 1) / tile_size[0];
1439 ntiles[1] = (dimOv[1] + tile_size[1] - 1) / tile_size[1];
1440 }
1441
1442
1443 memcpy(gt, gtOv, sizeof(double) * 6);
1444
1445
1446
1447 for (ytile = 0; ytile < ntiles[1]; ytile++) {
1448
1449
1450 if (!config->
pad_tile && ntiles[1] > 1 && (ytile + 1) == ntiles[1])
1451 _tile_size[1] = dimOv[1] - (ytile * tile_size[1]);
1452 else
1453 _tile_size[1] = tile_size[1];
1454
1455 for (xtile = 0; xtile < ntiles[0]; xtile++) {
1456
1457
1458
1459
1460
1461
1462 if (!config->
pad_tile && ntiles[0] > 1 && (xtile + 1) == ntiles[0])
1463 _tile_size[0] = dimOv[0] - (xtile * tile_size[0]);
1464 else
1465 _tile_size[0] = tile_size[0];
1466
1467
1468 GDALApplyGeoTransform(
1469 gtOv,
1470 xtile * tile_size[0], ytile * tile_size[1],
1471 &(gt[0]), &(gt[3])
1472 );
1473
1474
1475 hdsDst = VRTCreate(_tile_size[0], _tile_size[1]);
1476
1477
1478
1479 GDALSetProjection(hdsDst, info->
srs);
1480 GDALSetGeoTransform(hdsDst, gt);
1481
1482
1485 hbandDst = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsDst, j + 1);
1486
1488 GDALSetRasterNoDataValue(hbandDst, info->
nodataval[j]);
1489
1490 VRTAddSimpleSource(
1491 hbandDst, GDALGetRasterBand(hdsOv, j + 1),
1492 xtile * tile_size[0], ytile * tile_size[1],
1493 _tile_size[0], _tile_size[1],
1494 0, 0,
1495 _tile_size[0], _tile_size[1],
1496 "near", VRT_NODATA_UNSET
1497 );
1498 }
1499
1500
1501 VRTFlushCache(hdsDst);
1502
1503
1505 if (rast == NULL) {
1506 rterror(
_(
"build_overview: Could not convert VRT dataset to PostGIS raster"));
1507 GDALClose(hdsDst);
1508 return 0;
1509 }
1510
1511
1513
1514
1517
1518 if (hex == NULL) {
1519 rterror(
_(
"build_overview: Could not convert PostGIS raster to hex WKB"));
1520 GDALClose(hdsDst);
1521 return 0;
1522 }
1523
1524
1526
1527 GDALClose(hdsDst);
1528
1529
1534 config->copy_statements, config->out_srid,
1536 )) {
1537 rterror(
_(
"build_overview: Could not convert raster tiles into INSERT or COPY statements"));
1538 GDALClose(hdsSrc);
1539 return 0;
1540 }
1541
1543 }
1544 }
1545 }
1546
1547 GDALClose(hdsOv);
1548 GDALClose(hdsSrc);
1549 return 1;
1550}
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Datum buffer(PG_FUNCTION_ARGS)
static int append_stringbuffer(STRINGBUFFER *buffer, const char *str)
static void rtdealloc_stringbuffer(STRINGBUFFER *buffer, int freebuffer)
static int insert_records(const char *schema, const char *table, const char *column, const char *filename, const char *file_column_name, int copy_statements, int out_srid, STRINGBUFFER *tileset, STRINGBUFFER *buffer)
static void raster_destroy(rt_raster raster)
uint32_t max_tiles_per_copy
max tiles per copy
GDALDataType * gdalbandtype