7 #define NVERT 20 /* max number of vertices in a -v polygon */
8 #define HALFWIDTH 8192 /* output scaled to fit in -HALFWIDTH,HALFWIDTH */
9 #define LONGLINES (HALFWIDTH*4) /* permissible segment lengths */
10 #define SHORTLINES (HALFWIDTH/8)
11 #define SCALERATIO 10 /* of abs to rel data (see map(5)) */
12 #define RESOL 2. /* coarsest resolution for tracing grid (degrees) */
13 #define TWO_THRD 0.66666666666666667
15 int normproj(double, double, double *, double *);
16 int posproj(double, double, double *, double *);
17 int picut(struct place *, struct place *, double *);
18 double reduce(double);
19 short getshort(FILE *);
20 char *mapindex(char *);
24 static char *mapdir = "/lib/map"; /* default map directory */
30 static struct file dfltfile = {
31 "world", BLACK, SOLID /* default map */
33 static struct file *file = &dfltfile; /* list of map files */
34 static int nfile = 1; /* length of list */
35 static char *currcolor = BLACK; /* current color */
36 static char *gridcolor = BLACK;
37 static char *bordcolor = BLACK;
39 extern struct index index[];
40 int halfwidth = HALFWIDTH;
42 static int (*cut)(struct place *, struct place *, double *);
43 static int (*limb)(double*, double*, double);
44 static void dolimb(void);
47 static double orientation[3] = { 90., 0., 0. }; /* -o option */
48 static oriented; /* nonzero if -o option occurred */
49 static upright; /* 1 if orientation[0]==90, -1 if -90, else 0*/
50 static int delta = 1; /* -d setting */
51 static double limits[4] = { /* -l parameters */
52 -90., 90., -180., 180.
54 static double klimits[4] = { /* -k parameters */
55 -90., 90., -180., 180.
58 static double rlimits[4]; /* limits expressed in radians */
59 static double lolat, hilat, lolon, hilon;
60 static double window[4] = { /* option -w */
61 -90., 90., -180., 180.
63 static windowed; /* nozero if option -w */
64 static struct vert { double x, y; } v[NVERT+2]; /*clipping polygon*/
65 static struct edge { double a, b, c; } e[NVERT]; /* coeffs for linear inequality */
66 static int nvert; /* number of vertices in clipping polygon */
68 static double rwindow[4]; /* window, expressed in radians */
69 static double params[2]; /* projection params */
70 /* bounds on output values before scaling; found by coarse survey */
71 static double xmin = 100.;
72 static double xmax = -100.;
73 static double ymin = 100.;
74 static double ymax = -100.;
75 static double xcent, ycent;
76 static double xoff, yoff;
77 double xrange, yrange;
78 static int left = -HALFWIDTH;
79 static int right = HALFWIDTH;
80 static int bottom = -HALFWIDTH;
81 static int top = HALFWIDTH;
82 static int longlines = SHORTLINES; /* drop longer segments */
83 static int shortlines = SHORTLINES;
84 static int bflag = 1; /* 0 for option -b */
85 static int s1flag = 0; /* 1 for option -s1 */
86 static int s2flag = 0; /* 1 for option -s2 */
87 static int rflag = 0; /* 1 for option -r */
88 static int kflag = 0; /* 1 if option -k occurred */
89 static int xflag = 0; /* 1 for option -x */
90 int vflag = 1; /* -1 if option -v occurred */
91 static double position[3]; /* option -p */
92 static double center[3] = {0., 0., 0.}; /* option -c */
93 static struct coord crot; /* option -c */
94 static double grid[3] = { 10., 10., RESOL }; /* option -g */
95 static double dlat, dlon; /* resolution for tracing grid in lat and lon */
96 static double scaling; /* to compute final integer output */
97 static struct file *track; /* options -t and -u */
98 static int ntrack; /* number of tracks present */
99 static char *symbolfile; /* option -y */
101 void clamp(double *px, double v);
103 double diddle(struct place *, double, double);
104 double diddle(struct place *, double, double);
105 void dobounds(double, double, double, double, int);
106 void dogrid(double, double, double, double);
107 int duple(struct place *, double);
108 double fmax(double, double);
109 double fmin(double, double);
110 void getdata(char *);
111 int gridpt(double, double, int);
112 int inpoly(double, double);
113 int inwindow(struct place *);
114 void pathnames(void);
116 void radbds(double *w, double *rw);
117 void revlon(struct place *, double);
118 void satellite(struct file *);
119 int seeable(double, double);
127 if(s[0]=='-' && (s[1]<'0'||s[1]>'9'))
128 return(s[1]!='.'&&s[1]!=0);
134 conv(int k, struct coord *g)
136 g->l = (0.0001/SCALERATIO)*k;
141 main(int argc, char *argv[])
150 abort(); /* getshort() won't work */
154 s = getenv("MAPDIR");
158 error("usage: map projection params options");
159 for(k=0;index[k].name;k++) {
163 if(*s==0) goto found;
168 fprintf(stderr,"projections:\n");
169 for(i=0;index[i].name;i++) {
170 fprintf(stderr,"%s",index[i].name);
171 for(k=0; k<index[i].npar; k++)
172 fprintf(stderr," p%d", k);
173 fprintf(stderr,"\n");
180 limb = index[k].limb;
181 poles = index[k].poles;
182 for(i=0;i<index[k].npar;i++) {
183 if(i>=argc||option(argv[i])) {
184 fprintf(stderr,"%s needs %d params\n",index[k].name,index[k].npar);
187 params[i] = atof(argv[i]);
191 while(argc>0&&option(argv[0])) {
194 switch(argv[-1][1]) {
196 if(file == &dfltfile) {
200 while(argc && !option(*argv)) {
201 file = realloc(file,(nfile+1)*sizeof(*file));
202 file[nfile].name = *argv;
203 file[nfile].color = currcolor;
204 file[nfile].style = SOLID;
212 for(nvert=0;nvert<NVERT&&argc>=2;nvert++) {
215 v[nvert].x = atof(*argv++);
219 v[nvert].y = atof(*argv++);
223 error("too many clipping vertices");
226 gridcolor = currcolor;
227 for(i=0;i<3&&argc>i&&!option(argv[i]);i++)
228 grid[i] = atof(argv[i]);
231 grid[0] = grid[1] = 0.;
245 while(argc && !option(*argv)) {
246 track = realloc(track,(ntrack+1)*sizeof(*track));
247 track[ntrack].name = *argv;
248 track[ntrack].color = currcolor;
249 track[ntrack].style = style;
259 switch(argv[-1][2]) {
263 case 0: /* compatibility */
269 for(i=0;i<3&&i<argc&&!option(argv[i]);i++)
270 orientation[i] = atof(argv[i]);
276 bordcolor = currcolor;
277 for(i=0;i<argc&&i<4&&!option(argv[i]);i++)
278 limits[i] = atof(argv[i]);
284 for(i=0;i<argc&&i<4&&!option(argv[i]);i++)
285 klimits[i] = atof(argv[i]);
290 if(argc>0&&!option(argv[0])) {
291 delta = atoi(argv[0]);
297 bordcolor = currcolor;
299 for(i=0;i<argc&&i<4&&!option(argv[i]);i++)
300 window[i] = atof(argv[i]);
305 for(i=0;i<3&&argc>i&&!option(argv[i]);i++)
306 center[i] = atof(argv[i]);
311 for(i=0;i<3&&argc>i&&!option(argv[i]);i++)
312 position[i] = atof(argv[i]);
315 if(i!=3||position[2]<=0)
316 error("incomplete positioning");
319 if(argc>0&&!option(argv[0])) {
320 symbolfile = argv[0];
326 if(index[k].limb == 0)
327 error("-v does not apply here");
334 if(argc && !option(*argv)) {
335 currcolor = colorcode(*argv);
343 error("error in arguments");
345 clamp(&limits[0],-90.);
346 clamp(&limits[1],90.);
347 clamp(&klimits[0],-90.);
348 clamp(&klimits[1],90.);
349 clamp(&window[0],-90.);
350 clamp(&window[1],90.);
351 radbds(limits,rlimits);
352 limcase = limits[2]<-180.?0:
356 window[0]>=window[1]||
357 window[2]>=window[3]||
362 error("unreasonable window");
364 radbds(window,rwindow);
365 upright = orientation[0]==90? 1: orientation[0]==-90? -1: 0;
366 if(index[k].spheroid && !upright)
367 error("can't tilt the spheroid");
368 if(limits[2]>limits[3])
371 orientation[2] = (limits[2]+limits[3])/2;
372 orient(orientation[0],orientation[1],orientation[2]);
373 projection = (*index[k].prog)(params[0],params[1]);
375 error("unreasonable projection parameters");
377 grid[0] = fabs(grid[0]);
378 grid[1] = fabs(grid[1]);
381 klimits[i] = limits[i];
382 if(klimits[2]>klimits[3])
388 if(lolon>=hilon||lolat>=hilat||lolat<-90.||hilat>90.)
389 error("unreasonable limits");
390 wlim = kflag? klimits: window;
391 dlat = fmin(hilat-lolat,wlim[1]-wlim[0])/16;
392 dlon = fmin(hilon-lolon,wlim[3]-wlim[2])/32;
393 dd = fmax(dlat,dlon);
394 while(grid[2]>fmin(dlat,dlon)/2)
398 for(lat=klimits[0];lat<klimits[1]+dd-FUZZ;lat+=dd) {
401 for(lon=klimits[2];lon<klimits[3]+dd-FUZZ;lon+=dd) {
402 i = (kflag?posproj:normproj)
403 (lat,lon+(lon<klimits[3]?FUZZ:-FUZZ),
414 for(i=0; i<nvert; i++) {
423 xrange = xmax - xmin;
424 yrange = ymax - ymin;
425 if(xrange<=0||yrange<=0)
426 error("map seems to be empty");
427 scaling = 2; /*plotting area from -1 to 1*/
429 if(posproj(position[0]-.5,position[1],&xcent,&ycent)==0||
430 posproj(position[0]+.5,position[1],&x,&y)==0)
431 error("unreasonable position");
432 scaling /= (position[2]*hypot(x-xcent,y-ycent));
433 if(posproj(position[0],position[1],&xcent,&ycent)==0)
434 error("unreasonable position");
436 scaling /= (xrange>yrange?xrange:yrange);
437 xcent = (xmin+xmax)/2;
438 ycent = (ymin+ymax)/2;
440 xoff = center[0]/scaling;
441 yoff = center[1]/scaling;
442 crot.l = center[2]*RAD;
444 scaling *= HALFWIDTH*0.9;
451 range(left,bottom,right,top);
456 for(lat=ceil(lolat/grid[0])*grid[0];
457 lat<=hilat;lat+=grid[0])
458 dogrid(lat,lat,lolon,hilon);
460 for(lon=ceil(lolon/grid[1])*grid[1];
461 lon<=hilon;lon+=grid[1])
462 dogrid(lolat,hilat,lon,lon);
463 comment("border","");
468 dobounds(lolat,hilat,lolon,hilon,0);
469 dobounds(window[0],window[1],window[2],window[3],1);
471 lolat = floor(limits[0]/10)*10;
472 hilat = ceil(limits[1]/10)*10;
473 lolon = floor(limits[2]/10)*10;
474 hilon = ceil(limits[3]/10)*10;
477 /*do tracks first so as not to lose the standard input*/
478 for(i=0;i<ntrack;i++) {
479 longlines = LONGLINES;
480 satellite(&track[i]);
481 longlines = shortlines;
483 for(i=0;i<nfile;i++) {
484 comment("mapfile",file[i].name);
485 colorx(file[i].color);
487 getdata(file[i].name);
495 /* Out of perverseness (really to recover from a dubious,
496 but documented, convention) the returns from projection
497 functions (-1 unplottable, 0 wrong sheet, 1 good) are
498 recoded into -1 wrong sheet, 0 unplottable, 1 good. */
501 fixproj(struct place *g, double *x, double *y)
503 int i = (*projection)(g,x,y);
504 return i<0? 0: i==0? -1: 1;
508 normproj(double lat, double lon, double *x, double *y)
512 latlon(lat,lon,&geog);
519 i = fixproj(&geog,x,y);
524 fprintf(stderr,"%d %.3f %.3f\n",i,*x,*y);
530 posproj(double lat, double lon, double *x, double *y)
534 latlon(lat,lon,&geog);
536 i = fixproj(&geog,x,y);
543 inwindow(struct place *geog)
545 if(geog->nlat.l<rwindow[0]-FUZZ||
546 geog->nlat.l>rwindow[1]+FUZZ||
547 geog->wlon.l<rwindow[2]-FUZZ||
548 geog->wlon.l>rwindow[3]+FUZZ)
554 inlimits(struct place *g)
556 if(rlimits[0]-FUZZ>g->nlat.l||
557 rlimits[1]+FUZZ<g->nlat.l)
561 if(rlimits[2]+TWOPI-FUZZ>g->wlon.l&&
562 rlimits[3]+FUZZ<g->wlon.l)
566 if(rlimits[2]-FUZZ>g->wlon.l||
567 rlimits[3]+FUZZ<g->wlon.l)
571 if(rlimits[2]>g->wlon.l&&
572 rlimits[3]-TWOPI+FUZZ<g->wlon.l)
583 getdata(char *mapfile)
598 indexfile = mapindex(mapfile);
599 xfile = fopen(indexfile,"r");
601 filerror("can't find map index", indexfile);
603 for(i=0,p=patch[0];i<18*36;i++,p++)
605 while(!feof(xfile) && fscanf(xfile,"%d%d%ld",&i,&j,&b)==3)
606 patch[i+9][j+18] = b;
608 ifile = fopen(mapfile,"r");
610 filerror("can't find map data", mapfile);
611 for(lat=lolat;lat<hilat;lat+=10.)
612 for(lon=lolon;lon<hilon;lon+=10.) {
613 if(!seeable(lat,lon))
617 if((b=patch[i+9][j+18])&1)
620 while((ip=getc(ifile))>=0&&(jp=getc(ifile))>=0){
621 if(ip!=(i&0377)||jp!=(j&0377))
625 if(n > 0) { /* absolute coordinates */
626 kx = ky = 0; /* set */
628 kx = SCALERATIO*getshort(ifile);
629 ky = SCALERATIO*getshort(ifile);
630 if (((k%delta) != 0) && (k != (n-1)))
634 conn = plotpt(&g,conn);
636 } else { /* differential, scaled by SCALERATI0 */
638 kx = SCALERATIO*getshort(ifile);
639 ky = SCALERATIO*getshort(ifile);
642 if(c&0200) c|= ~0177;
645 if(c&0200) c|= ~0177;
647 if(k%delta!=0&&k!=n-1)
651 conn = plotpt(&g,conn);
665 seeable(double lat0, double lon0)
669 for(lat=lat0;lat<=lat0+10;lat+=2*grid[2])
670 for(lon=lon0;lon<=lon0+10;lon+=2*grid[2])
671 if(normproj(lat,lon,&x,&y)*vflag>0)
677 satellite(struct file *t)
685 static FILE *ifile = stdin;
686 if(t->name[0]!='-'||t->name[1]!=0) {
688 if((ifile=fopen(t->name,"r"))==NULL)
689 filerror("can't find track", t->name);
691 comment("track",t->name);
696 while(!feof(ifile) && fscanf(ifile,"%lf%lf",&lat,&lon)==2){
697 latlon(lat,lon,&place);
698 if(fscanf(ifile,"%1s",lbl) == 1) {
699 if(strchr("+-.0123456789",*lbl)==0)
703 conn = plotpt(&place,conn);
707 fscanf(ifile,"%[^\n]",lbl+1);
710 if(plotpt(&place,conn))
715 if(sscanf(lbl+1,"%s %lf",sym,&scale) <= 1)
717 if(plotpt(&place,conn?conn:-1)) {
718 int r = *lbl=='!'?0:rflag?-1:1;
720 if(putsym(&place,sym,scale,r) == 0)
726 if(plotpt(&place,conn))
739 if(i>=18) return(i-36);
740 if(i<-18) return(i+36);
747 fprintf(stderr,"map: \r\n%s\n",s);
752 filerror(char *s, char *f)
754 fprintf(stderr,"\r\n%s %s\n",s,f);
761 char *t = malloc(strlen(s)+3);
768 static ox = NOPT, oy = NOPT;
771 cpoint(int xi, int yi, int conn)
775 if(!xflag && (xi<left||xi>=right || yi<bottom||yi>=top)) {
779 if(conn == -1) /* isolated plotting symbol */
784 if(dx+dy>longlines) {
785 ox = oy = NOPT; /* don't leap across cuts */
792 return dx+dy<=2? 2: 1; /* 2=very near; see dogrid */
799 plotpt(struct place *g, int conn)
811 switch((*cut)(g,&oldg,&cutlon)) {
814 ret = duple(g,cutlon)|duple(g,cutlon);
820 default: /* prevent diags about bad return value */
823 ret = doproj(g,&kx,&ky);
824 if(ret==0 || !onlimb && ret*vflag<=0)
826 ret = cpoint(kx,ky,conn);
832 doproj(struct place *g, int *kx, int *ky)
836 /*fprintf(stderr,"dopr1 %f %f \n",g->nlat.l,g->wlon.l);*/
837 i = fixproj(g,&x,&y);
842 /*fprintf(stderr,"dopr2 %f %f\n",x,y);*/
848 x = (x1*crot.c - y1*crot.s + xoff)*scaling;
849 y = (x1*crot.s + y1*crot.c + yoff)*scaling;
850 *kx = x + (x>0?.5:-.5);
851 *ky = y + (y>0?.5:-.5);
856 duple(struct place *g, double cutlon)
862 revlon(&oldg,cutlon);
867 if(doproj(g,&kx,&ky)*vflag<=0 ||
868 doproj(&oldg,&okx,&oky)*vflag<=0)
876 revlon(struct place *g, double cutlon)
878 g->wlon.l = reduce(cutlon-reduce(g->wlon.l-cutlon));
883 /* recognize problems of cuts
884 * move a point across cut to side of its predecessor
885 * if its very close to the cut
886 * return(0) if cut interrupts the line
887 * return(1) if line is to be drawn normally
888 * return(2) if line is so close to cut as to
889 * be properly drawn on both sheets
893 picut(struct place *g, struct place *og, double *cutlon)
896 return(ckcut(g,og,PI));
900 nocut(struct place *g, struct place *og, double *cutlon)
912 ckcut(struct place *g1, struct place *g2, double lon)
917 d1 = reduce(g1->wlon.l -lon);
918 d2 = reduce(g2->wlon.l -lon);
919 if((f1=fabs(d1))<FUZZ)
920 d1 = diddle(g1,lon,d2);
921 if((f2=fabs(d2))<FUZZ) {
922 d2 = diddle(g2,lon,d1);
923 if(doproj(g2,&kx,&ky)*vflag>0)
928 if(f1>PI*TWO_THRD||f2>PI*TWO_THRD)
934 diddle(struct place *g, double lon, double d)
940 g->wlon.l = reduce(lon+d1);
956 double tetrapt = 35.26438968; /* atan(1/sqrt(2)) */
959 dogrid(double lat0, double lat1, double lon0, double lon1)
961 double slat,slon,tlat,tlon;
962 register int conn, oconn;
963 slat = tlat = slon = tlon = 0;
965 slat = tlat = fmin(grid[2],dlat);
967 slon = tlon = fmin(grid[2],dlon);;
969 while(lat0<=lat1&&lon0<=lon1) {
970 conn = gridpt(lat0,lon0,conn);
971 if(projection==Xguyou&&slat>0) {
972 if(lat0<-45&&lat0+slat>-45)
973 conn = gridpt(-45.,lon0,conn);
974 else if(lat0<45&&lat0+slat>45)
975 conn = gridpt(45.,lon0,conn);
976 } else if(projection==Xtetra&&slat>0) {
977 if(lat0<-tetrapt&&lat0+slat>-tetrapt) {
978 gridpt(-tetrapt-.001,lon0,conn);
979 conn = gridpt(-tetrapt+.001,lon0,0);
981 else if(lat0<tetrapt&&lat0+slat>tetrapt) {
982 gridpt(tetrapt-.001,lon0,conn);
983 conn = gridpt(tetrapt+.001,lon0,0);
986 if(conn==0 && oconn!=0) {
988 lat0 -= slat; /* steps too big */
989 lon0 -= slon; /* or near bdry */
992 conn = oconn = gridpt(lat0,lon0,conn);
1006 gridpt(lat1,lon1,conn);
1009 static gridinv; /* nonzero when doing window bounds */
1012 gridpt(double lat, double lon, int conn)
1015 /*fprintf(stderr,"%f %f\n",lat,lon);*/
1019 return(plotpt(&g,conn));
1022 /* win=0 ordinary grid lines, win=1 window lines */
1025 dobounds(double lolat, double hilat, double lolon, double hilon, int win)
1028 if(lolat>-90 || win && (poles&1)!=0)
1029 dogrid(lolat+FUZZ,lolat+FUZZ,lolon,hilon);
1030 if(hilat<90 || win && (poles&2)!=0)
1031 dogrid(hilat-FUZZ,hilat-FUZZ,lolon,hilon);
1032 if(hilon-lolon<360 || win && cut==picut) {
1033 dogrid(lolat,hilat,lolon+FUZZ,lolon+FUZZ);
1034 dogrid(lolat,hilat,hilon-FUZZ,hilon-FUZZ);
1043 double res = fmin(dlat, dlon)/4;
1048 onlimb = gridinv = 1;
1050 newconn = (*limb)(&lat, &lon, res);
1053 conn = gridpt(lat, lon, conn*newconn);
1055 onlimb = gridinv = 0;
1060 radbds(double *w, double *rw)
1074 double center = orientation[0];
1077 center = 180 - center;
1079 center = -180 - center;
1081 error("unreasonable orientation");
1082 colat = 90 - window[0];
1083 if(center-colat>limits[0])
1084 limits[0] = center - colat;
1085 if(center+colat<limits[1])
1086 limits[1] = center + colat;
1095 r = (c | getc(f)<<8);
1097 r |= ~0xFFFF; /* in case short > 16 bits */
1102 fmin(double x, double y)
1108 fmax(double x, double y)
1114 clamp(double *px, double v)
1116 *px = (v<0?fmax:fmin)(*px,v);
1123 char *t, *indexfile, *name;
1125 for(i=0; i<nfile; i++) {
1126 name = file[i].name;
1129 indexfile = mapindex(name);
1130 /* ansi equiv of unix access() call */
1131 f = fopen(name, "r");
1132 fx = fopen(indexfile, "r");
1138 t = malloc(strlen(name)+strlen(mapdir)+2);
1153 for(i=0; i<nvert; i++) { /*convert latlon to xy*/
1154 if(normproj(v[i].x,v[i].y,&v[i].x,&v[i].y)==0)
1155 error("invisible clipping vertex");
1157 if(nvert==2) { /*rectangle with diag specified*/
1160 v[1].x=v[0].x, v[1].y=v[2].y, v[3].x=v[2].x, v[3].y=v[0].y;
1165 for(i=1; i<=nvert; i++) { /*test for convexity*/
1166 t = (v[i-1].x-v[i].x)*(v[i+1].y-v[i].y) -
1167 (v[i-1].y-v[i].y)*(v[i+1].x-v[i].x);
1168 if(t<-FUZZ && s>=0) s = 1;
1169 if(t>FUZZ && s<=0) s = -1;
1170 if(-FUZZ<=t&&t<=FUZZ || t*s>0) {
1176 error("improper clipping polygon");
1177 for(i=0; i<nvert; i++) { /*edge equation ax+by=c*/
1178 e[i].a = s*(v[i+1].y - v[i].y);
1179 e[i].b = s*(v[i].x - v[i+1].x);
1180 e[i].c = s*(v[i].x*v[i+1].y - v[i].y*v[i+1].x);
1185 inpoly(double x, double y)
1188 for(i=0; i<nvert; i++) {
1189 register struct edge *ei = &e[i];
1190 double val = x*ei->a + y*ei->b - ei->c;
1203 if(cut != picut) /* punt on unusual cuts */
1205 for(lat=window[0]; lat<=window[1]; lat+=grid[2]) {
1218 longlines = shortlines = LONGLINES;
1219 cut = nocut; /* not necessary; small eff. gain */