~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/raster/window_map.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

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