PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ test_gdal_to_raster()

static void test_gdal_to_raster ( )
static

Definition at line 353 of file cu_gdal.c.

353  {
354  rt_pixtype pixtype = PT_64BF;
355  rt_band band = NULL;
356 
358  rt_raster rast;
359  const uint32_t width = 100;
360  const uint32_t height = 100;
361  uint32_t x;
362  uint32_t y;
363  int v;
364  double values[width][height];
365  int rtn = 0;
366  double value;
367 
368  GDALDriverH gddrv = NULL;
369  int destroy = 0;
370  GDALDatasetH gdds = NULL;
371 
372  raster = rt_raster_new(width, height);
373  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
374 
375  band = cu_add_band(raster, pixtype, 1, 0);
376  CU_ASSERT(band != NULL);
377 
378  for (x = 0; x < width; x++) {
379  for (y = 0; y < height; y++) {
380  values[x][y] = (((double) x * y) + (x + y) + (x + y * x)) / (x + y + 1);
381  rt_band_set_pixel(band, x, y, values[x][y], NULL);
382  }
383  }
384 
385  gdds = rt_raster_to_gdal_mem(raster, NULL, NULL, NULL, 0, &gddrv, &destroy);
386  CU_ASSERT(gddrv != NULL);
387  CU_ASSERT(gdds != NULL);
388  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterXSize(gdds), width);
389  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterYSize(gdds), height);
390 
392  CU_ASSERT(rast != NULL);
393  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rast), 1);
394 
396  CU_ASSERT(band != NULL);
397 
398  for (x = 0; x < width; x++) {
399  for (y = 0; y < height; y++) {
400  rtn = rt_band_get_pixel(band, x, y, &value, NULL);
401  CU_ASSERT_EQUAL(rtn, ES_NONE);
402  CU_ASSERT_DOUBLE_EQUAL(value, values[x][y], DBL_EPSILON);
403  }
404  }
405 
406  if (destroy && gddrv) {
407  GDALDeregisterDriver(gddrv);
408  GDALDestroyDriver(gddrv);
409  }
410  GDALClose(gdds);
411  gdds = NULL;
412  gddrv = NULL;
413 
416 
417  raster = rt_raster_new(width, height);
418  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
419 
420  pixtype = PT_8BSI;
421  band = cu_add_band(raster, pixtype, 1, 0);
422  CU_ASSERT(band != NULL);
423 
424  v = -127;
425  for (x = 0; x < width; x++) {
426  for (y = 0; y < height; y++) {
427  values[x][y] = v++;
428  rt_band_set_pixel(band, x, y, values[x][y], NULL);
429  if (v == 128)
430  v = -127;
431  }
432  }
433 
434  gdds = rt_raster_to_gdal_mem(raster, NULL, NULL, NULL, 0, &gddrv, &destroy);
435  CU_ASSERT(gddrv != NULL);
436  CU_ASSERT(gdds != NULL);
437  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterXSize(gdds), width);
438  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterYSize(gdds), height);
439 
441  CU_ASSERT(rast != NULL);
442  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rast), 1);
443 
445  CU_ASSERT(band != NULL);
446 #if POSTGIS_GDAL_VERSION < 30700
447  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_16BSI);
448 #else
449  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_8BSI);
450 #endif
451  for (x = 0; x < width; x++) {
452  for (y = 0; y < height; y++) {
453  rtn = rt_band_get_pixel(band, x, y, &value, NULL);
454 #if POSTGIS_GDAL_VERSION < 30700
455  CU_ASSERT_DOUBLE_EQUAL(value, values[x][y], 1.);
456  CU_ASSERT_DOUBLE_EQUAL(value, values[x][y], DBL_EPSILON);
457 #else
458  CU_ASSERT_EQUAL(rtn, ES_NONE);
459  CU_ASSERT_DOUBLE_EQUAL(value, values[x][y], DBL_EPSILON);
460 #endif
461  }
462  }
463  if (destroy && gddrv) {
464  GDALDeregisterDriver(gddrv);
465  GDALDestroyDriver(gddrv);
466  }
467 
468  GDALClose(gdds);
469  gdds = NULL;
470  gddrv = NULL;
471 
474 }
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition: rt_band.c:1527
rt_pixtype
Definition: librtcore.h:187
@ PT_8BSI
Definition: librtcore.h:191
@ PT_16BSI
Definition: librtcore.h:193
@ PT_64BF
Definition: librtcore.h:198
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:52
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_raster.c:2175
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
Definition: rt_band.c:1125
@ ES_NONE
Definition: librtcore.h:182
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:376
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:782
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.
Definition: rt_raster.c:1813
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:385
int value
Definition: genraster.py:62
band
Definition: ovdump.py:58
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
void cu_free_raster(rt_raster raster)

References ovdump::band, cu_add_band(), cu_free_raster(), ES_NONE, PT_16BSI, PT_64BF, PT_8BSI, rtpixdump::rast, rtrowdump::raster, rt_band_get_pixel(), rt_band_get_pixtype(), rt_band_set_pixel(), rt_raster_from_gdal_dataset(), rt_raster_get_band(), rt_raster_get_num_bands(), rt_raster_new(), rt_raster_to_gdal_mem(), genraster::value, pixval::x, and pixval::y.

Referenced by gdal_suite_setup().

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