Annotation of researchv10no/cmd/town/drnp.c, revision 1.1

1.1     ! root        1: # include "stdio.h"
        !             2: # include "cbt.h"
        !             3: # include "ctype.h"
        !             4: # include "assert.h"
        !             5: # include "math.h"
        !             6: # include "/usr/theo/PLOT/pplot.h"
        !             7: # define SAME 0
        !             8: # define torad 0.0174532925
        !             9: 
        !            10: double dist(), atof();
        !            11: double clat, clng;
        !            12: double radius=50.0;
        !            13: char countypat[100];
        !            14: char *countyname = "/usr/spool/town/countymap";
        !            15: char statepname[100];
        !            16: char *statename = "/usr/spool/town/statemap";
        !            17: double radlat, radlong;
        !            18: int labx[500];
        !            19: int laby[500];
        !            20: int nlab=0;
        !            21: int ctyflg=0;
        !            22: int countyline=0, numnames=1000;
        !            23: 
        !            24: # define abs(x) ( (t=(x))>= 0 ? t : -t )
        !            25: main(argc,argv)
        !            26:        char *argv[];
        !            27: {
        !            28: char line[100], request[100], rka[100], rra[300];
        !            29: char *s, *strchr();
        !            30: long l1, l2;
        !            31: char nline[100];
        !            32: mbuf key, rkey, rrec;
        !            33: bfile *townfile;
        !            34: FILE *cntypat, *countyf, *namepat, *namef, *pointf=NULL;
        !            35: FILE *statepat, *state, *patch, *fdata, *bigname, *bigpat;
        !            36: double lat, lng;
        !            37: int townfind, x, y, j, use, a1, a2;
        !            38: townfile = bopen("/usr/spool/town/ustowns", 0);
        !            39: argc--; argv++;
        !            40: while (argc-- > 0)
        !            41:        {
        !            42:        if (argv[0][0]=='-')
        !            43:        switch(argv[0][1])
        !            44:                {
        !            45:                case 'f':
        !            46:                        countyname = statename = argv[1];
        !            47:                        argv++; argc--;
        !            48:                        break;
        !            49:                case 'n':
        !            50:                        numnames = atoi(argv[0]+2);
        !            51:                        break;
        !            52:                case 'c':
        !            53:                        countyline = atoi(argv[0]+2);
        !            54:                        ctyflg = 1;
        !            55:                        break;
        !            56:                }
        !            57:        argv++;
        !            58:        }
        !            59: if (townfile==NULL)
        !            60:        {
        !            61:        fprintf(stderr, "No town list\n");
        !            62:        exit(1);
        !            63:        }
        !            64: sprintf(countypat, "%s.pat", countyname);
        !            65: sprintf(statepname, "%s.pat", statename);
        !            66: cntypat = fopen(countypat, "r");
        !            67: assert(cntypat!=NULL);
        !            68: countyf = fopen(countyname, "r");
        !            69: assert(countyf!=NULL);
        !            70: namepat = fopen("/usr/spool/town/ftownpat", "r");
        !            71: assert(namepat!=NULL);
        !            72: namef = fopen("/usr/spool/town/ftown", "r");
        !            73: assert(namef!=NULL);
        !            74: bigpat = fopen("/usr/spool/town/bigpat", "r");
        !            75: assert(bigpat!=NULL);
        !            76: bigname = fopen("/usr/spool/town/big", "r");
        !            77: assert(bigname!=NULL);
        !            78: state = fopen(statename, "r");
        !            79: assert(state!=NULL);
        !            80: statepat = fopen(statepname, "r");
        !            81: assert(statepat!=NULL);
        !            82: rkey.mdata = rka; rrec.mdata = rra;
        !            83: plopen("");
        !            84: while (gets(request))
        !            85:        {
        !            86:        s = strchr(request, '\t');
        !            87:        if (s!=NULL)
        !            88:                {
        !            89:                *s++ = 0;
        !            90:                radius= atof(s);
        !            91:                s = strchr(s, ' ');
        !            92:                if (s!=NULL)
        !            93:                        {
        !            94:                        while (*s==' ')
        !            95:                                s++;
        !            96:                        if (*s)
        !            97:                                pointf = fopen(s, "r");
        !            98:                        }
        !            99:                }
        !           100:        if (strlen(request)==0)
        !           101:                continue;
        !           102:        lcase(request);
        !           103:        townfind=0;
        !           104:        if (ctyflg == 0) countyline = (radius<100.);
        !           105:        nlab=0;
        !           106:        key.mdata = request; key.mlen = strlen(request);
        !           107:        bseek(townfile, key);
        !           108:        while (bread(townfile, &rkey, &rrec)==NULL)
        !           109:                {
        !           110:                rkey.mdata[rkey.mlen]=0;
        !           111:                rrec.mdata[rrec.mlen]=0;
        !           112:                if (strncmp(rkey.mdata, request, key.mlen)!=SAME)
        !           113:                        break;
        !           114:                sscanf(rra, "%lf%lf", &lat, &lng);
        !           115:                townfind++;
        !           116:                }
        !           117:        if (townfind==0)
        !           118:                {
        !           119:                fprintf(stderr, "Can't find that place\n");
        !           120:                continue;
        !           121:                }
        !           122:        if (townfind>1)
        !           123:                {
        !           124:                fprintf(stderr, "Ambiguous - specify state, please\n");
        !           125:                continue;
        !           126:                }
        !           127:        perase();
        !           128:        prange (-5000, 5000, -5000, 5000);
        !           129:        caps(rka);
        !           130:        clat = lat; clng=lng;
        !           131:        /* standard correction latitude = 40.0 N*/
        !           132:        clng = clng * cos(torad*40.0);
        !           133:        radlat = radius/dist(clat, clng, clat+1., clng);
        !           134:        radlong = radius*cos(torad*40.0)/dist(clat, clng, clat, clng+1.);
        !           135:        a1 = lat/4.; a2 = lng/4.;
        !           136:        use=0;
        !           137:        if (countyline)
        !           138:                {
        !           139:                patch = cntypat; fdata = countyf;
        !           140:                }
        !           141:        else
        !           142:                {
        !           143:                patch = statepat; fdata = state;
        !           144:                }
        !           145:        rewind(patch);
        !           146:        fgets(line, 100, patch);
        !           147:        while (fgets(nline, 100, patch))
        !           148:                {
        !           149:                x = atoi(line);
        !           150:                y = atoi(line+3);
        !           151:                if (okpat(x, y, lat, lng))
        !           152:                        {
        !           153:                        ctydraw( atol(line+6), atol(nline+6), fdata);
        !           154:                        use++;
        !           155:                        }
        !           156:                if (x==a1 && y==a2)
        !           157:                        {l1 = atol(line+6); l2 = atol(nline+6);}
        !           158:                strcpy(line, nline);
        !           159:                }
        !           160:        if (use==0)
        !           161:                ctydraw (l1, l2, fdata);
        !           162:        lcase(rka);
        !           163:        if (radius > 200.)
        !           164:                {
        !           165:                patch = bigpat; fdata = bigname;
        !           166:                }
        !           167:        else
        !           168:                {
        !           169:                patch = namepat; fdata = namef;
        !           170:                }
        !           171:        if (patch!=NULL && numnames>0)
        !           172:                {
        !           173:                rewind(patch);
        !           174:                use=0;
        !           175:                fgets(line, 100, patch);
        !           176:                while (fgets(nline, 100, patch))
        !           177:                        {
        !           178:                        x = atoi(line);
        !           179:                        y = atoi(line+3);
        !           180:                        if (okpat(x,y, lat, lng))
        !           181:                                {
        !           182:                                ckcit (atol(line+6), atol(nline+6), fdata);
        !           183:                                use++;
        !           184:                                }
        !           185:                        if (a1==x && a2==y)
        !           186:                                {l1 = atol(line+6); l2 = atol(nline+6);}
        !           187:                        strcpy(line, nline);
        !           188:                        }
        !           189:                if (use==0)
        !           190:                        {
        !           191:                        ckcit( l1, l2, fdata);
        !           192:                        }
        !           193:                }
        !           194:        if (pointf!=NULL)
        !           195:                {
        !           196:                pltpts(pointf);
        !           197:                fclose(pointf);
        !           198:                }
        !           199:        fflush(stdout);
        !           200:        }
        !           201: exit(0);
        !           202: }
        !           203: okpat(ax, ay, lat, lng)
        !           204:        double lat, lng;
        !           205: {
        !           206: double d;
        !           207: d = dist(ax*4., ay*4., lat, lng);
        !           208: if (d<radius) return(1);
        !           209: d = dist((ax+1)*4., ay*4., lat, lng);
        !           210: if (d<radius) return(1);
        !           211: d = dist(ax*4., (ay+1)*4., lat, lng);
        !           212: if (d<radius) return(1);
        !           213: d = dist((ax+1)*4., (ay+1)*4., lat, lng);
        !           214: if (d<radius) return(1);
        !           215: return(0);
        !           216: }
        !           217: ctydraw (pos1, pos2, ff)
        !           218:        FILE *ff;
        !           219:        long pos1, pos2;
        !           220: {
        !           221: float a[4];
        !           222: int xa, ya, xb, yb;
        !           223: register int t;
        !           224: double alat, alng, blat, blng;
        !           225: fseek (ff, pos1, 0);
        !           226: while (fread (a, 4, sizeof(float), ff))
        !           227:        {
        !           228:        if (ftell(ff)>= pos2) break;
        !           229:        alat = a[0];
        !           230:        alng = a[1];
        !           231:        blat = a[2];
        !           232:        blng = a[3];
        !           233: 
        !           234:        xa = (clng - alng)*5000/radlong;
        !           235:        ya = (alat - clat)*5000/radlat;
        !           236:        xb = (clng - blng)*5000/radlong;
        !           237:        yb = (blat - clat)*5000/radlat;
        !           238:        if (abs(xa)<5000 && abs(xb)<5000 && abs(ya)<5000 && abs(yb)<5000)
        !           239:                {
        !           240:                pline(xa,ya,xb,yb);
        !           241:                continue;
        !           242:                }
        !           243:        if ((abs(xa)<5000 && abs(ya)<5000) || (abs(xb)<5000 && abs(yb)<5000) )
        !           244:                {
        !           245:                partial(&xa, &ya, &xb, &yb);
        !           246:                pline(xa, ya, xb, yb);
        !           247:                }
        !           248:        }
        !           249: }
        !           250: double
        !           251: dist (aplat, aplon, bplat, bplon)
        !           252:        double aplon, aplat, bplon, bplat;
        !           253: {
        !           254: /* this code is from the picadad manual. I don't pretend
        !           255:    to understand it. */
        !           256: double as, bs, ac, bc, p, cd, d, er;
        !           257: double rad=0.0174532925;
        !           258: er = aplon-bplon;
        !           259: if (er<0) er= -er;
        !           260: if (er<0.001)
        !           261:        {
        !           262:        d = aplat-bplat;
        !           263:        if (d<0) d= -d;
        !           264:        return(d*69.055);
        !           265:        }
        !           266: as = sin(rad*aplat);
        !           267: bs = sin(rad*bplat);
        !           268: ac = cos(rad*aplat);
        !           269: bc = cos(rad*bplat);
        !           270: p = cos(er*rad);
        !           271: cd = (as*bs+ac*bc*p);
        !           272: return( acos(cd)*3956.56);
        !           273: }
        !           274: lcase (s)
        !           275:        char *s;
        !           276: {
        !           277: int c;
        !           278: for( ; c= *s; s++)
        !           279:        if (isupper(c))
        !           280:                *s = tolower(c);
        !           281: }
        !           282: caps(s)
        !           283:        char *s;
        !           284: {
        !           285: int c;
        !           286: if (islower(*s)) *s = toupper(*s);
        !           287: for( s++; c= *s; s++)
        !           288:        {
        !           289:        if (islower(c) && (s[-1]==' ' || s[-1]=='-'))
        !           290:                if (strncmp(s, "of ", 3)!=SAME)
        !           291:                        *s = toupper(c);
        !           292:        if (*s==',') break;
        !           293:        }
        !           294: for(s++; c= *s; s++)
        !           295:        if (islower(c)) *s=toupper(c);
        !           296: }
        !           297: ckcit(pos1, pos2, countyf)
        !           298:        FILE *countyf;
        !           299:        long pos1, pos2;
        !           300: {
        !           301: char nam[50], *s;
        !           302: int xa, yb, i, xthres, ythres, sz;
        !           303: register int t;
        !           304: double alat, alng;
        !           305: float a[2];
        !           306: fseek (countyf, pos1, 0);
        !           307: while (fread(a, 2, sizeof(float), countyf))
        !           308:        {
        !           309:        if (ftell(countyf) >=pos2) break;
        !           310:        alat = a[0];
        !           311:        alng = a[1];
        !           312:        sz = getc(countyf);
        !           313:        s=nam;
        !           314:        while (i= getc(countyf))
        !           315:                {
        !           316:                if (i=='\n' || i==0) break;
        !           317:                *s++ = i;
        !           318:                }
        !           319:        *s=0;
        !           320:        switch (sz)
        !           321:                {
        !           322:                case '9': xthres=0; ythres=0; break;
        !           323:                case '8': xthres=500; ythres=150; break;
        !           324:                case '7': xthres=1000; ythres=300; break;
        !           325:                case '6': xthres=1500; ythres=400; break;
        !           326:                default : xthres=2000; ythres=400; break;
        !           327:                }
        !           328:        xa = (clng-alng)*5000/radlong;
        !           329:        yb = (alat-clat)*5000/radlat;
        !           330:        if (abs(xa)<5000 && abs(yb)<5000)
        !           331:                {
        !           332:                for(i=0; i<nlab; i++)
        !           333:                        {
        !           334:                        if (abs(xa-labx[i])<xthres && abs(yb-laby[i])<ythres)
        !           335:                                break;
        !           336:                        }
        !           337:                if (i<nlab) continue;
        !           338:                labx[nlab]=xa; laby[nlab]=yb;
        !           339:                nlab++;
        !           340:                assert(nlab<500);
        !           341:                ppoint(xa,yb);
        !           342:                caps(nam);
        !           343:                ptext(nam);
        !           344:                }
        !           345:        if (nlab>numnames) break;
        !           346:        }
        !           347: }
        !           348: pltpts(fi)
        !           349:        FILE *fi;
        !           350: {
        !           351: char ln[100], *s;
        !           352: double alat, alng;
        !           353: int x, y;
        !           354: register int t;
        !           355: while (fgets(ln, 100, fi))
        !           356:        {
        !           357:        trimnl(s=ln);
        !           358:        while (isspace(*s)) s++;
        !           359:        alat = atof(s);
        !           360:        while (*s && !isspace(*s))s++;
        !           361:        while (isspace(*s))s++;
        !           362:        alng = atof(s);
        !           363:        while (*s && !isspace(*s)) s++;
        !           364:        while (isspace(*s)) s++;
        !           365:        if (s==NULL || alat <0.0 || alng < 0.0)
        !           366:                continue;
        !           367:        alng = alng * cos(torad*40.0); /* crummy map projection */
        !           368:        x = (clng - alng)*5000/radlong;
        !           369:        y = (alat - clat)*5000/radlat;
        !           370:        if (abs(x)<5000 && abs(y)<5000)
        !           371:                {
        !           372:                pmove(x,y);
        !           373:                ptext(s);
        !           374:                }
        !           375:        }
        !           376: }
        !           377: partial (x0p, y0p, x1p, y1p)
        !           378:        int *x0p, *y0p, *x1p, *y1p;
        !           379: { /* pick part of line within 5000 limit */
        !           380: int ax, ay, bx, by, cx, cy;
        !           381: double a, b; /* y= ax + b*/
        !           382: register int t;
        !           383: ax = *x0p; ay= *y0p; bx = *x1p; by = *y1p;
        !           384: if (abs(ax)>5000 || abs(ay)>5000)
        !           385:        {
        !           386:        cx = ax; ax = bx; bx = cx;
        !           387:        cy = ay; ay = by; by = cy;
        !           388:        }
        !           389: /* ax, ay is inside; bx, by is outside */
        !           390: if (ax == bx) /* vertical line*/
        !           391:        {
        !           392:        if (by>5000) by=5000;
        !           393:        else
        !           394:        if (by< -5000) by= -5000;
        !           395:        }
        !           396: else
        !           397: if (ay == by) /* horizontal line */
        !           398:        {
        !           399:        if (bx>5000) bx=5000;
        !           400:        else
        !           401:        if (bx< -5000) bx= -5000;
        !           402:        }
        !           403: else
        !           404:        {/* normal case, with slope */
        !           405:        a = ( (double) (by-ay) ) / (bx-ax);
        !           406:        b = ay - a*ax;
        !           407:        if (bx>5000)
        !           408:                {
        !           409:                bx=5000;
        !           410:                by = a*bx+b;
        !           411:                }
        !           412:        else
        !           413:        if (bx< -5000)
        !           414:                {
        !           415:                bx = -5000;
        !           416:                by = a*bx+b;
        !           417:                }
        !           418:        /* now bx is in range.  what about by */
        !           419:        if (by> 5000)
        !           420:                {
        !           421:                by = 5000;
        !           422:                bx = (by -b)/a;
        !           423:                }
        !           424:        else
        !           425:        if (by< -5000)
        !           426:                {
        !           427:                by = -5000;
        !           428:                bx = (by - b) /a;
        !           429:                }
        !           430:        }
        !           431: *x0p = ax;
        !           432: *x1p = bx;
        !           433: *y0p = ay;
        !           434: *y1p = by;
        !           435: }
        !           436: trimnl(s)
        !           437:        char *s;
        !           438: {
        !           439: while (*s)s++;
        !           440: if (*--s== '\n')*s=0;
        !           441: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.