GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
window_map.c
Go to the documentation of this file.
1 
17 #include <stdlib.h>
18 #include <grass/gis.h>
19 #include "G.h"
20 
21 
22 #define alloc_index(n) (COLUMN_MAPPING *) G_malloc((n)*sizeof(COLUMN_MAPPING))
23 
24 
37 {
38  struct fileinfo *fcb = &G__.fileinfo[fd];
39  COLUMN_MAPPING *col;
40  int i;
41  int x;
42  double C1, C2;
43  double west;
44 
46 
47  if (fcb->open_mode >= 0 && fcb->open_mode != OPEN_OLD) /* open for write? */
48  return 0;
49  if (fcb->open_mode == OPEN_OLD) /* already open ? */
50  G_free(fcb->col_map);
51 
52  col = fcb->col_map = alloc_index(G__.window.cols);
53 
54  /*
55  * for each column in the window, go to center of the cell,
56  * compute nearest column in the data file
57  * if column is not in data file, set column to 0
58  *
59  * for lat/lon move window so that west is bigger than
60  * cellhd west.
61  */
62  west = G__.window.west;
63  if (G__.window.proj == PROJECTION_LL) {
64  while (west > fcb->cellhd.west + 360.0)
65  west -= 360.0;
66  while (west < fcb->cellhd.west)
67  west += 360.0;
68  }
69 
70  C1 = G__.window.ew_res / fcb->cellhd.ew_res;
71  C2 = (west - fcb->cellhd.west +
72  G__.window.ew_res / 2.0) / fcb->cellhd.ew_res;
73  for (i = 0; i < G__.window.cols; i++) {
74  x = C2;
75  if (C2 < x) /* adjust for rounding of negatives */
76  x--;
77  if (x < 0 || x >= fcb->cellhd.cols) /* not in data file */
78  x = -1;
79  *col++ = x + 1;
80  C2 += C1;
81  }
82 
83  /* do wrap around for lat/lon */
84  if (G__.window.proj == PROJECTION_LL) {
85  col = fcb->col_map;
86  C2 = (west - 360.0 - fcb->cellhd.west +
87  G__.window.ew_res / 2.0) / fcb->cellhd.ew_res;
88  for (i = 0; i < G__.window.cols; i++) {
89  x = C2;
90  if (C2 < x) /* adjust for rounding of negatives */
91  x--;
92  if (x < 0 || x >= fcb->cellhd.cols) /* not in data file */
93  x = -1;
94  if (*col == 0) /* only change those not already set */
95  *col = x + 1;
96  col++;
97  C2 += C1;
98  }
99  }
100 
101  G_debug(3, "create window mapping (%d columns)", G__.window.cols);
102 /* for (i = 0; i < G__.window.cols; i++)
103  fprintf(stderr, "%s%ld", i % 15 ? " " : "\n", (long)fcb->col_map[i]);
104  fprintf(stderr, "\n");
105 */
106 
107  /* compute C1,C2 for row window mapping */
108  fcb->C1 = G__.window.ns_res / fcb->cellhd.ns_res;
109  fcb->C2 =
110  (fcb->cellhd.north - G__.window.north +
111  G__.window.ns_res / 2.0) / fcb->cellhd.ns_res;
112 
113  return 0;
114 }
115 
116 
129 double G_northing_to_row(double north, const struct Cell_head *window)
130 {
131  return (window->north - north) / window->ns_res;
132 }
133 
134 
149 double G_adjust_east_longitude(double east, double west)
150 {
151  while (east > west + 360.0)
152  east -= 360.0;
153  while (east <= west)
154  east += 360.0;
155 
156  return east;
157 }
158 
159 
174 double G_adjust_easting(double east, const struct Cell_head *window)
175 {
176  if (window->proj == PROJECTION_LL) {
177  east = G_adjust_east_longitude(east, window->west);
178  if (east > window->east && east == window->west + 360)
179  east = window->west;
180  }
181 
182  return east;
183 }
184 
185 
198 double G_easting_to_col(double east, const struct Cell_head *window)
199 {
200  east = G_adjust_easting(east, window);
201 
202  return (east - window->west) / window->ew_res;
203 }
204 
205 
223 double G_row_to_northing(double row, const struct Cell_head *window)
224 {
225  return window->north - row * window->ns_res;
226 }
227 
228 
243 double G_col_to_easting(double col, const struct Cell_head *window)
244 {
245  return window->west + col * window->ew_res;
246 }
247 
248 
273 int G_window_rows(void)
274 {
275  G__init_window();
276 
277  return G__.window.rows;
278 }
279 
280 
306 int G_window_cols(void)
307 {
308  G__init_window();
309 
310  return G__.window.cols;
311 }
312 
313 
320 int G__init_window(void)
321 {
322  if (!G__.window_set) {
323  G__.window_set = 1;
325  }
326 
327  return 0;
328 }
329 
330 
344 int G_row_repeat_nomask(int fd, int row)
345 {
346  struct fileinfo *fcb = &G__.fileinfo[fd];
347  double f;
348  int r1, r2;
349  int count;
350 
351  count = 1;
352 
353  /* r1 is the row in the raster map itself.
354  * r2 is the next row(s) in the raster map
355  * see get_row.c for details on this calculation
356  */
357  f = row * fcb->C1 + fcb->C2;
358  r1 = f;
359  if (f < r1)
360  r1--;
361 
362  while (++row < G__.window.rows) {
363  f = row * fcb->C1 + fcb->C2;
364  r2 = f;
365  if (f < r2)
366  r2--;
367  if (r1 != r2)
368  break;
369 
370  count++;
371  }
372 
373  return count;
374 }
#define alloc_index(n)
Definition: window_map.c:22
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
double G_easting_to_col(double east, const struct Cell_head *window)
Easting to column.
Definition: window_map.c:198
double G_adjust_east_longitude(double east, double west)
Adjust east longitude.
Definition: window_map.c:149
int G_row_repeat_nomask(int fd, int row)
Loops rows until mismatch?.
Definition: window_map.c:344
FILE * fd
Definition: g3dcolor.c:368
int count
COLUMN_MAPPING * col_map
Definition: G.h:52
double G_row_to_northing(double row, const struct Cell_head *window)
Row to northing.
Definition: window_map.c:223
double G_col_to_easting(double col, const struct Cell_head *window)
Column to easting.
Definition: window_map.c:243
struct Cell_head window
Definition: G.h:78
Definition: G.h:74
double C1
Definition: G.h:53
tuple window
Definition: tools.py:543
int open_mode
Definition: G.h:43
int window_set
Definition: G.h:79
int G_get_window(struct Cell_head *window)
read the database region
Definition: get_window.c:47
int G_window_cols(void)
Number of columns in active window.
Definition: window_map.c:306
double C2
Definition: G.h:53
struct fileinfo * fileinfo
Definition: G.h:95
#define OPEN_OLD
Definition: G.h:100
struct Cell_head cellhd
Definition: G.h:44
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
Definition: G.h:41
int COLUMN_MAPPING
Definition: G.h:19
double G_northing_to_row(double north, const struct Cell_head *window)
Northing to row.
Definition: window_map.c:129
int G__create_window_mapping(int fd)
Create window mapping.
Definition: window_map.c:36
int G__init_window(void)
Initialize window.
Definition: window_map.c:320
double G_adjust_easting(double east, const struct Cell_head *window)
Returns east larger than west.
Definition: window_map.c:174
int G_window_rows(void)
Number of rows in active window.
Definition: window_map.c:273