Return a raster from a GDAL dataset.
2296 uint32_t height = 0;
2297 uint32_t numBands = 0;
2299 char *authname = NULL;
2300 char *authcode = NULL;
2302 GDALRasterBandH gdband = NULL;
2303 GDALDataType gdpixtype = GDT_Unknown;
2314 uint32_t nXBlocks, nYBlocks;
2315 int nXBlockSize, nYBlockSize;
2316 uint32_t iXBlock, iYBlock;
2317 uint32_t nXValid, nYValid;
2320 uint8_t *values = NULL;
2321 uint32_t valueslen = 0;
2322 uint8_t *ptr = NULL;
2327 width = GDALGetRasterXSize(
ds);
2328 height = GDALGetRasterYSize(
ds);
2329 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d", width, height);
2335 rterror(
"rt_raster_from_gdal_dataset: Out of memory allocating new raster");
2341 cplerr = GDALGetGeoTransform(
ds,
gt);
2342 if (GDALGetGeoTransform(
ds,
gt) != CE_None) {
2343 RASTER_DEBUG(4,
"Using default geotransform matrix (0, 1, 0, 0, 0, -1)");
2355 RASTER_DEBUGF(3,
"Raster geotransform (%f, %f, %f, %f, %f, %f)",
2362 strcmp(authname,
"EPSG") == 0 &&
2369 if (authname != NULL)
2371 if (authcode != NULL)
2375 numBands = GDALGetRasterCount(
ds);
2377 #if POSTGIS_DEBUG_LEVEL > 3
2378 for (i = 1; i <= numBands; i++) {
2379 GDALRasterBandH _grb = NULL;
2385 _grb = GDALGetRasterBand(
ds, i);
2386 GDALComputeRasterStatistics(_grb,
FALSE, &_min, &_max, &_mean, &_stddev, NULL, NULL);
2387 RASTER_DEBUGF(4,
"GDAL Band %d stats: %f, %f, %f, %f", i, _min, _max, _mean, _stddev);
2392 for (i = 1; i <= numBands; i++) {
2395 gdband = GDALGetRasterBand(
ds, i);
2396 if (NULL == gdband) {
2397 rterror(
"rt_raster_from_gdal_dataset: Could not get GDAL band");
2404 gdpixtype = GDALGetRasterDataType(gdband);
2405 RASTER_DEBUGF(4,
"gdpixtype, size = %s, %d", GDALGetDataTypeName(gdpixtype), GDALGetDataTypeSize(gdpixtype) / 8);
2408 rterror(
"rt_raster_from_gdal_dataset: Unknown pixel type for GDAL band");
2415 width = GDALGetRasterBandXSize(gdband);
2416 height = GDALGetRasterBandYSize(gdband);
2417 RASTER_DEBUGF(3,
"GDAL band dimensions (width x height): %d x %d", width, height);
2420 nodataval = GDALGetRasterNoDataValue(gdband, &hasnodata);
2421 RASTER_DEBUGF(3,
"(hasnodata, nodataval) = (%d, %f)", hasnodata, nodataval);
2426 (hasnodata ? nodataval : 0),
2430 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for raster band");
2438 GDALGetBlockSize(gdband, &nXBlockSize, &nYBlockSize);
2439 nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
2440 nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
2441 RASTER_DEBUGF(4,
"(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
2442 RASTER_DEBUGF(4,
"(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
2445 valueslen = ptlen * nXBlockSize * nYBlockSize;
2447 if (values == NULL) {
2448 rterror(
"rt_raster_from_gdal_dataset: Could not allocate memory for GDAL band pixel values");
2452 RASTER_DEBUGF(3,
"values @ %p of length = %d", values, valueslen);
2454 for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) {
2455 for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) {
2456 x = iXBlock * nXBlockSize;
2457 y = iYBlock * nYBlockSize;
2458 RASTER_DEBUGF(4,
"(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
2461 memset(values, 0, valueslen);
2464 if ((iXBlock + 1) * nXBlockSize > width)
2465 nXValid = width - (iXBlock * nXBlockSize);
2467 nXValid = nXBlockSize;
2470 if ((iYBlock + 1) * nYBlockSize > height)
2471 nYValid = height - (iYBlock * nYBlockSize);
2473 nYValid = nYBlockSize;
2475 RASTER_DEBUGF(4,
"(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2477 cplerr = GDALRasterIO(
2481 values, nXValid, nYValid,
2485 if (cplerr != CE_None) {
2486 rterror(
"rt_raster_from_gdal_dataset: Could not get data from GDAL raster");
2493 if (nXBlocks == 1 && nYBlockSize > 1 && nXValid == width) {
2495 y = nYBlockSize * iYBlock;
2497 RASTER_DEBUGF(4,
"Setting set of pixel lines at (%d, %d) for %d pixels",
x,
y, nXValid * nYValid);
2502 x = nXBlockSize * iXBlock;
2503 for (iY = 0; iY < nYValid; iY++) {
2504 y = iY + (nYBlockSize * iYBlock);
2506 RASTER_DEBUGF(4,
"Setting pixel line at (%d, %d) for %d pixels",
x,
y, nXValid);
2508 ptr += (nXValid * ptlen);
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
void rtdealloc(void *mem)
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
int rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype, double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
Generate a new inline band and add it to a raster.
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.