PostGIS  2.1.10dev-r@@SVN_REVISION@@
static int build_overview ( int  idx,
RTLOADERCFG config,
RASTERINFO info,
int  ovx,
STRINGBUFFER tileset,
STRINGBUFFER buffer 
)
static

Definition at line 1332 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::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(), rtdealloc_stringbuffer(), rterror(), raster_loader_config::schema, rasterinfo_t::srid, rasterinfo_t::srs, and raster_loader_config::tile_size.

Referenced by process_rasters().

1332  {
1333  GDALDatasetH hdsSrc;
1334  VRTDatasetH hdsOv;
1335  VRTSourcedRasterBandH hbandOv;
1336  double gtOv[6] = {0.};
1337  int dimOv[2] = {0};
1338 
1339  int j = 0;
1340  int factor;
1341  const char *ovtable = NULL;
1342 
1343  VRTDatasetH hdsDst;
1344  VRTSourcedRasterBandH hbandDst;
1345  int tile_size[2] = {0};
1346  int _tile_size[2] = {0};
1347  int ntiles[2] = {1, 1};
1348  int xtile = 0;
1349  int ytile = 0;
1350  double gt[6] = {0.};
1351 
1352  rt_raster rast = NULL;
1353  char *hex;
1354  uint32_t hexlen = 0;
1355 
1356  hdsSrc = GDALOpenShared(config->rt_file[idx], GA_ReadOnly);
1357  if (hdsSrc == NULL) {
1358  rterror(_("build_overview: Could not open raster: %s"), config->rt_file[idx]);
1359  return 0;
1360  }
1361 
1362  /* working copy of geotransform matrix */
1363  memcpy(gtOv, info->gt, sizeof(double) * 6);
1364 
1365  if (ovx >= config->overview_count) {
1366  rterror(_("build_overview: Invalid overview index: %d"), ovx);
1367  return 0;
1368  }
1369  factor = config->overview[ovx];
1370  ovtable = (const char *) config->overview_table[ovx];
1371 
1372  /* factor must be within valid range */
1373  if (factor < MINOVFACTOR || factor > MAXOVFACTOR) {
1374  rterror(_("build_overview: Overview factor %d is not between %d and %d"), factor, MINOVFACTOR, MAXOVFACTOR);
1375  return 0;
1376  }
1377 
1378  dimOv[0] = (int) (info->dim[0] + (factor / 2)) / factor;
1379  dimOv[1] = (int) (info->dim[1] + (factor / 2)) / factor;
1380 
1381  /* create VRT dataset */
1382  hdsOv = VRTCreate(dimOv[0], dimOv[1]);
1383  /*
1384  GDALSetDescription(hdsOv, "/tmp/ov.vrt");
1385  */
1386  GDALSetProjection(hdsOv, info->srs);
1387 
1388  /* adjust scale */
1389  gtOv[1] *= factor;
1390  gtOv[5] *= factor;
1391 
1392  GDALSetGeoTransform(hdsOv, gtOv);
1393 
1394  /* add bands as simple sources */
1395  for (j = 0; j < info->nband_count; j++) {
1396  GDALAddBand(hdsOv, info->gdalbandtype[j], NULL);
1397  hbandOv = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsOv, j + 1);
1398 
1399  if (info->hasnodata[j])
1400  GDALSetRasterNoDataValue(hbandOv, info->nodataval[j]);
1401 
1402  VRTAddSimpleSource(
1403  hbandOv, GDALGetRasterBand(hdsSrc, info->nband[j]),
1404  0, 0,
1405  info->dim[0], info->dim[1],
1406  0, 0,
1407  dimOv[0], dimOv[1],
1408  "near", VRT_NODATA_UNSET
1409  );
1410  }
1411 
1412  /* make sure VRT reflects all changes */
1413  VRTFlushCache(hdsOv);
1414 
1415  /* decide on tile size */
1416  if (!config->tile_size[0])
1417  tile_size[0] = dimOv[0];
1418  else
1419  tile_size[0] = config->tile_size[0];
1420  if (!config->tile_size[1])
1421  tile_size[1] = dimOv[1];
1422  else
1423  tile_size[1] = config->tile_size[1];
1424 
1425  /* number of tiles */
1426  if (
1427  tile_size[0] != dimOv[0] &&
1428  tile_size[1] != dimOv[1]
1429  ) {
1430  ntiles[0] = (dimOv[0] + tile_size[0] - 1) / tile_size[0];
1431  ntiles[1] = (dimOv[1] + tile_size[1] - 1) / tile_size[1];
1432  }
1433 
1434  /* working copy of geotransform matrix */
1435  memcpy(gt, gtOv, sizeof(double) * 6);
1436 
1437  /* tile overview */
1438  /* each tile is a VRT with constraints set for just the data required for the tile */
1439  for (ytile = 0; ytile < ntiles[1]; ytile++) {
1440 
1441  /* edge y tile */
1442  if (!config->pad_tile && ntiles[1] > 1 && (ytile + 1) == ntiles[1])
1443  _tile_size[1] = dimOv[1] - (ytile * tile_size[1]);
1444  else
1445  _tile_size[1] = tile_size[1];
1446 
1447  for (xtile = 0; xtile < ntiles[0]; xtile++) {
1448  /*
1449  char fn[100];
1450  sprintf(fn, "/tmp/ovtile%d.vrt", (ytile * ntiles[0]) + xtile);
1451  */
1452 
1453  /* edge x tile */
1454  if (!config->pad_tile && ntiles[0] > 1 && (xtile + 1) == ntiles[0])
1455  _tile_size[0] = dimOv[0] - (xtile * tile_size[0]);
1456  else
1457  _tile_size[0] = tile_size[0];
1458 
1459  /* compute tile's upper-left corner */
1460  GDALApplyGeoTransform(
1461  gtOv,
1462  xtile * tile_size[0], ytile * tile_size[1],
1463  &(gt[0]), &(gt[3])
1464  );
1465 
1466  /* create VRT dataset */
1467  hdsDst = VRTCreate(_tile_size[0], _tile_size[1]);
1468  /*
1469  GDALSetDescription(hdsDst, fn);
1470  */
1471  GDALSetProjection(hdsDst, info->srs);
1472  GDALSetGeoTransform(hdsDst, gt);
1473 
1474  /* add bands as simple sources */
1475  for (j = 0; j < info->nband_count; j++) {
1476  GDALAddBand(hdsDst, info->gdalbandtype[j], NULL);
1477  hbandDst = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsDst, j + 1);
1478 
1479  if (info->hasnodata[j])
1480  GDALSetRasterNoDataValue(hbandDst, info->nodataval[j]);
1481 
1482  VRTAddSimpleSource(
1483  hbandDst, GDALGetRasterBand(hdsOv, j + 1),
1484  xtile * tile_size[0], ytile * tile_size[1],
1485  _tile_size[0], _tile_size[1],
1486  0, 0,
1487  _tile_size[0], _tile_size[1],
1488  "near", VRT_NODATA_UNSET
1489  );
1490  }
1491 
1492  /* make sure VRT reflects all changes */
1493  VRTFlushCache(hdsDst);
1494 
1495  /* convert VRT dataset to rt_raster */
1496  rast = rt_raster_from_gdal_dataset(hdsDst);
1497  if (rast == NULL) {
1498  rterror(_("build_overview: Could not convert VRT dataset to PostGIS raster"));
1499  GDALClose(hdsDst);
1500  return 0;
1501  }
1502 
1503  /* set srid if provided */
1504  rt_raster_set_srid(rast, info->srid);
1505 
1506  /* convert rt_raster to hexwkb */
1507  hex = rt_raster_to_hexwkb(rast, FALSE, &hexlen);
1508  raster_destroy(rast);
1509 
1510  if (hex == NULL) {
1511  rterror(_("build_overview: Could not convert PostGIS raster to hex WKB"));
1512  GDALClose(hdsDst);
1513  return 0;
1514  }
1515 
1516  /* add hexwkb to tileset */
1517  append_stringbuffer(tileset, hex);
1518 
1519  rtdealloc(hex);
1520  GDALClose(hdsDst);
1521 
1522  /* flush if tileset gets too big */
1523  if (tileset->length > 10) {
1524  if (!insert_records(
1525  config->schema, ovtable, config->raster_column,
1526  (config->file_column ? config->rt_filename[idx] : NULL), config->file_column_name,
1527  config->copy_statements,
1528  tileset, buffer
1529  )) {
1530  rterror(_("build_overview: Could not convert raster tiles into INSERT or COPY statements"));
1531  GDALClose(hdsSrc);
1532  return 0;
1533  }
1534 
1535  rtdealloc_stringbuffer(tileset, 0);
1536  }
1537  }
1538  }
1539 
1540  GDALClose(hdsOv);
1541  GDALClose(hdsSrc);
1542  return 1;
1543 }
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_api.c:9339
GDALDataType * gdalbandtype
Definition: raster2pgsql.h:174
void rtdealloc(void *mem)
Definition: rt_api.c:882
static void rtdealloc_stringbuffer(STRINGBUFFER *buffer, int freebuffer)
Definition: raster2pgsql.c:767
#define _(String)
Definition: shpcommon.h:23
tuple gt
Definition: window.py:79
uint32_t length
Definition: raster2pgsql.h:191
tuple rast
Definition: rtpixdump.py:62
static int append_stringbuffer(STRINGBUFFER *buffer, const char *str)
Definition: raster2pgsql.c:799
static int insert_records(const char *schema, const char *table, const char *column, const char *filename, const char *file_column_name, int copy_statements, STRINGBUFFER *tileset, STRINGBUFFER *buffer)
Definition: raster2pgsql.c:828
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_api.c:5668
#define MINOVFACTOR
Definition: raster2pgsql.h:56
uint32_t dim[2]
Definition: raster2pgsql.h:167
void rterror(const char *fmt,...)
Raster core error and info handlers.
Definition: rt_api.c:895
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
Definition: rt_api.c:8062
#define FALSE
Definition: dbfopen.c:169
static void raster_destroy(rt_raster raster)
Definition: raster2pgsql.c:77
double * nodataval
Definition: raster2pgsql.h:180
#define MAXOVFACTOR
Definition: raster2pgsql.h:57
double gt[6]
Definition: raster2pgsql.h:183
if(!(yy_init))
Definition: lwin_wkt_lex.c:860

Here is the call graph for this function:

Here is the caller graph for this function: