PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ rt_band_load_offline_data()

rt_errorstate rt_band_load_offline_data ( rt_band  band)

Load offline band's data.

Loaded data is internally owned and should not be released by the caller. Data will be released when band is destroyed with rt_band_destroy().

Parameters
band: the band who's data to get
Returns
ES_NONE if success, ES_ERROR if failure

Definition at line 429 of file rt_band.c.

429  {
430  GDALDatasetH hdsSrc = NULL;
431  int nband = 0;
432  VRTDatasetH hdsDst = NULL;
433  VRTSourcedRasterBandH hbandDst = NULL;
434  double ogt[6] = {0};
435  double offset[2] = {0};
436 
437  rt_raster _rast = NULL;
438  rt_band _band = NULL;
439  int aligned = 0;
440  int err = ES_NONE;
441 
442  assert(band != NULL);
443  assert(band->raster != NULL);
444 
445  if (!band->offline) {
446  rterror("rt_band_load_offline_data: Band is not offline");
447  return ES_ERROR;
448  }
449  else if (!strlen(band->data.offline.path)) {
450  rterror("rt_band_load_offline_data: Offline band does not a have a specified file");
451  return ES_ERROR;
452  }
453 
454  /* offline_data is disabled */
455  if (!enable_outdb_rasters) {
456  rterror("rt_band_load_offline_data: Access to offline bands disabled");
457  return ES_ERROR;
458  }
459 
461  hdsSrc = rt_util_gdal_open(band->data.offline.path, GA_ReadOnly, 1);
462  if (hdsSrc == NULL) {
463  rterror("rt_band_load_offline_data: Cannot open offline raster: %s", band->data.offline.path);
464  return ES_ERROR;
465  }
466 
467  /* # of bands */
468  nband = GDALGetRasterCount(hdsSrc);
469  if (!nband) {
470  rterror("rt_band_load_offline_data: No bands found in offline raster: %s", band->data.offline.path);
471  GDALClose(hdsSrc);
472  return ES_ERROR;
473  }
474  /* bandNum is 0-based */
475  else if (band->data.offline.bandNum + 1 > nband) {
476  rterror("rt_band_load_offline_data: Specified band %d not found in offline raster: %s", band->data.offline.bandNum, band->data.offline.path);
477  GDALClose(hdsSrc);
478  return ES_ERROR;
479  }
480 
481  /* get offline raster's geotransform */
482  if (GDALGetGeoTransform(hdsSrc, ogt) != CE_None) {
483  RASTER_DEBUG(4, "Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
484  ogt[0] = 0;
485  ogt[1] = 1;
486  ogt[2] = 0;
487  ogt[3] = 0;
488  ogt[4] = 0;
489  ogt[5] = -1;
490  }
491  RASTER_DEBUGF(3, "Offline geotransform (%f, %f, %f, %f, %f, %f)",
492  ogt[0], ogt[1], ogt[2], ogt[3], ogt[4], ogt[5]);
493 
494  /* are rasters aligned? */
495  _rast = rt_raster_new(1, 1);
497  rt_raster_set_srid(_rast, band->raster->srid);
498  err = rt_raster_same_alignment(band->raster, _rast, &aligned, NULL);
499  rt_raster_destroy(_rast);
500 
501  if (err != ES_NONE) {
502  rterror("rt_band_load_offline_data: Could not test alignment of in-db representation of out-db raster");
503  GDALClose(hdsSrc);
504  return ES_ERROR;
505  }
506  else if (!aligned) {
507  rtwarn("The in-db representation of the out-db raster is not aligned. Band data may be incorrect");
508  }
509 
510  /* get offsets */
512  band->raster,
513  ogt[0], ogt[3],
514  &(offset[0]), &(offset[1]),
515  NULL
516  );
517 
518  RASTER_DEBUGF(4, "offsets: (%f, %f)", offset[0], offset[1]);
519 
520  /* create VRT dataset */
521  hdsDst = VRTCreate(band->width, band->height);
522  GDALSetGeoTransform(hdsDst, ogt);
523  /*
524  GDALSetDescription(hdsDst, "/tmp/offline.vrt");
525  */
526 
527  /* add band as simple sources */
528  GDALAddBand(hdsDst, rt_util_pixtype_to_gdal_datatype(band->pixtype), NULL);
529  hbandDst = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsDst, 1);
530 
531  if (band->hasnodata)
532  GDALSetRasterNoDataValue(hbandDst, band->nodataval);
533 
534  VRTAddSimpleSource(
535  hbandDst, GDALGetRasterBand(hdsSrc, band->data.offline.bandNum + 1),
536  fabs(offset[0]), fabs(offset[1]),
537  band->width, band->height,
538  0, 0,
539  band->width, band->height,
540  "near", VRT_NODATA_UNSET
541  );
542 
543  /* make sure VRT reflects all changes */
544  VRTFlushCache(hdsDst);
545 
546  /* convert VRT dataset to rt_raster */
547  _rast = rt_raster_from_gdal_dataset(hdsDst);
548 
549  GDALClose(hdsDst);
550  GDALClose(hdsSrc);
551  /*
552  {
553  FILE *fp;
554  fp = fopen("/tmp/gdal_open_files.log", "w");
555  GDALDumpOpenDatasets(fp);
556  fclose(fp);
557  }
558  */
559 
560  if (_rast == NULL) {
561  rterror("rt_band_load_offline_data: Cannot load data from offline raster: %s", band->data.offline.path);
562  return ES_ERROR;
563  }
564 
565  _band = rt_raster_get_band(_rast, 0);
566  if (_band == NULL) {
567  rterror("rt_band_load_offline_data: Cannot load data from offline raster: %s", band->data.offline.path);
568  rt_raster_destroy(_rast);
569  return ES_ERROR;
570  }
571 
572  /* band->data.offline.mem not NULL, free first */
573  if (band->data.offline.mem != NULL) {
574  rtdealloc(band->data.offline.mem);
575  band->data.offline.mem = NULL;
576  }
577 
578  band->data.offline.mem = _band->data.mem;
579 
580  rtdealloc(_band); /* cannot use rt_band_destroy */
581  rt_raster_destroy(_rast);
582 
583  return ES_NONE;
584 }
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
int rt_util_gdal_register_all(int force_register_all)
Definition: rt_util.c:336
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:299
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
Definition: rt_raster.c:727
rt_errorstate rt_raster_geopoint_to_cell(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw, yw map point to a xr, yr raster point.
Definition: rt_raster.c:806
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
Definition: rt_util.c:118
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_raster.c:2182
void rtwarn(const char *fmt,...)
Definition: rt_context.c:224
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
Definition: rt_util.c:381
@ ES_NONE
Definition: librtcore.h:180
@ ES_ERROR
Definition: librtcore.h:181
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_raster.c:363
void rtdealloc(void *mem)
Definition: rt_context.c:186
rt_errorstate rt_raster_same_alignment(rt_raster rast1, rt_raster rast2, int *aligned, char **reason)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
band
Definition: ovdump.py:57
nband
Definition: pixval.py:52
char enable_outdb_rasters
Definition: rt_band.c:417
union rt_band_t::@8 data
void * mem
Definition: librtcore.h:2327

References ovdump::band, rt_band_t::data, enable_outdb_rasters, ES_ERROR, ES_NONE, rt_band_t::mem, pixval::nband, RASTER_DEBUG, RASTER_DEBUGF, rt_raster_destroy(), rt_raster_from_gdal_dataset(), rt_raster_geopoint_to_cell(), rt_raster_get_band(), rt_raster_new(), rt_raster_same_alignment(), rt_raster_set_geotransform_matrix(), rt_raster_set_srid(), rt_util_gdal_open(), rt_util_gdal_register_all(), rt_util_pixtype_to_gdal_datatype(), rtdealloc(), rterror(), and rtwarn().

Referenced by rt_band_get_data(), and test_band_metadata().

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