File: do_grid.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (233 lines) | stat: -rw-r--r-- 6,336 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* Functions: do_grid, do_grid_numbers, format_northing, format_easting
**
** These functions are much modified versions of the p.map functions.
**
** Author: Paul W. Carlson	April 1992
*/
#include <string.h>
#include <math.h>
#include "ps_info.h"
#include "local_proto.h"

#define LEFT 0
#define RIGHT 1
#define LOWER 0
#define UPPER 1
#define CENTER 2

static char *format_northing(double,int);
static char *format_easting(double,int);

int do_grid (void)
{
    double g, e1, e2;
    int j;

    if (PS.grid <= 0) return 1;

    /* set color and set line width to 1 */
    set_rgb_color(PS.grid_color);
    set_line_width(PS.grid_width);

    /* draw horizontal lines in 3 pieces -- lat-lon lines must not
     * extend more than half the globe
     * start with first grid line just south of the window north
     */
    e1 = (PS.w.east*2 + PS.w.west)/3;
    e2 = (PS.w.west*2 + PS.w.east)/3;

    g = floor(PS.w.north / PS.grid) * PS.grid ;
    for (j = 0; g >= PS.w.south; j ++, g -= PS.grid)
    {
        if (g == PS.w.north || g == PS.w.south) continue;

        start_line(PS.w.east, g);
        sec_draw = 0;
	G_plot_line (PS.w.east, g, e1, g);
	fprintf(PS.fp, " D ");

        start_line(e1, g);
        sec_draw = 0;
	G_plot_line (e1, g, e2, g);
	fprintf(PS.fp, " D ");

        start_line(e2, g);
        sec_draw = 0;
	G_plot_line (e2, g, PS.w.west, g);
	fprintf(PS.fp, " D\n");
    }

    /* vertical lines */
    /* start with first grid line just west of the window east */
    g = floor (PS.w.east / PS.grid) * PS.grid ;
    for (j = 0; g > PS.w.west; j++, g -= PS.grid)
    {
	if (g == PS.w.east || g == PS.w.west) continue;
	start_line(g, PS.w.north);
	sec_draw = 0;
	G_plot_line(g, PS.w.north, g, PS.w.south);
	if (j & 1) fprintf(PS.fp, " D\n");
	else fprintf(PS.fp, " D ");
    }

    return 0;
}

int do_grid_cross (void)
{
    double g_north, g_east;
    int j, k;

    if (PS.grid <= 0) return 1;

    /* set color and set line width to 1 */
    set_rgb_color(PS.grid_color);
    set_line_width(PS.grid_width);

    g_north = floor(PS.w.north / PS.grid) * PS.grid ;
    g_east = floor (PS.w.east / PS.grid) * PS.grid ;
    for (j = 0; g_north >= PS.w.south; j++, g_north -= PS.grid) {
    	for (k = 0; g_east > PS.w.west; k++, g_east -= PS.grid) {
    	
    	if (g_north == PS.w.north || g_north == PS.w.south) continue;
    	if (g_east == PS.w.east || g_east == PS.w.west) continue;
    	
    	start_line(g_east-PS.grid_cross, g_north);
    	G_plot_line (g_east-PS.grid_cross, g_north, g_east+PS.grid_cross, g_north);
    	fprintf(PS.fp, " D ");
	start_line(g_east, g_north-PS.grid_cross);
    	G_plot_line (g_east, g_north-PS.grid_cross, g_east, g_north+PS.grid_cross);
    	fprintf(PS.fp, " D ");
    	}
	g_east = floor (PS.w.east / PS.grid) * PS.grid ;
    }

    return 0;
}

