1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*!
\file lib/raster/sample.c
\brief Raster library - Sampling methods (extract a cell value from
raster map)
1/2006: moved to libgis from v.sample/v.drape for clone removal
(C) 2001-2009 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author James Darrell McCauley <darrell mccauley-usa.com>,
http://mccauley-usa.com/
*/
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/glocale.h>
/* prototypes */
static double scancatlabel(const char *);
/*!
* \brief Extract a cell value from raster map.
*
* Extract a cell value from raster map at given northing and easting
* with a sampled 3x3 window using a specified interpolation method.
*
* - NEAREST neighbor interpolation
* - BILINEAR bilinear interpolation
* - CUBIC cubic interpolation
*
* \param fd file descriptor
* \param window region settings
* \param cats categories
* \param north northing position
* \param east easting position
* \param usedesc flag to scan category label
* \param itype interpolation method
*
* \return cell value at given position
*/
DCELL Rast_get_sample(int fd, const struct Cell_head *window,
struct Categories *cats, double north, double east,
int usedesc, INTERP_TYPE itype)
{
double retval;
switch (itype) {
case INTERP_NEAREST:
retval =
Rast_get_sample_nearest(fd, window, cats, north, east, usedesc);
break;
case INTERP_BILINEAR:
retval =
Rast_get_sample_bilinear(fd, window, cats, north, east, usedesc);
break;
case INTERP_BICUBIC:
retval = Rast_get_sample_cubic(fd, window, cats, north, east, usedesc);
break;
default:
G_fatal_error("Rast_get_sample: %s", _("Unknown interpolation type"));
}
return retval;
}
/*!
* \brief Extract a cell value from raster map (neighbor interpolation)
*
* Extract a cell value from raster map at given northing and easting
* with a sampled 3x3 window using a neighbor interpolation.
*
* \param fd file descriptor
* \param window region settings
* \param cats categories
* \param north northing position
* \param east easting position
* \param usedesc flag to scan category label
*
* \return cell value at given position
*/
DCELL Rast_get_sample_nearest(int fd, const struct Cell_head *window,
struct Categories *cats, double north,
double east, int usedesc)
{
int row, col;
DCELL result;
DCELL *maprow = Rast_allocate_d_buf();
/* convert northing and easting to row and col, resp */
row = (int)floor(Rast_northing_to_row(north, window));
col = (int)floor(Rast_easting_to_col(east, window));
if (row < 0 || row >= Rast_window_rows() || col < 0 ||
col >= Rast_window_cols()) {
Rast_set_d_null_value(&result, 1);
goto done;
}
Rast_get_d_row(fd, maprow, row);
if (Rast_is_d_null_value(&maprow[col])) {
Rast_set_d_null_value(&result, 1);
goto done;
}
if (usedesc) {
char *buf = Rast_get_c_cat((CELL *)&(maprow[col]), cats);
G_squeeze(buf);
result = scancatlabel(buf);
}
else
result = maprow[col];
done:
G_free(maprow);
return result;
}
/*!
* \brief Extract a cell value from raster map (bilinear interpolation).
*
* Extract a cell value from raster map at given northing and easting
* with a sampled 3x3 window using a bilinear interpolation.
*
* \param fd file descriptor
* \param window region settings
* \param cats categories
* \param north northing position
* \param east easting position
* \param usedesc flag to scan category label
*
* \return cell value at given position
*/
DCELL Rast_get_sample_bilinear(int fd, const struct Cell_head *window,
struct Categories *cats, double north,
double east, int usedesc)
{
int row, col;
double grid[2][2];
DCELL *arow = Rast_allocate_d_buf();
DCELL *brow = Rast_allocate_d_buf();
double frow, fcol, trow, tcol;
DCELL result;
frow = Rast_northing_to_row(north, window);
fcol = Rast_easting_to_col(east, window);
/* convert northing and easting to row and col, resp */
row = (int)floor(frow - 0.5);
col = (int)floor(fcol - 0.5);
trow = frow - row - 0.5;
tcol = fcol - col - 0.5;
if (row < 0 || row + 1 >= Rast_window_rows() || col < 0 ||
col + 1 >= Rast_window_cols()) {
Rast_set_d_null_value(&result, 1);
goto done;
}
Rast_get_d_row(fd, arow, row);
Rast_get_d_row(fd, brow, row + 1);
if (Rast_is_d_null_value(&arow[col]) ||
Rast_is_d_null_value(&arow[col + 1]) ||
Rast_is_d_null_value(&brow[col]) ||
Rast_is_d_null_value(&brow[col + 1])) {
Rast_set_d_null_value(&result, 1);
goto done;
}
/*-
* now were ready to do bilinear interpolation over
* arow[col], arow[col+1],
* brow[col], brow[col+1]
*/
if (usedesc) {
char *buf;
G_squeeze(buf = Rast_get_c_cat((int *)&(arow[col]), cats));
grid[0][0] = scancatlabel(buf);
G_squeeze(buf = Rast_get_c_cat((CELL *)&(arow[col + 1]), cats));
grid[0][1] = scancatlabel(buf);
G_squeeze(buf = Rast_get_c_cat((CELL *)&(brow[col]), cats));
grid[1][0] = scancatlabel(buf);
G_squeeze(buf = Rast_get_c_cat((CELL *)&(brow[col + 1]), cats));
grid[1][1] = scancatlabel(buf);
}
else {
grid[0][0] = arow[col];
grid[0][1] = arow[col + 1];
grid[1][0] = brow[col];
grid[1][1] = brow[col + 1];
}
result = Rast_interp_bilinear(tcol, trow, grid[0][0], grid[0][1],
grid[1][0], grid[1][1]);
done:
G_free(arow);
G_free(brow);
return result;
}
/*!
* \brief Extract a cell value from raster map (cubic interpolation).
*
* Extract a cell value from raster map at given northing and easting
* with a sampled 3x3 window using a cubic interpolation.
*
* \param fd file descriptor
* \param window region settings
* \param cats categories
* \param north northing position
* \param east easting position
* \param usedesc flag to scan category label
*
* \return cell value at given position
*/
DCELL Rast_get_sample_cubic(int fd, const struct Cell_head *window,
struct Categories *cats, double north, double east,
int usedesc)
{
int i, j, row, col;
double grid[4][4];
DCELL *rows[4];
double frow, fcol, trow, tcol;
DCELL result;
for (i = 0; i < 4; i++)
rows[i] = Rast_allocate_d_buf();
frow = Rast_northing_to_row(north, window);
fcol = Rast_easting_to_col(east, window);
/* convert northing and easting to row and col, resp */
row = (int)floor(frow - 1.5);
col = (int)floor(fcol - 1.5);
trow = frow - row - 1.5;
tcol = fcol - col - 1.5;
if (row < 0 || row + 3 >= Rast_window_rows() || col < 0 ||
col + 3 >= Rast_window_cols()) {
Rast_set_d_null_value(&result, 1);
goto done;
}
for (i = 0; i < 4; i++)
Rast_get_d_row(fd, rows[i], row + i);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (Rast_is_d_null_value(&rows[i][col + j])) {
Rast_set_d_null_value(&result, 1);
goto done;
}
/*
* now were ready to do cubic interpolation over
* arow[col], arow[col+1], arow[col+2], arow[col+3],
* brow[col], brow[col+1], brow[col+2], brow[col+3],
* crow[col], crow[col+1], crow[col+2], crow[col+3],
* drow[col], drow[col+1], drow[col+2], drow[col+3],
*/
if (usedesc) {
char *buf;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
G_squeeze(
buf = Rast_get_c_cat((CELL *)&(rows[i][col + j]), cats));
grid[i][j] = scancatlabel(buf);
}
}
}
else {
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
grid[i][j] = rows[i][col + j];
}
result = Rast_interp_bicubic(
tcol, trow, grid[0][0], grid[0][1], grid[0][2], grid[0][3], grid[1][0],
grid[1][1], grid[1][2], grid[1][3], grid[2][0], grid[2][1], grid[2][2],
grid[2][3], grid[3][0], grid[3][1], grid[3][2], grid[3][3]);
done:
for (i = 0; i < 4; i++)
G_free(rows[i]);
return result;
}
static double scancatlabel(const char *str)
{
double val;
if (strcmp(str, "no data") != 0)
sscanf(str, "%lf", &val);
else {
G_warning(_("\"no data\" label found; setting to zero"));
val = 0.0;
}
return val;
}
|