PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ rt_raster_to_gdal()

uint8_t* rt_raster_to_gdal ( rt_raster  raster,
const char *  srs,
char *  format,
char **  options,
uint64_t *  gdalsize 
)

Return formatted GDAL raster from raster.

Parameters
raster: the raster to convert
srs: the raster's coordinate system in OGC WKT
format: format to convert to. GDAL driver short name
options: list of format creation options. array of strings
gdalsize: will be set to the size of returned bytea
Returns
formatted GDAL raster. the calling function is responsible for freeing the returned data using CPLFree()

Definition at line 1602 of file rt_raster.c.

References FALSE, RASTER_DEBUG, RASTER_DEBUGF, rt_raster_to_gdal_mem(), rt_util_gdal_register_all(), rterror(), ovdump::src_ds, and TRUE.

Referenced by RASTER_asGDALRaster(), and test_raster_to_gdal().

1605  {
1606  GDALDriverH src_drv = NULL;
1607  int destroy_src_drv = 0;
1608  GDALDatasetH src_ds = NULL;
1609 
1610  vsi_l_offset rtn_lenvsi;
1611  uint64_t rtn_len = 0;
1612 
1613  GDALDriverH rtn_drv = NULL;
1614  GDALDatasetH rtn_ds = NULL;
1615  uint8_t *rtn = NULL;
1616 
1617  assert(NULL != raster);
1618  assert(NULL != gdalsize);
1619 
1620  /* any supported format is possible */
1622  RASTER_DEBUG(3, "loaded all supported GDAL formats");
1623 
1624  /* output format not specified */
1625  if (format == NULL || !strlen(format))
1626  format = "GTiff";
1627  RASTER_DEBUGF(3, "output format is %s", format);
1628 
1629  /* load raster into a GDAL MEM raster */
1630  src_ds = rt_raster_to_gdal_mem(raster, srs, NULL, NULL, 0, &src_drv, &destroy_src_drv);
1631  if (NULL == src_ds) {
1632  rterror("rt_raster_to_gdal: Could not convert raster to GDAL MEM format");
1633  return 0;
1634  }
1635 
1636  /* load driver */
1637  rtn_drv = GDALGetDriverByName(format);
1638  if (NULL == rtn_drv) {
1639  rterror("rt_raster_to_gdal: Could not load the output GDAL driver");
1640  GDALClose(src_ds);
1641  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1642  return 0;
1643  }
1644  RASTER_DEBUG(3, "Output driver loaded");
1645 
1646  /* convert GDAL MEM raster to output format */
1647  RASTER_DEBUG(3, "Copying GDAL MEM raster to memory file in output format");
1648  rtn_ds = GDALCreateCopy(
1649  rtn_drv,
1650  "/vsimem/out.dat", /* should be fine assuming this is in a process */
1651  src_ds,
1652  FALSE, /* should copy be strictly equivelent? */
1653  options, /* format options */
1654  NULL, /* progress function */
1655  NULL /* progress data */
1656  );
1657 
1658  /* close source dataset */
1659  GDALClose(src_ds);
1660  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1661  RASTER_DEBUG(3, "Closed GDAL MEM raster");
1662 
1663  if (NULL == rtn_ds) {
1664  rterror("rt_raster_to_gdal: Could not create the output GDAL dataset");
1665  return 0;
1666  }
1667 
1668  RASTER_DEBUGF(4, "dataset SRS: %s", GDALGetProjectionRef(rtn_ds));
1669 
1670  /* close dataset, this also flushes any pending writes */
1671  GDALClose(rtn_ds);
1672  RASTER_DEBUG(3, "Closed GDAL output raster");
1673 
1674  RASTER_DEBUG(3, "Done copying GDAL MEM raster to memory file in output format");
1675 
1676  /* from memory file to buffer */
1677  RASTER_DEBUG(3, "Copying GDAL memory file to buffer");
1678  rtn = VSIGetMemFileBuffer("/vsimem/out.dat", &rtn_lenvsi, TRUE);
1679  RASTER_DEBUG(3, "Done copying GDAL memory file to buffer");
1680  if (NULL == rtn) {
1681  rterror("rt_raster_to_gdal: Could not create the output GDAL raster");
1682  return 0;
1683  }
1684 
1685  rtn_len = (uint64_t) rtn_lenvsi;
1686  *gdalsize = rtn_len;
1687 
1688  return rtn;
1689 }
int rt_util_gdal_register_all(int force_register_all)
Definition: rt_util.c:334
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
GDALDatasetH rt_raster_to_gdal_mem(rt_raster raster, const char *srs, uint32_t *bandNums, int *excludeNodataValues, int count, GDALDriverH *rtn_drv, int *destroy_rtn_drv)
Return GDAL dataset using GDAL MEM driver from raster.
Definition: rt_raster.c:1809
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:299
#define FALSE
Definition: dbfopen.c:168
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
src_ds
MAIN.
Definition: ovdump.py:53
unsigned char uint8_t
Definition: uthash.h:79
#define TRUE
Definition: dbfopen.c:169
Here is the call graph for this function:
Here is the caller graph for this function: