1 /* Function: map_setup
2  **
3  ** Author: Paul W. Carlson     3/92
4  */
5 
6 #include <grass/gis.h>
7 #include <grass/glocale.h>
8 #include "local_proto.h"
9 #include "distance.h"
10 
11 
map_setup(void)12 int map_setup(void)
13 {
14     double w, h;
15 
16     /* set top of map */
17     if (PS.set_y < PS.min_y)
18 	PS.min_y = PS.set_y;
19     PS.map_y_orig = PS.min_y / 72.0;
20 
21     if (!PS.do_raster && !grp.do_group) {
22 	/* if scale has been set... */
23 	if (PS.scaletext[0]) {
24 	    /* if scaled map will fit in map limits... */
25 	    w = scale(PS.scaletext);
26 	    h = w * (PS.w.north - PS.w.south) / (PS.w.east - PS.w.west);
27 	    if (w <= PS.map_width && h <= PS.map_height) {
28 		PS.map_width = w;
29 		PS.map_height = h;
30 		PS.map_pix_wide = 72.0 * PS.map_width;
31 		PS.map_pix_high = 72.0 * PS.map_height;
32 	    }
33 	    else    /* kill the scale */
34 		PS.scaletext[0] = 0;
35 	}
36 
37 	/* fit map to bounding box */
38 	fit_map_to_box();
39     }
40 
41     else {
42 	if (PS.scaletext[0]) {
43 	    /* if scaled map will fit in map limits... */
44 	    w = scale(PS.scaletext);
45 	    h = w * PS.w.ns_res * (double)PS.w.rows /
46 		(PS.w.ew_res * (double)PS.w.cols);
47 	    if (w <= PS.map_width && h <= PS.map_height) {
48 		PS.map_width = w;
49 		PS.map_height = h;
50 		PS.map_pix_wide = 72.0 * PS.map_width;
51 		PS.map_pix_high = 72.0 * PS.map_height;
52 	    }
53 	    else    /* kill the scale */
54 		PS.scaletext[0] = 0;
55 	}
56 
57 	/* fit map to bounding box */
58 	fit_map_to_box();
59 
60 	PS.cells_high = PS.w.rows;
61 	PS.cells_wide = PS.w.cols;
62 	PS.ew_res = PS.w.ew_res;
63 	PS.ns_res = PS.w.ns_res;
64 
65 	PS.row_delta = 1;
66 	PS.col_delta = 1;
67 
68 	/* compute conversion factors */
69 	PS.ew_to_x = PS.map_pix_wide / (PS.w.east - PS.w.west);
70 	PS.ns_to_y = PS.map_pix_high / (PS.w.north - PS.w.south);
71     }
72 
73     /* set the scale */
74     /*   work from height not width to minimize lat/lon curvature problems?? */
75     if (!PS.scaletext[0]) {
76 	sprintf(PS.scaletext, "1 : %.0f",
77 		METERS_TO_INCHES * distance(PS.w.east,
78 					    PS.w.west) * 72.0 /
79 		PS.map_pix_wide);
80     }
81 
82     G_message(_("Scale set to %s"), PS.scaletext);
83 
84     /* compute map edges */
85     PS.map_left = 72.0 * PS.map_x_orig;
86     PS.map_top = 72.0 * PS.map_y_orig;
87     PS.map_bot = PS.map_top - PS.map_pix_high;
88     PS.map_right = PS.map_left + PS.map_pix_wide;
89     PS.min_y = PS.map_bot;
90 
91     /* we want the size to be 10 times biger, because G_plot_where_xy()
92        returns integer values (pixels) for x and y, and we want doubles
93        until the first decimal point. Then in move() and cont() we will
94        divide x and y by 10. to get double coordinates */
95     G_setup_plot(PS.map_top * 10., PS.map_bot * 10., PS.map_left * 10.,
96 		 PS.map_right * 10., move_local, cont_local);
97 
98     /* debug fprintf (stdout,"t %.1f b %.1f l %.1f r %.1f\n", PS.map_top,
99        PS.map_bot, PS.map_left, PS.map_right);
100      */
101 
102     /* no need to go on if we're just here for a look-see. (-b flag) */
103     if (!PS.fp)
104 	return 0;
105 
106 
107     /* save original graphics state */
108     fprintf(PS.fp, "gsave ");
109 
110     /* compute conversion factor from meters to PostScript window coordinates */
111     /*
112        G_begin_distance_calculations();
113        meters_to_PS = (PS.map_top - PS.map_bot) / G_distance(0., PS.w.south, 0., PS.w.north);
114      */
115 
116     /* clip to edge of border */
117     box_clip(PS.map_top - 1.0, PS.map_bot + 1.0,
118 	     PS.map_left + 1.0, PS.map_right - 1.0);
119 
120     return 0;
121 }
122