mirror of
https://git.osgeo.org/gitea/postgis/postgis
synced 2024-10-24 09:02:37 +00:00
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:
parent
064209f7a7
commit
13ac027977
|
@ -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;
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> /* for strtod in RASTER_reclass */
|
||||
|
|
Loading…
Reference in a new issue