1 #include <stdio.h>
2 #include <grass/gis.h>
3 #include <grass/vector.h>
4 #include <grass/glocale.h>
5 #include "grid_structs.h"
6 #include "local_proto.h"
7 
8 static double xarray[10];
9 static double yarray[10];
10 
11 #define  NUM_POINTS  2
12 
write_vect(double x1,double y1,double x2,double y2,struct Map_info * Map,struct line_pnts * Points,int out_type)13 int write_vect(double x1, double y1, double x2, double y2,
14 	       struct Map_info *Map, struct line_pnts *Points, int out_type)
15 {
16     static struct line_cats *Cats = NULL;
17 
18     if (!Cats) {
19 	Cats = Vect_new_cats_struct();
20     }
21 
22     xarray[0] = x1;
23     xarray[1] = x2;
24     yarray[0] = y1;
25     yarray[1] = y2;
26 
27     if (0 > Vect_copy_xyz_to_pnts(Points, xarray, yarray, NULL, NUM_POINTS))
28 	G_fatal_error(_("Out of memory"));
29     Vect_write_line(Map, out_type, Points, Cats);
30 
31     return 0;
32 }
33 
write_grid(struct grid_description * grid_info,struct Map_info * Map,int nbreaks,int out_type,int diag)34 int write_grid(struct grid_description *grid_info, struct Map_info *Map,
35                int nbreaks, int out_type, int diag)
36 {
37 
38     int i, k, j;
39     int rows, cols;
40     int num_v_rows, num_v_cols;
41     double x, y, x_len, y_len;
42     double sx, sy;
43     double width, height;
44     double next_x, next_y;
45     double snext_x, snext_y;
46     double xdiag, ydiag;
47     double angle, dum;
48 
49     struct line_pnts *Points;
50 
51     Points = Vect_new_line_struct();
52 
53     num_v_rows = grid_info->num_vect_rows;
54     num_v_cols = grid_info->num_vect_cols;
55     rows = grid_info->num_rows;
56     cols = grid_info->num_cols;
57     width = grid_info->width;
58     height = grid_info->height;
59     angle = grid_info->angle;
60 
61     /*
62      * For latlon, must draw in shorter sections
63      * to make sure that each section of the grid
64      * line is less than half way around the globe
65      */
66     x_len = width / (1. * nbreaks + 1);
67     y_len = height / (1. * nbreaks + 1);
68 
69     /* write out all the vector lengths (x vectors) of the entire grid  */
70     G_message(_("Writing out vector rows..."));
71     y = grid_info->south;
72     for (i = 0; i < num_v_rows; ++i) {
73 	double startx;
74 
75 	startx = grid_info->west;
76 	G_percent(i, num_v_rows, 2);
77 
78 	for (k = 0; k < cols; k++) {
79 	    x = startx;
80             j = 0;
81 	    do {
82 		if (j < nbreaks)
83 		    next_x = x + x_len;
84 		else
85 		    next_x = startx + width;
86 
87 		sx = x;
88 		sy = y;
89 		snext_x = next_x;
90 		dum = y;
91 
92 		rotate(&x, &y, grid_info->xo, grid_info->yo,
93 		       angle);
94 		rotate(&next_x, &dum, grid_info->xo, grid_info->yo,
95 		       angle);
96 		write_vect(x, y, next_x, dum, Map, Points, out_type);
97 		if (diag && i < num_v_rows - 1) {
98 		    xdiag = snext_x;
99 		    ydiag = sy + height;
100 		    rotate(&xdiag, &ydiag, grid_info->xo, grid_info->yo,
101 			   angle);
102 		    write_vect(x, y, xdiag, ydiag, Map, Points, out_type);
103 
104 		    xdiag = sx;
105 		    ydiag = sy + height;
106 		    rotate(&xdiag, &ydiag, grid_info->xo, grid_info->yo,
107 			   angle);
108 		    write_vect(xdiag, ydiag, next_x, dum, Map, Points, out_type);
109 		}
110 
111 		y = sy;
112 		x = next_x = snext_x;
113                 j++;
114 	    } while (j <= nbreaks);
115 	    startx += width;
116 	}
117 	y += height;
118     }
119     G_percent(1, 1, 1);
120 
121     /* write out all the vector widths (y vectors) of the entire grid  */
122     G_message(_("Writing out vector columns..."));
123     x = grid_info->west;
124     for (i = 0; i < num_v_cols; ++i) {
125         double starty;
126 	starty = grid_info->south;
127 	G_percent(i, num_v_cols, 2);
128 
129 	for (k = 0; k < rows; k++) {
130 	    y = starty;
131 	    j = 0;
132 	    do {
133 	        if (j < nbreaks)
134 		    next_y = y + y_len;
135 	        else
136 		    next_y = starty + height;
137 
138 		sx = x;
139 		sy = y;
140 		snext_y = next_y;
141 		dum = x;
142 		rotate(&x, &y, grid_info->xo, grid_info->yo, angle);
143 		rotate(&dum, &next_y, grid_info->xo, grid_info->yo,
144 		    angle);
145 
146 		write_vect(x, y, dum, next_y, Map, Points, out_type);
147 
148 	        x = sx;
149 	        y = next_y = snext_y;
150 	        j++;
151 	    } while (j <= nbreaks);
152 	    /* To get exactly the same coordinates as above, y+=width is wrong */
153 	    starty += height;
154 	}
155 	x += width;
156     }
157     G_percent(1, 1, 1);
158 
159     /* new with Vlib */
160     Vect_destroy_line_struct(Points);
161 
162     return (0);
163 }
164