PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ rt_raster_from_gdal_dataset()

rt_raster rt_raster_from_gdal_dataset ( GDALDatasetH  ds)

Return a raster from a GDAL dataset.

Parameters
ds: the GDAL dataset to convert to a raster
Returns
raster or NULL

Definition at line 2291 of file rt_raster.c.

2291  {
2292  rt_raster rast = NULL;
2293  double gt[6] = {0};
2294  CPLErr cplerr;
2295  uint32_t width = 0;
2296  uint32_t height = 0;
2297  uint32_t numBands = 0;
2298  uint32_t i = 0;
2299  char *authname = NULL;
2300  char *authcode = NULL;
2301 
2302  GDALRasterBandH gdband = NULL;
2303  GDALDataType gdpixtype = GDT_Unknown;
2304  rt_band band;
2305  int32_t idx;
2306  rt_pixtype pt = PT_END;
2307  uint32_t ptlen = 0;
2308  int hasnodata = 0;
2309  double nodataval;
2310 
2311  int x;
2312  int y;
2313 
2314  uint32_t nXBlocks, nYBlocks;
2315  int nXBlockSize, nYBlockSize;
2316  uint32_t iXBlock, iYBlock;
2317  uint32_t nXValid, nYValid;
2318  uint32_t iY;
2319 
2320  uint8_t *values = NULL;
2321  uint32_t valueslen = 0;
2322  uint8_t *ptr = NULL;
2323 
2324  assert(NULL != ds);
2325 
2326  /* raster size */
2327  width = GDALGetRasterXSize(ds);
2328  height = GDALGetRasterYSize(ds);
2329  RASTER_DEBUGF(3, "Raster dimensions (width x height): %d x %d", width, height);
2330 
2331  /* create new raster */
2332  RASTER_DEBUG(3, "Creating new raster");
2333  rast = rt_raster_new(width, height);
2334  if (NULL == rast) {
2335  rterror("rt_raster_from_gdal_dataset: Out of memory allocating new raster");
2336  return NULL;
2337  }
2338  RASTER_DEBUGF(3, "Created raster dimensions (width x height): %d x %d", rast->width, rast->height);
2339 
2340  /* get raster attributes */
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)");
2344  gt[0] = 0;
2345  gt[1] = 1;
2346  gt[2] = 0;
2347  gt[3] = 0;
2348  gt[4] = 0;
2349  gt[5] = -1;
2350  }
2351 
2352  /* apply raster attributes */
2354 
2355  RASTER_DEBUGF(3, "Raster geotransform (%f, %f, %f, %f, %f, %f)",
2356  gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]);
2357 
2358  /* srid */
2359  if (rt_util_gdal_sr_auth_info(ds, &authname, &authcode) == ES_NONE) {
2360  if (
2361  authname != NULL &&
2362  strcmp(authname, "EPSG") == 0 &&
2363  authcode != NULL
2364  ) {
2365  rt_raster_set_srid(rast, atoi(authcode));
2366  RASTER_DEBUGF(3, "New raster's SRID = %d", rast->srid);
2367  }
2368 
2369  if (authname != NULL)
2370  rtdealloc(authname);
2371  if (authcode != NULL)
2372  rtdealloc(authcode);
2373  }
2374 
2375  numBands = GDALGetRasterCount(ds);
2376 
2377 #if POSTGIS_DEBUG_LEVEL > 3
2378  for (i = 1; i <= numBands; i++) {
2379  GDALRasterBandH _grb = NULL;
2380  double _min;
2381  double _max;
2382  double _mean;
2383  double _stddev;
2384 
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);
2388  }
2389 #endif
2390 
2391  /* copy bands */
2392  for (i = 1; i <= numBands; i++) {
2393  RASTER_DEBUGF(3, "Processing band %d of %d", i, numBands);
2394  gdband = NULL;
2395  gdband = GDALGetRasterBand(ds, i);
2396  if (NULL == gdband) {
2397  rterror("rt_raster_from_gdal_dataset: Could not get GDAL band");
2399  return NULL;
2400  }
2401  RASTER_DEBUGF(4, "gdband @ %p", gdband);
2402 
2403  /* pixtype */
2404  gdpixtype = GDALGetRasterDataType(gdband);
2405  RASTER_DEBUGF(4, "gdpixtype, size = %s, %d", GDALGetDataTypeName(gdpixtype), GDALGetDataTypeSize(gdpixtype) / 8);
2406  pt = rt_util_gdal_datatype_to_pixtype(gdpixtype);
2407  if (pt == PT_END) {
2408  rterror("rt_raster_from_gdal_dataset: Unknown pixel type for GDAL band");
2410  return NULL;
2411  }
2412  ptlen = rt_pixtype_size(pt);
2413 
2414  /* size: width and height */
2415  width = GDALGetRasterBandXSize(gdband);
2416  height = GDALGetRasterBandYSize(gdband);
2417  RASTER_DEBUGF(3, "GDAL band dimensions (width x height): %d x %d", width, height);
2418 
2419  /* nodata */
2420  nodataval = GDALGetRasterNoDataValue(gdband, &hasnodata);
2421  RASTER_DEBUGF(3, "(hasnodata, nodataval) = (%d, %f)", hasnodata, nodataval);
2422 
2423  /* create band object */
2425  rast, pt,
2426  (hasnodata ? nodataval : 0),
2427  hasnodata, nodataval, rt_raster_get_num_bands(rast)
2428  );
2429  if (idx < 0) {
2430  rterror("rt_raster_from_gdal_dataset: Could not allocate memory for raster band");
2432  return NULL;
2433  }
2434  band = rt_raster_get_band(rast, idx);
2435  RASTER_DEBUGF(3, "Created band of dimension (width x height): %d x %d", band->width, band->height);
2436 
2437  /* this makes use of GDAL's "natural" blocks */
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);
2443 
2444  /* allocate memory for values */
2445  valueslen = ptlen * nXBlockSize * nYBlockSize;
2446  values = rtalloc(valueslen);
2447  if (values == NULL) {
2448  rterror("rt_raster_from_gdal_dataset: Could not allocate memory for GDAL band pixel values");
2450  return NULL;
2451  }
2452  RASTER_DEBUGF(3, "values @ %p of length = %d", values, valueslen);
2453 
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);
2459  RASTER_DEBUGF(4, "(x, y) = (%d, %d)", x, y);
2460 
2461  memset(values, 0, valueslen);
2462 
2463  /* valid block width */
2464  if ((iXBlock + 1) * nXBlockSize > width)
2465  nXValid = width - (iXBlock * nXBlockSize);
2466  else
2467  nXValid = nXBlockSize;
2468 
2469  /* valid block height */
2470  if ((iYBlock + 1) * nYBlockSize > height)
2471  nYValid = height - (iYBlock * nYBlockSize);
2472  else
2473  nYValid = nYBlockSize;
2474 
2475  RASTER_DEBUGF(4, "(nXValid, nYValid) = (%d, %d)", nXValid, nYValid);
2476 
2477  cplerr = GDALRasterIO(
2478  gdband, GF_Read,
2479  x, y,
2480  nXValid, nYValid,
2481  values, nXValid, nYValid,
2482  gdpixtype,
2483  0, 0
2484  );
2485  if (cplerr != CE_None) {
2486  rterror("rt_raster_from_gdal_dataset: Could not get data from GDAL raster");
2487  rtdealloc(values);
2489  return NULL;
2490  }
2491 
2492  /* if block width is same as raster width, shortcut */
2493  if (nXBlocks == 1 && nYBlockSize > 1 && nXValid == width) {
2494  x = 0;
2495  y = nYBlockSize * iYBlock;
2496 
2497  RASTER_DEBUGF(4, "Setting set of pixel lines at (%d, %d) for %d pixels", x, y, nXValid * nYValid);
2498  rt_band_set_pixel_line(band, x, y, values, nXValid * nYValid);
2499  }
2500  else {
2501  ptr = values;
2502  x = nXBlockSize * iXBlock;
2503  for (iY = 0; iY < nYValid; iY++) {
2504  y = iY + (nYBlockSize * iYBlock);
2505 
2506  RASTER_DEBUGF(4, "Setting pixel line at (%d, %d) for %d pixels", x, y, nXValid);
2507  rt_band_set_pixel_line(band, x, y, ptr, nXValid);
2508  ptr += (nXValid * ptlen);
2509  }
2510  }
2511  }
2512  }
2513 
2514  /* free memory */
2515  rtdealloc(values);
2516  }
2517 
2518  return rast;
2519 }
#define FALSE
Definition: dbfopen.c:72
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:219
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:191
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
Definition: rt_util.c:159
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:302
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:306
rt_pixtype
Definition: librtcore.h:187
@ PT_END
Definition: librtcore.h:199
@ ES_NONE
Definition: librtcore.h:182
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
Definition: rt_band.c:853
void rtdealloc(void *mem)
Definition: rt_context.c:206
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
Definition: rt_util.c:276
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_pixel.c:39
band
Definition: ovdump.py:58
ds
Definition: pixval.py:67
gt
Definition: window.py:78
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.
Definition: rt_raster.c:489
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
Definition: rt_raster.c:731
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:52
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:376
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:385
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_raster.c:367

References ovdump::band, pixval::ds, ES_NONE, FALSE, window::gt, PT_END, rtpixdump::rast, RASTER_DEBUG, RASTER_DEBUGF, rt_band_set_pixel_line(), rt_pixtype_size(), rt_raster_destroy(), rt_raster_generate_new_band(), rt_raster_get_band(), rt_raster_get_num_bands(), rt_raster_new(), rt_raster_set_geotransform_matrix(), rt_raster_set_srid(), rt_util_gdal_datatype_to_pixtype(), rt_util_gdal_sr_auth_info(), rtalloc(), rtdealloc(), rterror(), pixval::x, and pixval::y.

Referenced by RASTER_fromGDALRaster(), rt_band_load_offline_data(), rt_raster_gdal_rasterize(), rt_raster_gdal_warp(), and test_gdal_to_raster().

Here is the call graph for this function:
Here is the caller graph for this function: