Refactored the GDALRasterIO part of rt_raster_from_gdal_dataset to use scanlines instead of "natural" blocks, which dramatically simplifies the code and makes it easier to maintain.

git-svn-id: http://svn.osgeo.org/postgis/trunk@8313 b70326c6-7e19-0410-871a-916f4a2858ee
This commit is contained in:
Bborie Park 2011-12-07 15:29:46 +00:00
parent 064209f7a7
commit 13ac027977
3 changed files with 24 additions and 63 deletions

View file

@ -33,8 +33,6 @@
#include <stdarg.h> /* for va_list, va_start etc */
#include <string.h> /* for memcpy and strlen */
#include <assert.h>
#include <float.h> /* for FLT_EPSILON and float type limits */
#include <limits.h> /* for integer type limits */
#include <time.h> /* for time */
#include "rt_api.h"
@ -6636,12 +6634,6 @@ rt_raster_from_gdal_dataset(GDALDatasetH ds) {
int x;
int y;
double value;
int nXBlocks, nYBlocks;
int nXBlockSize, nYBlockSize;
int iXBlock, iYBlock;
int nXValid, nYValid;
int iY, iX;
double *values = NULL;
assert(NULL != ds);
@ -6751,67 +6743,35 @@ rt_raster_from_gdal_dataset(GDALDatasetH ds) {
}
band = rt_raster_get_band(rast, idx);
/* this makes use of GDAL's "natural" blocks */
GDALGetBlockSize(gdband, &nXBlockSize, &nYBlockSize);
nXBlocks = (width + nXBlockSize - 1) / nXBlockSize;
nYBlocks = (height + nYBlockSize - 1) / nYBlockSize;
RASTER_DEBUGF(4, "(nXBlockSize, nYBlockSize) = (%d, %d)", nXBlockSize, nYBlockSize);
RASTER_DEBUGF(4, "(nXBlocks, nYBlocks) = (%d, %d)", nXBlocks, nYBlocks);
/* use rows */
values = rtalloc(sizeof(double) * width);
for (y = 0; y < height; y++) {
cplerr = GDALRasterIO(
gdband, GF_Read,
0, y,
width, 1,
values, width, 1,
GDT_Float64,
0, 0
);
if (cplerr != CE_None) {
rterror("rt_raster_from_gdal_dataset: Unable to get data from GDAL band");
rtdealloc(values);
rt_raster_destroy(rast);
return NULL;
}
for (iYBlock = 0; iYBlock < nYBlocks; iYBlock++) { /* row */
for (iXBlock = 0; iXBlock < nXBlocks; iXBlock++) { /* column */
x = iXBlock * nXBlockSize;
y = iYBlock * nYBlockSize;
RASTER_DEBUGF(4, "(iXBlock, iYBlock) = (%d, %d)", iXBlock, iYBlock);
RASTER_DEBUGF(4, "(x, y) = (%d, %d)", x, y);
if ((iXBlock + 1) * nXBlockSize > width)
nXValid = width - (iXBlock * nXBlockSize);
else
nXValid = nXBlockSize;
if ((iYBlock + 1) * nYBlockSize > height)
nYValid = height - (iYBlock * nYBlockSize);
else
nYValid = nYBlockSize;
values = rtalloc(nXValid * nYValid * sizeof(double));
/* values is ordered from left to right, top to bottom */
cplerr = GDALRasterIO(
gdband, GF_Read,
x, y,
nXValid, nYValid,
values, nXValid, nYValid,
GDT_Float64,
0, 0
);
if (cplerr != CE_None) {
rterror("rt_raster_from_gdal_dataset: Unable to get data from GDAL band");
for (x = 0; x < width; x++) {
value = values[x];
if (rt_band_set_pixel(band, x, y, value) < 0) {
rterror("rt_raster_from_gdal_dataset: Unable to save data from GDAL band");
rtdealloc(values);
rt_raster_destroy(rast);
return NULL;
}
for (iY = 0; iY < nYValid; iY++) {
for (iX = 0; iX < nXValid; iX++) {
x = iX + (nXBlockSize * iXBlock);
y = iY + (nYBlockSize * iYBlock);
value = values[iX + iY * nXBlockSize];
RASTER_DEBUGF(5, "(x, y, value) = (%d, %d, %f)", x, y, value);
if (rt_band_set_pixel(band, x, y, value) < 0) {
rterror("rt_raster_from_gdal_dataset: Unable to save data from GDAL band");
rtdealloc(values);
rt_raster_destroy(rast);
return NULL;
}
}
}
rtdealloc(values);
}
}
rtdealloc(values);
}
return rast;

View file

@ -92,6 +92,8 @@
#include <stdlib.h> /* For size_t, srand and rand */
#include <stdint.h> /* For C99 int types */
#include <float.h> /* for FLT_EPSILON and float type limits */
#include <limits.h> /* for integer type limits */
#include "lwgeom_geos.h"
#include "liblwgeom.h"

View file

@ -29,7 +29,6 @@
*/
#include <math.h>
#include <float.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h> /* for strtod in RASTER_reclass */