Return GDAL dataset using GDAL MEM driver from raster.
1817 GDALDriverH drv = NULL;
1818 GDALDatasetH
ds = NULL;
1819 double gt[6] = {0.0};
1821 GDALDataType gdal_pt = GDT_Unknown;
1822 GDALRasterBandH
band;
1824 char *pszDataPointer;
1825 char szGDALOption[50];
1826 char *apszOptions[4];
1827 double nodata = 0.0;
1828 int allocBandNums = 0;
1829 int allocNodataValues = 0;
1838 assert(NULL != raster);
1839 assert(NULL != rtn_drv);
1840 assert(NULL != destroy_rtn_drv);
1842 *destroy_rtn_drv = 0;
1848 *destroy_rtn_drv = 1;
1850 drv = GDALGetDriverByName(
"MEM");
1852 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1858 if (*destroy_rtn_drv) {
1860 GDALDeregisterDriver(drv);
1871 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1877 cplerr = GDALSetGeoTransform(ds, gt);
1878 if (cplerr != CE_None) {
1879 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1885 if (NULL != srs && strlen(srs)) {
1888 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1893 cplerr = GDALSetProjection(ds, _srs);
1895 if (cplerr != CE_None) {
1896 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1900 RASTER_DEBUGF(3,
"Projection set to: %s", GDALGetProjectionRef(ds));
1905 if (NULL != bandNums &&
count > 0) {
1906 for (i = 0; i <
count; i++) {
1907 if (bandNums[i] >= numBands) {
1908 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1917 if (NULL == bandNums) {
1918 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1923 for (i = 0; i <
count; i++) bandNums[i] = i;
1927 if (NULL == excludeNodataValues) {
1928 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
1929 if (NULL == excludeNodataValues) {
1930 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1934 allocNodataValues = 1;
1935 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1939 for (i = 0; i <
count; i++) {
1941 if (NULL == rtband) {
1942 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1944 if (allocNodataValues)
rtdealloc(excludeNodataValues);
1951 if (gdal_pt == GDT_Unknown)
1952 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1961 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1962 sprintf(pszDataPointer,
"%p", pVoid);
1963 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1966 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
1967 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
1969 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
1971 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1973 apszOptions[0] = szGDALOption;
1974 apszOptions[1] = NULL;
1975 apszOptions[2] = NULL;
1976 apszOptions[3] = NULL;
1982 if (GDALAddBand(ds, gdal_pt, apszOptions) == CE_Failure) {
1983 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
1995 if (GDALAddBand(ds, gdal_pt, NULL) == CE_Failure) {
1996 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
1998 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2005 if (GDALGetRasterCount(ds) != i + 1) {
2006 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2008 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2015 band = GDALGetRasterBand(ds, i + 1);
2017 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2019 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2026 int nXBlocks, nYBlocks;
2027 int nXBlockSize, nYBlockSize;
2028 int iXBlock, iYBlock;
2029 int nXValid, nYValid;
2035 int16_t *values = NULL;
2039 GDALGetBlockSize(band, &nXBlockSize, &nYBlockSize);
2040 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2041 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2042 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2043 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2048 if (NULL == values) {
2049 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2051 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2056 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2057 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2058 memset(values, 0, valueslen);
2060 x = iXBlock * nXBlockSize;
2061 y = iYBlock * nYBlockSize;
2062 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2066 if ((iXBlock + 1) * nXBlockSize > width)
2067 nXValid = width - (iXBlock * nXBlockSize);
2069 nXValid = nXBlockSize;
2072 if ((iYBlock + 1) * nYBlockSize >
height)
2073 nYValid = height - (iYBlock * nYBlockSize);
2075 nYValid = nYBlockSize;
2077 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2081 iYMax = y + nYValid;
2082 iXMax = x + nXValid;
2083 for (iY = y; iY < iYMax; iY++) {
2084 for (iX = x; iX < iXMax; iX++) {
2086 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2089 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2103 values, nXValid, nYValid,
2107 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2110 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2123 if (GDALSetRasterNoDataValue(band, nodata) != CE_None)
2124 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2125 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(band, NULL));
2128 #if POSTGIS_DEBUG_LEVEL > 3 2130 GDALRasterBandH _grb = NULL;
2136 _grb = GDALGetRasterBand(ds, i + 1);
2137 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2138 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2148 if (allocNodataValues)
rtdealloc(excludeNodataValues);
int rt_raster_get_num_bands(rt_raster raster)
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
int rt_util_gdal_driver_registered(const char *drv)
void rtwarn(const char *fmt,...)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
#define RASTER_DEBUGF(level, msg,...)
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
void rtdealloc(void *mem)
#define RASTER_DEBUG(level, msg)
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
uint16_t rt_raster_get_height(rt_raster raster)
uint16_t rt_raster_get_width(rt_raster raster)
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
int16_t rt_util_clamp_to_16BSI(double value)