Return GDAL dataset using GDAL MEM driver from raster.
1820 {
1821 GDALDriverH drv = NULL;
1822 GDALDatasetH
ds = NULL;
1823 double gt[6] = {0.0};
1824 CPLErr cplerr;
1825 GDALDataType gdal_pt = GDT_Unknown;
1826 GDALRasterBandH
band;
1827 void *pVoid;
1828 char *pszDataPointer;
1829 char szGDALOption[50];
1830 char *apszOptions[4];
1831 double nodata = 0.0;
1832 int allocBandNums = 0;
1833 int allocNodataValues = 0;
1834
1835 int i;
1836 uint32_t numBands;
1837 uint32_t width = 0;
1838 uint32_t height = 0;
1841
1842 assert(NULL != raster);
1843 assert(NULL != rtn_drv);
1844 assert(NULL != destroy_rtn_drv);
1845
1846 *destroy_rtn_drv = 0;
1847
1848
1851 GDALRegister_MEM();
1852 *destroy_rtn_drv = 1;
1853 }
1854 drv = GDALGetDriverByName("MEM");
1855 if (NULL == drv) {
1856 rterror(
"rt_raster_to_gdal_mem: Could not load the MEM GDAL driver");
1857 return 0;
1858 }
1859 *rtn_drv = drv;
1860
1861
1862 if (*destroy_rtn_drv) {
1864 GDALDeregisterDriver(drv);
1865 }
1866
1870 drv, "",
1871 width, height,
1872 0, GDT_Byte, NULL
1873 );
1874 if (NULL == ds) {
1875 rterror(
"rt_raster_to_gdal_mem: Could not create a GDALDataset to convert into");
1876 return 0;
1877 }
1878
1879
1881 cplerr = GDALSetGeoTransform(ds, gt);
1882 if (cplerr != CE_None) {
1883 rterror(
"rt_raster_to_gdal_mem: Could not set geotransformation");
1884 GDALClose(ds);
1885 return 0;
1886 }
1887
1888
1889 if (NULL != srs && strlen(srs)) {
1891 if (_srs == NULL) {
1892 rterror(
"rt_raster_to_gdal_mem: Could not convert srs to GDAL accepted format");
1893 GDALClose(ds);
1894 return 0;
1895 }
1896
1897 cplerr = GDALSetProjection(ds, _srs);
1898 CPLFree(_srs);
1899 if (cplerr != CE_None) {
1900 rterror(
"rt_raster_to_gdal_mem: Could not set projection");
1901 GDALClose(ds);
1902 return 0;
1903 }
1904 RASTER_DEBUGF(3,
"Projection set to: %s", GDALGetProjectionRef(ds));
1905 }
1906
1907
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]);
1913 GDALClose(ds);
1914 return 0;
1915 }
1916 }
1917 }
1918 else {
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");
1923 GDALClose(ds);
1924 return 0;
1925 }
1926 allocBandNums = 1;
1927 for (i = 0; i <
count; i++) bandNums[i] = i;
1928 }
1929
1930
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");
1935 GDALClose(ds);
1936 return 0;
1937 }
1938 allocNodataValues = 1;
1939 for (i = 0; i <
count; i++) excludeNodataValues[i] = 1;
1940 }
1941
1942
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);
1949 GDALClose(ds);
1950 return 0;
1951 }
1952
1955 if (gdal_pt == GDT_Unknown)
1956 rtwarn(
"rt_raster_to_gdal_mem: Unknown pixel type for band");
1957
1958
1959
1960
1961#if POSTGIS_GDAL_VERSION < 30700
1963#endif
1966
1967 pszDataPointer = (
char *)
rtalloc(20 *
sizeof (
char));
1968 sprintf(pszDataPointer, "%p", pVoid);
1969 RASTER_DEBUGF(4,
"rt_raster_to_gdal_mem: szDatapointer is %p",
1970 pszDataPointer);
1971
1972 if (strncasecmp(pszDataPointer, "0x", 2) == 0)
1973 sprintf(szGDALOption, "DATAPOINTER=%s", pszDataPointer);
1974 else
1975 sprintf(szGDALOption, "DATAPOINTER=0x%s", pszDataPointer);
1976
1977 RASTER_DEBUG(3,
"Storing info for GDAL MEM raster band");
1978
1979 apszOptions[0] = szGDALOption;
1980 apszOptions[1] = NULL;
1981 apszOptions[2] = NULL;
1982 apszOptions[3] = NULL;
1983
1984
1986
1987
1988 if (GDALAddBand(ds, gdal_pt, apszOptions) == CE_Failure) {
1989 rterror(
"rt_raster_to_gdal_mem: Could not add GDAL raster band");
1991 GDALClose(ds);
1992 return 0;
1993 }
1994#if POSTGIS_GDAL_VERSION < 30700
1995
1996
1997
1998
1999 } else {
2000
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);
2005 GDALClose(ds);
2006 return 0;
2007 }
2008 }
2009#endif
2010
2011
2012 if (GDALGetRasterCount(ds) != i + 1) {
2013 rterror(
"rt_raster_to_gdal_mem: Error creating GDAL MEM raster band");
2015 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2016 GDALClose(ds);
2017 return 0;
2018 }
2019
2020
2022 band = GDALGetRasterBand(ds, i + 1);
2023 if (NULL == band) {
2024 rterror(
"rt_raster_to_gdal_mem: Could not get GDAL band for additional processing");
2026 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2027 GDALClose(ds);
2028 return 0;
2029 }
2030
2031#if POSTGIS_GDAL_VERSION < 30700
2032
2033
2035 uint32_t nXBlocks, nYBlocks;
2036 int nXBlockSize, nYBlockSize;
2037 uint32_t iXBlock, iYBlock;
2038 int nXValid, nYValid;
2039 int iX, iY;
2040 int iXMax, iYMax;
2041
2043 uint32_t valueslen = 0;
2044 int16_t *values = NULL;
2046
2047
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);
2053
2054
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);
2061 GDALClose(ds);
2062 return 0;
2063 }
2064
2065 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2066 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2067 memset(values, 0, valueslen);
2068
2069 x = iXBlock * nXBlockSize;
2070 y = iYBlock * nYBlockSize;
2071 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2073
2074
2075 if ((iXBlock + 1) * nXBlockSize > width)
2076 nXValid = width - (iXBlock * nXBlockSize);
2077 else
2078 nXValid = nXBlockSize;
2079
2080
2081 if ((iYBlock + 1) * nYBlockSize > height)
2082 nYValid = height - (iYBlock * nYBlockSize);
2083 else
2084 nYValid = nYBlockSize;
2085
2086 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2087
2088
2089 z = 0;
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);
2099 GDALClose(ds);
2100 return 0;
2101 }
2102
2104 }
2105 }
2106
2107
2108 if (GDALRasterIO(
2109 band, GF_Write,
2110 x, y,
2111 nXValid, nYValid,
2112 values, nXValid, nYValid,
2113 gdal_pt,
2114 0, 0
2115 ) != CE_None) {
2116 rterror(
"rt_raster_to_gdal_mem: Could not write converted 8BSI to 16BSI values to GDAL band");
2119 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2120 GDALClose(ds);
2121 return 0;
2122 }
2123 }
2124 }
2125
2127 }
2128#endif
2129
2130
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));
2136 }
2137
2138#if POSTGIS_DEBUG_LEVEL > 3
2139 {
2140 GDALRasterBandH _grb = NULL;
2141 double _min;
2142 double _max;
2143 double _mean;
2144 double _stddev;
2145
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);
2149 }
2150#endif
2151
2152 }
2153
2154
2155 GDALFlushCache(ds);
2156
2158 if (allocNodataValues)
rtdealloc(excludeNodataValues);
2159
2161}
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
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.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
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)
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
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.