Return GDAL dataset using GDAL MEM driver from raster.
1834 GDALDriverH drv = NULL;
1835 GDALDatasetH
ds = NULL;
1836 double gt[6] = {0.0};
1838 GDALDataType gdal_pt = GDT_Unknown;
1839 GDALRasterBandH
band;
1841 char *pszDataPointer;
1842 char szGDALOption[50];
1843 char *apszOptions[4];
1844 double nodata = 0.0;
1845 int allocBandNums = 0;
1846 int allocNodataValues = 0;
1856 assert(NULL != rtn_drv);
1857 assert(NULL != destroy_rtn_drv);
1859 *destroy_rtn_drv = 0;
1865 *destroy_rtn_drv = 1;
1867 drv = GDALGetDriverByName(
"MEM");
1869 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1875 if (*destroy_rtn_drv) {
1877 GDALDeregisterDriver(drv);
1888 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1894 cplerr = GDALSetGeoTransform(
ds,
gt);
1895 if (cplerr != CE_None) {
1896 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1902 if (NULL != srs && strlen(srs)) {
1905 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1910 cplerr = GDALSetProjection(
ds, _srs);
1912 if (cplerr != CE_None) {
1913 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1922 if (NULL != bandNums &&
count > 0) {
1923 for (i = 0; i <
count; i++) {
1924 if (bandNums[i] >= numBands) {
1925 rterror(
"rt_raster_to_gdal_mem: The band index %d is invalid", bandNums[i]);
1934 if (NULL == bandNums) {
1935 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for band indices");
1940 for (i = 0; i <
count; i++) bandNums[i] = i;
1944 if (NULL == excludeNodataValues) {
1945 excludeNodataValues = (
int *)
rtalloc(
sizeof(
int) *
count);
1946 if (NULL == excludeNodataValues) {
1947 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for NODATA flags");
1951 allocNodataValues = 1;
1952 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1956 for (i = 0; i <
count; i++) {
1958 if (NULL == rtband) {
1959 rterror(
"rt_raster_to_gdal_mem: Could not get requested band index %d", bandNums[i]);
1961 if (allocNodataValues)
rtdealloc(excludeNodataValues);
1968 if (gdal_pt == GDT_Unknown)
1969 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1978 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1979 sprintf(pszDataPointer,
"%p", pVoid);
1980 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1983 if (strncasecmp(pszDataPointer,
"0x", 2) == 0)
1984 sprintf(szGDALOption,
"DATAPOINTER=%s", pszDataPointer);
1986 sprintf(szGDALOption,
"DATAPOINTER=0x%s", pszDataPointer);
1988 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1990 apszOptions[0] = szGDALOption;
1991 apszOptions[1] = NULL;
1992 apszOptions[2] = NULL;
1993 apszOptions[3] = NULL;
1999 if (GDALAddBand(
ds, gdal_pt, apszOptions) == CE_Failure) {
2000 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2012 if (GDALAddBand(
ds, gdal_pt, NULL) == CE_Failure) {
2013 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
2015 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2022 if (GDALGetRasterCount(
ds) != i + 1) {
2023 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2025 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2032 band = GDALGetRasterBand(
ds, i + 1);
2034 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2036 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2044 int nXBlockSize, nYBlockSize;
2046 int nXValid, nYValid;
2052 int16_t *values = NULL;
2056 GDALGetBlockSize(
band, &nXBlockSize, &nYBlockSize);
2057 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2058 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2059 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2060 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2065 if (NULL == values) {
2066 rterror(
"rt_raster_to_gdal_mem: Could not allocate memory for GDAL band pixel values");
2068 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2073 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2074 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2075 memset(values, 0, valueslen);
2077 x = iXBlock * nXBlockSize;
2078 y = iYBlock * nYBlockSize;
2079 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2083 if ((iXBlock + 1) * nXBlockSize > width)
2084 nXValid = width - (iXBlock * nXBlockSize);
2086 nXValid = nXBlockSize;
2089 if ((iYBlock + 1) * nYBlockSize > height)
2090 nYValid = height - (iYBlock * nYBlockSize);
2092 nYValid = nYBlockSize;
2094 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2098 iYMax =
y + nYValid;
2099 iXMax =
x + nXValid;
2100 for (iY =
y; iY < iYMax; iY++) {
2101 for (iX =
x; iX < iXMax; iX++) {
2103 rterror(
"rt_raster_to_gdal_mem: Could not get pixel value to convert from 8BSI to 16BSI");
2106 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2120 values, nXValid, nYValid,
2124 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2127 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2140 if (GDALSetRasterNoDataValue(
band, nodata) != CE_None)
2141 rtwarn(
"rt_raster_to_gdal_mem: Could not set nodata value for band");
2142 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(
band, NULL));
2145 #if POSTGIS_DEBUG_LEVEL > 3
2147 GDALRasterBandH _grb = NULL;
2153 _grb = GDALGetRasterBand(
ds, i + 1);
2154 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2155 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i + 1, _min, _max, _mean, _stddev);
2165 if (allocNodataValues)
rtdealloc(excludeNodataValues);
void rterror(const char *fmt,...)
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,...)
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.
void rtwarn(const char *fmt,...)
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.