Return GDAL dataset using GDAL MEM driver from raster.
1821 GDALDriverH drv = NULL;
1822 GDALDatasetH
ds = NULL;
1823 double gt[6] = {0.0};
1825 GDALDataType gdal_pt = GDT_Unknown;
1826 GDALRasterBandH
band;
1828 char *pszDataPointer;
1829 char szGDALOption[50];
1830 char *apszOptions[4];
1831 double nodata = 0.0;
1832 int allocBandNums = 0;
1833 int allocNodataValues = 0;
1838 uint32_t height = 0;
1843 assert(NULL != rtn_drv);
1844 assert(NULL != destroy_rtn_drv);
1846 *destroy_rtn_drv = 0;
1852 *destroy_rtn_drv = 1;
1854 drv = GDALGetDriverByName(
"MEM");
1856 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1862 if (*destroy_rtn_drv) {
1864 GDALDeregisterDriver(drv);
1875 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1881 cplerr = GDALSetGeoTransform(
ds,
gt);
1882 if (cplerr != CE_None) {
1883 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1889 if (NULL != srs && strlen(srs)) {
1892 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1897 cplerr = GDALSetProjection(
ds, _srs);
1899 if (cplerr != CE_None) {
1900 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1909 if (NULL != bandNums &&
count > 0) {
1910 for (i = 0; i <
count; i++) {
1911 if (bandNums[i] >= numBands) {
1912 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1920 bandNums = (uint32_t *)
rtalloc(
sizeof(uint32_t) *
count);
1921 if (NULL == bandNums) {
1922 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1927 for (i = 0; i <
count; i++) bandNums[i] = i;
1931 if (NULL == excludeNodataValues) {
1932 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
1933 if (NULL == excludeNodataValues) {
1934 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1938 allocNodataValues = 1;
1939 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1943 for (i = 0; i <
count; i++) {
1945 if (NULL == rtband) {
1946 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1948 if (allocNodataValues)
rtdealloc(excludeNodataValues);
1955 if (gdal_pt == GDT_Unknown)
1956 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1961 #if POSTGIS_GDAL_VERSION < 30700
1967 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1968 sprintf(pszDataPointer,
"%p", pVoid);
1969 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1972 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
1973 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
1975 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
1977 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1979 apszOptions[0] = szGDALOption;
1980 apszOptions[1] = NULL;
1981 apszOptions[2] = NULL;
1982 apszOptions[3] = NULL;
1988 if (GDALAddBand(
ds, gdal_pt, apszOptions) == CE_Failure) {
1989 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
1994 #if POSTGIS_GDAL_VERSION < 30700
2001 if (GDALAddBand(
ds, gdal_pt, NULL) == CE_Failure) {
2002 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2004 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2012 if (GDALGetRasterCount(
ds) != i + 1) {
2013 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2015 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2022 band = GDALGetRasterBand(
ds, i + 1);
2024 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2026 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2031 #if POSTGIS_GDAL_VERSION < 30700
2035 uint32_t nXBlocks, nYBlocks;
2036 int nXBlockSize, nYBlockSize;
2037 uint32_t iXBlock, iYBlock;
2038 int nXValid, nYValid;
2043 uint32_t valueslen = 0;
2044 int16_t *values = NULL;
2048 GDALGetBlockSize(
band, &nXBlockSize, &nYBlockSize);
2049 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2050 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2051 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2052 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2057 if (NULL == values) {
2058 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2060 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2065 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2066 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2067 memset(values, 0, valueslen);
2069 x = iXBlock * nXBlockSize;
2070 y = iYBlock * nYBlockSize;
2071 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2075 if ((iXBlock + 1) * nXBlockSize > width)
2076 nXValid = width - (iXBlock * nXBlockSize);
2078 nXValid = nXBlockSize;
2081 if ((iYBlock + 1) * nYBlockSize > height)
2082 nYValid = height - (iYBlock * nYBlockSize);
2084 nYValid = nYBlockSize;
2086 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2090 iYMax =
y + nYValid;
2091 iXMax =
x + nXValid;
2092 for (iY =
y; iY < iYMax; iY++) {
2093 for (iX =
x; iX < iXMax; iX++) {
2095 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2098 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2112 values, nXValid, nYValid,
2116 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2119 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2133 if (GDALSetRasterNoDataValue(
band, nodata) != CE_None)
2134 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2135 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(
band, NULL));
2138 #if POSTGIS_DEBUG_LEVEL > 3
2140 GDALRasterBandH _grb = NULL;
2146 _grb = GDALGetRasterBand(
ds, i + 1);
2147 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2148 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2158 if (allocNodataValues)
rtdealloc(excludeNodataValues);
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
int16_t rt_util_clamp_to_16BSI(double value)
int rt_util_gdal_driver_registered(const char *drv)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
void rtdealloc(void *mem)
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
uint16_t rt_raster_get_num_bands(rt_raster raster)
uint16_t rt_raster_get_height(rt_raster raster)
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
uint16_t rt_raster_get_width(rt_raster raster)
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.