PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ build_overview()

static int build_overview ( int  idx,
RTLOADERCFG config,
RASTERINFO info,
uint32_t  ovx,
STRINGBUFFER tileset,
STRINGBUFFER buffer 
)
static

Definition at line 1340 of file raster2pgsql.c.

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
1360 rt_raster rast = NULL;
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 /* working copy of geotransform matrix */
1371 memcpy(gtOv, info->gt, sizeof(double) * 6);
1372
1373 if (ovx >= config->overview_count) {
1374 rterror(_("build_overview: Invalid overview index: %d"), ovx);
1375 return 0;
1376 }
1377 factor = config->overview[ovx];
1378 ovtable = (const char *) config->overview_table[ovx];
1379
1380 /* factor must be within valid range */
1381 if (factor < MINOVFACTOR || factor > MAXOVFACTOR) {
1382 rterror(_("build_overview: Overview factor %d is not between %d and %d"), factor, MINOVFACTOR, 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 /* create VRT dataset */
1390 hdsOv = VRTCreate(dimOv[0], dimOv[1]);
1391 /*
1392 GDALSetDescription(hdsOv, "/tmp/ov.vrt");
1393 */
1394 GDALSetProjection(hdsOv, info->srs);
1395
1396 /* adjust scale */
1397 gtOv[1] *= factor;
1398 gtOv[5] *= factor;
1399
1400 GDALSetGeoTransform(hdsOv, gtOv);
1401
1402 /* add bands as simple sources */
1403 for (j = 0; j < info->nband_count; j++) {
1404 GDALAddBand(hdsOv, info->gdalbandtype[j], NULL);
1405 hbandOv = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsOv, j + 1);
1406
1407 if (info->hasnodata[j])
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 /* make sure VRT reflects all changes */
1421 VRTFlushCache(hdsOv);
1422
1423 /* decide on tile size */
1424 if (!config->tile_size[0])
1425 tile_size[0] = dimOv[0];
1426 else
1427 tile_size[0] = config->tile_size[0];
1428 if (!config->tile_size[1])
1429 tile_size[1] = dimOv[1];
1430 else
1431 tile_size[1] = config->tile_size[1];
1432
1433 /* number of tiles */
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 /* working copy of geotransform matrix */
1443 memcpy(gt, gtOv, sizeof(double) * 6);
1444
1445 /* tile overview */
1446 /* each tile is a VRT with constraints set for just the data required for the tile */
1447 for (ytile = 0; ytile < ntiles[1]; ytile++) {
1448
1449 /* edge y tile */
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 char fn[100];
1458 sprintf(fn, "/tmp/ovtile%d.vrt", (ytile * ntiles[0]) + xtile);
1459 */
1460
1461 /* edge x tile */
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 /* compute tile's upper-left corner */
1468 GDALApplyGeoTransform(
1469 gtOv,
1470 xtile * tile_size[0], ytile * tile_size[1],
1471 &(gt[0]), &(gt[3])
1472 );
1473
1474 /* create VRT dataset */
1475 hdsDst = VRTCreate(_tile_size[0], _tile_size[1]);
1476 /*
1477 GDALSetDescription(hdsDst, fn);
1478 */
1479 GDALSetProjection(hdsDst, info->srs);
1480 GDALSetGeoTransform(hdsDst, gt);
1481
1482 /* add bands as simple sources */
1483 for (j = 0; j < info->nband_count; j++) {
1484 GDALAddBand(hdsDst, info->gdalbandtype[j], NULL);
1485 hbandDst = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsDst, j + 1);
1486
1487 if (info->hasnodata[j])
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 /* make sure VRT reflects all changes */
1501 VRTFlushCache(hdsDst);
1502
1503 /* convert VRT dataset to rt_raster */
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 /* set srid if provided */
1512 rt_raster_set_srid(rast, info->srid);
1513
1514 /* convert rt_raster to hexwkb */
1515 hex = rt_raster_to_hexwkb(rast, FALSE, &hexlen);
1516 raster_destroy(rast);
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 /* add hexwkb to tileset */
1525 append_stringbuffer(tileset, hex);
1526
1527 GDALClose(hdsDst);
1528
1529 /* flush if tileset gets too big */
1530 if (tileset->length >= config->max_tiles_per_copy) {
1531 if (!insert_records(
1532 config->schema, ovtable, config->raster_column,
1533 (config->file_column ? config->rt_filename[idx] : NULL), config->file_column_name,
1534 config->copy_statements, config->out_srid,
1535 tileset, buffer
1536 )) {
1537 rterror(_("build_overview: Could not convert raster tiles into INSERT or COPY statements"));
1538 GDALClose(hdsSrc);
1539 return 0;
1540 }
1541
1542 rtdealloc_stringbuffer(tileset, 0);
1543 }
1544 }
1545 }
1546
1547 GDALClose(hdsOv);
1548 GDALClose(hdsSrc);
1549 return 1;
1550}
#define FALSE
Definition dbfopen.c:72
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.
Definition rt_raster.c:2175
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
Definition rt_wkb.c:693
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition rt_raster.c:367
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)
#define MAXOVFACTOR
#define MINOVFACTOR
#define _(String)
Definition shpcommon.h:24
uint32_t max_tiles_per_copy
max tiles per copy
double * nodataval
double gt[6]
uint32_t nband_count
GDALDataType * gdalbandtype
uint32_t dim[2]

References _, append_stringbuffer(), buffer(), raster_loader_config::copy_statements, rasterinfo_t::dim, FALSE, raster_loader_config::file_column, raster_loader_config::file_column_name, rasterinfo_t::gdalbandtype, rasterinfo_t::gt, rasterinfo_t::hasnodata, insert_records(), stringbuffer_t::length, raster_loader_config::max_tiles_per_copy, MAXOVFACTOR, MINOVFACTOR, rasterinfo_t::nband, rasterinfo_t::nband_count, rasterinfo_t::nodataval, raster_loader_config::out_srid, raster_loader_config::overview, raster_loader_config::overview_count, raster_loader_config::overview_table, raster_loader_config::pad_tile, raster_loader_config::raster_column, raster_destroy(), raster_loader_config::rt_file, raster_loader_config::rt_filename, rt_raster_from_gdal_dataset(), rt_raster_set_srid(), rt_raster_to_hexwkb(), rtdealloc_stringbuffer(), rterror(), raster_loader_config::schema, rasterinfo_t::srid, rasterinfo_t::srs, and raster_loader_config::tile_size.

Referenced by process_rasters().

Here is the call graph for this function:
Here is the caller graph for this function: