From: Bborie Park Date: Wed, 7 Dec 2011 15:29:46 +0000 (+0000) Subject: Refactored the GDALRasterIO part of rt_raster_from_gdal_dataset to use scanlines... X-Git-Tag: 2.0.0alpha1~555 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=13ac027977add4dd661d0edf947d204bcdd09f71;p=postgis 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 --- diff --git a/raster/rt_core/rt_api.c b/raster/rt_core/rt_api.c index fa6d4c606..59a9d48ee 100644 --- a/raster/rt_core/rt_api.c +++ b/raster/rt_core/rt_api.c @@ -33,8 +33,6 @@ #include /* for va_list, va_start etc */ #include /* for memcpy and strlen */ #include -#include /* for FLT_EPSILON and float type limits */ -#include /* for integer type limits */ #include /* 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); - - 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; + /* 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; + } - 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; diff --git a/raster/rt_core/rt_api.h b/raster/rt_core/rt_api.h index 82bce6c93..4dfe28ad9 100644 --- a/raster/rt_core/rt_api.h +++ b/raster/rt_core/rt_api.h @@ -92,6 +92,8 @@ #include /* For size_t, srand and rand */ #include /* For C99 int types */ +#include /* for FLT_EPSILON and float type limits */ +#include /* for integer type limits */ #include "lwgeom_geos.h" #include "liblwgeom.h" diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 712301a0d..88eb7e1f2 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -29,7 +29,6 @@ */ #include -#include #include #include #include /* for strtod in RASTER_reclass */