GRASS Programmer's Manual  6.4.2(2012)
cnversions.c
Go to the documentation of this file.
00001 #include <grass/gis.h>
00002 #include <grass/display.h>
00003 
00004 /****** OLD CODE
00005 * #include "windround.h"
00006 **********/
00007 /*  D_do_conversions(window, t, b, l, r)
00008  *       struct Cell_head *window ;
00009  *       int t, b, l, r ;
00010  *
00011  *  Sets up conversion coefficients to translate between three 
00012  *  coordinate systems:
00013  *
00014  *  1.  Screen coordinates   (given by t, b, l, r values)
00015  *  2.  UTM coordinates      (given by values in window structure)
00016  *  3.  Window array coors   (given by values in window structure)
00017  *
00018  *  Once D_do_conversions is called, lots of conversion coefficients
00019  *  and conversion routines are available.
00020  *
00021  *  Calls to convert row and column (x and y) values in one system to
00022  *  another system are available.  In addition calls which return the
00023  *  conversion coefficients are alos provided.
00024  */
00025 
00026 struct rectangle
00027 {
00028     double west;
00029     double east;
00030     double south;
00031     double north;
00032 };
00033 
00034 struct vector
00035 {
00036     double x, y;
00037 };
00038 
00039 /* Bounding rectangles */
00040 static struct rectangle U;      /* UTM coordinates, meters, (0,0) towards SW */
00041 static struct rectangle A;      /* Map array coordinates, integers, (0,0) towards NW */
00042 static struct rectangle D;      /* Display coordinates, pixels, (0,0) towards NW */
00043 
00044 /* Conversion factors */
00045 static struct vector U_to_D_conv;       /* UTM to Display   */
00046 static struct vector D_to_A_conv;       /* Display to Array */
00047 
00048 /* others */
00049 static int is_lat_lon;
00050 static struct vector resolution;
00051 
00069 int D_do_conversions(const struct Cell_head *window, int t, int b, int l,
00070                      int r)
00071 {
00072     struct vector ARRAY_SIZE;
00073     struct rectangle WIND;
00074     struct vector D_size, U_size;
00075 
00076     WIND.north = (double)t;
00077     WIND.south = (double)b;
00078     WIND.west = (double)l;
00079     WIND.east = (double)r;
00080 
00081     is_lat_lon = (window->proj == PROJECTION_LL);
00082 
00083     resolution.y = window->ns_res;
00084     resolution.x = window->ew_res;
00085 
00086     /* Key all coordinate limits off UTM window limits  */
00087     U.west = window->west;
00088     U.east = window->east;
00089     U.south = window->south;
00090     U.north = window->north;
00091 
00092     U_size.y = U.north - U.south;
00093     U_size.x = U.east - U.west;
00094 
00095     D_size.x = WIND.east - WIND.west;
00096     D_size.y = WIND.south - WIND.north;
00097 
00098     U_to_D_conv.x = D_size.x / U_size.x;
00099     U_to_D_conv.y = D_size.y / U_size.y;
00100 
00101     if (U_to_D_conv.x > U_to_D_conv.y) {
00102         U_to_D_conv.x = U_to_D_conv.y;
00103         D.west =
00104             (double)(int)((WIND.west + WIND.east -
00105                            U_size.x * U_to_D_conv.x) / 2);
00106         D.east =
00107             (double)(int)((WIND.west + WIND.east +
00108                            U_size.x * U_to_D_conv.x) / 2);
00109         D.north = WIND.north;
00110         D.south = WIND.south;
00111     }
00112     else {
00113         U_to_D_conv.y = U_to_D_conv.x;
00114         D.west = WIND.west;
00115         D.east = WIND.east;
00116         D.north =
00117             (double)(int)((WIND.north + WIND.south -
00118                            U_size.y * U_to_D_conv.y) / 2);
00119         D.south =
00120             (double)(int)((WIND.north + WIND.south +
00121                            U_size.y * U_to_D_conv.y) / 2);
00122     }
00123 
00124     D_size.x = D.east - D.west;
00125     D_size.y = D.south - D.north;
00126 
00127     ARRAY_SIZE.x = window->cols;
00128     ARRAY_SIZE.y = window->rows;
00129 
00130     A.west = 0.0;
00131     A.north = 0.0;
00132     A.east = (double)ARRAY_SIZE.x;
00133     A.south = (double)ARRAY_SIZE.y;
00134 
00135     D_to_A_conv.x = (double)ARRAY_SIZE.x / D_size.x;
00136     D_to_A_conv.y = (double)ARRAY_SIZE.y / D_size.y;
00137 
00138 #ifdef DEBUG
00139     fprintf(stderr,
00140             " D_w %10.1f  D_e %10.1f  D_s %10.1f  D_n %10.1f\n",
00141             D.west, D.east, D.south, D.north);
00142     fprintf(stderr,
00143             " A_w %10.1f  A_e %10.1f  A_s %10.1f  A_n %10.1f\n",
00144             A.west, A.east, A.south, A.north);
00145     fprintf(stderr,
00146             " U_w %10.1f  U_e %10.1f  U_s %10.1f  U_n %10.1f\n",
00147             U.west, U.east, U.south, U.north);
00148     fprintf(stderr,
00149             " ARRAY_ROWS %d  resolution_ns %10.2f\n", ARRAY_SIZE.y,
00150             window->ns_res);
00151     fprintf(stderr, " ARRAY_COLS %d  resolution_ew %10.2f\n", ARRAY_SIZE.x,
00152             window->ew_res);
00153     fprintf(stderr, " D_to_A_conv.x %10.1f D_to_A_conv.y %10.1f \n",
00154             D_to_A_conv.x, D_to_A_conv.y);
00155     fprintf(stderr, " BOT %10.1f  TOP %10.1f  LFT %10.1f  RHT %10.1f\n",
00156             WIND.south, WIND.north, WIND.west, WIND.east);
00157 #endif /* DEBUG */
00158 
00159     return (0);
00160 }
00161 
00162 int D_is_lat_lon(void)
00163 {
00164     return (is_lat_lon);
00165 }
00166 
00167 double D_get_ns_resolution(void)
00168 {
00169     return (resolution.y);
00170 }
00171 double D_get_ew_resolution(void)
00172 {
00173     return (resolution.x);
00174 }
00175 
00176 double D_get_u_to_d_xconv(void)
00177 {
00178     return (U_to_D_conv.x);
00179 }
00180 double D_get_u_to_d_yconv(void)
00181 {
00182     return (U_to_D_conv.y);
00183 }
00184 
00185 double D_get_u_west(void)
00186 {
00187     return (U.west);
00188 }
00189 double D_get_u_east(void)
00190 {
00191     return (U.east);
00192 }
00193 double D_get_u_north(void)
00194 {
00195     return (U.north);
00196 }
00197 double D_get_u_south(void)
00198 {
00199     return (U.south);
00200 }
00201 
00202 double D_get_a_west(void)
00203 {
00204     return (A.west);
00205 }
00206 double D_get_a_east(void)
00207 {
00208     return (A.east);
00209 }
00210 double D_get_a_north(void)
00211 {
00212     return (A.north);
00213 }
00214 double D_get_a_south(void)
00215 {
00216     return (A.south);
00217 }
00218 
00219 double D_get_d_west(void)
00220 {
00221     return (D.west);
00222 }
00223 double D_get_d_east(void)
00224 {
00225     return (D.east);
00226 }
00227 double D_get_d_north(void)
00228 {
00229     return (D.north);
00230 }
00231 double D_get_d_south(void)
00232 {
00233     return (D.south);
00234 }
00235 
00236 void D_get_u(double x[2][2])
00237 {
00238     x[0][0] = U.west;
00239     x[0][1] = U.east;
00240     x[1][0] = U.north;
00241     x[1][1] = U.south;
00242 }
00243 
00244 void D_get_a(int x[2][2])
00245 {
00246     x[0][0] = (int)A.west;
00247     x[0][1] = (int)A.east;
00248     x[1][0] = (int)A.north;
00249     x[1][1] = (int)A.south;
00250 }
00251 
00252 void D_get_d(int x[2][2])
00253 {
00254     x[0][0] = (int)D.west;
00255     x[0][1] = (int)D.east;
00256     x[1][0] = (int)D.north;
00257     x[1][1] = (int)D.south;
00258 }
00259 
00270 double D_u_to_a_row(double U_row)
00271 {
00272     return (U.north - U_row) / resolution.y;
00273 }
00274 
00275 
00286 double D_u_to_a_col(double U_col)
00287 {
00288     return (U_col - U.west) / resolution.x;
00289 }
00290 
00291 
00302 double D_a_to_d_row(double A_row)
00303 {
00304     return D.north + (A_row - A.north) / D_to_A_conv.y;
00305 }
00306 
00307 
00319 double D_a_to_d_col(double A_col)
00320 {
00321     return D.west + (A_col - A.west) / D_to_A_conv.x;
00322 }
00323 
00324 
00335 double D_u_to_d_row(double U_row)
00336 {
00337     return D.north + (U.north - U_row) * U_to_D_conv.y;
00338 }
00339 
00340 
00351 double D_u_to_d_col(double U_col)
00352 {
00353     return D.west + (U_col - U.west) * U_to_D_conv.x;
00354 }
00355 
00356 
00367 double D_d_to_u_row(double D_row)
00368 {
00369     return U.north - (D_row - D.north) / U_to_D_conv.y;
00370 }
00371 
00372 
00383 double D_d_to_u_col(double D_col)
00384 {
00385     return U.west + (D_col - D.west) / U_to_D_conv.x;
00386 }
00387 
00388 
00399 double D_d_to_a_row(double D_row)
00400 {
00401     return A.north + (D_row - D.north) * D_to_A_conv.y;
00402 }
00403 
00404 
00415 double D_d_to_a_col(double D_col)
00416 {
00417     return A.west + (D_col - D.west) * D_to_A_conv.x;
00418 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines