PostGIS  2.4.9dev-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 331 of file rt_band.c.

References rt_band_t::data, enable_outdb_rasters, ES_ERROR, ES_NONE, window::gt, 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_get_geotransform_matrix(), 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().

331  {
332  GDALDatasetH hdsSrc = NULL;
333  int nband = 0;
334  VRTDatasetH hdsDst = NULL;
335  VRTSourcedRasterBandH hbandDst = NULL;
336  double gt[6] = {0.};
337  double ogt[6] = {0};
338  double offset[2] = {0};
339 
340  rt_raster _rast = NULL;
341  rt_band _band = NULL;
342  int aligned = 0;
343  int err = ES_NONE;
344 
345  assert(band != NULL);
346  assert(band->raster != NULL);
347 
348  if (!band->offline) {
349  rterror("rt_band_load_offline_data: Band is not offline");
350  return ES_ERROR;
351  }
352  else if (!strlen(band->data.offline.path)) {
353  rterror("rt_band_load_offline_data: Offline band does not a have a specified file");
354  return ES_ERROR;
355  }
356 
357  /* offline_data is disabled */
358  if (!enable_outdb_rasters) {
359  rterror("rt_band_load_offline_data: Access to offline bands disabled");
360  return ES_ERROR;
361  }
362 
364  /*
365  hdsSrc = rt_util_gdal_open(band->data.offline.path, GA_ReadOnly, 1);
366  */
367  hdsSrc = rt_util_gdal_open(band->data.offline.path, GA_ReadOnly, 0);
368  if (hdsSrc == NULL) {
369  rterror("rt_band_load_offline_data: Cannot open offline raster: %s", band->data.offline.path);
370  return ES_ERROR;
371  }
372 
373  /* # of bands */
374  nband = GDALGetRasterCount(hdsSrc);
375  if (!nband) {
376  rterror("rt_band_load_offline_data: No bands found in offline raster: %s", band->data.offline.path);
377  GDALClose(hdsSrc);
378  return ES_ERROR;
379  }
380  /* bandNum is 0-based */
381  else if (band->data.offline.bandNum + 1 > nband) {
382  rterror("rt_band_load_offline_data: Specified band %d not found in offline raster: %s", band->data.offline.bandNum, band->data.offline.path);
383  GDALClose(hdsSrc);
384  return ES_ERROR;
385  }
386 
387  /* get raster's geotransform */
389  RASTER_DEBUGF(3, "Raster geotransform (%f, %f, %f, %f, %f, %f)",
390  gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]);
391 
392  /* get offline raster's geotransform */
393  if (GDALGetGeoTransform(hdsSrc, ogt) != CE_None) {
394  RASTER_DEBUG(4, "Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
395  ogt[0] = 0;
396  ogt[1] = 1;
397  ogt[2] = 0;
398  ogt[3] = 0;
399  ogt[4] = 0;
400  ogt[5] = -1;
401  }
402  RASTER_DEBUGF(3, "Offline geotransform (%f, %f, %f, %f, %f, %f)",
403  ogt[0], ogt[1], ogt[2], ogt[3], ogt[4], ogt[5]);
404 
405  /* are rasters aligned? */
406  _rast = rt_raster_new(1, 1);
408  rt_raster_set_srid(_rast, band->raster->srid);
409  err = rt_raster_same_alignment(band->raster, _rast, &aligned, NULL);
410  rt_raster_destroy(_rast);
411 
412  if (err != ES_NONE) {
413  rterror("rt_band_load_offline_data: Could not test alignment of in-db representation of out-db raster");
414  GDALClose(hdsSrc);
415  return ES_ERROR;
416  }
417  else if (!aligned) {
418  rtwarn("The in-db representation of the out-db raster is not aligned. Band data may be incorrect");
419  }
420 
421  /* get offsets */
423  band->raster,
424  ogt[0], ogt[3],
425  &(offset[0]), &(offset[1]),
426  NULL
427  );
428 
429  RASTER_DEBUGF(4, "offsets: (%f, %f)", offset[0], offset[1]);
430 
431  /* create VRT dataset */
432  hdsDst = VRTCreate(band->width, band->height);
433  GDALSetGeoTransform(hdsDst, gt);
434  /*
435  GDALSetDescription(hdsDst, "/tmp/offline.vrt");
436  */
437 
438  /* add band as simple sources */
439  GDALAddBand(hdsDst, rt_util_pixtype_to_gdal_datatype(band->pixtype), NULL);
440  hbandDst = (VRTSourcedRasterBandH) GDALGetRasterBand(hdsDst, 1);
441 
442  if (band->hasnodata)
443  GDALSetRasterNoDataValue(hbandDst, band->nodataval);
444 
445  VRTAddSimpleSource(
446  hbandDst, GDALGetRasterBand(hdsSrc, band->data.offline.bandNum + 1),
447  fabs(offset[0]), fabs(offset[1]),
448  band->width, band->height,
449  0, 0,
450  band->width, band->height,
451  "near", VRT_NODATA_UNSET
452  );
453 
454  /* make sure VRT reflects all changes */
455  VRTFlushCache(hdsDst);
456 
457  /* convert VRT dataset to rt_raster */
458  _rast = rt_raster_from_gdal_dataset(hdsDst);
459 
460  GDALClose(hdsDst);
461  GDALClose(hdsSrc);
462  /*
463  {
464  FILE *fp;
465  fp = fopen("/tmp/gdal_open_files.log", "w");
466  GDALDumpOpenDatasets(fp);
467  fclose(fp);
468  }
469  */
470 
471  if (_rast == NULL) {
472  rterror("rt_band_load_offline_data: Cannot load data from offline raster: %s", band->data.offline.path);
473  return ES_ERROR;
474  }
475 
476  _band = rt_raster_get_band(_rast, 0);
477  if (_band == NULL) {
478  rterror("rt_band_load_offline_data: Cannot load data from offline raster: %s", band->data.offline.path);
479  rt_raster_destroy(_rast);
480  return ES_ERROR;
481  }
482 
483  /* band->data.offline.mem not NULL, free first */
484  if (band->data.offline.mem != NULL) {
485  rtdealloc(band->data.offline.mem);
486  band->data.offline.mem = NULL;
487  }
488 
489  band->data.offline.mem = _band->data.mem;
490 
491  rtdealloc(_band); /* cannot use rt_band_destroy */
492  rt_raster_destroy(_rast);
493 
494  return ES_NONE;
495 }
char enable_outdb_rasters
Definition: rt_band.c:319
int32_t srid
Definition: librtcore.h:2251
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:2275
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition: rt_raster.c:706
rt_pixtype pixtype
Definition: librtcore.h:2265
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
uint16_t height
Definition: librtcore.h:2268
gt
Definition: window.py:77
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
union rt_band_t::@6 data
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:2272
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:2267
int32_t offline
Definition: librtcore.h:2266
void rtdealloc(void *mem)
Definition: rt_context.c:186
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
void * mem
Definition: librtcore.h:2278
int32_t hasnodata
Definition: librtcore.h:2269
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_raster.c:2165
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
Definition: rt_util.c:379
Here is the call graph for this function:
Here is the caller graph for this function: