PostGIS  3.4.0dev-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 1720 of file rt_raster.c.

1723  {
1724  const char *cc;
1725  const char *vio;
1726 
1727  GDALDriverH src_drv = NULL;
1728  int destroy_src_drv = 0;
1729  GDALDatasetH src_ds = NULL;
1730 
1731  vsi_l_offset rtn_lenvsi;
1732  uint64_t rtn_len = 0;
1733 
1734  GDALDriverH rtn_drv = NULL;
1735  GDALDatasetH rtn_ds = NULL;
1736  uint8_t *rtn = NULL;
1737 
1738  assert(NULL != raster);
1739  assert(NULL != gdalsize);
1740 
1741  /* any supported format is possible */
1743  RASTER_DEBUG(3, "loaded all supported GDAL formats");
1744 
1745  /* output format not specified */
1746  if (format == NULL || !strlen(format))
1747  format = "GTiff";
1748  RASTER_DEBUGF(3, "output format is %s", format);
1749 
1750  /* load raster into a GDAL MEM raster */
1751  src_ds = rt_raster_to_gdal_mem(raster, srs, NULL, NULL, 0, &src_drv, &destroy_src_drv);
1752  if (NULL == src_ds) {
1753  rterror("rt_raster_to_gdal: Could not convert raster to GDAL MEM format");
1754  return 0;
1755  }
1756 
1757  /* load driver */
1758  rtn_drv = GDALGetDriverByName(format);
1759  if (NULL == rtn_drv) {
1760  rterror("rt_raster_to_gdal: Could not load the output GDAL driver");
1761  GDALClose(src_ds);
1762  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1763  return 0;
1764  }
1765  RASTER_DEBUG(3, "Output driver loaded");
1766 
1767  /* CreateCopy support */
1768  cc = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_CREATECOPY, NULL);
1769  /* VirtualIO support */
1770  vio = GDALGetMetadataItem(rtn_drv, GDAL_DCAP_VIRTUALIO, NULL);
1771 
1772  if (cc == NULL || vio == NULL) {
1773  rterror("rt_raster_to_gdal: Output GDAL driver does not support CreateCopy and/or VirtualIO");
1774  GDALClose(src_ds);
1775  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1776  return 0;
1777  }
1778 
1779  /* convert GDAL MEM raster to output format */
1780  RASTER_DEBUG(3, "Copying GDAL MEM raster to memory file in output format");
1781  rtn_ds = GDALCreateCopy(
1782  rtn_drv,
1783  "/vsimem/out.dat", /* should be fine assuming this is in a process */
1784  src_ds,
1785  FALSE, /* should copy be strictly equivelent? */
1786  options, /* format options */
1787  NULL, /* progress function */
1788  NULL /* progress data */
1789  );
1790 
1791  /* close source dataset */
1792  GDALClose(src_ds);
1793  if (destroy_src_drv) GDALDestroyDriver(src_drv);
1794  RASTER_DEBUG(3, "Closed GDAL MEM raster");
1795 
1796  if (NULL == rtn_ds) {
1797  rterror("rt_raster_to_gdal: Could not create the output GDAL dataset");
1798  return 0;
1799  }
1800 
1801  RASTER_DEBUGF(4, "dataset SRS: %s", GDALGetProjectionRef(rtn_ds));
1802 
1803  /* close dataset, this also flushes any pending writes */
1804  GDALClose(rtn_ds);
1805  RASTER_DEBUG(3, "Closed GDAL output raster");
1806 
1807  RASTER_DEBUG(3, "Done copying GDAL MEM raster to memory file in output format");
1808 
1809  /* from memory file to buffer */
1810  RASTER_DEBUG(3, "Copying GDAL memory file to buffer");
1811  rtn = VSIGetMemFileBuffer("/vsimem/out.dat", &rtn_lenvsi, TRUE);
1812  RASTER_DEBUG(3, "Done copying GDAL memory file to buffer");
1813  if (NULL == rtn) {
1814  rterror("rt_raster_to_gdal: Could not create the output GDAL raster");
1815  return 0;
1816  }
1817 
1818  rtn_len = (uint64_t) rtn_lenvsi;
1819  *gdalsize = rtn_len;
1820 
1821  return rtn;
1822 }
#define TRUE
Definition: dbfopen.c:73
#define FALSE
Definition: dbfopen.c:72
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:219
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:302
int rt_util_gdal_register_all(int force_register_all)
Definition: rt_util.c:342
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:306
src_ds
MAIN.
Definition: ovdump.py:54
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:1935

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: