get_stp.c 8.2 KB

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