File: plmap.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 571,248 kB
  • ctags: 39,971
  • sloc: ansic: 460,578; java: 29,439; perl: 13,573; sh: 12,740; makefile: 3,275; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (317 lines) | stat: -rw-r--r-- 10,308 bytes parent folder | download | duplicates (9)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* $Id: plmap.c,v 1.2 2007/05/08 09:09:37 rice Exp $

	Continental Outline and Political Boundary Backgrounds

	Some plots need a geographical background such as the global
	surface temperatures or the population density.  The routine
	plmap() will draw one of the following backgrounds: continental
	outlines, political boundaries, the United States, and the United
	States with the continental outlines.  The routine plmeridians()
	will add the latitudes and longitudes to the background.  After
	the background has been drawn, one can use a contour routine or a
	symbol plotter to finish off the plot.

	Copyright (C) 1991, 1993, 1994  Wesley Ebisuzaki
        Copyright (C) 1994, 2000, 2001  Maurice LeBrun
        Copyright (C) 1999  Geoffrey Furnish
        Copyright (C) 2000, 2001, 2002  Alan W. Irwin
        Copyright (C) 2001  Andrew Roach
        Copyright (C) 2001, 2004  Rafael Laboissiere
        Copyright (C) 2002  Vincent Darley
        Copyright (C) 2003  Joao Cardoso

        This file is part of PLplot.

	PLplot is free software; you can redistribute it and/or modify
	it under the terms of the GNU Library General Public License
	as published by the Free Software Foundation; version 2 of the
	License.

	PLplot is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License for more details.

	You should have received a copy of the GNU Library General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
        USA

*/

#include "plplotP.h"

/*----------------------------------------------------------------------*\
 * void plmap(void (*mapform)(PLINT, PLFLT *, PLFLT *), char *type,
 *            PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat);
 *
 * plot continental outline in world coordinates
 *
 * v1.4: machine independant version
 * v1.3: replaced plcontinent by plmap, added plmeridians
 * v1.2: 2 arguments:  mapform, type of plot
 *
 * mapform(PLINT n, PLFLT *x, PLFLT *y) is a routine to transform the
 * coordinate longitudes and latitudes to a plot coordinate system.  By
 * using this transform, we can change from a longitude, latitude
 * coordinate to a polar stereographic project, for example.  Initially,
 * x[0]..[n-1] are the longitudes and y[0]..y[n-1] are the corresponding
 * latitudes.  After the call to mapform(), x[] and y[] should be replaced
 * by the corresponding plot coordinates.  If no transform is desired,
 * mapform can be replaced by NULL.
 *
 * type is a character string. The value of this parameter determines the
 * type of background. The possible values are,
 *
 * 	"globe"		continental outlines
 * 	"usa"		USA and state boundaries
 * 	"cglobe"	continental outlines and countries
 * 	"usaglobe"	USA, state boundaries and continental outlines
 *
 * minlong, maxlong are the values of the longitude on the left and right
 * side of the plot, respectively. The value of minlong must be less than
 * the values of maxlong, and the values of maxlong-minlong must be less
 * or equal to 360.
 *
 * minlat, maxlat are the minimum and maximum latitudes to be plotted on
 * the background.  One can always use -90.0 and 90.0 as the boundary
 * outside the plot window will be automatically eliminated.  However, the
 * program will be faster if one can reduce the size of the background
 * plotted.
\*----------------------------------------------------------------------*/

#define MAP_FILE ".map"
#define OFFSET (180*100)
#define SCALE 100.0
#define W_BUFSIZ	(32*1024)

