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