GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cnversions.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/display.h>
3 
4 /****** OLD CODE
5 * #include "windround.h"
6 **********/
7 /* D_do_conversions(window, t, b, l, r)
8  * struct Cell_head *window ;
9  * int t, b, l, r ;
10  *
11  * Sets up conversion coefficients to translate between three
12  * coordinate systems:
13  *
14  * 1. Screen coordinates (given by t, b, l, r values)
15  * 2. UTM coordinates (given by values in window structure)
16  * 3. Window array coors (given by values in window structure)
17  *
18  * Once D_do_conversions is called, lots of conversion coefficients
19  * and conversion routines are available.
20  *
21  * Calls to convert row and column (x and y) values in one system to
22  * another system are available. In addition calls which return the
23  * conversion coefficients are alos provided.
24  */
25 
26 struct rectangle
27 {
28  double west;
29  double east;
30  double south;
31  double north;
32 };
33 
34 struct vector
35 {
36  double x, y;
37 };
38 
39 /* Bounding rectangles */
40 static struct rectangle U; /* UTM coordinates, meters, (0,0) towards SW */
41 static struct rectangle A; /* Map array coordinates, integers, (0,0) towards NW */
42 static struct rectangle D; /* Display coordinates, pixels, (0,0) towards NW */
43 
44 /* Conversion factors */
45 static struct vector U_to_D_conv; /* UTM to Display */
46 static struct vector D_to_A_conv; /* Display to Array */
47 
48 /* others */
49 static int is_lat_lon;
50 static struct vector resolution;
51 
69 int D_do_conversions(const struct Cell_head *window, int t, int b, int l,
70  int r)
71 {
72  struct vector ARRAY_SIZE;
73  struct rectangle WIND;
74  struct vector D_size, U_size;
75 
76  WIND.north = (double)t;
77  WIND.south = (double)b;
78  WIND.west = (double)l;
79  WIND.east = (double)r;
80 
81  is_lat_lon = (window->proj == PROJECTION_LL);
82 
83  resolution.y = window->ns_res;
84  resolution.x = window->ew_res;
85 
86  /* Key all coordinate limits off UTM window limits */
87  U.west = window->west;
88  U.east = window->east;
89  U.south = window->south;
90  U.north = window->north;
91 
92  U_size.y = U.north - U.south;
93  U_size.x = U.east - U.west;
94 
95  D_size.x = WIND.east - WIND.west;
96  D_size.y = WIND.south - WIND.north;
97 
98  U_to_D_conv.x = D_size.x / U_size.x;
99  U_to_D_conv.y = D_size.y / U_size.y;
100 
101  if (U_to_D_conv.x > U_to_D_conv.y) {
102  U_to_D_conv.x = U_to_D_conv.y;
103  D.west =
104  (double)(int)((WIND.west + WIND.east -
105  U_size.x * U_to_D_conv.x) / 2);
106  D.east =
107  (double)(int)((WIND.west + WIND.east +
108  U_size.x * U_to_D_conv.x) / 2);
109  D.north = WIND.north;
110  D.south = WIND.south;
111  }
112  else {
113  U_to_D_conv.y = U_to_D_conv.x;
114  D.west = WIND.west;
115  D.east = WIND.east;
116  D.north =
117  (double)(int)((WIND.north + WIND.south -
118  U_size.y * U_to_D_conv.y) / 2);
119  D.south =
120  (double)(int)((WIND.north + WIND.south +
121  U_size.y * U_to_D_conv.y) / 2);
122  }
123 
124  D_size.x = D.east - D.west;
125  D_size.y = D.south - D.north;
126 
127  ARRAY_SIZE.x = window->cols;
128  ARRAY_SIZE.y = window->rows;
129 
130  A.west = 0.0;
131  A.north = 0.0;
132  A.east = (double)ARRAY_SIZE.x;
133  A.south = (double)ARRAY_SIZE.y;
134 
135  D_to_A_conv.x = (double)ARRAY_SIZE.x / D_size.x;
136  D_to_A_conv.y = (double)ARRAY_SIZE.y / D_size.y;
137 
138 #ifdef DEBUG
139  fprintf(stderr,
140  " D_w %10.1f D_e %10.1f D_s %10.1f D_n %10.1f\n",
141  D.west, D.east, D.south, D.north);
142  fprintf(stderr,
143  " A_w %10.1f A_e %10.1f A_s %10.1f A_n %10.1f\n",
144  A.west, A.east, A.south, A.north);
145  fprintf(stderr,
146  " U_w %10.1f U_e %10.1f U_s %10.1f U_n %10.1f\n",
147  U.west, U.east, U.south, U.north);
148  fprintf(stderr,
149  " ARRAY_ROWS %d resolution_ns %10.2f\n", ARRAY_SIZE.y,
150  window->ns_res);
151  fprintf(stderr, " ARRAY_COLS %d resolution_ew %10.2f\n", ARRAY_SIZE.x,
152  window->ew_res);
153  fprintf(stderr, " D_to_A_conv.x %10.1f D_to_A_conv.y %10.1f \n",
154  D_to_A_conv.x, D_to_A_conv.y);
155  fprintf(stderr, " BOT %10.1f TOP %10.1f LFT %10.1f RHT %10.1f\n",
156  WIND.south, WIND.north, WIND.west, WIND.east);
157 #endif /* DEBUG */
158 
159  return (0);
160 }
161 
162 int D_is_lat_lon(void)
163 {
164  return (is_lat_lon);
165 }
166 
168 {
169  return (resolution.y);
170 }
172 {
173  return (resolution.x);
174 }
175 
176 double D_get_u_to_d_xconv(void)
177 {
178  return (U_to_D_conv.x);
179 }
180 double D_get_u_to_d_yconv(void)
181 {
182  return (U_to_D_conv.y);
183 }
184 
185 double D_get_u_west(void)
186 {
187  return (U.west);
188 }
189 double D_get_u_east(void)
190 {
191  return (U.east);
192 }
193 double D_get_u_north(void)
194 {
195  return (U.north);
196 }
197 double D_get_u_south(void)
198 {
199  return (U.south);
200 }
201 
202 double D_get_a_west(void)
203 {
204  return (A.west);
205 }
206 double D_get_a_east(void)
207 {
208  return (A.east);
209 }
210 double D_get_a_north(void)
211 {
212  return (A.north);
213 }
214 double D_get_a_south(void)
215 {
216  return (A.south);
217 }
218 
219 double D_get_d_west(void)
220 {
221  return (D.west);
222 }
223 double D_get_d_east(void)
224 {
225  return (D.east);
226 }
227 double D_get_d_north(void)
228 {
229  return (D.north);
230 }
231 double D_get_d_south(void)
232 {
233  return (D.south);
234 }
235 
236 void D_get_u(double x[2][2])
237 {
238  x[0][0] = U.west;
239  x[0][1] = U.east;
240  x[1][0] = U.north;
241  x[1][1] = U.south;
242 }
243 
244 void D_get_a(int x[2][2])
245 {
246  x[0][0] = (int)A.west;
247  x[0][1] = (int)A.east;
248  x[1][0] = (int)A.north;
249  x[1][1] = (int)A.south;
250 }
251 
252 void D_get_d(int x[2][2])
253 {
254  x[0][0] = (int)D.west;
255  x[0][1] = (int)D.east;
256  x[1][0] = (int)D.north;
257  x[1][1] = (int)D.south;
258 }
259 
270 double D_u_to_a_row(double U_row)
271 {
272  return (U.north - U_row) / resolution.y;
273 }
274 
275 
286 double D_u_to_a_col(double U_col)
287 {
288  return (U_col - U.west) / resolution.x;
289 }
290 
291 
302 double D_a_to_d_row(double A_row)
303 {
304  return D.north + (A_row - A.north) / D_to_A_conv.y;
305 }
306 
307 
319 double D_a_to_d_col(double A_col)
320 {
321  return D.west + (A_col - A.west) / D_to_A_conv.x;
322 }
323 
324 
335 double D_u_to_d_row(double U_row)
336 {
337  return D.north + (U.north - U_row) * U_to_D_conv.y;
338 }
339 
340 
351 double D_u_to_d_col(double U_col)
352 {
353  return D.west + (U_col - U.west) * U_to_D_conv.x;
354 }
355 
356 
367 double D_d_to_u_row(double D_row)
368 {
369  return U.north - (D_row - D.north) / U_to_D_conv.y;
370 }
371 
372 
383 double D_d_to_u_col(double D_col)
384 {
385  return U.west + (D_col - D.west) / U_to_D_conv.x;
386 }
387 
388 
399 double D_d_to_a_row(double D_row)
400 {
401  return A.north + (D_row - D.north) * D_to_A_conv.y;
402 }
403 
404 
415 double D_d_to_a_col(double D_col)
416 {
417  return A.west + (D_col - D.west) * D_to_A_conv.x;
418 }
double D_d_to_a_col(double D_col)
screen to array (x)
Definition: cnversions.c:415
int l
Definition: dataquad.c:292
float b
Definition: named_colr.c:8
double D_u_to_a_row(double U_row)
earth to array (north)
Definition: cnversions.c:270
double D_get_a_east(void)
Definition: cnversions.c:206
double D_get_u_to_d_yconv(void)
Definition: cnversions.c:180
int D_is_lat_lon(void)
Definition: cnversions.c:162
float r
Definition: named_colr.c:8
double D_d_to_a_row(double D_row)
screen to array (y)
Definition: cnversions.c:399
double D_d_to_u_col(double D_col)
screen to earth (x)
Definition: cnversions.c:383
double D_get_d_west(void)
Definition: cnversions.c:219
double D_get_u_north(void)
Definition: cnversions.c:193
double D_get_a_south(void)
Definition: cnversions.c:214
double D_get_ew_resolution(void)
Definition: cnversions.c:171
double east
Definition: cnversions.c:29
double north
Definition: cnversions.c:31
#define D
Definition: gis/intersect.c:74
double D_get_a_west(void)
Definition: cnversions.c:202
int D_do_conversions(const struct Cell_head *window, int t, int b, int l, int r)
initialize conversions
Definition: cnversions.c:69
double D_get_u_east(void)
Definition: cnversions.c:189
double D_get_d_east(void)
Definition: cnversions.c:223
double D_get_d_north(void)
Definition: cnversions.c:227
double D_u_to_d_col(double U_col)
earth to screen (east)
Definition: cnversions.c:351
void D_get_u(double x[2][2])
Definition: cnversions.c:236
tuple window
Definition: tools.py:543
double D_get_a_north(void)
Definition: cnversions.c:210
void D_get_d(int x[2][2])
Definition: cnversions.c:252
double south
Definition: cnversions.c:30
double D_get_ns_resolution(void)
Definition: cnversions.c:167
int
Definition: g3dcolor.c:48
double D_get_u_south(void)
Definition: cnversions.c:197
double D_u_to_a_col(double U_col)
earth to array (east
Definition: cnversions.c:286
double D_a_to_d_row(double A_row)
array to screen (row)
Definition: cnversions.c:302
double D_get_d_south(void)
Definition: cnversions.c:231
double D_get_u_to_d_xconv(void)
Definition: cnversions.c:176
double west
Definition: cnversions.c:28
double D_d_to_u_row(double D_row)
screen to earth (y)
Definition: cnversions.c:367
double y
Definition: cnversions.c:36
double x
Definition: cnversions.c:36
void D_get_a(int x[2][2])
Definition: cnversions.c:244
double D_u_to_d_row(double U_row)
earth to screen (north)
Definition: cnversions.c:335
double D_get_u_west(void)
Definition: cnversions.c:185
double D_a_to_d_col(double A_col)
array to screen (column)
Definition: cnversions.c:319