PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ rt_raster_to_gdal_mem()

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.

Parameters
raster: raster to convert to GDAL MEM
srs: the raster's coordinate system in OGC WKT
bandNums: array of band numbers to extract from raster and include in the GDAL dataset (0 based)
excludeNodataValues: array of zero, nonzero where if non-zero, ignore nodata values for the band to check for pixels with value
count: number of elements in bandNums and exclude_nodata_values
rtn_drv: is set to the GDAL driver object
destroy_rtn_drv: if non-zero, caller must destroy the MEM driver
Returns
GDAL dataset using GDAL MEM driver
Parameters
raster: raster to convert to GDAL MEM
srs: the raster's coordinate system in OGC WKT
bandNums: array of band numbers to extract from raster and include in the GDAL dataset (0 based)
excludeNodataValues: array of zero, nonzero where if non-zero, ignore nodata values for the band
count: number of elements in bandNums
rtn_drv: is set to the GDAL driver object
destroy_rtn_drv: if non-zero, caller must destroy the MEM driver
Returns
GDAL dataset using GDAL MEM driver

Definition at line 1808 of file rt_raster.c.

1815  {
1816  GDALDriverH drv = NULL;
1817  GDALDatasetH ds = NULL;
1818  double gt[6] = {0.0};
1819  CPLErr cplerr;
1820  GDALDataType gdal_pt = GDT_Unknown;
1821  GDALRasterBandH band;
1822  void *pVoid;
1823  char *pszDataPointer;
1824  char szGDALOption[50];
1825  char *apszOptions[4];
1826  double nodata = 0.0;
1827  int allocBandNums = 0;
1828  int allocNodataValues = 0;
1829 
1830  int i;
1831  uint32_t numBands;
1832  uint32_t width = 0;
1833  uint32_t height = 0;
1834  rt_band rtband = NULL;
1835  rt_pixtype pt = PT_END;
1836 
1837  assert(NULL != raster);
1838  assert(NULL != rtn_drv);
1839  assert(NULL != destroy_rtn_drv);
1840 
1841  *destroy_rtn_drv = 0;
1842 
1843  /* store raster in GDAL MEM raster */
1844  if (!rt_util_gdal_driver_registered("MEM")) {
1845  RASTER_DEBUG(4, "Registering MEM driver");
1846  GDALRegister_MEM();
1847  *destroy_rtn_drv = 1;
1848  }
1849  drv = GDALGetDriverByName("MEM");
1850  if (NULL == drv) {
1851  rterror("rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1852  return 0;
1853  }
1854  *rtn_drv = drv;
1855 
1856  /* unload driver from GDAL driver manager */
1857  if (*destroy_rtn_drv) {
1858  RASTER_DEBUG(4, "Deregistering MEM driver");
1859  GDALDeregisterDriver(drv);
1860  }
1861 
1862  width = rt_raster_get_width(raster);
1863  height = rt_raster_get_height(raster);
1864  ds = GDALCreate(
1865  drv, "",
1866  width, height,
1867  0, GDT_Byte, NULL
1868  );
1869  if (NULL == ds) {
1870  rterror("rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1871  return 0;
1872  }
1873 
1874  /* add geotransform */
1876  cplerr = GDALSetGeoTransform(ds, gt);
1877  if (cplerr != CE_None) {
1878  rterror("rt_raster_to_gdal_mem: Could not set geotransformation");
1879  GDALClose(ds);
1880  return 0;
1881  }
1882 
1883  /* set spatial reference */
1884  if (NULL != srs && strlen(srs)) {
1885  char *_srs = rt_util_gdal_convert_sr(srs, 0);
1886  if (_srs == NULL) {
1887  rterror("rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1888  GDALClose(ds);
1889  return 0;
1890  }
1891 
1892  cplerr = GDALSetProjection(ds, _srs);
1893  CPLFree(_srs);
1894  if (cplerr != CE_None) {
1895  rterror("rt_raster_to_gdal_mem: Could not set projection");
1896  GDALClose(ds);
1897  return 0;
1898  }
1899  RASTER_DEBUGF(3, "Projection set to: %s", GDALGetProjectionRef(ds));
1900  }
1901 
1902  /* process bandNums */
1903  numBands = rt_raster_get_num_bands(raster);
1904  if (NULL != bandNums && count > 0) {
1905  for (i = 0; i < count; i++) {
1906  if (bandNums[i] >= numBands) {
1907  rterror("rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1908  GDALClose(ds);
1909  return 0;
1910  }
1911  }
1912  }
1913  else {
1914  count = numBands;
1915  bandNums = (uint32_t *) rtalloc(sizeof(uint32_t) * count);
1916  if (NULL == bandNums) {
1917  rterror("rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1918  GDALClose(ds);
1919  return 0;
1920  }
1921  allocBandNums = 1;
1922  for (i = 0; i < count; i++) bandNums[i] = i;
1923  }
1924 
1925  /* process exclude_nodata_values */
1926  if (NULL == excludeNodataValues) {
1927  excludeNodataValues = (int *) rtalloc(sizeof(int) * count);
1928  if (NULL == excludeNodataValues) {
1929  rterror("rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1930  GDALClose(ds);
1931  return 0;
1932  }
1933  allocNodataValues = 1;
1934  for (i = 0; i < count; i++) excludeNodataValues[i] = 1;
1935  }
1936 
1937  /* add band(s) */
1938  for (i = 0; i < count; i++) {
1939  rtband = rt_raster_get_band(raster, bandNums[i]);
1940  if (NULL == rtband) {
1941  rterror("rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1942  if (allocBandNums) rtdealloc(bandNums);
1943  if (allocNodataValues) rtdealloc(excludeNodataValues);
1944  GDALClose(ds);
1945  return 0;
1946  }
1947 
1948  pt = rt_band_get_pixtype(rtband);
1949  gdal_pt = rt_util_pixtype_to_gdal_datatype(pt);
1950  if (gdal_pt == GDT_Unknown)
1951  rtwarn("rt_raster_to_gdal_mem: Unknown pixel type for band");
1952 
1953  /*
1954  For all pixel types other than PT_8BSI, set pointer to start of data
1955  */
1956  if (pt != PT_8BSI) {
1957  pVoid = rt_band_get_data(rtband);
1958  RASTER_DEBUGF(4, "Band data is at pos %p", pVoid);
1959 
1960  pszDataPointer = (char *) rtalloc(20 * sizeof (char));
1961  sprintf(pszDataPointer, "%p", pVoid);
1962  RASTER_DEBUGF(4, "rt_raster_to_gdal_mem: szDatapointer is %p",
1963  pszDataPointer);
1964 
1965  if (strncasecmp(pszDataPointer, "0x", 2) == 0)
1966  sprintf(szGDALOption, "DATAPOINTER=%s", pszDataPointer);
1967  else
1968  sprintf(szGDALOption, "DATAPOINTER=0x%s", pszDataPointer);
1969 
1970  RASTER_DEBUG(3, "Storing info for GDAL MEM raster band");
1971 
1972  apszOptions[0] = szGDALOption;
1973  apszOptions[1] = NULL; /* pixel offset, not needed */
1974  apszOptions[2] = NULL; /* line offset, not needed */
1975  apszOptions[3] = NULL;
1976 
1977  /* free */
1978  rtdealloc(pszDataPointer);
1979 
1980  /* add band */
1981  if (GDALAddBand(ds, gdal_pt, apszOptions) == CE_Failure) {
1982  rterror("rt_raster_to_gdal_mem: Could not add GDAL raster band");
1983  if (allocBandNums) rtdealloc(bandNums);
1984  GDALClose(ds);
1985  return 0;
1986  }
1987  }
1988  /*
1989  PT_8BSI is special as GDAL has no equivalent pixel type.
1990  Must convert 8BSI to 16BSI so create basic band
1991  */
1992  else {
1993  /* add band */
1994  if (GDALAddBand(ds, gdal_pt, NULL) == CE_Failure) {
1995  rterror("rt_raster_to_gdal_mem: Could not add GDAL raster band");
1996  if (allocBandNums) rtdealloc(bandNums);
1997  if (allocNodataValues) rtdealloc(excludeNodataValues);
1998  GDALClose(ds);
1999  return 0;
2000  }
2001  }
2002 
2003  /* check band count */
2004  if (GDALGetRasterCount(ds) != i + 1) {
2005  rterror("rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2006  if (allocBandNums) rtdealloc(bandNums);
2007  if (allocNodataValues) rtdealloc(excludeNodataValues);
2008  GDALClose(ds);
2009  return 0;
2010  }
2011 
2012  /* get new band */
2013  band = NULL;
2014  band = GDALGetRasterBand(ds, i + 1);
2015  if (NULL == band) {
2016  rterror("rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2017  if (allocBandNums) rtdealloc(bandNums);
2018  if (allocNodataValues) rtdealloc(excludeNodataValues);
2019  GDALClose(ds);
2020  return 0;
2021  }
2022 
2023  /* PT_8BSI requires manual setting of pixels */
2024  if (pt == PT_8BSI) {
2025  uint32_t nXBlocks, nYBlocks;
2026  int nXBlockSize, nYBlockSize;
2027  uint32_t iXBlock, iYBlock;
2028  int nXValid, nYValid;
2029  int iX, iY;
2030  int iXMax, iYMax;
2031 
2032  int x, y, z;
2033  uint32_t valueslen = 0;
2034  int16_t *values = NULL;
2035  double value = 0.;
2036 
2037  /* this makes use of GDAL's "natural" blocks */
2038  GDALGetBlockSize(band, &nXBlockSize, &nYBlockSize);
2039  nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2040  nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2041  RASTER_DEBUGF(4, "(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2042  RASTER_DEBUGF(4, "(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2043 
2044  /* length is for the desired pixel type */
2045  valueslen = rt_pixtype_size(PT_16BSI) * nXBlockSize * nYBlockSize;
2046  values = rtalloc(valueslen);
2047  if (NULL == values) {
2048  rterror("rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2049  if (allocBandNums) rtdealloc(bandNums);
2050  if (allocNodataValues) rtdealloc(excludeNodataValues);
2051  GDALClose(ds);
2052  return 0;
2053  }
2054 
2055  for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2056  for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2057  memset(values, 0, valueslen);
2058 
2059  x = iXBlock * nXBlockSize;
2060  y = iYBlock * nYBlockSize;
2061  RASTER_DEBUGF(4, "(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2062  RASTER_DEBUGF(4, "(x, y) = (%d, %d)", x, y);
2063 
2064  /* valid block width */
2065  if ((iXBlock + 1) * nXBlockSize > width)
2066  nXValid = width - (iXBlock * nXBlockSize);
2067  else
2068  nXValid = nXBlockSize;
2069 
2070  /* valid block height */
2071  if ((iYBlock + 1) * nYBlockSize > height)
2072  nYValid = height - (iYBlock * nYBlockSize);
2073  else
2074  nYValid = nYBlockSize;
2075 
2076  RASTER_DEBUGF(4, "(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2077 
2078  /* convert 8BSI values to 16BSI */
2079  z = 0;
2080  iYMax = y + nYValid;
2081  iXMax = x + nXValid;
2082  for (iY = y; iY < iYMax; iY++) {
2083  for (iX = x; iX < iXMax; iX++) {
2084  if (rt_band_get_pixel(rtband, iX, iY, &value, NULL) != ES_NONE) {
2085  rterror("rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2086  rtdealloc(values);
2087  if (allocBandNums) rtdealloc(bandNums);
2088  if (allocNodataValues) rtdealloc(excludeNodataValues);
2089  GDALClose(ds);
2090  return 0;
2091  }
2092 
2093  values[z++] = rt_util_clamp_to_16BSI(value);
2094  }
2095  }
2096 
2097  /* burn values */
2098  if (GDALRasterIO(
2099  band, GF_Write,
2100  x, y,
2101  nXValid, nYValid,
2102  values, nXValid, nYValid,
2103  gdal_pt,
2104  0, 0
2105  ) != CE_None) {
2106  rterror("rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2107  rtdealloc(values);
2108  if (allocBandNums) rtdealloc(bandNums);
2109  if (allocNodataValues) rtdealloc(excludeNodataValues);
2110  GDALClose(ds);
2111  return 0;
2112  }
2113  }
2114  }
2115 
2116  rtdealloc(values);
2117  }
2118 
2119  /* Add nodata value for band */
2120  if (rt_band_get_hasnodata_flag(rtband) != FALSE && excludeNodataValues[i]) {
2121  rt_band_get_nodata(rtband, &nodata);
2122  if (GDALSetRasterNoDataValue(band, nodata) != CE_None)
2123  rtwarn("rt_raster_to_gdal_mem: Could not set nodata value for band");
2124  RASTER_DEBUGF(3, "nodata value set to %f", GDALGetRasterNoDataValue(band, NULL));
2125  }
2126 
2127 #if POSTGIS_DEBUG_LEVEL > 3
2128  {
2129  GDALRasterBandH _grb = NULL;
2130  double _min;
2131  double _max;
2132  double _mean;
2133  double _stddev;
2134 
2135  _grb = GDALGetRasterBand(ds, i + 1);
2136  GDALComputeRasterStatistics(_grb, FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2137  RASTER_DEBUGF(4, "GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2138  }
2139 #endif
2140 
2141  }
2142 
2143  /* necessary??? */
2144  GDALFlushCache(ds);
2145 
2146  if (allocBandNums) rtdealloc(bandNums);
2147  if (allocNodataValues) rtdealloc(excludeNodataValues);
2148 
2149  return ds;
2150 }
#define FALSE
Definition: dbfopen.c:72
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:299
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
Definition: rt_util.c:214
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:674
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition: rt_band.c:1221
rt_pixtype
Definition: librtcore.h:185
@ PT_END
Definition: librtcore.h:197
@ PT_8BSI
Definition: librtcore.h:189
@ PT_16BSI
Definition: librtcore.h:191
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
Definition: rt_util.c:120
void rtwarn(const char *fmt,...)
Definition: rt_context.c:224
@ ES_NONE
Definition: librtcore.h:180
int16_t rt_util_clamp_to_16BSI(double value)
Definition: rt_util.c:59
int rt_util_gdal_driver_registered(const char *drv)
Definition: rt_util.c:357
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1730
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:631
void rtdealloc(void *mem)
Definition: rt_context.c:186
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
Definition: rt_band.c:400
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_pixel.c:39
int value
Definition: genraster.py:62
int count
Definition: genraster.py:57
band
Definition: ovdump.py:58
ds
Definition: pixval.py:67
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
gt
Definition: window.py:78
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition: rt_raster.c:706

References ovdump::band, genraster::count, pixval::ds, ES_NONE, FALSE, window::gt, PT_16BSI, PT_8BSI, PT_END, rtrowdump::raster, RASTER_DEBUG, RASTER_DEBUGF, rt_band_get_data(), rt_band_get_hasnodata_flag(), rt_band_get_nodata(), rt_band_get_pixel(), rt_band_get_pixtype(), rt_pixtype_size(), rt_raster_get_band(), rt_raster_get_geotransform_matrix(), rt_raster_get_height(), rt_raster_get_num_bands(), rt_raster_get_width(), rt_util_clamp_to_16BSI(), rt_util_gdal_convert_sr(), rt_util_gdal_driver_registered(), rt_util_pixtype_to_gdal_datatype(), rtalloc(), rtdealloc(), rterror(), rtwarn(), genraster::value, pixval::x, and pixval::y.

Referenced by rt_raster_gdal_polygonize(), rt_raster_gdal_warp(), rt_raster_to_gdal(), and test_gdal_to_raster().

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