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