1 28994509 2004-04-21 devnull #include <u.h>
2 28994509 2004-04-21 devnull #include <libc.h>
3 28994509 2004-04-21 devnull #include <stdio.h>
4 28994509 2004-04-21 devnull #include "map.h"
5 28994509 2004-04-21 devnull #include "iplot.h"
7 28994509 2004-04-21 devnull #define NVERT 20 /* max number of vertices in a -v polygon */
8 28994509 2004-04-21 devnull #define HALFWIDTH 8192 /* output scaled to fit in -HALFWIDTH,HALFWIDTH */
9 28994509 2004-04-21 devnull #define LONGLINES (HALFWIDTH*4) /* permissible segment lengths */
10 28994509 2004-04-21 devnull #define SHORTLINES (HALFWIDTH/8)
11 28994509 2004-04-21 devnull #define SCALERATIO 10 /* of abs to rel data (see map(5)) */
12 28994509 2004-04-21 devnull #define RESOL 2. /* coarsest resolution for tracing grid (degrees) */
13 28994509 2004-04-21 devnull #define TWO_THRD 0.66666666666666667
15 28994509 2004-04-21 devnull int normproj(double, double, double *, double *);
16 28994509 2004-04-21 devnull int posproj(double, double, double *, double *);
17 28994509 2004-04-21 devnull int picut(struct place *, struct place *, double *);
18 28994509 2004-04-21 devnull double reduce(double);
19 28994509 2004-04-21 devnull short getshort(FILE *);
20 28994509 2004-04-21 devnull char *mapindex(char *);
21 28994509 2004-04-21 devnull proj projection;
24 28994509 2004-04-21 devnull static char *mapdir = "/lib/map"; /* default map directory */
25 28994509 2004-04-21 devnull struct file {
26 28994509 2004-04-21 devnull char *name;
27 28994509 2004-04-21 devnull char *color;
28 28994509 2004-04-21 devnull char *style;
30 28994509 2004-04-21 devnull static struct file dfltfile = {
31 28994509 2004-04-21 devnull "world", BLACK, SOLID /* default map */
33 28994509 2004-04-21 devnull static struct file *file = &dfltfile; /* list of map files */
34 28994509 2004-04-21 devnull static int nfile = 1; /* length of list */
35 28994509 2004-04-21 devnull static char *currcolor = BLACK; /* current color */
36 28994509 2004-04-21 devnull static char *gridcolor = BLACK;
37 28994509 2004-04-21 devnull static char *bordcolor = BLACK;
39 28994509 2004-04-21 devnull extern struct index index[];
40 28994509 2004-04-21 devnull int halfwidth = HALFWIDTH;
42 28994509 2004-04-21 devnull static int (*cut)(struct place *, struct place *, double *);
43 28994509 2004-04-21 devnull static int (*limb)(double*, double*, double);
44 28994509 2004-04-21 devnull static void dolimb(void);
45 28994509 2004-04-21 devnull static int onlimb;
46 28994509 2004-04-21 devnull static int poles;
47 28994509 2004-04-21 devnull static double orientation[3] = { 90., 0., 0. }; /* -o option */
48 28994509 2004-04-21 devnull static int oriented; /* nonzero if -o option occurred */
49 28994509 2004-04-21 devnull static int upright; /* 1 if orientation[0]==90, -1 if -90, else 0*/
50 28994509 2004-04-21 devnull static int delta = 1; /* -d setting */
51 28994509 2004-04-21 devnull static double limits[4] = { /* -l parameters */
52 28994509 2004-04-21 devnull -90., 90., -180., 180.
54 28994509 2004-04-21 devnull static double klimits[4] = { /* -k parameters */
55 28994509 2004-04-21 devnull -90., 90., -180., 180.
57 28994509 2004-04-21 devnull static int limcase;
58 28994509 2004-04-21 devnull static double rlimits[4]; /* limits expressed in radians */
59 28994509 2004-04-21 devnull static double lolat, hilat, lolon, hilon;
60 28994509 2004-04-21 devnull static double window[4] = { /* option -w */
61 28994509 2004-04-21 devnull -90., 90., -180., 180.
63 28994509 2004-04-21 devnull static int windowed; /* nozero if option -w */
64 28994509 2004-04-21 devnull static struct vert { double x, y; } v[NVERT+2]; /*clipping polygon*/
65 28994509 2004-04-21 devnull static struct edge { double a, b, c; } e[NVERT]; /* coeffs for linear inequality */
66 28994509 2004-04-21 devnull static int nvert; /* number of vertices in clipping polygon */
68 28994509 2004-04-21 devnull static double rwindow[4]; /* window, expressed in radians */
69 28994509 2004-04-21 devnull static double params[2]; /* projection params */
70 28994509 2004-04-21 devnull /* bounds on output values before scaling; found by coarse survey */
71 28994509 2004-04-21 devnull static double xmin = 100.;
72 28994509 2004-04-21 devnull static double xmax = -100.;
73 28994509 2004-04-21 devnull static double ymin = 100.;
74 28994509 2004-04-21 devnull static double ymax = -100.;
75 28994509 2004-04-21 devnull static double xcent, ycent;
76 28994509 2004-04-21 devnull static double xoff, yoff;
77 28994509 2004-04-21 devnull double xrange, yrange;
78 28994509 2004-04-21 devnull static int left = -HALFWIDTH;
79 28994509 2004-04-21 devnull static int right = HALFWIDTH;
80 28994509 2004-04-21 devnull static int bottom = -HALFWIDTH;
81 28994509 2004-04-21 devnull static int top = HALFWIDTH;
82 28994509 2004-04-21 devnull static int longlines = SHORTLINES; /* drop longer segments */
83 28994509 2004-04-21 devnull static int shortlines = SHORTLINES;
84 28994509 2004-04-21 devnull static int bflag = 1; /* 0 for option -b */
85 28994509 2004-04-21 devnull static int s1flag = 0; /* 1 for option -s1 */
86 28994509 2004-04-21 devnull static int s2flag = 0; /* 1 for option -s2 */
87 28994509 2004-04-21 devnull static int rflag = 0; /* 1 for option -r */
88 28994509 2004-04-21 devnull static int kflag = 0; /* 1 if option -k occurred */
89 28994509 2004-04-21 devnull static int xflag = 0; /* 1 for option -x */
90 28994509 2004-04-21 devnull int vflag = 1; /* -1 if option -v occurred */
91 28994509 2004-04-21 devnull static double position[3]; /* option -p */
92 28994509 2004-04-21 devnull static double center[3] = {0., 0., 0.}; /* option -c */
93 28994509 2004-04-21 devnull static struct coord crot; /* option -c */
94 28994509 2004-04-21 devnull static double grid[3] = { 10., 10., RESOL }; /* option -g */
95 28994509 2004-04-21 devnull static double dlat, dlon; /* resolution for tracing grid in lat and lon */
96 28994509 2004-04-21 devnull static double scaling; /* to compute final integer output */
97 28994509 2004-04-21 devnull static struct file *track; /* options -t and -u */
98 28994509 2004-04-21 devnull static int ntrack; /* number of tracks present */
99 28994509 2004-04-21 devnull static char *symbolfile; /* option -y */
101 28994509 2004-04-21 devnull void clamp(double *px, double v);
102 28994509 2004-04-21 devnull void clipinit(void);
103 28994509 2004-04-21 devnull double diddle(struct place *, double, double);
104 28994509 2004-04-21 devnull double diddle(struct place *, double, double);
105 28994509 2004-04-21 devnull void dobounds(double, double, double, double, int);
106 28994509 2004-04-21 devnull void dogrid(double, double, double, double);
107 28994509 2004-04-21 devnull int duple(struct place *, double);
108 28994509 2004-04-21 devnull double fmax(double, double);
109 28994509 2004-04-21 devnull double fmin(double, double);
110 28994509 2004-04-21 devnull void getdata(char *);
111 28994509 2004-04-21 devnull int gridpt(double, double, int);
112 28994509 2004-04-21 devnull int inpoly(double, double);
113 28994509 2004-04-21 devnull int inwindow(struct place *);
114 28994509 2004-04-21 devnull void pathnames(void);
115 28994509 2004-04-21 devnull int pnorm(double);
116 28994509 2004-04-21 devnull void radbds(double *w, double *rw);
117 28994509 2004-04-21 devnull void revlon(struct place *, double);
118 28994509 2004-04-21 devnull void satellite(struct file *);
119 28994509 2004-04-21 devnull int seeable(double, double);
120 28994509 2004-04-21 devnull void windlim(void);
121 28994509 2004-04-21 devnull void realcut(void);
124 28994509 2004-04-21 devnull option(char *s)
127 28994509 2004-04-21 devnull if(s[0]=='-' && (s[1]<'0'||s[1]>'9'))
128 28994509 2004-04-21 devnull return(s[1]!='.'&&s[1]!=0);
130 28994509 2004-04-21 devnull return(0);
134 28994509 2004-04-21 devnull conv(int k, struct coord *g)
136 28994509 2004-04-21 devnull g->l = (0.0001/SCALERATIO)*k;
137 28994509 2004-04-21 devnull sincos(g);
141 28994509 2004-04-21 devnull main(int argc, char *argv[])
143 28994509 2004-04-21 devnull int i,k;
144 28994509 2004-04-21 devnull char *s, *t, *style;
145 28994509 2004-04-21 devnull double x, y;
146 28994509 2004-04-21 devnull double lat, lon;
147 28994509 2004-04-21 devnull double *wlim;
148 28994509 2004-04-21 devnull double dd;
149 28994509 2004-04-21 devnull if(sizeof(short)!=2)
150 28994509 2004-04-21 devnull abort(); /* getshort() won't work */
151 28994509 2004-04-21 devnull s = getenv("MAP");
153 28994509 2004-04-21 devnull file[0].name = s;
154 28994509 2004-04-21 devnull s = getenv("MAPDIR");
156 28994509 2004-04-21 devnull mapdir = s;
157 28994509 2004-04-21 devnull if(argc<=1)
158 28994509 2004-04-21 devnull error("usage: map projection params options");
159 28994509 2004-04-21 devnull for(k=0;index[k].name;k++) {
160 28994509 2004-04-21 devnull s = index[k].name;
161 28994509 2004-04-21 devnull t = argv[1];
162 28994509 2004-04-21 devnull while(*s == *t){
163 28994509 2004-04-21 devnull if(*s==0) goto found;
168 28994509 2004-04-21 devnull fprintf(stderr,"projections:\n");
169 28994509 2004-04-21 devnull for(i=0;index[i].name;i++) {
170 28994509 2004-04-21 devnull fprintf(stderr,"%s",index[i].name);
171 28994509 2004-04-21 devnull for(k=0; k<index[i].npar; k++)
172 28994509 2004-04-21 devnull fprintf(stderr," p%d", k);
173 28994509 2004-04-21 devnull fprintf(stderr,"\n");
175 28994509 2004-04-21 devnull exits("error");
177 28994509 2004-04-21 devnull argv += 2;
178 28994509 2004-04-21 devnull argc -= 2;
179 28994509 2004-04-21 devnull cut = index[k].cut;
180 28994509 2004-04-21 devnull limb = index[k].limb;
181 28994509 2004-04-21 devnull poles = index[k].poles;
182 28994509 2004-04-21 devnull for(i=0;i<index[k].npar;i++) {
183 28994509 2004-04-21 devnull if(i>=argc||option(argv[i])) {
184 28994509 2004-04-21 devnull fprintf(stderr,"%s needs %d params\n",index[k].name,index[k].npar);
185 28994509 2004-04-21 devnull exits("error");
187 28994509 2004-04-21 devnull params[i] = atof(argv[i]);
189 28994509 2004-04-21 devnull argv += i;
190 28994509 2004-04-21 devnull argc -= i;
191 28994509 2004-04-21 devnull while(argc>0&&option(argv[0])) {
194 28994509 2004-04-21 devnull switch(argv[-1][1]) {
195 28994509 2004-04-21 devnull case 'm':
196 28994509 2004-04-21 devnull if(file == &dfltfile) {
197 28994509 2004-04-21 devnull file = 0;
198 28994509 2004-04-21 devnull nfile = 0;
200 28994509 2004-04-21 devnull while(argc && !option(*argv)) {
201 28994509 2004-04-21 devnull file = realloc(file,(nfile+1)*sizeof(*file));
202 28994509 2004-04-21 devnull file[nfile].name = *argv;
203 28994509 2004-04-21 devnull file[nfile].color = currcolor;
204 28994509 2004-04-21 devnull file[nfile].style = SOLID;
205 28994509 2004-04-21 devnull nfile++;
210 28994509 2004-04-21 devnull case 'b':
211 28994509 2004-04-21 devnull bflag = 0;
212 28994509 2004-04-21 devnull for(nvert=0;nvert<NVERT&&argc>=2;nvert++) {
213 28994509 2004-04-21 devnull if(option(*argv))
215 28994509 2004-04-21 devnull v[nvert].x = atof(*argv++);
217 28994509 2004-04-21 devnull if(option(*argv))
219 28994509 2004-04-21 devnull v[nvert].y = atof(*argv++);
222 28994509 2004-04-21 devnull if(nvert>=NVERT)
223 28994509 2004-04-21 devnull error("too many clipping vertices");
225 28994509 2004-04-21 devnull case 'g':
226 28994509 2004-04-21 devnull gridcolor = currcolor;
227 28994509 2004-04-21 devnull for(i=0;i<3&&argc>i&&!option(argv[i]);i++)
228 28994509 2004-04-21 devnull grid[i] = atof(argv[i]);
229 28994509 2004-04-21 devnull switch(i) {
231 28994509 2004-04-21 devnull grid[0] = grid[1] = 0.;
234 28994509 2004-04-21 devnull grid[1] = grid[0];
236 28994509 2004-04-21 devnull argc -= i;
237 28994509 2004-04-21 devnull argv += i;
239 28994509 2004-04-21 devnull case 't':
240 28994509 2004-04-21 devnull style = SOLID;
241 28994509 2004-04-21 devnull goto casetu;
242 28994509 2004-04-21 devnull case 'u':
243 28994509 2004-04-21 devnull style = DOTDASH;
245 28994509 2004-04-21 devnull while(argc && !option(*argv)) {
246 28994509 2004-04-21 devnull track = realloc(track,(ntrack+1)*sizeof(*track));
247 28994509 2004-04-21 devnull track[ntrack].name = *argv;
248 28994509 2004-04-21 devnull track[ntrack].color = currcolor;
249 28994509 2004-04-21 devnull track[ntrack].style = style;
250 28994509 2004-04-21 devnull ntrack++;
255 28994509 2004-04-21 devnull case 'r':
256 28994509 2004-04-21 devnull rflag++;
258 28994509 2004-04-21 devnull case 's':
259 28994509 2004-04-21 devnull switch(argv[-1][2]) {
260 28994509 2004-04-21 devnull case '1':
261 28994509 2004-04-21 devnull s1flag++;
263 28994509 2004-04-21 devnull case 0: /* compatibility */
264 28994509 2004-04-21 devnull case '2':
265 28994509 2004-04-21 devnull s2flag++;
268 28994509 2004-04-21 devnull case 'o':
269 28994509 2004-04-21 devnull for(i=0;i<3&&i<argc&&!option(argv[i]);i++)
270 28994509 2004-04-21 devnull orientation[i] = atof(argv[i]);
271 28994509 2004-04-21 devnull oriented++;
272 28994509 2004-04-21 devnull argv += i;
273 28994509 2004-04-21 devnull argc -= i;
275 28994509 2004-04-21 devnull case 'l':
276 28994509 2004-04-21 devnull bordcolor = currcolor;
277 28994509 2004-04-21 devnull for(i=0;i<argc&&i<4&&!option(argv[i]);i++)
278 28994509 2004-04-21 devnull limits[i] = atof(argv[i]);
279 28994509 2004-04-21 devnull argv += i;
280 28994509 2004-04-21 devnull argc -= i;
282 28994509 2004-04-21 devnull case 'k':
283 28994509 2004-04-21 devnull kflag++;
284 28994509 2004-04-21 devnull for(i=0;i<argc&&i<4&&!option(argv[i]);i++)
285 28994509 2004-04-21 devnull klimits[i] = atof(argv[i]);
286 28994509 2004-04-21 devnull argv += i;
287 28994509 2004-04-21 devnull argc -= i;
289 28994509 2004-04-21 devnull case 'd':
290 28994509 2004-04-21 devnull if(argc>0&&!option(argv[0])) {
291 28994509 2004-04-21 devnull delta = atoi(argv[0]);
296 28994509 2004-04-21 devnull case 'w':
297 28994509 2004-04-21 devnull bordcolor = currcolor;
298 28994509 2004-04-21 devnull windowed++;
299 28994509 2004-04-21 devnull for(i=0;i<argc&&i<4&&!option(argv[i]);i++)
300 28994509 2004-04-21 devnull window[i] = atof(argv[i]);
301 28994509 2004-04-21 devnull argv += i;
302 28994509 2004-04-21 devnull argc -= i;
304 28994509 2004-04-21 devnull case 'c':
305 28994509 2004-04-21 devnull for(i=0;i<3&&argc>i&&!option(argv[i]);i++)
306 28994509 2004-04-21 devnull center[i] = atof(argv[i]);
307 28994509 2004-04-21 devnull argc -= i;
308 28994509 2004-04-21 devnull argv += i;
310 28994509 2004-04-21 devnull case 'p':
311 28994509 2004-04-21 devnull for(i=0;i<3&&argc>i&&!option(argv[i]);i++)
312 28994509 2004-04-21 devnull position[i] = atof(argv[i]);
313 28994509 2004-04-21 devnull argc -= i;
314 28994509 2004-04-21 devnull argv += i;
315 28994509 2004-04-21 devnull if(i!=3||position[2]<=0)
316 28994509 2004-04-21 devnull error("incomplete positioning");
318 28994509 2004-04-21 devnull case 'y':
319 28994509 2004-04-21 devnull if(argc>0&&!option(argv[0])) {
320 28994509 2004-04-21 devnull symbolfile = argv[0];
325 28994509 2004-04-21 devnull case 'v':
326 28994509 2004-04-21 devnull if(index[k].limb == 0)
327 28994509 2004-04-21 devnull error("-v does not apply here");
328 28994509 2004-04-21 devnull vflag = -1;
330 28994509 2004-04-21 devnull case 'x':
331 28994509 2004-04-21 devnull xflag = 1;
333 28994509 2004-04-21 devnull case 'C':
334 28994509 2004-04-21 devnull if(argc && !option(*argv)) {
335 28994509 2004-04-21 devnull currcolor = colorcode(*argv);
342 28994509 2004-04-21 devnull if(argc>0)
343 28994509 2004-04-21 devnull error("error in arguments");
344 28994509 2004-04-21 devnull pathnames();
345 28994509 2004-04-21 devnull clamp(&limits[0],-90.);
346 28994509 2004-04-21 devnull clamp(&limits[1],90.);
347 28994509 2004-04-21 devnull clamp(&klimits[0],-90.);
348 28994509 2004-04-21 devnull clamp(&klimits[1],90.);
349 28994509 2004-04-21 devnull clamp(&window[0],-90.);
350 28994509 2004-04-21 devnull clamp(&window[1],90.);
351 28994509 2004-04-21 devnull radbds(limits,rlimits);
352 28994509 2004-04-21 devnull limcase = limits[2]<-180.?0:
353 28994509 2004-04-21 devnull limits[3]>180.?2:
356 28994509 2004-04-21 devnull window[0]>=window[1]||
357 28994509 2004-04-21 devnull window[2]>=window[3]||
358 28994509 2004-04-21 devnull window[0]>90.||
359 28994509 2004-04-21 devnull window[1]<-90.||
360 28994509 2004-04-21 devnull window[2]>180.||
361 28994509 2004-04-21 devnull window[3]<-180.)
362 28994509 2004-04-21 devnull error("unreasonable window");
363 28994509 2004-04-21 devnull windlim();
364 28994509 2004-04-21 devnull radbds(window,rwindow);
365 28994509 2004-04-21 devnull upright = orientation[0]==90? 1: orientation[0]==-90? -1: 0;
366 28994509 2004-04-21 devnull if(index[k].spheroid && !upright)
367 28994509 2004-04-21 devnull error("can't tilt the spheroid");
368 28994509 2004-04-21 devnull if(limits[2]>limits[3])
369 28994509 2004-04-21 devnull limits[3] += 360;
370 28994509 2004-04-21 devnull if(!oriented)
371 28994509 2004-04-21 devnull orientation[2] = (limits[2]+limits[3])/2;
372 28994509 2004-04-21 devnull orient(orientation[0],orientation[1],orientation[2]);
373 28994509 2004-04-21 devnull projection = (*index[k].prog)(params[0],params[1]);
374 28994509 2004-04-21 devnull if(projection == 0)
375 28994509 2004-04-21 devnull error("unreasonable projection parameters");
376 28994509 2004-04-21 devnull clipinit();
377 28994509 2004-04-21 devnull grid[0] = fabs(grid[0]);
378 28994509 2004-04-21 devnull grid[1] = fabs(grid[1]);
379 28994509 2004-04-21 devnull if(!kflag)
380 28994509 2004-04-21 devnull for(i=0;i<4;i++)
381 28994509 2004-04-21 devnull klimits[i] = limits[i];
382 28994509 2004-04-21 devnull if(klimits[2]>klimits[3])
383 28994509 2004-04-21 devnull klimits[3] += 360;
384 28994509 2004-04-21 devnull lolat = limits[0];
385 28994509 2004-04-21 devnull hilat = limits[1];
386 28994509 2004-04-21 devnull lolon = limits[2];
387 28994509 2004-04-21 devnull hilon = limits[3];
388 28994509 2004-04-21 devnull if(lolon>=hilon||lolat>=hilat||lolat<-90.||hilat>90.)
389 28994509 2004-04-21 devnull error("unreasonable limits");
390 28994509 2004-04-21 devnull wlim = kflag? klimits: window;
391 28994509 2004-04-21 devnull dlat = fmin(hilat-lolat,wlim[1]-wlim[0])/16;
392 28994509 2004-04-21 devnull dlon = fmin(hilon-lolon,wlim[3]-wlim[2])/32;
393 28994509 2004-04-21 devnull dd = fmax(dlat,dlon);
394 28994509 2004-04-21 devnull while(grid[2]>fmin(dlat,dlon)/2)
395 28994509 2004-04-21 devnull grid[2] /= 2;
396 28994509 2004-04-21 devnull realcut();
397 28994509 2004-04-21 devnull if(nvert<=0) {
398 28994509 2004-04-21 devnull for(lat=klimits[0];lat<klimits[1]+dd-FUZZ;lat+=dd) {
399 28994509 2004-04-21 devnull if(lat>klimits[1])
400 28994509 2004-04-21 devnull lat = klimits[1];
401 28994509 2004-04-21 devnull for(lon=klimits[2];lon<klimits[3]+dd-FUZZ;lon+=dd) {
402 28994509 2004-04-21 devnull i = (kflag?posproj:normproj)
403 28994509 2004-04-21 devnull (lat,lon+(lon<klimits[3]?FUZZ:-FUZZ),
405 28994509 2004-04-21 devnull if(i*vflag <= 0)
406 28994509 2004-04-21 devnull continue;
407 28994509 2004-04-21 devnull if(x<xmin) xmin = x;
408 28994509 2004-04-21 devnull if(x>xmax) xmax = x;
409 28994509 2004-04-21 devnull if(y<ymin) ymin = y;
410 28994509 2004-04-21 devnull if(y>ymax) ymax = y;
413 28994509 2004-04-21 devnull } else {
414 28994509 2004-04-21 devnull for(i=0; i<nvert; i++) {
415 28994509 2004-04-21 devnull x = v[i].x;
416 28994509 2004-04-21 devnull y = v[i].y;
417 28994509 2004-04-21 devnull if(x<xmin) xmin = x;
418 28994509 2004-04-21 devnull if(x>xmax) xmax = x;
419 28994509 2004-04-21 devnull if(y<ymin) ymin = y;
420 28994509 2004-04-21 devnull if(y>ymax) ymax = y;
423 28994509 2004-04-21 devnull xrange = xmax - xmin;
424 28994509 2004-04-21 devnull yrange = ymax - ymin;
425 28994509 2004-04-21 devnull if(xrange<=0||yrange<=0)
426 28994509 2004-04-21 devnull error("map seems to be empty");
427 28994509 2004-04-21 devnull scaling = 2; /*plotting area from -1 to 1*/
428 28994509 2004-04-21 devnull if(position[2]!=0) {
429 28994509 2004-04-21 devnull if(posproj(position[0]-.5,position[1],&xcent,&ycent)==0||
430 28994509 2004-04-21 devnull posproj(position[0]+.5,position[1],&x,&y)==0)
431 28994509 2004-04-21 devnull error("unreasonable position");
432 28994509 2004-04-21 devnull scaling /= (position[2]*hypot(x-xcent,y-ycent));
433 28994509 2004-04-21 devnull if(posproj(position[0],position[1],&xcent,&ycent)==0)
434 28994509 2004-04-21 devnull error("unreasonable position");
435 28994509 2004-04-21 devnull } else {
436 28994509 2004-04-21 devnull scaling /= (xrange>yrange?xrange:yrange);
437 28994509 2004-04-21 devnull xcent = (xmin+xmax)/2;
438 28994509 2004-04-21 devnull ycent = (ymin+ymax)/2;
440 28994509 2004-04-21 devnull xoff = center[0]/scaling;
441 28994509 2004-04-21 devnull yoff = center[1]/scaling;
442 28994509 2004-04-21 devnull crot.l = center[2]*RAD;
443 28994509 2004-04-21 devnull sincos(&crot);
444 28994509 2004-04-21 devnull scaling *= HALFWIDTH*0.9;
445 28994509 2004-04-21 devnull if(symbolfile)
446 28994509 2004-04-21 devnull getsyms(symbolfile);
447 28994509 2004-04-21 devnull if(!s2flag) {
448 28994509 2004-04-21 devnull openpl();
449 28994509 2004-04-21 devnull erase();
451 28994509 2004-04-21 devnull range(left,bottom,right,top);
452 28994509 2004-04-21 devnull comment("grid","");
453 28994509 2004-04-21 devnull colorx(gridcolor);
454 28994509 2004-04-21 devnull pen(DOTTED);
455 28994509 2004-04-21 devnull if(grid[0]>0.)
456 28994509 2004-04-21 devnull for(lat=ceil(lolat/grid[0])*grid[0];
457 28994509 2004-04-21 devnull lat<=hilat;lat+=grid[0])
458 28994509 2004-04-21 devnull dogrid(lat,lat,lolon,hilon);
459 28994509 2004-04-21 devnull if(grid[1]>0.)
460 28994509 2004-04-21 devnull for(lon=ceil(lolon/grid[1])*grid[1];
461 28994509 2004-04-21 devnull lon<=hilon;lon+=grid[1])
462 28994509 2004-04-21 devnull dogrid(lolat,hilat,lon,lon);
463 28994509 2004-04-21 devnull comment("border","");
464 28994509 2004-04-21 devnull colorx(bordcolor);
465 28994509 2004-04-21 devnull pen(SOLID);
466 28994509 2004-04-21 devnull if(bflag) {
467 28994509 2004-04-21 devnull dolimb();
468 28994509 2004-04-21 devnull dobounds(lolat,hilat,lolon,hilon,0);
469 28994509 2004-04-21 devnull dobounds(window[0],window[1],window[2],window[3],1);
471 28994509 2004-04-21 devnull lolat = floor(limits[0]/10)*10;
472 28994509 2004-04-21 devnull hilat = ceil(limits[1]/10)*10;
473 28994509 2004-04-21 devnull lolon = floor(limits[2]/10)*10;
474 28994509 2004-04-21 devnull hilon = ceil(limits[3]/10)*10;
475 28994509 2004-04-21 devnull if(lolon>hilon)
476 28994509 2004-04-21 devnull hilon += 360.;
477 28994509 2004-04-21 devnull /*do tracks first so as not to lose the standard input*/
478 28994509 2004-04-21 devnull for(i=0;i<ntrack;i++) {
479 28994509 2004-04-21 devnull longlines = LONGLINES;
480 28994509 2004-04-21 devnull satellite(&track[i]);
481 28994509 2004-04-21 devnull longlines = shortlines;
483 28994509 2004-04-21 devnull for(i=0;i<nfile;i++) {
484 28994509 2004-04-21 devnull comment("mapfile",file[i].name);
485 28994509 2004-04-21 devnull colorx(file[i].color);
486 28994509 2004-04-21 devnull pen(file[i].style);
487 28994509 2004-04-21 devnull getdata(file[i].name);
489 28994509 2004-04-21 devnull move(right,bottom);
490 28994509 2004-04-21 devnull if(!s1flag)
491 28994509 2004-04-21 devnull closepl();
492 28994509 2004-04-21 devnull return 0;
495 28994509 2004-04-21 devnull /* Out of perverseness (really to recover from a dubious,
496 28994509 2004-04-21 devnull but documented, convention) the returns from projection
497 28994509 2004-04-21 devnull functions (-1 unplottable, 0 wrong sheet, 1 good) are
498 28994509 2004-04-21 devnull recoded into -1 wrong sheet, 0 unplottable, 1 good. */
501 28994509 2004-04-21 devnull fixproj(struct place *g, double *x, double *y)
503 28994509 2004-04-21 devnull int i = (*projection)(g,x,y);
504 28994509 2004-04-21 devnull return i<0? 0: i==0? -1: 1;
508 28994509 2004-04-21 devnull normproj(double lat, double lon, double *x, double *y)
511 28994509 2004-04-21 devnull struct place geog;
512 28994509 2004-04-21 devnull latlon(lat,lon,&geog);
514 28994509 2004-04-21 devnull printp(&geog);
516 28994509 2004-04-21 devnull normalize(&geog);
517 28994509 2004-04-21 devnull if(!inwindow(&geog))
518 28994509 2004-04-21 devnull return(-1);
519 28994509 2004-04-21 devnull i = fixproj(&geog,x,y);
520 28994509 2004-04-21 devnull if(rflag)
521 28994509 2004-04-21 devnull *x = -*x;
523 28994509 2004-04-21 devnull printp(&geog);
524 28994509 2004-04-21 devnull fprintf(stderr,"%d %.3f %.3f\n",i,*x,*y);
526 28994509 2004-04-21 devnull return(i);
530 28994509 2004-04-21 devnull posproj(double lat, double lon, double *x, double *y)
533 28994509 2004-04-21 devnull struct place geog;
534 28994509 2004-04-21 devnull latlon(lat,lon,&geog);
535 28994509 2004-04-21 devnull normalize(&geog);
536 28994509 2004-04-21 devnull i = fixproj(&geog,x,y);
537 28994509 2004-04-21 devnull if(rflag)
538 28994509 2004-04-21 devnull *x = -*x;
539 28994509 2004-04-21 devnull return(i);
543 28994509 2004-04-21 devnull inwindow(struct place *geog)
545 28994509 2004-04-21 devnull if(geog->nlat.l<rwindow[0]-FUZZ||
546 28994509 2004-04-21 devnull geog->nlat.l>rwindow[1]+FUZZ||
547 28994509 2004-04-21 devnull geog->wlon.l<rwindow[2]-FUZZ||
548 28994509 2004-04-21 devnull geog->wlon.l>rwindow[3]+FUZZ)
549 28994509 2004-04-21 devnull return(0);
550 28994509 2004-04-21 devnull else return(1);
554 28994509 2004-04-21 devnull inlimits(struct place *g)
556 28994509 2004-04-21 devnull if(rlimits[0]-FUZZ>g->nlat.l||
557 28994509 2004-04-21 devnull rlimits[1]+FUZZ<g->nlat.l)
558 28994509 2004-04-21 devnull return(0);
559 28994509 2004-04-21 devnull switch(limcase) {
561 28994509 2004-04-21 devnull if(rlimits[2]+TWOPI-FUZZ>g->wlon.l&&
562 28994509 2004-04-21 devnull rlimits[3]+FUZZ<g->wlon.l)
563 28994509 2004-04-21 devnull return(0);
566 28994509 2004-04-21 devnull if(rlimits[2]-FUZZ>g->wlon.l||
567 28994509 2004-04-21 devnull rlimits[3]+FUZZ<g->wlon.l)
568 28994509 2004-04-21 devnull return(0);
571 28994509 2004-04-21 devnull if(rlimits[2]>g->wlon.l&&
572 28994509 2004-04-21 devnull rlimits[3]-TWOPI+FUZZ<g->wlon.l)
573 28994509 2004-04-21 devnull return(0);
576 28994509 2004-04-21 devnull return(1);
580 28994509 2004-04-21 devnull long patch[18][36];
583 28994509 2004-04-21 devnull getdata(char *mapfile)
585 28994509 2004-04-21 devnull char *indexfile;
586 28994509 2004-04-21 devnull int kx,ky,c;
589 28994509 2004-04-21 devnull long *p;
590 28994509 2004-04-21 devnull int ip, jp;
592 28994509 2004-04-21 devnull struct place g;
593 28994509 2004-04-21 devnull int i, j;
594 28994509 2004-04-21 devnull double lat, lon;
595 28994509 2004-04-21 devnull int conn;
596 28994509 2004-04-21 devnull FILE *ifile, *xfile;
598 28994509 2004-04-21 devnull indexfile = mapindex(mapfile);
599 28994509 2004-04-21 devnull xfile = fopen(indexfile,"r");
600 28994509 2004-04-21 devnull if(xfile==NULL)
601 28994509 2004-04-21 devnull filerror("can't find map index", indexfile);
602 28994509 2004-04-21 devnull free(indexfile);
603 28994509 2004-04-21 devnull for(i=0,p=patch[0];i<18*36;i++,p++)
605 28994509 2004-04-21 devnull while(!feof(xfile) && fscanf(xfile,"%d%d%ld",&i,&j,&b)==3)
606 28994509 2004-04-21 devnull patch[i+9][j+18] = b;
607 28994509 2004-04-21 devnull fclose(xfile);
608 28994509 2004-04-21 devnull ifile = fopen(mapfile,"r");
609 28994509 2004-04-21 devnull if(ifile==NULL)
610 28994509 2004-04-21 devnull filerror("can't find map data", mapfile);
611 28994509 2004-04-21 devnull for(lat=lolat;lat<hilat;lat+=10.)
612 28994509 2004-04-21 devnull for(lon=lolon;lon<hilon;lon+=10.) {
613 28994509 2004-04-21 devnull if(!seeable(lat,lon))
614 28994509 2004-04-21 devnull continue;
615 28994509 2004-04-21 devnull i = pnorm(lat);
616 28994509 2004-04-21 devnull j = pnorm(lon);
617 28994509 2004-04-21 devnull if((b=patch[i+9][j+18])&1)
618 28994509 2004-04-21 devnull continue;
619 28994509 2004-04-21 devnull fseek(ifile,b,0);
620 28994509 2004-04-21 devnull while((ip=getc(ifile))>=0&&(jp=getc(ifile))>=0){
621 28994509 2004-04-21 devnull if(ip!=(i&0377)||jp!=(j&0377))
623 28994509 2004-04-21 devnull n = getshort(ifile);
624 28994509 2004-04-21 devnull conn = 0;
625 28994509 2004-04-21 devnull if(n > 0) { /* absolute coordinates */
626 28994509 2004-04-21 devnull kx = ky = 0; /* set */
627 28994509 2004-04-21 devnull for(k=0;k<n;k++){
628 28994509 2004-04-21 devnull kx = SCALERATIO*getshort(ifile);
629 28994509 2004-04-21 devnull ky = SCALERATIO*getshort(ifile);
630 28994509 2004-04-21 devnull if (((k%delta) != 0) && (k != (n-1)))
631 28994509 2004-04-21 devnull continue;
632 28994509 2004-04-21 devnull conv(kx,&g.nlat);
633 28994509 2004-04-21 devnull conv(ky,&g.wlon);
634 28994509 2004-04-21 devnull conn = plotpt(&g,conn);
636 28994509 2004-04-21 devnull } else { /* differential, scaled by SCALERATI0 */
638 28994509 2004-04-21 devnull kx = SCALERATIO*getshort(ifile);
639 28994509 2004-04-21 devnull ky = SCALERATIO*getshort(ifile);
640 28994509 2004-04-21 devnull for(k=0; k<n; k++) {
641 28994509 2004-04-21 devnull c = getc(ifile);
642 28994509 2004-04-21 devnull if(c&0200) c|= ~0177;
643 28994509 2004-04-21 devnull kx += c;
644 28994509 2004-04-21 devnull c = getc(ifile);
645 28994509 2004-04-21 devnull if(c&0200) c|= ~0177;
646 28994509 2004-04-21 devnull ky += c;
647 28994509 2004-04-21 devnull if(k%delta!=0&&k!=n-1)
648 28994509 2004-04-21 devnull continue;
649 28994509 2004-04-21 devnull conv(kx,&g.nlat);
650 28994509 2004-04-21 devnull conv(ky,&g.wlon);
651 28994509 2004-04-21 devnull conn = plotpt(&g,conn);
654 28994509 2004-04-21 devnull if(k==1) {
655 28994509 2004-04-21 devnull conv(kx,&g.nlat);
656 28994509 2004-04-21 devnull conv(ky,&g.wlon);
657 28994509 2004-04-21 devnull plotpt(&g,conn);
661 28994509 2004-04-21 devnull fclose(ifile);
665 28994509 2004-04-21 devnull seeable(double lat0, double lon0)
667 28994509 2004-04-21 devnull double x, y;
668 28994509 2004-04-21 devnull double lat, lon;
669 28994509 2004-04-21 devnull for(lat=lat0;lat<=lat0+10;lat+=2*grid[2])
670 28994509 2004-04-21 devnull for(lon=lon0;lon<=lon0+10;lon+=2*grid[2])
671 28994509 2004-04-21 devnull if(normproj(lat,lon,&x,&y)*vflag>0)
672 28994509 2004-04-21 devnull return(1);
673 28994509 2004-04-21 devnull return(0);
677 28994509 2004-04-21 devnull satellite(struct file *t)
679 28994509 2004-04-21 devnull char sym[50];
680 28994509 2004-04-21 devnull char lbl[50];
681 28994509 2004-04-21 devnull double scale;
682 28994509 2004-04-21 devnull int conn;
683 28994509 2004-04-21 devnull double lat,lon;
684 28994509 2004-04-21 devnull struct place place;
685 28994509 2004-04-21 devnull static FILE *ifile;
687 28994509 2004-04-21 devnull if(ifile == nil)
688 28994509 2004-04-21 devnull ifile = stdin;
689 28994509 2004-04-21 devnull if(t->name[0]!='-'||t->name[1]!=0) {
690 28994509 2004-04-21 devnull fclose(ifile);
691 28994509 2004-04-21 devnull if((ifile=fopen(t->name,"r"))==NULL)
692 28994509 2004-04-21 devnull filerror("can't find track", t->name);
694 28994509 2004-04-21 devnull comment("track",t->name);
695 28994509 2004-04-21 devnull colorx(t->color);
696 28994509 2004-04-21 devnull pen(t->style);
697 28994509 2004-04-21 devnull for(;;) {
698 28994509 2004-04-21 devnull conn = 0;
699 28994509 2004-04-21 devnull while(!feof(ifile) && fscanf(ifile,"%lf%lf",&lat,&lon)==2){
700 28994509 2004-04-21 devnull latlon(lat,lon,&place);
701 28994509 2004-04-21 devnull if(fscanf(ifile,"%1s",lbl) == 1) {
702 28994509 2004-04-21 devnull if(strchr("+-.0123456789",*lbl)==0)
704 28994509 2004-04-21 devnull ungetc(*lbl,ifile);
706 28994509 2004-04-21 devnull conn = plotpt(&place,conn);
708 28994509 2004-04-21 devnull if(feof(ifile))
710 28994509 2004-04-21 devnull fscanf(ifile,"%[^\n]",lbl+1);
711 28994509 2004-04-21 devnull switch(*lbl) {
712 28994509 2004-04-21 devnull case '"':
713 28994509 2004-04-21 devnull if(plotpt(&place,conn))
714 28994509 2004-04-21 devnull text(lbl+1);
716 28994509 2004-04-21 devnull case ':':
717 28994509 2004-04-21 devnull case '!':
718 28994509 2004-04-21 devnull if(sscanf(lbl+1,"%s %lf",sym,&scale) <= 1)
719 28994509 2004-04-21 devnull scale = 1;
720 28994509 2004-04-21 devnull if(plotpt(&place,conn?conn:-1)) {
721 28994509 2004-04-21 devnull int r = *lbl=='!'?0:rflag?-1:1;
722 28994509 2004-04-21 devnull pen(SOLID);
723 28994509 2004-04-21 devnull if(putsym(&place,sym,scale,r) == 0)
724 28994509 2004-04-21 devnull text(lbl);
725 28994509 2004-04-21 devnull pen(t->style);
728 28994509 2004-04-21 devnull default:
729 28994509 2004-04-21 devnull if(plotpt(&place,conn))
730 28994509 2004-04-21 devnull text(lbl);
737 28994509 2004-04-21 devnull pnorm(double x)
740 28994509 2004-04-21 devnull i = x/10.;
741 28994509 2004-04-21 devnull i %= 36;
742 28994509 2004-04-21 devnull if(i>=18) return(i-36);
743 28994509 2004-04-21 devnull if(i<-18) return(i+36);
744 28994509 2004-04-21 devnull return(i);
748 28994509 2004-04-21 devnull error(char *s)
750 28994509 2004-04-21 devnull fprintf(stderr,"map: \r\n%s\n",s);
751 28994509 2004-04-21 devnull exits("error");
755 28994509 2004-04-21 devnull filerror(char *s, char *f)
757 28994509 2004-04-21 devnull fprintf(stderr,"\r\n%s %s\n",s,f);
758 28994509 2004-04-21 devnull exits("error");
762 28994509 2004-04-21 devnull mapindex(char *s)
764 28994509 2004-04-21 devnull char *t = malloc(strlen(s)+3);
765 28994509 2004-04-21 devnull strcpy(t,s);
766 28994509 2004-04-21 devnull strcat(t,".x");
767 28994509 2004-04-21 devnull return t;
770 28994509 2004-04-21 devnull #define NOPT 32767
771 28994509 2004-04-21 devnull static int ox = NOPT;
772 28994509 2004-04-21 devnull static int oy = NOPT;
775 28994509 2004-04-21 devnull cpoint(int xi, int yi, int conn)
777 28994509 2004-04-21 devnull int dx = abs(ox-xi);
778 28994509 2004-04-21 devnull int dy = abs(oy-yi);
779 28994509 2004-04-21 devnull if(!xflag && (xi<left||xi>=right || yi<bottom||yi>=top)) {
780 28994509 2004-04-21 devnull ox = oy = NOPT;
781 28994509 2004-04-21 devnull return 0;
783 28994509 2004-04-21 devnull if(conn == -1) /* isolated plotting symbol */
785 28994509 2004-04-21 devnull else if(!conn)
786 28994509 2004-04-21 devnull point(xi,yi);
788 28994509 2004-04-21 devnull if(dx+dy>longlines) {
789 28994509 2004-04-21 devnull ox = oy = NOPT; /* don't leap across cuts */
790 28994509 2004-04-21 devnull return 0;
792 28994509 2004-04-21 devnull if(dx || dy)
793 28994509 2004-04-21 devnull vec(xi,yi);
795 28994509 2004-04-21 devnull ox = xi, oy = yi;
796 28994509 2004-04-21 devnull return dx+dy<=2? 2: 1; /* 2=very near; see dogrid */
800 28994509 2004-04-21 devnull struct place oldg;
803 28994509 2004-04-21 devnull plotpt(struct place *g, int conn)
805 28994509 2004-04-21 devnull int kx,ky;
806 28994509 2004-04-21 devnull int ret;
807 28994509 2004-04-21 devnull double cutlon;
808 28994509 2004-04-21 devnull if(!inlimits(g)) {
809 28994509 2004-04-21 devnull return(0);
811 28994509 2004-04-21 devnull normalize(g);
812 28994509 2004-04-21 devnull if(!inwindow(g)) {
813 28994509 2004-04-21 devnull return(0);
815 28994509 2004-04-21 devnull switch((*cut)(g,&oldg,&cutlon)) {
817 28994509 2004-04-21 devnull if(conn) {
818 28994509 2004-04-21 devnull ret = duple(g,cutlon)|duple(g,cutlon);
819 28994509 2004-04-21 devnull oldg = *g;
820 28994509 2004-04-21 devnull return(ret);
823 28994509 2004-04-21 devnull conn = 0;
824 28994509 2004-04-21 devnull default: /* prevent diags about bad return value */
826 28994509 2004-04-21 devnull oldg = *g;
827 28994509 2004-04-21 devnull ret = doproj(g,&kx,&ky);
828 28994509 2004-04-21 devnull if(ret==0 || !onlimb && ret*vflag<=0)
829 28994509 2004-04-21 devnull return(0);
830 28994509 2004-04-21 devnull ret = cpoint(kx,ky,conn);
831 28994509 2004-04-21 devnull return ret;
836 28994509 2004-04-21 devnull doproj(struct place *g, int *kx, int *ky)
839 28994509 2004-04-21 devnull double x,y,x1,y1;
840 28994509 2004-04-21 devnull /*fprintf(stderr,"dopr1 %f %f \n",g->nlat.l,g->wlon.l);*/
841 28994509 2004-04-21 devnull i = fixproj(g,&x,&y);
842 28994509 2004-04-21 devnull if(i == 0)
843 28994509 2004-04-21 devnull return(0);
844 28994509 2004-04-21 devnull if(rflag)
846 28994509 2004-04-21 devnull /*fprintf(stderr,"dopr2 %f %f\n",x,y);*/
847 28994509 2004-04-21 devnull if(!inpoly(x,y)) {
848 28994509 2004-04-21 devnull return 0;
850 28994509 2004-04-21 devnull x1 = x - xcent;
851 28994509 2004-04-21 devnull y1 = y - ycent;
852 28994509 2004-04-21 devnull x = (x1*crot.c - y1*crot.s + xoff)*scaling;
853 28994509 2004-04-21 devnull y = (x1*crot.s + y1*crot.c + yoff)*scaling;
854 28994509 2004-04-21 devnull *kx = x + (x>0?.5:-.5);
855 28994509 2004-04-21 devnull *ky = y + (y>0?.5:-.5);
856 28994509 2004-04-21 devnull return(i);
860 28994509 2004-04-21 devnull duple(struct place *g, double cutlon)
862 28994509 2004-04-21 devnull int kx,ky;
863 28994509 2004-04-21 devnull int okx,oky;
864 28994509 2004-04-21 devnull struct place ig;
865 28994509 2004-04-21 devnull revlon(g,cutlon);
866 28994509 2004-04-21 devnull revlon(&oldg,cutlon);
867 28994509 2004-04-21 devnull ig = *g;
868 28994509 2004-04-21 devnull invert(&ig);
869 28994509 2004-04-21 devnull if(!inlimits(&ig))
870 28994509 2004-04-21 devnull return(0);
871 28994509 2004-04-21 devnull if(doproj(g,&kx,&ky)*vflag<=0 ||
872 28994509 2004-04-21 devnull doproj(&oldg,&okx,&oky)*vflag<=0)
873 28994509 2004-04-21 devnull return(0);
874 28994509 2004-04-21 devnull cpoint(okx,oky,0);
875 28994509 2004-04-21 devnull cpoint(kx,ky,1);
876 28994509 2004-04-21 devnull return(1);
880 28994509 2004-04-21 devnull revlon(struct place *g, double cutlon)
882 28994509 2004-04-21 devnull g->wlon.l = reduce(cutlon-reduce(g->wlon.l-cutlon));
883 28994509 2004-04-21 devnull sincos(&g->wlon);
887 28994509 2004-04-21 devnull /* recognize problems of cuts
888 28994509 2004-04-21 devnull * move a point across cut to side of its predecessor
889 28994509 2004-04-21 devnull * if its very close to the cut
890 28994509 2004-04-21 devnull * return(0) if cut interrupts the line
891 28994509 2004-04-21 devnull * return(1) if line is to be drawn normally
892 28994509 2004-04-21 devnull * return(2) if line is so close to cut as to
893 28994509 2004-04-21 devnull * be properly drawn on both sheets
897 28994509 2004-04-21 devnull picut(struct place *g, struct place *og, double *cutlon)
899 28994509 2004-04-21 devnull *cutlon = PI;
900 28994509 2004-04-21 devnull return(ckcut(g,og,PI));
904 28994509 2004-04-21 devnull nocut(struct place *g, struct place *og, double *cutlon)
906 28994509 2004-04-21 devnull USED(g);
907 28994509 2004-04-21 devnull USED(og);
908 28994509 2004-04-21 devnull USED(cutlon);
910 28994509 2004-04-21 devnull #pragma ref g
911 28994509 2004-04-21 devnull #pragma ref og
912 28994509 2004-04-21 devnull #pragma ref cutlon
914 28994509 2004-04-21 devnull return(1);
918 28994509 2004-04-21 devnull ckcut(struct place *g1, struct place *g2, double lon)
920 28994509 2004-04-21 devnull double d1, d2;
921 28994509 2004-04-21 devnull double f1, f2;
922 28994509 2004-04-21 devnull int kx,ky;
923 28994509 2004-04-21 devnull d1 = reduce(g1->wlon.l -lon);
924 28994509 2004-04-21 devnull d2 = reduce(g2->wlon.l -lon);
925 28994509 2004-04-21 devnull if((f1=fabs(d1))<FUZZ)
926 28994509 2004-04-21 devnull d1 = diddle(g1,lon,d2);
927 28994509 2004-04-21 devnull if((f2=fabs(d2))<FUZZ) {
928 28994509 2004-04-21 devnull d2 = diddle(g2,lon,d1);
929 28994509 2004-04-21 devnull if(doproj(g2,&kx,&ky)*vflag>0)
930 28994509 2004-04-21 devnull cpoint(kx,ky,0);
932 28994509 2004-04-21 devnull if(f1<FUZZ&&f2<FUZZ)
933 28994509 2004-04-21 devnull return(2);
934 28994509 2004-04-21 devnull if(f1>PI*TWO_THRD||f2>PI*TWO_THRD)
935 28994509 2004-04-21 devnull return(1);
936 28994509 2004-04-21 devnull return(d1*d2>=0);
940 28994509 2004-04-21 devnull diddle(struct place *g, double lon, double d)
942 28994509 2004-04-21 devnull double d1;
943 28994509 2004-04-21 devnull d1 = FUZZ/2;
945 28994509 2004-04-21 devnull d1 = -d1;
946 28994509 2004-04-21 devnull g->wlon.l = reduce(lon+d1);
947 28994509 2004-04-21 devnull sincos(&g->wlon);
948 28994509 2004-04-21 devnull return(d1);
952 28994509 2004-04-21 devnull reduce(double lon)
954 28994509 2004-04-21 devnull if(lon>PI)
955 28994509 2004-04-21 devnull lon -= 2*PI;
956 28994509 2004-04-21 devnull else if(lon<-PI)
957 28994509 2004-04-21 devnull lon += 2*PI;
958 28994509 2004-04-21 devnull return(lon);
962 28994509 2004-04-21 devnull double tetrapt = 35.26438968; /* atan(1/sqrt(2)) */
965 28994509 2004-04-21 devnull dogrid(double lat0, double lat1, double lon0, double lon1)
967 28994509 2004-04-21 devnull double slat,slon,tlat,tlon;
968 28994509 2004-04-21 devnull register int conn, oconn;
969 28994509 2004-04-21 devnull slat = tlat = slon = tlon = 0;
970 28994509 2004-04-21 devnull if(lat1>lat0)
971 28994509 2004-04-21 devnull slat = tlat = fmin(grid[2],dlat);
973 28994509 2004-04-21 devnull slon = tlon = fmin(grid[2],dlon);;
974 28994509 2004-04-21 devnull conn = oconn = 0;
975 28994509 2004-04-21 devnull while(lat0<=lat1&&lon0<=lon1) {
976 28994509 2004-04-21 devnull conn = gridpt(lat0,lon0,conn);
977 28994509 2004-04-21 devnull if(projection==Xguyou&&slat>0) {
978 28994509 2004-04-21 devnull if(lat0<-45&&lat0+slat>-45)
979 28994509 2004-04-21 devnull conn = gridpt(-45.,lon0,conn);
980 28994509 2004-04-21 devnull else if(lat0<45&&lat0+slat>45)
981 28994509 2004-04-21 devnull conn = gridpt(45.,lon0,conn);
982 28994509 2004-04-21 devnull } else if(projection==Xtetra&&slat>0) {
983 28994509 2004-04-21 devnull if(lat0<-tetrapt&&lat0+slat>-tetrapt) {
984 28994509 2004-04-21 devnull gridpt(-tetrapt-.001,lon0,conn);
985 28994509 2004-04-21 devnull conn = gridpt(-tetrapt+.001,lon0,0);
987 28994509 2004-04-21 devnull else if(lat0<tetrapt&&lat0+slat>tetrapt) {
988 28994509 2004-04-21 devnull gridpt(tetrapt-.001,lon0,conn);
989 28994509 2004-04-21 devnull conn = gridpt(tetrapt+.001,lon0,0);
992 28994509 2004-04-21 devnull if(conn==0 && oconn!=0) {
993 28994509 2004-04-21 devnull if(slat+slon>.05) {
994 28994509 2004-04-21 devnull lat0 -= slat; /* steps too big */
995 28994509 2004-04-21 devnull lon0 -= slon; /* or near bdry */
996 28994509 2004-04-21 devnull slat /= 2;
997 28994509 2004-04-21 devnull slon /= 2;
998 28994509 2004-04-21 devnull conn = oconn = gridpt(lat0,lon0,conn);
1000 28994509 2004-04-21 devnull oconn = 0;
1001 28994509 2004-04-21 devnull } else {
1002 28994509 2004-04-21 devnull if(conn==2) {
1003 28994509 2004-04-21 devnull slat = tlat;
1004 28994509 2004-04-21 devnull slon = tlon;
1005 28994509 2004-04-21 devnull conn = 1;
1007 28994509 2004-04-21 devnull oconn = conn;
1009 28994509 2004-04-21 devnull lat0 += slat;
1010 28994509 2004-04-21 devnull lon0 += slon;
1012 28994509 2004-04-21 devnull gridpt(lat1,lon1,conn);
1015 28994509 2004-04-21 devnull static int gridinv; /* nonzero when doing window bounds */
1018 28994509 2004-04-21 devnull gridpt(double lat, double lon, int conn)
1020 28994509 2004-04-21 devnull struct place g;
1021 28994509 2004-04-21 devnull /*fprintf(stderr,"%f %f\n",lat,lon);*/
1022 28994509 2004-04-21 devnull latlon(lat,lon,&g);
1023 28994509 2004-04-21 devnull if(gridinv)
1024 28994509 2004-04-21 devnull invert(&g);
1025 28994509 2004-04-21 devnull return(plotpt(&g,conn));
1028 28994509 2004-04-21 devnull /* win=0 ordinary grid lines, win=1 window lines */
1031 28994509 2004-04-21 devnull dobounds(double lolat, double hilat, double lolon, double hilon, int win)
1033 28994509 2004-04-21 devnull gridinv = win;
1034 28994509 2004-04-21 devnull if(lolat>-90 || win && (poles&1)!=0)
1035 28994509 2004-04-21 devnull dogrid(lolat+FUZZ,lolat+FUZZ,lolon,hilon);
1036 28994509 2004-04-21 devnull if(hilat<90 || win && (poles&2)!=0)
1037 28994509 2004-04-21 devnull dogrid(hilat-FUZZ,hilat-FUZZ,lolon,hilon);
1038 28994509 2004-04-21 devnull if(hilon-lolon<360 || win && cut==picut) {
1039 28994509 2004-04-21 devnull dogrid(lolat,hilat,lolon+FUZZ,lolon+FUZZ);
1040 28994509 2004-04-21 devnull dogrid(lolat,hilat,hilon-FUZZ,hilon-FUZZ);
1042 28994509 2004-04-21 devnull gridinv = 0;
1045 28994509 2004-04-21 devnull static void
1046 28994509 2004-04-21 devnull dolimb(void)
1048 28994509 2004-04-21 devnull double lat, lon;
1049 28994509 2004-04-21 devnull double res = fmin(dlat, dlon)/4;
1050 28994509 2004-04-21 devnull int conn = 0;
1051 28994509 2004-04-21 devnull int newconn;
1052 28994509 2004-04-21 devnull if(limb == 0)
1053 28994509 2004-04-21 devnull return;
1054 28994509 2004-04-21 devnull onlimb = gridinv = 1;
1055 28994509 2004-04-21 devnull for(;;) {
1056 28994509 2004-04-21 devnull newconn = (*limb)(&lat, &lon, res);
1057 28994509 2004-04-21 devnull if(newconn == -1)
1059 28994509 2004-04-21 devnull conn = gridpt(lat, lon, conn*newconn);
1061 28994509 2004-04-21 devnull onlimb = gridinv = 0;
1066 28994509 2004-04-21 devnull radbds(double *w, double *rw)
1069 28994509 2004-04-21 devnull for(i=0;i<4;i++)
1070 28994509 2004-04-21 devnull rw[i] = w[i]*RAD;
1071 28994509 2004-04-21 devnull rw[0] -= FUZZ;
1072 28994509 2004-04-21 devnull rw[1] += FUZZ;
1073 28994509 2004-04-21 devnull rw[2] -= FUZZ;
1074 28994509 2004-04-21 devnull rw[3] += FUZZ;
1078 28994509 2004-04-21 devnull windlim(void)
1080 28994509 2004-04-21 devnull double center = orientation[0];
1081 28994509 2004-04-21 devnull double colat;
1082 28994509 2004-04-21 devnull if(center>90)
1083 28994509 2004-04-21 devnull center = 180 - center;
1084 28994509 2004-04-21 devnull if(center<-90)
1085 28994509 2004-04-21 devnull center = -180 - center;
1086 28994509 2004-04-21 devnull if(fabs(center)>90)
1087 28994509 2004-04-21 devnull error("unreasonable orientation");
1088 28994509 2004-04-21 devnull colat = 90 - window[0];
1089 28994509 2004-04-21 devnull if(center-colat>limits[0])
1090 28994509 2004-04-21 devnull limits[0] = center - colat;
1091 28994509 2004-04-21 devnull if(center+colat<limits[1])
1092 28994509 2004-04-21 devnull limits[1] = center + colat;
1097 28994509 2004-04-21 devnull getshort(FILE *f)
1099 28994509 2004-04-21 devnull int c, r;
1100 28994509 2004-04-21 devnull c = getc(f);
1101 28994509 2004-04-21 devnull r = (c | getc(f)<<8);
1102 28994509 2004-04-21 devnull if (r&0x8000)
1103 28994509 2004-04-21 devnull r |= ~0xFFFF; /* in case short > 16 bits */
1104 28994509 2004-04-21 devnull return r;
1108 28994509 2004-04-21 devnull fmin(double x, double y)
1110 28994509 2004-04-21 devnull return(x<y?x:y);
1114 28994509 2004-04-21 devnull fmax(double x, double y)
1116 28994509 2004-04-21 devnull return(x>y?x:y);
1120 28994509 2004-04-21 devnull clamp(double *px, double v)
1122 28994509 2004-04-21 devnull *px = (v<0?fmax:fmin)(*px,v);
1126 28994509 2004-04-21 devnull pathnames(void)
1129 28994509 2004-04-21 devnull char *t, *indexfile, *name;
1130 28994509 2004-04-21 devnull FILE *f, *fx;
1131 28994509 2004-04-21 devnull for(i=0; i<nfile; i++) {
1132 28994509 2004-04-21 devnull name = file[i].name;
1133 28994509 2004-04-21 devnull if(*name=='/')
1134 28994509 2004-04-21 devnull continue;
1135 28994509 2004-04-21 devnull indexfile = mapindex(name);
1136 28994509 2004-04-21 devnull /* ansi equiv of unix access() call */
1137 28994509 2004-04-21 devnull f = fopen(name, "r");
1138 28994509 2004-04-21 devnull fx = fopen(indexfile, "r");
1139 28994509 2004-04-21 devnull if(f) fclose(f);
1140 28994509 2004-04-21 devnull if(fx) fclose(fx);
1141 28994509 2004-04-21 devnull free(indexfile);
1142 28994509 2004-04-21 devnull if(f && fx)
1143 28994509 2004-04-21 devnull continue;
1144 28994509 2004-04-21 devnull t = malloc(strlen(name)+strlen(mapdir)+2);
1145 28994509 2004-04-21 devnull strcpy(t,mapdir);
1146 28994509 2004-04-21 devnull strcat(t,"/");
1147 28994509 2004-04-21 devnull strcat(t,name);
1148 28994509 2004-04-21 devnull file[i].name = t;
1153 28994509 2004-04-21 devnull clipinit(void)
1156 28994509 2004-04-21 devnull double s,t;
1157 28994509 2004-04-21 devnull if(nvert<=0)
1158 28994509 2004-04-21 devnull return;
1159 28994509 2004-04-21 devnull for(i=0; i<nvert; i++) { /*convert latlon to xy*/
1160 28994509 2004-04-21 devnull if(normproj(v[i].x,v[i].y,&v[i].x,&v[i].y)==0)
1161 28994509 2004-04-21 devnull error("invisible clipping vertex");
1163 28994509 2004-04-21 devnull if(nvert==2) { /*rectangle with diag specified*/
1164 28994509 2004-04-21 devnull nvert = 4;
1165 28994509 2004-04-21 devnull v[2] = v[1];
1166 28994509 2004-04-21 devnull v[1].x=v[0].x, v[1].y=v[2].y, v[3].x=v[2].x, v[3].y=v[0].y;
1168 28994509 2004-04-21 devnull v[nvert] = v[0];
1169 28994509 2004-04-21 devnull v[nvert+1] = v[1];
1171 28994509 2004-04-21 devnull for(i=1; i<=nvert; i++) { /*test for convexity*/
1172 28994509 2004-04-21 devnull t = (v[i-1].x-v[i].x)*(v[i+1].y-v[i].y) -
1173 28994509 2004-04-21 devnull (v[i-1].y-v[i].y)*(v[i+1].x-v[i].x);
1174 28994509 2004-04-21 devnull if(t<-FUZZ && s>=0) s = 1;
1175 28994509 2004-04-21 devnull if(t>FUZZ && s<=0) s = -1;
1176 28994509 2004-04-21 devnull if(-FUZZ<=t&&t<=FUZZ || t*s>0) {
1181 28994509 2004-04-21 devnull if(s==0)
1182 28994509 2004-04-21 devnull error("improper clipping polygon");
1183 28994509 2004-04-21 devnull for(i=0; i<nvert; i++) { /*edge equation ax+by=c*/
1184 28994509 2004-04-21 devnull e[i].a = s*(v[i+1].y - v[i].y);
1185 28994509 2004-04-21 devnull e[i].b = s*(v[i].x - v[i+1].x);
1186 28994509 2004-04-21 devnull e[i].c = s*(v[i].x*v[i+1].y - v[i].y*v[i+1].x);
1191 28994509 2004-04-21 devnull inpoly(double x, double y)
1194 28994509 2004-04-21 devnull for(i=0; i<nvert; i++) {
1195 28994509 2004-04-21 devnull register struct edge *ei = &e[i];
1196 28994509 2004-04-21 devnull double val = x*ei->a + y*ei->b - ei->c;
1197 28994509 2004-04-21 devnull if(val>10*FUZZ)
1198 28994509 2004-04-21 devnull return(0);
1200 28994509 2004-04-21 devnull return 1;
1204 28994509 2004-04-21 devnull realcut()
1206 28994509 2004-04-21 devnull struct place g;
1207 28994509 2004-04-21 devnull double lat;
1209 28994509 2004-04-21 devnull if(cut != picut) /* punt on unusual cuts */
1210 28994509 2004-04-21 devnull return;
1211 28994509 2004-04-21 devnull for(lat=window[0]; lat<=window[1]; lat+=grid[2]) {
1212 28994509 2004-04-21 devnull g.wlon.l = PI;
1213 28994509 2004-04-21 devnull sincos(&g.wlon);
1214 28994509 2004-04-21 devnull g.nlat.l = lat*RAD;
1215 28994509 2004-04-21 devnull sincos(&g.nlat);
1216 28994509 2004-04-21 devnull if(!inwindow(&g)) {
1219 28994509 2004-04-21 devnull invert(&g);
1220 28994509 2004-04-21 devnull if(inlimits(&g)) {
1221 28994509 2004-04-21 devnull return;
1224 28994509 2004-04-21 devnull longlines = shortlines = LONGLINES;
1225 28994509 2004-04-21 devnull cut = nocut; /* not necessary; small eff. gain */