1 #include <string.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <grass/gis.h>
5 #include <grass/glocale.h>
6 #include "local_proto.h"
7 
get_stp_proj(char string[])8 void get_stp_proj(char string[])
9 {
10     int code;
11     char answer[50], buff[50];
12 
13     while ((code = get_stp_num()) == 0) {
14 	if (G_yes
15 	    ("Are you sure you want to exit without making any changes", 0))
16 	    leave(SP_NOCHANGE);
17     }
18     for (;;) {
19 
20 	do {
21 	    fprintf(stderr, "\nSpecify State Plane 1927 or 1983\n");
22 	    fprintf(stderr, "Enter '27' or '83'\n");
23 	    fprintf(stderr, "Hit RETURN to cancel request\n");
24 	    fprintf(stderr, ">");
25 	} while (!G_gets(answer));
26 
27 	G_strip(answer);
28 	if (strlen(answer) == 0) {
29 	    leave(SP_NOCHANGE);
30 	}
31 	else if (strcmp(answer, "27") == 0) {
32 	    sprintf(buff, STP1927PARAMS);
33 	    break;
34 	}
35 	else if (strcmp(answer, "83") == 0) {
36 	    sprintf(buff, STP1983PARAMS);
37 	    break;
38 	}
39 	else
40 	    fprintf(stderr, "\nInvalid Co-ordinate System Specification\n");
41     }
42     if (get_stp_code(code, string, buff) == 0)
43 	G_fatal_error(_("This should not happen. See your system admin."));
44 
45     return;
46 }
47 
get_stp_code(int code,char * string,char * paramfile)48 int get_stp_code(int code, char *string, char *paramfile)
49 {
50     char nad27[256], buff[256], *p;
51     int gotit = 0, stp;
52     FILE *fp;
53 
54 
55     sprintf(nad27, "%s%s", G_gisbase(), paramfile);
56     fp = fopen(nad27, "r");
57     if (fp == NULL) {
58 	sprintf(buff, "Can not open NAD27 file %s", nad27);
59 	G_fatal_error(buff);
60     }
61     while (!gotit) {
62 	if (fgets(buff, 200, fp) == NULL)
63 	    break;
64 	if (buff[0] != '#') {
65 	    sscanf(buff, "%d:", &stp);
66 	    if (stp == code) {
67 		p = strtok(buff, ":");
68 		p = strtok(NULL, "\n");
69 		while (*p == ' ')
70 		    p++;
71 		sprintf(string, "%s", p);
72 		gotit = 1;
73 	    }
74 	}
75     }
76     fclose(fp);
77     return (gotit);
78 }
79 
80 
81 
82 
83 
get_stp_num(void)84 int get_stp_num(void)
85 {
86     FILE *fipsfile;
87     char FIPSfile[256], buff[256];
88     int NUM_ZON, sfips, cfips, SFIPS = 0, CFIPS = 0;
89     int record, icode, reccnt, special_case;
90     char STabbr[50], COname[50];
91 
92     sprintf(FIPSfile, "%s/etc/proj/FIPS.code", G_gisbase());
93 
94 
95     for (;;) {
96 
97 	fipsfile = fopen(FIPSfile, "r");
98 	if (fipsfile == NULL) {
99 	    G_fatal_error(_("Unable to open FIPS code file"));
100 	}
101 	ask_fips(fipsfile, &SFIPS, &CFIPS, &special_case);
102 	if (special_case == -1) {
103 	    fclose(fipsfile);
104 	    return (0);
105 	}
106 	/* combine SFIPS and CFIPS to make lookup */
107 	/*DEBUG fprintf(stderr,"FIPS = %d %d\n",SFIPS,CFIPS); */
108 
109 	for (record = 0;; ++record) {
110 	    icode = 0;
111 	    reccnt++;
112 	    if (fgets(buff, 80, fipsfile) == NULL)
113 		break;
114 	    sscanf(buff, "%d%d%s%s%d", &sfips, &cfips, STabbr, COname,
115 		   &NUM_ZON);
116 	    /* compare for match */
117 	    if (SFIPS == sfips && CFIPS == cfips) {
118 		icode = 1;
119 		break;
120 	    }
121 	}			/* end file search */
122 	if (icode != 0)
123 	    break;
124 	else {			/* no match */
125 	    G_warning(_("No match of FIPS state %d county %d"), SFIPS, CFIPS);
126 	    fclose(fipsfile);
127 	}
128     }
129 
130 /**** SPECIAL CASE FOR MICHIGAN ****, could be mercator or lambert */
131     if (SFIPS == 26) {
132 	if (special_case == 2)
133 	    NUM_ZON = NUM_ZON + 10;
134     }
135 
136 /**** SPECIAL CASE FOR ALASKA *****  */
137     if (SFIPS == 2) {
138 	NUM_ZON = NUM_ZON + special_case;
139     }
140     /* all done, good-bye */
141     fclose(fipsfile);
142     return (NUM_ZON);
143 }
144 
145 
146 
147 
ask_fips(FILE * fp,int * s,int * c,int * sc)148 int ask_fips(FILE * fp, int *s, int *c, int *sc)
149 {
150     int ii, FIPS = 0, NUM_ZON, sfips, cfips;
151     char STabbr[50], STabbr_prev[50], COname[50], answer[50], buff[256];
152     char *Tmp_file1, *Tmp_file2, *a, *b;
153     FILE *Tmp_fd1 = NULL, *Tmp_fd2 = NULL;
154     int in_stat;
155     struct Key_Value *sf_keys, *cf_keys;
156 
157     *sc = 0;
158     *s = 0;
159     *c = 0;
160     Tmp_file1 = G_tempfile();
161     if (NULL == (Tmp_fd1 = fopen(Tmp_file1, "w")))
162 	G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file1);
163     Tmp_file2 = G_tempfile();
164     if (NULL == (Tmp_fd2 = fopen(Tmp_file2, "w")))
165 	G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file2);
166     while (fgets(buff, 80, fp) != NULL) {
167 	sscanf(buff, "%d%d%s%s%d", &sfips, &cfips, STabbr, COname, &NUM_ZON);
168 	if (strncmp(STabbr, STabbr_prev, 2) != 0) {	/* TODO CHECK THIS */
169 	    fprintf(Tmp_fd1, "%4d -- %s\n", sfips, STabbr);
170 	    fprintf(Tmp_fd2, "%d:%s\n", sfips, STabbr);
171 	}
172 	sprintf(STabbr_prev, "%s", STabbr);
173     }
174     fclose(Tmp_fd1);
175     fclose(Tmp_fd2);
176 
177     sf_keys = G_read_key_value_file(Tmp_file2, &in_stat);
178     if (in_stat != 0)
179 	G_fatal_error(_("Reading sf key_value temp file"));
180 
181     for (;;) {
182 
183 	do {
184 	    fprintf(stderr, "\nSpecify State FIPS (numeric) code\n");
185 	    fprintf(stderr,
186 		    "Enter 'list' for the list of states with corresponding FIPS codes\n");
187 	    fprintf(stderr, "Hit RETURN to cancel request\n");
188 	    fprintf(stderr, ">");
189 	} while (!G_gets(answer));
190 
191 	G_strip(answer);
192 	if (strlen(answer) == 0) {
193 	    *sc = -1;
194 	    return 0;
195 	}
196 	if (strcmp(answer, "list") == 0) {
197 	    char *pager;
198 
199 	    pager = getenv("GRASS_PAGER");
200 	    if (!pager || strlen(pager) == 0)
201 		pager = "cat";
202 
203 	    /* Always print interactive output to stderr */
204 	    sprintf(buff, "%s \"%s\" 1>&2", pager,
205 		    G_convert_dirseps_to_host(Tmp_file1));
206 	    G_system(buff);
207 	}
208 	else {
209 	    a = G_find_key_value(answer, sf_keys);
210 	    sprintf(buff, "You have chosen state %s, Correct", a);
211 	    if (a == NULL)
212 		G_warning(_("Invalid State FIPS code"));
213 	    else if (G_yes(buff, 1))
214 		break;
215 	}
216     }
217     rewind(fp);
218 
219     sscanf(answer, "%d", s);
220 
221     FIPS = *s;
222 
223 /**** SPECIAL CASE FOR MICHIGAN ****, could be mercator or lambert */
224     if (FIPS == 26) {
225 	/*
226 	   fprintf(stderr,"\nFor Michigan select- 1- East to West\n");
227 	   fprintf(stderr,"                     2- North to South\n: ");
228 	 */
229 	ii = 0;
230 	for (;;) {
231 	    do {
232 		fprintf(stderr, "\nFor Michigan select- 1- East to West\n");
233 		fprintf(stderr, "                     2- North to South\n: ");
234 		fprintf(stderr, "Hit RETURN to cancel request\n> ");
235 
236 	    } while (!G_gets(answer));
237 
238 	    G_strip(answer);
239 	    if (strlen(answer) == 0) {
240 		*sc = -1;
241 		return 0;
242 	    }
243 	    sscanf(answer, "%d", &ii);
244 	    if (ii != 1 && ii != 2)
245 		fprintf(stderr, "\n Invalid Entry\n ");
246 	    else
247 		break;
248 	}
249 	*sc = ii;
250     }
251 
252 /**** SPECIAL CASE FOR ALASKA *****  */
253     if (FIPS == 2) {
254 	ii = 0;
255 	for (;;) {
256 
257 	    do {
258 		fprintf(stderr,
259 			"\nFor Alaska enter the zone (1 through 9): \n");
260 		fprintf(stderr, "Hit RETURN to cancel request\n");
261 		fprintf(stderr, "> ");
262 	    } while (!G_gets(answer));
263 
264 	    G_strip(answer);
265 	    if (strlen(answer) == 0) {
266 		*sc = -1;
267 		return 0;
268 	    }
269 	    sscanf(answer, "%d", &ii);
270 	    if (ii < 1 || ii > 9)
271 		fprintf(stderr, "\n Invalid Entry\n ");
272 	    else
273 		break;
274 	}
275 	*sc = ii;
276     }
277     unlink(Tmp_file1);
278     unlink(Tmp_file2);
279 
280     Tmp_file1 = G_tempfile();
281     if (NULL == (Tmp_fd1 = fopen(Tmp_file1, "w"))) {
282 	G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file1);
283     }
284     Tmp_file2 = G_tempfile();
285     if (NULL == (Tmp_fd2 = fopen(Tmp_file2, "w"))) {
286 	G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file2);
287     }
288     while (fgets(buff, 80, fp) != NULL) {
289 	sscanf(buff, "%d%d%s%[A-Z ]%d", &sfips, &cfips, STabbr, COname,
290 	       &NUM_ZON);
291 	G_strip(COname);
292 	if (sfips == *s) {
293 	    fprintf(Tmp_fd1, "%4d -- %s\n", cfips, COname);
294 	    fprintf(Tmp_fd2, "%d:%s\n", cfips, COname);
295 	}			/* ADDED THESE BRACKETS - BB 5/2000 */
296     }
297     fclose(Tmp_fd1);
298     fclose(Tmp_fd2);
299 
300     cf_keys = G_read_key_value_file(Tmp_file2, &in_stat);
301     if (in_stat != 0)
302 	G_fatal_error(_("Reading cf key_value temp file"));
303 
304     for (;;) {
305 	do {
306 	    fprintf(stderr,
307 		    "\nSpecify County FIPS (numeric) code for state %s\n", a);
308 	    fprintf(stderr,
309 		    "Enter 'list' for the list of counties in %s with corresponding FIPS codes\n",
310 		    a);
311 	    fprintf(stderr, "Hit RETURN to cancel request\n");
312 	    fprintf(stderr, ">");
313 	} while (!G_gets(answer));
314 
315 	G_strip(answer);
316 	if (strlen(answer) == 0) {
317 	    *sc = -1;
318 	    return 0;
319 	}
320 	if (strcmp(answer, "list") == 0) {
321 	    char *pager;
322 
323 	    pager = getenv("GRASS_PAGER");
324 	    if (!pager || strlen(pager) == 0)
325 		pager = "cat";
326 
327 	    /* Always print interactive output to stderr */
328 	    sprintf(buff, "%s \"%s\" 1>&2", pager,
329 		    G_convert_dirseps_to_host(Tmp_file1));
330 	    G_system(buff);
331 	}
332 	else {
333 	    b = G_find_key_value(answer, cf_keys);
334 	    sprintf(buff, "You have chosen %s county, correct", b);
335 	    if (b == NULL)
336 		G_warning(_("Invalid County FIPS code"));
337 	    else if (G_yes(buff, 1))
338 		break;
339 	}
340     }
341     sscanf(answer, "%d", c);
342     rewind(fp);
343     unlink(Tmp_file1);
344     unlink(Tmp_file2);
345     return 0;
346 }
347