PostGIS  2.5.7dev-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 1598 of file rt_raster.c.

1601  {
1602  const char *cc;
1603  const char *vio;
1604 
1605  GDALDriverH src_drv = NULL;
1606  int destroy_src_drv = 0;
1607  GDALDatasetH src_ds = NULL;
1608 
1609  vsi_l_offset rtn_lenvsi;
1610  uint64_t rtn_len = 0;
1611 
1612  GDALDriverH rtn_drv = NULL;
1613  GDALDatasetH rtn_ds = NULL;
1614  uint8_t *rtn = NULL;
1615 
1616  assert(NULL != raster);
1617  assert(NULL != gdalsize);
1618 
1619  /* any supported format is possible */
1621  RASTER_DEBUG(3, "loaded all supported GDAL formats");
1622 
1623  /* output format not specified */
1624  if (format == NULL || !strlen(format))
1625  format = "GTiff";
1626  RASTER_DEBUGF(3, "output format is %s", format);
1627 
1628  /* load raster into a GDAL MEM raster */
1629  src_ds = rt_raster_to_gdal_mem(raster, srs, NULL, NULL, 0, &src_drv, &destroy_src_drv);
1630  if (NULL == src_ds) {
1631  rterror("rt_raster_to_gdal: Could not convert raster to GDAL MEM format");
1632  return 0;
1633  }
1634 
1635  /* load driver */
1636  rtn_drv = GDALGetDriverByName(format);
1637  if (NULL == rtn_drv) {
1638  rterror("rt_raster_to_gdal: Could not load the output GDAL driver");
1639  GDALClose(src_ds);
1640  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1641  return 0;
1642  }
1643  RASTER_DEBUG(3, "Output driver loaded");
1644 
1645  /* CreateCopy support */
1646  cc = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_CREATECOPY, NULL);
1647  /* VirtualIO support */
1648  vio = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_VIRTUALIO, NULL);
1649 
1650  if (cc == NULL || vio == NULL) {
1651  rterror("rt_raster_to_gdal: Output GDAL driver does not support CreateCopy and/or VirtualIO");
1652  GDALClose(src_ds);
1653  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1654  return 0;
1655  }
1656 
1657  /* convert GDAL MEM raster to output format */
1658  RASTER_DEBUG(3, "Copying GDAL MEM raster to memory file in output format");
1659  rtn_ds = GDALCreateCopy(
1660  rtn_drv,
1661  "/vsimem/out.dat", /* should be fine assuming this is in a process */
1662  src_ds,
1663  FALSE, /* should copy be strictly equivelent? */
1664  options, /* format options */
1665  NULL, /* progress function */
1666  NULL /* progress data */
1667  );
1668 
1669  /* close source dataset */
1670  GDALClose(src_ds);
1671  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1672  RASTER_DEBUG(3, "Closed GDAL MEM raster");
1673 
1674  if (NULL == rtn_ds) {
1675  rterror("rt_raster_to_gdal: Could not create the output GDAL dataset");
1676  return 0;
1677  }
1678 
1679  RASTER_DEBUGF(4, "dataset SRS: %s", GDALGetProjectionRef(rtn_ds));
1680 
1681  /* close dataset, this also flushes any pending writes */
1682  GDALClose(rtn_ds);
1683  RASTER_DEBUG(3, "Closed GDAL output raster");
1684 
1685  RASTER_DEBUG(3, "Done copying GDAL MEM raster to memory file in output format");
1686 
1687  /* from memory file to buffer */
1688  RASTER_DEBUG(3, "Copying GDAL memory file to buffer");
1689  rtn = VSIGetMemFileBuffer("/vsimem/out.dat", &rtn_lenvsi, TRUE);
1690  RASTER_DEBUG(3, "Done copying GDAL memory file to buffer");
1691  if (NULL == rtn) {
1692  rterror("rt_raster_to_gdal: Could not create the output GDAL raster");
1693  return 0;
1694  }
1695 
1696  rtn_len = (uint64_t) rtn_lenvsi;
1697  *gdalsize = rtn_len;
1698 
1699  return rtn;
1700 }
#define TRUE
Definition: dbfopen.c:169
#define FALSE
Definition: dbfopen.c:168
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
src_ds
MAIN.
Definition: ovdump.py:53
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
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:1826
unsigned char uint8_t
Definition: uthash.h:79

References FALSE, rtrowdump::raster, 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().

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