int do_grid_numbers (void)
{
    double g;
    char num_text[50];
    int grid, vy, vx, hy=0, hx=0;
    int first, len, x, y, last_bottom, last_right;
    int rounded_grid, margin;

    if (PS.grid <= 0 || PS.grid_numbers <= 0) return 1;
    grid = PS.grid * PS.grid_numbers;

    /* round grid to multiple of 10 */
    rounded_grid = 1;
    if (PS.w.proj != PROJECTION_LL)
    {
	sprintf(num_text, "%d", PS.grid);
	len = strlen(num_text);
	while (len-- && num_text[len] == '0') rounded_grid *= 10;
	if (rounded_grid == 10) rounded_grid = 1;
    }

    /* initialize */
    set_font_name(PS.grid_font);
    set_font_size(PS.grid_fontsize);
    set_rgb_color(PS.grid_numbers_color);
    first = 1;

    /* horizontal grid numbers
     * these numbers only appear on the left edge of the first panel.
     * center the numbers on each grid line.
     * suppress number if it falls off the map or would overlay the previous
     *  label
     */
    margin = (int)(0.2 * (double)PS.grid_fontsize + 0.5);
    if (margin < 2) margin = 2;
    fprintf(PS.fp, "/mg %d def\n", margin);
    g = floor(PS.w.north / grid) * grid ;
    last_bottom = (int)PS.map_top;
    first = 1;
    /* x = XCONV(PS.w.west);*/

    for (; g > PS.w.south; g -= grid)
    {
	/*y = YCONV(g);*/

	G_plot_where_xy(PS.w.west, g, &vx, &vy);
	x = (double) vx / 10.;
	y = (double) vy / 10.;

	if (y + PS.grid_fontsize > last_bottom) continue;
	if (y - PS.grid_fontsize < (int)PS.map_bot) continue;
        sprintf(num_text, "%s", format_northing(g, rounded_grid));
	text_box_path(x, y, LEFT, CENTER, num_text, PS.grid_fontsize, 0);
	set_rgb_color(WHITE);
	fprintf(PS.fp, "F ");
	set_rgb_color(PS.grid_numbers_color);
	fprintf(PS.fp, "TIB\n");
	last_bottom = y - PS.grid_fontsize;
	if (first)
	{
	    first = 0;
	    hy = y + (int)(0.5 * (double)PS.grid_fontsize + 0.5) + margin;
	    hx = x + 0.7 * strlen(num_text) * PS.grid_fontsize + 2 * margin;
	}
    }

    /* vertical grid numbers 
     * center the numbers on each grid line.
     * suppress number if it falls of the map or would overlay the previous
     *  label
     */
    g = floor(PS.w.west / grid) * grid ;
    last_right = (int)PS.map_left;
    /* y = YCONV(PS.w.north);*/
    for (; g < PS.w.east; g += grid)
    {
	/* x = XCONV(g);*/

	G_plot_where_xy(g, PS.w.north, &vx, &vy);
	x = (double) vx / 10.;
	y = (double) vy / 10.;

	if (x - PS.grid_fontsize < last_right) continue;
	if (x + PS.grid_fontsize > (int)PS.map_right) continue;
        sprintf(num_text, "%s", format_easting(g, rounded_grid));
	vy = y - 0.7 * strlen(num_text) * PS.grid_fontsize - 2 * margin;
	vx = x - (int)(0.5 * (double)PS.grid_fontsize + 0.5) - margin;
	if (vx < hx && vy < hy) continue;
    	fprintf(PS.fp, "ZB (%s) PB 90 rotate\n", num_text);
	fprintf(PS.fp, "%d br sub bl add mg add\n", y);
    	fprintf(PS.fp, "%d bt bb sub D2 add mg sub neg TR TB\n", x);
	set_rgb_color(WHITE);
	fprintf(PS.fp, "F ");
	set_rgb_color(PS.grid_numbers_color);
	fprintf(PS.fp, "TIB\n");
	last_right = x + PS.grid_fontsize;
    }

    return 0;
}

static char *format_northing( double n, int round)
{
    static char text[50];

    if (PS.w.proj == PROJECTION_LL) G_format_northing(n, text, PS.w.proj);
    else
    {
	n = floor(n / round);
	sprintf(text, "%.0f", n);
    }
    return text;
}

static char *format_easting( double e, int round)
{
    static char text[50];

    if (PS.w.proj == PROJECTION_LL) G_format_easting(e, text, PS.w.proj);
    else
    {
	e = floor(e / round);
	sprintf(text, "%.0f", e);
    }
    return text;
}