points.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <grass/gis.h>
  6. #include <grass/dbmi.h>
  7. #include <grass/Vect.h>
  8. #include <grass/glocale.h>
  9. #include "local_proto.h"
  10. /* Determine if the string is integer, e.g. 123, +123, -123,
  11. * return 1 if integer, 0 otherwise */
  12. static int is_int(char *str)
  13. {
  14. char *tail;
  15. if (strtol(str, &tail, 10), tail == str || *tail != '\0') {
  16. /* doesn't look like a number,
  17. or has extra characters after what looks to be a number */
  18. return 0;
  19. }
  20. return 1;
  21. }
  22. /* Determine if the string is double, e.g. 123.456, +123.456, -123.456, 1.23456e2
  23. * return 1 if double, 0 otherwise */
  24. static int is_double(char *str)
  25. {
  26. char *tail;
  27. if (strtod(str, &tail), tail == str || *tail != '\0') {
  28. /* doesn't look like a number,
  29. or has extra characters after what looks to be a number */
  30. return 0;
  31. }
  32. return 1;
  33. }
  34. /* Analyse points ascii file. Determine number of columns and column types.
  35. * ascii_tmp: write copy of tempfile to ascii_tmp:
  36. * rowlength: maximum row length
  37. * ncolumns: number of columns
  38. * minncolumns: minimum number of columns
  39. * nrows: number of rows
  40. * column_type: column types
  41. * column_length: column lengths (string only)
  42. */
  43. int points_analyse(FILE * ascii_in, FILE * ascii, char *fs,
  44. int *rowlength, int *ncolumns, int *minncolumns,
  45. int *nrows, int **column_type, int **column_length,
  46. int skip_lines, int xcol, int ycol, int region_flag)
  47. {
  48. int i;
  49. int buflen; /* buffer length */
  50. char *buf, *buf_raw; /* buffer */
  51. int row = 1; /* line number, first is 1 */
  52. int ncols = 0; /* number of columns */
  53. int minncols = -1;
  54. int *coltype = NULL; /* column types */
  55. int *collen = NULL; /* column lengths */
  56. char **tokens;
  57. int ntokens; /* number of tokens */
  58. int len, rowlen = 0; /* maximum row length */
  59. struct Cell_head window;
  60. double northing = .0;
  61. double easting = .0;
  62. char *coorbuf, *tmp_token, *sav_buf;
  63. int skip = FALSE, skipped = 0;
  64. buflen = 4000;
  65. buf = (char *)G_malloc(buflen);
  66. buf_raw = (char *)G_malloc(buflen);
  67. coorbuf = (char *)G_malloc(256);
  68. tmp_token = (char *)G_malloc(256);
  69. sav_buf = NULL;
  70. G_message(_("Scanning input for column types..."));
  71. /* fetch projection for LatLong test */
  72. G_get_window(&window);
  73. while (1) {
  74. len = 0; /* not really needed, but what the heck */
  75. skip = FALSE; /* reset out-of-region check */
  76. if (G_getl2(buf, buflen - 1, ascii_in) == 0)
  77. break; /* EOF */
  78. if (row <= skip_lines) {
  79. G_debug(3, "skipping header row %d : %d chars", row,
  80. (int)strlen(buf));
  81. /* this fn is read-only, write to hist with points_to_bin() */
  82. fprintf(ascii, "%s\n", buf);
  83. len = strlen(buf) + 1;
  84. if (len > rowlen)
  85. rowlen = len;
  86. row++;
  87. continue;
  88. }
  89. if ((buf[0] == '#') || (buf[0] == '\0')) {
  90. G_debug(3, "skipping comment row %d : %d chars", row,
  91. (int)strlen(buf));
  92. continue;
  93. }
  94. /* no G_chop() as first/last column may be empty fs=tab value */
  95. G_debug(3, "row %d : %d chars", row, (int)strlen(buf));
  96. /* G_tokenize() will modify the buffer, so we make a copy */
  97. strcpy(buf_raw, buf);
  98. len = strlen(buf) + 1;
  99. if (len > rowlen)
  100. rowlen = len;
  101. tokens = G_tokenize(buf, fs);
  102. ntokens = G_number_of_tokens(tokens);
  103. if (ntokens > ncols) {
  104. int c;
  105. coltype = (int *)G_realloc(coltype, ntokens * sizeof(int));
  106. collen = (int *)G_realloc(collen, ntokens * sizeof(int));
  107. for (c = ncols; c < ntokens; c++) {
  108. coltype[c] = DB_C_TYPE_INT; /* default type */
  109. collen[c] = 0;
  110. }
  111. ncols = ntokens;
  112. }
  113. if (minncols == -1 || minncols > ntokens)
  114. minncols = ntokens;
  115. /* Determine column types */
  116. for (i = 0; i < ntokens; i++) {
  117. if ((G_projection() == PROJECTION_LL)) {
  118. if (i == xcol || i == ycol) {
  119. if (i == 0) { /* Save position of original internal token buffer */
  120. /* Prevent memory leaks */
  121. sav_buf = tokens[0];
  122. }
  123. /* check if coordinates are DMS or decimal or not latlong at all */
  124. sprintf(coorbuf, "%s", tokens[i]);
  125. G_debug(4, "token: %s", coorbuf);
  126. if (i == xcol) {
  127. if (G_scan_easting(coorbuf, &easting, window.proj)) {
  128. G_debug(4, "is_latlong east: %f", easting);
  129. sprintf(tmp_token, "%.12f", easting);
  130. /* replace current DMS token by decimal degree */
  131. tokens[i] = tmp_token;
  132. if (region_flag) {
  133. if ((window.east < easting) ||
  134. (window.west > easting))
  135. skip = TRUE;
  136. }
  137. }
  138. else {
  139. fprintf(stderr, "Current row: '%s'\n", buf_raw);
  140. G_fatal_error(_("Unparsable longitude value in column <%d>: %s"),
  141. i, tokens[i]);
  142. }
  143. }
  144. if (i == ycol) {
  145. if (G_scan_northing(coorbuf, &northing, window.proj)) {
  146. G_debug(4, "is_latlong north: %f", northing);
  147. sprintf(tmp_token, "%.12f", northing);
  148. /* replace current DMS token by decimal degree */
  149. tokens[i] = tmp_token;
  150. if (region_flag) {
  151. if ((window.north < northing) ||
  152. (window.south > northing))
  153. skip = TRUE;
  154. }
  155. }
  156. else {
  157. fprintf(stderr, "Current row: '%s'\n", buf_raw);
  158. G_fatal_error(_("Unparsable latitude value in column <%d>: %s"),
  159. i, tokens[i]);
  160. }
  161. }
  162. } /* if (x or y) */
  163. if (i == ntokens - 1 && sav_buf != NULL) {
  164. /* Restore original token buffer so free_tokens works */
  165. /* Only do this if tokens[0] was re-assigned */
  166. tokens[0] = sav_buf;
  167. sav_buf = NULL;
  168. }
  169. } /* PROJECTION_LL */
  170. else {
  171. if (region_flag) {
  172. /* consider z range if -z flag is used? */
  173. /* change to if(>= east,north){skip=1;} to allow correct tiling */
  174. /* don't "continue;" so multiple passes will have the
  175. same column types and length for patching */
  176. if (i == xcol) {
  177. easting = atof(tokens[i]);
  178. if ((window.east < easting) ||
  179. (window.west > easting))
  180. skip = TRUE;
  181. }
  182. if (i == ycol) {
  183. northing = atof(tokens[i]);
  184. if ((window.north < northing) ||
  185. (window.south > northing))
  186. skip = TRUE;
  187. }
  188. }
  189. }
  190. G_debug(4, "row %d col %d: '%s' is_int = %d is_double = %d",
  191. row, i, tokens[i], is_int(tokens[i]),
  192. is_double(tokens[i]));
  193. if (is_int(tokens[i])) {
  194. continue; /* integer */
  195. }
  196. if (is_double(tokens[i])) { /* double */
  197. if (coltype[i] == DB_C_TYPE_INT) {
  198. coltype[i] = DB_C_TYPE_DOUBLE;
  199. }
  200. continue;
  201. }
  202. /* string */
  203. coltype[i] = DB_C_TYPE_STRING;
  204. len = strlen(tokens[i]);
  205. if (len > collen[i])
  206. collen[i] = len;
  207. }
  208. /* write dataline to tmp file */
  209. if (!skip)
  210. fprintf(ascii, "%s\n", buf_raw);
  211. else
  212. skipped++;
  213. G_free_tokens(tokens);
  214. row++;
  215. }
  216. *rowlength = rowlen;
  217. *ncolumns = ncols;
  218. *minncolumns = minncols;
  219. *column_type = coltype;
  220. *column_length = collen;
  221. *nrows = row - 1; /* including skipped lines */
  222. G_free(buf);
  223. G_free(buf_raw);
  224. G_free(coorbuf);
  225. G_free(tmp_token);
  226. if (region_flag)
  227. G_message(_("Skipping %d of %d rows falling outside of current region"),
  228. skipped, row - 1);
  229. return 0;
  230. }
  231. /* Import points from ascii file.
  232. *
  233. * fs: field separator
  234. * xcol, ycol, zcol, catcol: x,y,z,cat column in input file, first column is 1,
  235. * zcol and catcol may be 0 (do not use)
  236. * rowlen: maximum row length
  237. * Note: column types (both in header or coldef) must be supported by driver
  238. */
  239. int points_to_bin(FILE * ascii, int rowlen, struct Map_info *Map,
  240. dbDriver * driver, char *table, char *fs, int nrows,
  241. int ncols, int *coltype, int xcol, int ycol, int zcol,
  242. int catcol, int skip_lines)
  243. {
  244. char *buf, buf2[4000];
  245. int cat = 0;
  246. int row = 1;
  247. struct line_pnts *Points;
  248. struct line_cats *Cats;
  249. dbString sql, val;
  250. struct Cell_head window;
  251. G_message(_("Importing points..."));
  252. /* fetch projection for LatLong test */
  253. G_get_window(&window);
  254. rewind(ascii);
  255. Points = Vect_new_line_struct();
  256. Cats = Vect_new_cats_struct();
  257. buf = (char *)G_malloc(rowlen + 1);
  258. db_init_string(&sql);
  259. db_init_string(&val);
  260. if (skip_lines > 0) {
  261. sprintf(buf2, "HEADER: (%d lines)\n", skip_lines);
  262. Vect_hist_write(Map, buf2);
  263. }
  264. while (G_getl2(buf, rowlen, ascii) != 0) {
  265. int i, len;
  266. double x, y, z;
  267. char **tokens;
  268. int ntokens; /* number of tokens */
  269. if (row <= skip_lines) {
  270. G_debug(4, "writing skip line %d to hist : %d chars", row,
  271. (int)strlen(buf));
  272. Vect_hist_write(Map, buf);
  273. Vect_hist_write(Map, "\n");
  274. row++;
  275. continue;
  276. }
  277. len = strlen(buf);
  278. if (len == 0)
  279. continue; /* should not happen */
  280. G_debug(4, "row: %s", buf);
  281. tokens = G_tokenize(buf, fs);
  282. ntokens = G_number_of_tokens(tokens);
  283. if ((G_projection() == PROJECTION_LL)) {
  284. G_scan_easting(tokens[xcol], &x, window.proj);
  285. G_scan_northing(tokens[ycol], &y, window.proj);
  286. }
  287. else {
  288. x = atof(tokens[xcol]);
  289. y = atof(tokens[ycol]);
  290. }
  291. G_debug(4, "x: %f, y: %f", x, y);
  292. if (zcol >= 0)
  293. z = atof(tokens[zcol]);
  294. else
  295. z = 0.0;
  296. if (catcol >= 0)
  297. cat = atof(tokens[catcol]);
  298. else
  299. cat++;
  300. Vect_reset_line(Points);
  301. Vect_reset_cats(Cats);
  302. Vect_append_point(Points, x, y, z);
  303. Vect_cat_set(Cats, 1, cat);
  304. Vect_write_line(Map, GV_POINT, Points, Cats);
  305. /* Attributes */
  306. if (driver) {
  307. sprintf(buf2, "insert into %s values ( ", table);
  308. db_set_string(&sql, buf2);
  309. if (catcol < 0) {
  310. sprintf(buf2, "%d, ", cat);
  311. db_append_string(&sql, buf2);
  312. }
  313. for (i = 0; i < ntokens; i++) {
  314. if (i > 0)
  315. db_append_string(&sql, ", ");
  316. if (strlen(tokens[i]) > 0) {
  317. if (coltype[i] == DB_C_TYPE_INT ||
  318. coltype[i] == DB_C_TYPE_DOUBLE) {
  319. if (G_projection() == PROJECTION_LL &&
  320. (i == xcol || i == ycol)) {
  321. if (i == xcol)
  322. sprintf(buf2, "%.15g", x);
  323. else
  324. sprintf(buf2, "%.15g", y);
  325. }
  326. else
  327. sprintf(buf2, "%s", tokens[i]);
  328. }
  329. else {
  330. db_set_string(&val, tokens[i]);
  331. /* TODO: strip leading and trailing "quotes" from input string */
  332. db_double_quote_string(&val);
  333. sprintf(buf2, "'%s'", db_get_string(&val));
  334. }
  335. }
  336. else {
  337. sprintf(buf2, "null");
  338. }
  339. db_append_string(&sql, buf2);
  340. }
  341. db_append_string(&sql, ")");
  342. G_debug(3, db_get_string(&sql));
  343. if (db_execute_immediate(driver, &sql) != DB_OK) {
  344. G_fatal_error(_("Unable to insert new record: %s"),
  345. db_get_string(&sql));
  346. }
  347. }
  348. G_percent(row, nrows, 2);
  349. G_free_tokens(tokens);
  350. row++;
  351. }
  352. return 0;
  353. }