d3618f506cd14821ff303e1cda804b87c8d12498
[debian/splat] / utils / citydecoder.c
1 /****************************************************************************
2 *                CITYDECODER: A SPLAT! File Conversion Utility              *
3 *                  Copyright John A. Magliacane, KD2BD 2002                 *
4 *                         Last update: 13-Apr-2002                          *
5 *****************************************************************************
6 *                                                                           *
7 * This utility reads ASCII Metadata Cartographic Boundary Files available   *
8 * through the U.S. Census Bureau, and generates a lists of cities, states,  *
9 * and counties along with the latitude and longitude corresponding to their *
10 * geographic centers.  Such data may be (optionally) sorted and written to  *
11 * files for use with SPLAT! software.  This utility takes as an argument,   *
12 * two-letter prefix plus the FIPS code for the state being processed (ie:   *
13 * "citydecoder pl34" will read files "pl34_d00.dat" and "pl34_d00a.dat",    *
14 * and produce a list of city names and geographical coordinates for the     *
15 * state of New Jersey.                                                      *
16 *                                                                           *
17 *****************************************************************************
18 *                                                                           *
19 * This program is free software; you can redistribute it and/or modify it   *
20 * under the terms of the GNU General Public License as published by the     *
21 * Free Software Foundation; either version 2 of the License or any later    *
22 * version.                                                                  *
23 *                                                                           *
24 * This program is distributed in the hope that it will useful, but WITHOUT  *
25 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     *
26 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     *
27 * for more details.                                                         *
28 *                                                                           *
29 *****************************************************************************
30 *          To compile: gcc -Wall -O6 -s citydecoder.c -o citydecoder        *
31 *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36
37 int main(argc,argv)
38 char argc, *argv[];
39 {
40         int x, y, z;
41         long attributefile_id, coordinatefile_id;
42         char string[80], name[80], attributefilename[15], coordinatefilename[15];
43         double lat, lon;
44         FILE *attributefile=NULL, *coordinatefile=NULL;
45
46         if (argc==1)
47         {
48                 fprintf(stderr,"\n*** Usage: citydecoder pl34 pl36 pl42 | sort > outputfile\n\n");
49                 exit(1);
50         }
51
52         for (z=1; z<argc; z++)
53         {
54                 sprintf(attributefilename,"%s_d00a.dat",argv[z]);
55                 sprintf(coordinatefilename,"%s_d00.dat",argv[z]);
56
57                 attributefile=fopen(attributefilename,"r");
58                 coordinatefile=fopen(coordinatefilename,"r");
59
60                 if (attributefile!=NULL && coordinatefile!=NULL)
61                 {
62                         /* Skip First ASCII File Record (ID=0) */
63
64                         for (x=0; x<7; x++)
65                                 fgets(string,80,attributefile);
66
67                         do
68                         {
69                                 string[0]=0;
70                                 fscanf(coordinatefile,"%ld", &coordinatefile_id);
71
72                                 if (coordinatefile_id!=-99999)
73                                 {
74                                         name[0]=0;
75
76                                         fscanf(coordinatefile,"%lf %lf",&lon, &lat);
77
78                                         /* Read ID Number From Attribute File */
79
80                                         fgets(string,80,attributefile);
81                                         sscanf(string,"%ld",&attributefile_id);
82
83
84                                         /* Skip Two Strings in Attribute File */
85
86                                         fgets(string,80,attributefile);
87                                         fgets(string,80,attributefile);
88
89
90                                         /* Read City Name From Attribute File */
91
92                                         fgets(string,80,attributefile);
93
94                                         /* Strip "quote" characters from name */
95
96                                         for (x=2, y=0; string[x]!='"' && string[x]!=0; x++, y++)
97                                                 name[y]=string[x];
98
99                                         name[y]=0;
100
101                                         /* Skip Two Strings in Attribute File */
102
103                                         fgets(string,80,attributefile);
104                                         fgets(string,80,attributefile);
105
106                                         /* Skip blank line between records */
107
108                                         fgets(string,80,attributefile);
109
110                                         if (name[0]!=0 && name[0]!=' ' && feof(attributefile)==0 && attributefile_id==coordinatefile_id)
111                                                 printf("%s, %f, %f\n",name,lat,-lon);
112                                 }
113
114
115                                 /* Read to the end of the current coordinatefile record */
116
117                                 do
118                                 {
119                                         string[0]=0;
120                                         fscanf(coordinatefile,"%s",string);
121
122                                 } while (strncmp(string,"END",3)!=0 && feof(coordinatefile)==0);
123
124                         } while (feof(coordinatefile)==0);
125
126                         fclose(attributefile);
127                         fclose(coordinatefile);
128                 }
129
130                 else
131                 {
132                         /* Houston, We Have A Problem... */
133
134                         fprintf(stderr,"%c\n",7);
135
136                         if (coordinatefile==NULL)
137                                 fprintf(stderr,"*** Error opening coordinate file: \"%s\"!\n",coordinatefilename);
138
139                         if (attributefile==NULL)
140                                 fprintf(stderr,"*** Error opening attribute file : \"%s\"!\n",attributefilename);
141                         fprintf(stderr,"\n");
142                 }
143         }
144                 
145         exit(0);
146 }