GRASS Programmer's Manual  6.4.1(2011)
window_map.c
Go to the documentation of this file.
00001 
00017 #include <stdlib.h>
00018 #include <grass/gis.h>
00019 #include "G.h"
00020 
00021 
00022 #define alloc_index(n) (COLUMN_MAPPING *) G_malloc((n)*sizeof(COLUMN_MAPPING))
00023 
00024 
00036 int G__create_window_mapping(int fd)
00037 {
00038     struct fileinfo *fcb = &G__.fileinfo[fd];
00039     COLUMN_MAPPING *col;
00040     int i;
00041     int x;
00042     double C1, C2;
00043     double west;
00044 
00045     G__init_window();
00046 
00047     if (fcb->open_mode >= 0 && fcb->open_mode != OPEN_OLD)      /* open for write? */
00048         return 0;
00049     if (fcb->open_mode == OPEN_OLD)     /* already open ? */
00050         G_free(fcb->col_map);
00051 
00052     col = fcb->col_map = alloc_index(G__.window.cols);
00053 
00054     /*
00055      * for each column in the window, go to center of the cell,
00056      * compute nearest column in the data file
00057      * if column is not in data file, set column to 0
00058      *
00059      * for lat/lon move window so that west is bigger than
00060      * cellhd west.
00061      */
00062     west = G__.window.west;
00063     if (G__.window.proj == PROJECTION_LL) {
00064         while (west > fcb->cellhd.west + 360.0)
00065             west -= 360.0;
00066         while (west < fcb->cellhd.west)
00067             west += 360.0;
00068     }
00069 
00070     C1 = G__.window.ew_res / fcb->cellhd.ew_res;
00071     C2 = (west - fcb->cellhd.west +
00072           G__.window.ew_res / 2.0) / fcb->cellhd.ew_res;
00073     for (i = 0; i < G__.window.cols; i++) {
00074         x = C2;
00075         if (C2 < x)             /* adjust for rounding of negatives */
00076             x--;
00077         if (x < 0 || x >= fcb->cellhd.cols)     /* not in data file */
00078             x = -1;
00079         *col++ = x + 1;
00080         C2 += C1;
00081     }
00082 
00083     /* do wrap around for lat/lon */
00084     if (G__.window.proj == PROJECTION_LL) {
00085         col = fcb->col_map;
00086         C2 = (west - 360.0 - fcb->cellhd.west +
00087               G__.window.ew_res / 2.0) / fcb->cellhd.ew_res;
00088         for (i = 0; i < G__.window.cols; i++) {
00089             x = C2;
00090             if (C2 < x)         /* adjust for rounding of negatives */
00091                 x--;
00092             if (x < 0 || x >= fcb->cellhd.cols) /* not in data file */
00093                 x = -1;
00094             if (*col == 0)      /* only change those not already set */
00095                 *col = x + 1;
00096             col++;
00097             C2 += C1;
00098         }
00099     }
00100 
00101     G_debug(3, "create window mapping (%d columns)", G__.window.cols);
00102 /*  for (i = 0; i < G__.window.cols; i++)
00103         fprintf(stderr, "%s%ld", i % 15 ? " " : "\n", (long)fcb->col_map[i]);
00104     fprintf(stderr, "\n");
00105 */
00106 
00107     /* compute C1,C2 for row window mapping */
00108     fcb->C1 = G__.window.ns_res / fcb->cellhd.ns_res;
00109     fcb->C2 =
00110         (fcb->cellhd.north - G__.window.north +
00111          G__.window.ns_res / 2.0) / fcb->cellhd.ns_res;
00112 
00113     return 0;
00114 }
00115 
00116 
00129 double G_northing_to_row(double north, const struct Cell_head *window)
00130 {
00131     return (window->north - north) / window->ns_res;
00132 }
00133 
00134 
00149 double G_adjust_east_longitude(double east, double west)
00150 {
00151     while (east > west + 360.0)
00152         east -= 360.0;
00153     while (east <= west)
00154         east += 360.0;
00155 
00156     return east;
00157 }
00158 
00159 
00174 double G_adjust_easting(double east, const struct Cell_head *window)
00175 {
00176     if (window->proj == PROJECTION_LL) {
00177         east = G_adjust_east_longitude(east, window->west);
00178         if (east > window->east && east == window->west + 360)
00179             east = window->west;
00180     }
00181 
00182     return east;
00183 }
00184 
00185 
00198 double G_easting_to_col(double east, const struct Cell_head *window)
00199 {
00200     east = G_adjust_easting(east, window);
00201 
00202     return (east - window->west) / window->ew_res;
00203 }
00204 
00205 
00223 double G_row_to_northing(double row, const struct Cell_head *window)
00224 {
00225     return window->north - row * window->ns_res;
00226 }
00227 
00228 
00243 double G_col_to_easting(double col, const struct Cell_head *window)
00244 {
00245     return window->west + col * window->ew_res;
00246 }
00247 
00248 
00273 int G_window_rows(void)
00274 {
00275     G__init_window();
00276 
00277     return G__.window.rows;
00278 }
00279 
00280 
00306 int G_window_cols(void)
00307 {
00308     G__init_window();
00309 
00310     return G__.window.cols;
00311 }
00312 
00313 
00320 int G__init_window(void)
00321 {
00322     if (!G__.window_set) {
00323         G__.window_set = 1;
00324         G_get_window(&G__.window);
00325     }
00326 
00327     return 0;
00328 }
00329 
00330 
00344 int G_row_repeat_nomask(int fd, int row)
00345 {
00346     struct fileinfo *fcb = &G__.fileinfo[fd];
00347     double f;
00348     int r1, r2;
00349     int count;
00350 
00351     count = 1;
00352 
00353     /* r1 is the row in the raster map itself.
00354      * r2 is the next row(s) in the raster map
00355      * see get_row.c for details on this calculation
00356      */
00357     f = row * fcb->C1 + fcb->C2;
00358     r1 = f;
00359     if (f < r1)
00360         r1--;
00361 
00362     while (++row < G__.window.rows) {
00363         f = row * fcb->C1 + fcb->C2;
00364         r2 = f;
00365         if (f < r2)
00366             r2--;
00367         if (r1 != r2)
00368             break;
00369 
00370         count++;
00371     }
00372 
00373     return count;
00374 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines