PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ 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 1821 of file rt_raster.c.

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

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: