2
* \file lib/raster/window_map.c
4
* \brief Raster Library - Window mapping functions.
6
* (C) 2001-2009 by the GRASS Development Team
8
* This program is free software under the GNU General Public License
9
* (>=v2). Read the file COPYING that comes with GRASS for details.
11
* \author Original author CERL
15
#include <grass/gis.h>
16
#include <grass/raster.h>
21
#define alloc_index(n) (COLUMN_MAPPING *) G_malloc((n)*sizeof(COLUMN_MAPPING))
25
* \brief Create window mapping.
27
* Creates mapping from cell header into window. The boundaries and
28
* resolution of the two spaces do not have to be the same or aligned in
31
* \param fd file descriptor
33
void Rast__create_window_mapping(int fd)
35
struct fileinfo *fcb = &R__.fileinfo[fd];
42
if (fcb->open_mode >= 0 && fcb->open_mode != OPEN_OLD) /* open for write? */
44
if (fcb->open_mode == OPEN_OLD) /* already open ? */
47
col = fcb->col_map = alloc_index(R__.rd_window.cols);
50
* for each column in the window, go to center of the cell,
51
* compute nearest column in the data file
52
* if column is not in data file, set column to 0
54
* for lat/lon move window so that west is bigger than
57
west = R__.rd_window.west;
58
if (R__.rd_window.proj == PROJECTION_LL) {
59
while (west > fcb->cellhd.west + 360.0)
61
while (west < fcb->cellhd.west)
65
C1 = R__.rd_window.ew_res / fcb->cellhd.ew_res;
66
C2 = (west - fcb->cellhd.west +
67
R__.rd_window.ew_res / 2.0) / fcb->cellhd.ew_res;
68
for (i = 0; i < R__.rd_window.cols; i++) {
70
if (C2 < x) /* adjust for rounding of negatives */
72
if (x < 0 || x >= fcb->cellhd.cols) /* not in data file */
78
/* do wrap around for lat/lon */
79
if (R__.rd_window.proj == PROJECTION_LL) {
81
C2 = (west - 360.0 - fcb->cellhd.west +
82
R__.rd_window.ew_res / 2.0) / fcb->cellhd.ew_res;
83
for (i = 0; i < R__.rd_window.cols; i++) {
85
if (C2 < x) /* adjust for rounding of negatives */
87
if (x < 0 || x >= fcb->cellhd.cols) /* not in data file */
89
if (*col == 0) /* only change those not already set */
96
G_debug(3, "create window mapping (%d columns)", R__.rd_window.cols);
97
/* for (i = 0; i < R__.rd_window.cols; i++)
98
fprintf(stderr, "%s%ld", i % 15 ? " " : "\n", (long)fcb->col_map[i]);
99
fprintf(stderr, "\n");
102
/* compute C1,C2 for row window mapping */
103
fcb->C1 = R__.rd_window.ns_res / fcb->cellhd.ns_res;
105
(fcb->cellhd.north - R__.rd_window.north +
106
R__.rd_window.ns_res / 2.0) / fcb->cellhd.ns_res;
111
* \brief Loops rows until mismatch?.
113
* This routine works fine if the mask is not set. It may give
114
* incorrect results with a mask, since the mask row may have a
115
* different repeat value. The issue can be fixed by doing it for the
116
* mask as well and using the smaller value.
118
* \param fd file descriptor
119
* \param row starting row
121
* \return number of rows completed
123
int Rast_row_repeat_nomask(int fd, int row)
125
struct fileinfo *fcb = &R__.fileinfo[fd];
132
/* r1 is the row in the raster map itself.
133
* r2 is the next row(s) in the raster map
134
* see get_row.c for details on this calculation
136
f = row * fcb->C1 + fcb->C2;
141
while (++row < R__.rd_window.rows) {
142
f = row * fcb->C1 + fcb->C2;