GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
raster/window_map.c
Go to the documentation of this file.
1/*!
2 * \file lib/raster/window_map.c
3 *
4 * \brief Raster Library - Window mapping functions.
5 *
6 * SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <stdlib.h>
13#include <grass/gis.h>
14#include <grass/raster.h>
15
16#include "R.h"
17
18#define alloc_index(n) (COLUMN_MAPPING *)G_malloc((n) * sizeof(COLUMN_MAPPING))
19
20/*!
21 * \brief Create window mapping.
22 *
23 * Creates mapping from cell header into window. The boundaries and
24 * resolution of the two spaces do not have to be the same or aligned in
25 * any way.
26 *
27 * \param fd file descriptor
28 */
30{
31 struct fileinfo *fcb = &R__.fileinfo[fd];
33 int i;
34 int x;
35 double C1, C2;
36 double west, east;
37
38 if (fcb->open_mode >= 0 && fcb->open_mode != OPEN_OLD) /* open for write? */
39 return;
40 if (fcb->open_mode == OPEN_OLD) /* already open ? */
41 G_free(fcb->col_map);
42
43 col = fcb->col_map = alloc_index(R__.rd_window.cols);
44
45 /*
46 * for each column in the window, go to center of the cell,
47 * compute nearest column in the data file
48 * if column is not in data file, set column to 0
49 *
50 * for lat/lon move window so that west is bigger than
51 * cellhd west.
52 */
53 west = R__.rd_window.west;
54 east = R__.rd_window.east;
55 if (R__.rd_window.proj == PROJECTION_LL) {
56 while (west > fcb->cellhd.west + 360.0) {
57 west -= 360.0;
58 east -= 360.0;
59 }
60 while (west < fcb->cellhd.west) {
61 west += 360.0;
62 east += 360.0;
63 }
64 }
65
66 C1 = R__.rd_window.ew_res / fcb->cellhd.ew_res;
67 C2 = (west - fcb->cellhd.west + R__.rd_window.ew_res / 2.0) /
68 fcb->cellhd.ew_res;
69 for (i = 0; i < R__.rd_window.cols; i++) {
70 x = C2;
71 if (C2 < x) /* adjust for rounding of negatives */
72 x--;
73 if (x < 0 || x >= fcb->cellhd.cols) /* not in data file */
74 x = -1;
75 *col++ = x + 1;
76 C2 += C1;
77 }
78
79 /* do wrap around for lat/lon */
80 if (R__.rd_window.proj == PROJECTION_LL) {
81
82 while (east - 360.0 > fcb->cellhd.west) {
83 east -= 360.0;
84 west -= 360.0;
85
86 col = fcb->col_map;
87 C2 = (west - fcb->cellhd.west + R__.rd_window.ew_res / 2.0) /
88 fcb->cellhd.ew_res;
89 for (i = 0; i < R__.rd_window.cols; i++) {
90 x = C2;
91 if (C2 < x) /* adjust for rounding of negatives */
92 x--;
93 if (x < 0 || x >= fcb->cellhd.cols) /* not in data file */
94 x = -1;
95 if (*col == 0) /* only change those not already set */
96 *col = x + 1;
97 col++;
98 C2 += C1;
99 }
100 }
101 }
102
103 G_debug(3, "create window mapping (%d columns)", R__.rd_window.cols);
104 /* for (i = 0; i < R__.rd_window.cols; i++)
105 fprintf(stderr, "%s%ld", i % 15 ? " " : "\n", (long)fcb->col_map[i]);
106 fprintf(stderr, "\n");
107 */
108
109 /* compute C1,C2 for row window mapping */
110 fcb->C1 = R__.rd_window.ns_res / fcb->cellhd.ns_res;
111 fcb->C2 =
112 (fcb->cellhd.north - R__.rd_window.north + R__.rd_window.ns_res / 2.0) /
113 fcb->cellhd.ns_res;
114}
115
116/*!
117 * \brief Loops rows until mismatch?.
118 *
119 * This routine works fine if the mask is not set. It may give
120 * incorrect results with a mask, since the mask row may have a
121 * different repeat value. The issue can be fixed by doing it for the
122 * mask as well and using the smaller value.
123 *
124 * \param fd file descriptor
125 * \param row starting row
126 *
127 * \return number of rows completed
128 */
129int Rast_row_repeat_nomask(int fd, int row)
130{
131 struct fileinfo *fcb = &R__.fileinfo[fd];
132 double f;
133 int r1, r2;
134 int count;
135
136 count = 1;
137
138 /* r1 is the row in the raster map itself.
139 * r2 is the next row(s) in the raster map
140 * see get_row.c for details on this calculation
141 */
142 f = row * fcb->C1 + fcb->C2;
143 r1 = f;
144 if (f < r1)
145 r1--;
146
147 while (++row < R__.rd_window.rows) {
148 f = row * fcb->C1 + fcb->C2;
149 r2 = f;
150 if (f < r2)
151 r2--;
152 if (r1 != r2)
153 break;
154
155 count++;
156 }
157
158 return count;
159}
#define OPEN_OLD
Definition R.h:101
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
int G_debug(int, const char *,...) __attribute__((format(printf
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition gis.h:126
int count
void Rast__create_window_mapping(int fd)
Create window mapping.
int Rast_row_repeat_nomask(int fd, int row)
Loops rows until mismatch?.
#define alloc_index(n)
Definition R.h:82
struct fileinfo * fileinfo
Definition R.h:96
struct Cell_head rd_window
Definition R.h:92
Definition R.h:48
struct Cell_head cellhd
Definition R.h:50
double C2
Definition R.h:59
double C1
Definition R.h:59
#define x