void
plmap( void (*mapform)(PLINT, PLFLT *, PLFLT *), char *type,
         PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat )
{
    PLINT wrap, sign;
    int i, j;
    PLFLT bufx[200], bufy[200], x[2], y[2];
    short int test[400];
    register PDFstrm *in;
    char filename[100];

    unsigned char n_buff[2], buff[800];
    int n;
    long int t;

    (void) minlat;			/* pmr: make it used */
    (void) maxlat;			/* pmr: make it used */

    /*
     * read map outline
     */
    strcpy(filename,type);
    strcat(filename,MAP_FILE);

    if ((in = plLibOpenPdfstrm(filename)) == NULL)
	return;

    for (;;) {
	/* read in # points in segment */
	if (pdf_rdx(n_buff, sizeof (unsigned char)* 2, in) == 0) break;
	n = (n_buff[0] << 8) + n_buff[1];
	if (n == 0) break;

	pdf_rdx(buff, sizeof (unsigned char)*4*n, in);
	if (n == 1) continue;

	for (j = i = 0; i < n; i++, j += 2) {
	    t = (buff[j] << 8) + buff[j+1];
	    bufx[i] = (t - OFFSET) / SCALE;
	}
	for (i = 0; i < n; i++, j += 2) {
	    t = (buff[j] << 8) + buff[j+1];
	    bufy[i] = (t - OFFSET) / SCALE;
	}

	for (i = 0; i < n; i++) {
	    while (bufx[i] < minlong) {
		bufx[i] += 360.0;
	    }
	    while (bufx[i] > maxlong) {
		bufx[i] -= 360.0;
	    }
	}

	/* remove last 2 points if both outside of domain and won't plot */

/* AR: 18/11/01
*       I have commented out the next section which supposedly
*       removes points that do not plot within the domain.
*
*       This code appears at any rate to be superseded by the next
*       block of code that checks for wrapping problems. Removing
*       this code seems to have fixed up the problems with mapping
*       function, but I do not wish to delete it outright just now in
*       case I have managed to miss something.
*/

/*	while (n > 1) {
	    if ((bufx[n-1] < minlong && bufx[n-2] < minlong) ||
		(bufx[n-1] > maxlong && bufx[n-2] > maxlong) ||
		(bufy[n-1] < minlat && bufy[n-2] < minlat) ||
		(bufy[n-1] > maxlat && bufy[n-2] > maxlat)) {
		n--;
	    }
	    else {
		break;
	    }
	}
	if (n <= 1) continue;
*/

       if (mapform != NULL) (*mapform)(n,bufx,bufy); /* moved transformation to here   */
                                                     /* so bound-checking worked right */

	wrap = 0;
	/* check for wrap around problems */
	for (i = 0; i < n-1; i++) {

	  /* jc: this code is wrong, as the bufx/y are floats that are
	     converted to ints before abs() is called. Thus, small
	     differences are masked off. The proof that it is wrong is
	     that when replacing abs() with fabs(), as it should be,
	     the code works the wrong way. What a proof :-)

	    test[i] = abs((bufx[i]-bufx[i+1])) > abs(bufy[i]/3);

	    If the intended behaviour is *really* that, than an
	    explicit cast should be used to help other programmers, as
	    is done bellow!!!
	  */

	    test[i] = abs((int)(bufx[i]-bufx[i+1])) > abs((int)bufy[i]/3); /* Changed this from 30 degrees so it is now "polar sensitive" */
	    if (test[i]) wrap = 1;
	}

	if (wrap == 0) {
	    plline(n,bufx,bufy);
	}
	else {
	    for (i = 0; i < n-1; i++) {
		x[0] = bufx[i];
		x[1] = bufx[i+1];
		y[0] = bufy[i];
		y[1] = bufy[i+1];
		if (test[i] == 0) {
		    plline(2,x,y);
		}
		else {  /* this code seems to supercede the block commented out above */
		    /* segment goes off the edge */
		    sign = (x[1] > x[0]) ? 1 : -1;
		    x[1] -= sign * 360.0;
		    plline(2,x,y);
		    x[0] = bufx[i];
		    x[1] = bufx[i+1];
		    y[0] = bufy[i];
		    y[1] = bufy[i+1];
		    x[0] += sign * 360.0;
		    plline(2,x,y);
		}
	    }
	}
    }
    /* Close map file */
    pdf_close(in);
}

/*----------------------------------------------------------------------*\
 * void plmeridians(void (*mapform)(PLINT, PLFLT *, PLFLT *),
 *		    PLFLT dlong, PLFLT dlat, PLFLT minlong, PLFLT maxlong,
 *		    PLFLT minlat, PLFLT maxlat);
 *
 * Plot the latitudes and longitudes on the background.  The lines
 * are plotted in the current color and line style.
 *
 * mapform(PLINT n, PLFLT *x, PLFLT *y) is a routine to transform the
 * coordinate longitudes and latitudes to a plot coordinate system.  By
 * using this transform, we can change from a longitude, latitude
 * coordinate to a polar stereographic project, for example.  Initially,
 * x[0]..x[n-1] are the longitudes and y[0]..y[n-1] are the corresponding
 * latitudes.  After the call to mapform(), x[] and y[] should be replaced
 * by the corresponding plot coordinates.  If no transform is desired,
 * mapform can be replaced by NULL.
 *
 * dlat, dlong are the interval in degrees that the latitude and longitude
 * lines are to be plotted.
 *
 * minlong, maxlong are the values of the longitude on the left and right
 * side of the plot, respectively. The value of minlong must be less than
 * the values of maxlong, and the values of maxlong-minlong must be less
 * or equal to 360.
 *
 * minlat, maxlat are the minimum and maximum latitudes to be plotted on
 * the background.  One can always use -90.0 and 90.0 as the boundary
 * outside the plot window will be automatically eliminated.  However, the
 * program will be faster if one can reduce the size of the background
 * plotted.
\*----------------------------------------------------------------------*/

#define NSEG 100

void
plmeridians( void (*mapform)(PLINT, PLFLT *, PLFLT *),
               PLFLT dlong, PLFLT dlat,
               PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat )
{
    PLFLT yy, xx, temp, x[2], y[2], dx, dy;

    if (minlong > maxlong) {
	temp = minlong;
	minlong = maxlong;
	maxlong = temp;
    }
    if (minlat > maxlat) {
	temp = minlat;
	minlat = maxlat;
	maxlat = temp;
    }
    dx = (maxlong - minlong) / NSEG;
    dy = (maxlat - minlat) / NSEG;

    /* latitudes */

    for (yy = dlat * ceil(minlat/dlat); yy <= maxlat; yy += dlat) {
	if (mapform == NULL) {
	    y[0] = y[1] = yy;
	    x[0] = minlong;
	    x[1] = maxlong;
	    plline(2,x,y);
	}
	else {
	    for (xx = minlong; xx < maxlong; xx += dx) {
	        y[0] = y[1] = yy;
		x[0] = xx;
		x[1] = xx + dx;
	 	(*mapform)(2,x,y);
		plline(2,x,y);
	    }
	}
    }

    /* longitudes */

    for (xx = dlong * ceil(minlong/dlong); xx <= maxlong; xx += dlong) {
        if (mapform == NULL) {
            x[0] = x[1] = xx;
            y[0] = minlat;
            y[1] = maxlat;
            plline(2,x,y);
        }
        else {
            for (yy = minlat; yy < maxlat; yy += dy) {
                x[0] = x[1] = xx;
                y[0] = yy;
                y[1] = yy + dy;
                (*mapform)(2,x,y);
                plline(2,x,y);
            }
        }
    }
}