PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/inptools-0.0.1/src/inpproj.c

#
C | 285 lines | 157 code | 29 blank | 99 comment | 38 complexity | 3373b34f9e01953fa87c3a23c061a338 MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * inpproj.c Projection tool for EPANET INP files
  3. *
  4. * Copyright (c) 2008 Steffen Macke <sdteffen@sdteffen.de>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "epanet2.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "inpproj.h"
  25. #include <proj_api.h>
  26. /**
  27. * These sections have to be projected.
  28. */
  29. char *SectTxtI[] = { "[COORDINATES]", "[VERTICES]", NULL
  30. };
  31. /**
  32. * Input file resource pointer.
  33. */
  34. FILE *InFile;
  35. /**
  36. * Output file resource pointer.
  37. */
  38. FILE *OutFile;
  39. /**
  40. * Tokens in INP file
  41. */
  42. char *Tok[MAXTOKS];
  43. /**
  44. * Project an EPANET INP file.
  45. * @param argc is the number of arguments provided from the command line
  46. * (including the command name).
  47. * @param argv is an array of strings that contains the argc arguments.
  48. * @return An error code if something goes wrong or 0 if there was no error
  49. * during the conversion process.
  50. */
  51. int
  52. main (int argc, char **argv)
  53. {
  54. char line[MAXLINE];
  55. char wline[MAXLINE];
  56. int Ntokens;
  57. int newsect, sect;
  58. char *args_gk[] = { "+proj=tmerc", "+lat_0=0", "+lon_0=9", "+k=1", "+x_0=3500000",
  59. "+y_0=0", "+ellps=bessel", "+datum=potsdam", "+units=m", "+no_defs"};
  60. char *args_wgs84[] = { "+proj=longlat", "+ellps=WGS84", "+datum=WGS84", "+no_defs"};
  61. projPJ pj_gk;
  62. projPJ pj_wgs84;
  63. double x;
  64. double y;
  65. double z;
  66. if(!(pj_gk = pj_init(10, args_gk)))
  67. {
  68. printf("failed to init GK projection\n");
  69. exit(1);
  70. }
  71. if(!(pj_wgs84 = pj_init(14, args_wgs84)))
  72. {
  73. printf("failed to init WGS84 projection\n");
  74. exit(1);
  75. }
  76. sect = -1;
  77. /**
  78. * Check parameters
  79. */
  80. if (argc != 3)
  81. {
  82. printf
  83. ("inpproj 0.0.1 (c) 2008 Steffen Macke <sdteffen@sdteffen.de>\n");
  84. printf ("usage: inpproj input.inp output.inp\n");
  85. exit (1);
  86. }
  87. /**
  88. * Open the files
  89. */
  90. InFile = fopen (argv[1], "rt");
  91. OutFile = fopen (argv[2], "wt");
  92. while (fgets (line, MAXLINE, InFile) != NULL)
  93. {
  94. strcpy (wline, line);
  95. Ntokens = gettokens (wline);
  96. /**
  97. * Skip blank lines and comments
  98. */
  99. if (Ntokens == 0)
  100. {
  101. fprintf(OutFile, "%s", line);
  102. continue;
  103. }
  104. if (*Tok[0] == ';')
  105. {
  106. fprintf(OutFile, "%s", line);
  107. continue;
  108. }
  109. /* Check if max. length exceeded */
  110. if (strlen (line) >= MAXLINE)
  111. {
  112. printf ("WARNING: Line too long.\n");
  113. }
  114. /* Check if line begins with a new section heading */
  115. if (*Tok[0] == '[')
  116. {
  117. fprintf(OutFile, "%s", line);
  118. sect = (-1);
  119. newsect = findmatch (Tok[0], SectTxtI);
  120. if (newsect >= 0)
  121. {
  122. sect = newsect;
  123. continue;
  124. }
  125. else
  126. continue;
  127. }
  128. switch (sect)
  129. {
  130. /* [COORDINATES] */
  131. case 0:
  132. /**
  133. * [VERTICES]
  134. */
  135. case 1:
  136. if (Ntokens == 3)
  137. {
  138. x = strtod(Tok[1], NULL);
  139. y = strtod(Tok[2], NULL);
  140. z = 0.0;
  141. pj_transform(pj_gk, pj_wgs84, 1, 0, &x, &y, &z);
  142. fprintf(OutFile, " %s\t%f\t%f\n", Tok[0], x/DEG_TO_RAD, y/DEG_TO_RAD);
  143. }
  144. break;
  145. default:
  146. fprintf(OutFile, "%s", line);
  147. break;
  148. }
  149. }
  150. exit_inpproj (0);
  151. return 0;
  152. }
  153. /**
  154. * Exit the program with the given error code.
  155. * Closes open files if necessary.
  156. * @param error is the error code to be returned.
  157. */
  158. void
  159. exit_inpproj (int error)
  160. {
  161. if(OutFile)
  162. fclose(OutFile);
  163. if(InFile)
  164. fclose(InFile);
  165. exit (error);
  166. }
  167. int findmatch(char *line, char *keyword[])
  168. /*
  169. **--------------------------------------------------------------
  170. ** Input: *line = line from input file
  171. ** *keyword[] = list of NULL terminated keywords
  172. ** Output: returns index of matching keyword or
  173. ** -1 if no match found
  174. ** Purpose: determines which keyword appears on input line
  175. **--------------------------------------------------------------
  176. */
  177. {
  178. int i = 0;
  179. while (keyword[i] != NULL)
  180. {
  181. if (match(line,keyword[i])) return(i);
  182. i++;
  183. }
  184. return(-1);
  185. } /* end of findmatch */
  186. int match(char *str, char *substr)
  187. /*
  188. **--------------------------------------------------------------
  189. ** Input: *str = string being searched
  190. ** *substr = substring being searched for
  191. ** Output: returns 1 if substr found in str, 0 if not
  192. ** Purpose: sees if substr matches any part of str
  193. **
  194. ** (Not case sensitive)
  195. **--------------------------------------------------------------
  196. */
  197. {
  198. int i,j;
  199. /*** Updated 9/7/00 ***/
  200. /* Fail if substring is empty */
  201. if (!substr[0]) return(0);
  202. /* Skip leading blanks of str. */
  203. for (i=0; str[i]; i++)
  204. if (str[i] != ' ') break;
  205. /* Check if substr matches remainder of str. */
  206. for (i=i,j=0; substr[j]; i++,j++)
  207. if (!str[i] || UCHAR(str[i]) != UCHAR(substr[j]))
  208. return(0);
  209. return(1);
  210. } /* end of match */
  211. /*** Updated 10/25/00 ***/
  212. /* The gettokens function has been totally re-written. */
  213. int gettokens(char *s)
  214. /*
  215. **--------------------------------------------------------------
  216. ** Input: *s = string to be tokenized
  217. ** Output: returns number of tokens in s
  218. ** Purpose: scans string for tokens, saving pointers to them
  219. ** in module global variable Tok[]
  220. **
  221. ** Tokens can be separated by the characters listed in SEPSTR
  222. ** (spaces, tabs, newline, carriage return) which is defined
  223. ** in TYPES.H. Text between quotes is treated as a single token.
  224. **--------------------------------------------------------------
  225. */
  226. {
  227. int len, m, n;
  228. char *c;
  229. /* Begin with no tokens */
  230. for (n=0; n<MAXTOKS; n++) Tok[n] = NULL;
  231. n = 0;
  232. /* Truncate s at start of comment */
  233. c = strchr(s,';');
  234. if (c) *c = '\0';
  235. len = strlen(s);
  236. /* Scan s for tokens until nothing left */
  237. while (len > 0 && n < MAXTOKS)
  238. {
  239. m = strcspn(s,SEPSTR); /* Find token length */
  240. len -= m+1; /* Update length of s */
  241. if (m == 0) s++; /* No token found */
  242. else
  243. {
  244. if (*s == '"') /* Token begins with quote */
  245. {
  246. s++; /* Start token after quote */
  247. m = strcspn(s,"\"\n\r"); /* Find end quote (or EOL) */
  248. }
  249. s[m] = '\0'; /* Null-terminate the token */
  250. Tok[n] = s; /* Save pointer to token */
  251. n++; /* Update token count */
  252. s += m+1; /* Begin next token */
  253. }
  254. }
  255. return(n);
  256. } /* End of gettokens */