PostGIS  3.0.6dev-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 1593 of file rt_raster.c.

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

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: