1 /****************************************************************************
2 * CITYDECODER: A SPLAT! File Conversion Utility *
3 * Copyright John A. Magliacane, KD2BD 2002 *
4 * Last update: 08-Feb-2009 *
5 *****************************************************************************
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. *
17 *****************************************************************************
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 *
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 *
29 *****************************************************************************
30 * To compile: gcc -Wall -O6 -s citydecoder.c -o citydecoder *
31 *****************************************************************************/
41 long attributefile_id, coordinatefile_id;
42 char string[80], name[80], attributefilename[15], coordinatefilename[15], *s=NULL;
44 FILE *attributefile=NULL, *coordinatefile=NULL;
48 fprintf(stderr,"\n*** Usage: citydecoder pl34 pl36 pl42 | sort > outputfile\n\n");
52 for (z=1; z<argc; z++)
54 sprintf(attributefilename,"%s_d00a.dat",argv[z]);
55 sprintf(coordinatefilename,"%s_d00.dat",argv[z]);
57 attributefile=fopen(attributefilename,"r");
58 coordinatefile=fopen(coordinatefilename,"r");
60 if (attributefile!=NULL && coordinatefile!=NULL)
62 /* Skip First ASCII File Record (ID=0) */
65 s=fgets(string,80,attributefile);
70 n=fscanf(coordinatefile,"%ld", &coordinatefile_id);
72 if (coordinatefile_id!=-99999)
76 n=fscanf(coordinatefile,"%lf %lf",&lon, &lat);
78 /* Read ID Number From Attribute File */
80 s=fgets(string,80,attributefile);
81 n=sscanf(string,"%ld",&attributefile_id);
84 /* Skip Two Strings in Attribute File */
86 s=fgets(string,80,attributefile);
87 s=fgets(string,80,attributefile);
90 /* Read City Name From Attribute File */
92 s=fgets(string,80,attributefile);
94 /* Strip "quote" characters from name */
96 for (x=2, y=0; string[x]!='"' && string[x]!=0; x++, y++)
101 /* Skip Two Strings in Attribute File */
103 s=fgets(string,80,attributefile);
104 s=fgets(string,80,attributefile);
106 /* Skip blank line between records */
108 s=fgets(string,80,attributefile);
110 if (name[0]!=0 && name[0]!=' ' && feof(attributefile)==0 && attributefile_id==coordinatefile_id)
111 printf("%s, %f, %f\n",name,lat,-lon);
115 /* Read to the end of the current coordinatefile record */
120 n=fscanf(coordinatefile,"%s",string);
122 } while (strncmp(string,"END",3)!=0 && feof(coordinatefile)==0);
124 } while (feof(coordinatefile)==0);
126 fclose(attributefile);
127 fclose(coordinatefile);
132 /* Houston, We Have A Problem... */
134 fprintf(stderr,"%c\n",7);
136 if (coordinatefile==NULL)
137 fprintf(stderr,"*** Error opening coordinate file: \"%s\"!\n",coordinatefilename);
139 if (attributefile==NULL)
140 fprintf(stderr,"*** Error opening attribute file : \"%s\"!\n",attributefilename);
141 fprintf(stderr,"\n");