PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ build_overview()

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

Definition at line 1350 of file raster2pgsql.c.

References _, append_stringbuffer(), raster_loader_config::copy_statements, rasterinfo_t::dim, FALSE, raster_loader_config::file_column, raster_loader_config::file_column_name, rasterinfo_t::gdalbandtype, window::gt, rasterinfo_t::gt, rasterinfo_t::hasnodata, if(), insert_records(), stringbuffer_t::length, 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, rtpixdump::rast, 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().

1350  {
1351  GDALDatasetH hdsSrc;
1352  VRTDatasetH hdsOv;
1353  VRTSourcedRasterBandH hbandOv;
1354  double gtOv[6] = {0.};
1355  int dimOv[2] = {0};
1356 
1357  int j = 0;
1358  int factor;
1359  const char *ovtable = NULL;
1360 
1361  VRTDatasetH hdsDst;
1362  VRTSourcedRasterBandH hbandDst;
1363  int tile_size[2] = {0};
1364  int _tile_size[2] = {0};
1365  int ntiles[2] = {1, 1};
1366  int xtile = 0;
1367  int ytile = 0;
1368  double gt[6] = {0.};
1369 
1370  rt_raster rast = NULL;
1371  char *hex;
1372  uint32_t hexlen = 0;
1373 
1374  hdsSrc = GDALOpenShared(config->rt_file[idx], GA_ReadOnly);
1375  if (hdsSrc == NULL) {
1376  rterror(_("build_overview: Could not open raster: %s"), config->rt_file[idx]);
1377  return 0;
1378  }
1379 
1380  /* working copy of geotransform matrix */
1381  memcpy(gtOv, info->gt, sizeof(double) * 6);
1382 
1383  if (ovx >= config->overview_count) {
1384  rterror(_("build_overview: Invalid overview index: %d"), ovx);
1385  return 0;
1386  }
1387  factor = config->overview[ovx];
1388  ovtable = (const char *) config->overview_table[ovx];
1389 
1390  /* factor must be within valid range */
1391  if (factor < MINOVFACTOR || factor > MAXOVFACTOR) {
1392  rterror(_("build_overview: Overview factor %d is not between %d and %d"), factor, MINOVFACTOR, MAXOVFACTOR);
1393  return 0;
1394  }
1395 
1396  dimOv[0] = (int) (info->dim[0] + (factor / 2)) / factor;
1397  dimOv[1] = (int) (info->dim[1] + (factor / 2)) / factor;
1398 
1399  /* create VRT dataset */
1400  hdsOv = VRTCreate(dimOv[0], dimOv[1]);
1401  /*
1402  GDALSetDescription(hdsOv, "/tmp/ov.vrt");
1403  */
1404  GDALSetProjection(hdsOv, info->srs);
1405 
1406  /* adjust scale */
1407  gtOv[1] *= factor;
1408  gtOv[5] *= factor;
1409 
1410  GDALSetGeoTransform(hdsOv, gtOv);
1411 
1412  /* add bands as simple sources */
1413  for (j = 0; j < info->nband_count; j++) {
1414  GDALAddBand(hdsOv, info->gdalbandtype[j], NULL);
1415  hbandOv = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsOv, j + 1);
1416 
1417  if (info->hasnodata[j])
1418  GDALSetRasterNoDataValue(hbandOv, info->nodataval[j]);
1419 
1420  VRTAddSimpleSource(
1421  hbandOv, GDALGetRasterBand(hdsSrc, info->nband[j]),
1422  0, 0,
1423  info->dim[0], info->dim[1],
1424  0, 0,
1425  dimOv[0], dimOv[1],
1426  "near", VRT_NODATA_UNSET
1427  );
1428  }
1429 
1430  /* make sure VRT reflects all changes */
1431  VRTFlushCache(hdsOv);
1432 
1433  /* decide on tile size */
1434  if (!config->tile_size[0])
1435  tile_size[0] = dimOv[0];
1436  else
1437  tile_size[0] = config->tile_size[0];
1438  if (!config->tile_size[1])
1439  tile_size[1] = dimOv[1];
1440  else
1441  tile_size[1] = config->tile_size[1];
1442 
1443  /* number of tiles */
1444  if (
1445  tile_size[0] != dimOv[0] &&
1446  tile_size[1] != dimOv[1]
1447  ) {
1448  ntiles[0] = (dimOv[0] + tile_size[0] - 1) / tile_size[0];
1449  ntiles[1] = (dimOv[1] + tile_size[1] - 1) / tile_size[1];
1450  }
1451 
1452  /* working copy of geotransform matrix */
1453  memcpy(gt, gtOv, sizeof(double) * 6);
1454 
1455  /* tile overview */
1456  /* each tile is a VRT with constraints set for just the data required for the tile */
1457  for (ytile = 0; ytile < ntiles[1]; ytile++) {
1458 
1459  /* edge y tile */
1460  if (!config->pad_tile && ntiles[1] > 1 && (ytile + 1) == ntiles[1])
1461  _tile_size[1] = dimOv[1] - (ytile * tile_size[1]);
1462  else
1463  _tile_size[1] = tile_size[1];
1464 
1465  for (xtile = 0; xtile < ntiles[0]; xtile++) {
1466  /*
1467  char fn[100];
1468  sprintf(fn, "/tmp/ovtile%d.vrt", (ytile * ntiles[0]) + xtile);
1469  */
1470 
1471  /* edge x tile */
1472  if (!config->pad_tile && ntiles[0] > 1 && (xtile + 1) == ntiles[0])
1473  _tile_size[0] = dimOv[0] - (xtile * tile_size[0]);
1474  else
1475  _tile_size[0] = tile_size[0];
1476 
1477  /* compute tile's upper-left corner */
1478  GDALApplyGeoTransform(
1479  gtOv,
1480  xtile * tile_size[0], ytile * tile_size[1],
1481  &(gt[0]), &(gt[3])
1482  );
1483 
1484  /* create VRT dataset */
1485  hdsDst = VRTCreate(_tile_size[0], _tile_size[1]);
1486  /*
1487  GDALSetDescription(hdsDst, fn);
1488  */
1489  GDALSetProjection(hdsDst, info->srs);
1490  GDALSetGeoTransform(hdsDst, gt);
1491 
1492  /* add bands as simple sources */
1493  for (j = 0; j < info->nband_count; j++) {
1494  GDALAddBand(hdsDst, info->gdalbandtype[j], NULL);
1495  hbandDst = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsDst, j + 1);
1496 
1497  if (info->hasnodata[j])
1498  GDALSetRasterNoDataValue(hbandDst, info->nodataval[j]);
1499 
1500  VRTAddSimpleSource(
1501  hbandDst, GDALGetRasterBand(hdsOv, j + 1),
1502  xtile * tile_size[0], ytile * tile_size[1],
1503  _tile_size[0], _tile_size[1],
1504  0, 0,
1505  _tile_size[0], _tile_size[1],
1506  "near", VRT_NODATA_UNSET
1507  );
1508  }
1509 
1510  /* make sure VRT reflects all changes */
1511  VRTFlushCache(hdsDst);
1512 
1513  /* convert VRT dataset to rt_raster */
1514  rast = rt_raster_from_gdal_dataset(hdsDst);
1515  if (rast == NULL) {
1516  rterror(_("build_overview: Could not convert VRT dataset to PostGIS raster"));
1517  GDALClose(hdsDst);
1518  return 0;
1519  }
1520 
1521  /* set srid if provided */
1522  rt_raster_set_srid(rast, info->srid);
1523 
1524  /* convert rt_raster to hexwkb */
1525  hex = rt_raster_to_hexwkb(rast, FALSE, &hexlen);
1526  raster_destroy(rast);
1527 
1528  if (hex == NULL) {
1529  rterror(_("build_overview: Could not convert PostGIS raster to hex WKB"));
1530  GDALClose(hdsDst);
1531  return 0;
1532  }
1533 
1534  /* add hexwkb to tileset */
1535  append_stringbuffer(tileset, hex);
1536 
1537  GDALClose(hdsDst);
1538 
1539  /* flush if tileset gets too big */
1540  if (tileset->length > 10) {
1541  if (!insert_records(
1542  config->schema, ovtable, config->raster_column,
1543  (config->file_column ? config->rt_filename[idx] : NULL), config->file_column_name,
1544  config->copy_statements, config->out_srid,
1545  tileset, buffer
1546  )) {
1547  rterror(_("build_overview: Could not convert raster tiles into INSERT or COPY statements"));
1548  GDALClose(hdsSrc);
1549  return 0;
1550  }
1551 
1552  rtdealloc_stringbuffer(tileset, 0);
1553  }
1554  }
1555  }
1556 
1557  GDALClose(hdsOv);
1558  GDALClose(hdsSrc);
1559  return 1;
1560 }
GDALDataType * gdalbandtype
Definition: raster2pgsql.h:179
static void rtdealloc_stringbuffer(STRINGBUFFER *buffer, int freebuffer)
Definition: raster2pgsql.c:774
#define _(String)
Definition: shpcommon.h:24
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
uint32_t length
Definition: raster2pgsql.h:196
static int append_stringbuffer(STRINGBUFFER *buffer, const char *str)
Definition: raster2pgsql.c:807
gt
Definition: window.py:77
unsigned int uint32_t
Definition: uthash.h:78
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)
Definition: raster2pgsql.c:877
#define MINOVFACTOR
Definition: raster2pgsql.h:58
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:669
uint32_t dim[2]
Definition: raster2pgsql.h:172
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster&#39;s SRID.
Definition: rt_raster.c:363
#define FALSE
Definition: dbfopen.c:168
static void raster_destroy(rt_raster raster)
Definition: raster2pgsql.c:76
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_raster.c:2165
double * nodataval
Definition: raster2pgsql.h:185
#define MAXOVFACTOR
Definition: raster2pgsql.h:59
double gt[6]
Definition: raster2pgsql.h:188
if(!(yy_init))
Here is the call graph for this function:
Here is the caller graph for this function: