PostGIS  2.5.0dev-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 425 of file rt_band.c.

References rt_band_t::data, enable_outdb_rasters, ES_ERROR, ES_NONE, rt_band_t::hasnodata, rt_band_t::height, rt_band_t::mem, pixval::nband, rt_band_t::nodataval, rt_band_t::offline, rt_band_t::pixtype, rt_band_t::raster, 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(), rtwarn(), rt_raster_t::srid, and rt_band_t::width.

Referenced by rt_band_get_data(), and test_band_metadata().

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