/grass-6.4.2/vector/v.out.vtk/writeVTK.c

# · C · 930 lines · 641 code · 143 blank · 146 comment · 256 complexity · 4e27fd1a89ae18014fe2ff9c1208ae12 MD5 · raw file

  1. /***************************************************************************
  2. *
  3. * MODULE: v.out.vtk
  4. * AUTHOR(S): Soeren Gebbert
  5. *
  6. * PURPOSE: v.out.vtk: writes ASCII VTK file
  7. * this module is based on v.out.ascii
  8. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. ****************************************************************************/
  15. #include <stdlib.h>
  16. #include <grass/Vect.h>
  17. #include <grass/dbmi.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. #include "writeVTK.h"
  21. #include "local_proto.h"
  22. #include <grass/config.h>
  23. /*Prototype */
  24. /*Formated coordinates output */
  25. static void write_point_coordinates(struct line_pnts *Points, int dp, double scale, FILE * ascii);
  26. /* ************************************************************************* */
  27. /* This function writes the vtk points and coordinates ********************* */
  28. /* ************************************************************************* */
  29. int write_vtk_points(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  30. int *types, int typenum, int dp, double scale)
  31. {
  32. int type, cur, i, k, centroid;
  33. int pointoffset = 0;
  34. int lineoffset = 0;
  35. int polygonoffset = 0;
  36. static struct line_pnts *Points;
  37. struct line_cats *Cats;
  38. Points = Vect_new_line_struct(); /* init line_pnts struct */
  39. Cats = Vect_new_cats_struct();
  40. G_message("Writing the coordinates");
  41. /*For every available vector type */
  42. for (k = 0; k < typenum; k++) {
  43. /*POINT KERNEL CENTROID */
  44. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  45. types[k] == GV_CENTROID) {
  46. /*Get the number of the points to generate */
  47. info->typeinfo[types[k]]->pointoffset = pointoffset;
  48. info->typeinfo[types[k]]->numpoints =
  49. Vect_get_num_primitives(Map, types[k]);
  50. pointoffset += info->typeinfo[types[k]]->numpoints;
  51. info->typeinfo[types[k]]->numvertices =
  52. info->typeinfo[types[k]]->numpoints;
  53. info->maxnumvertices += info->typeinfo[types[k]]->numpoints;
  54. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  55. /*
  56. * printf("Points Type %i Number %i offset %i\n", types[k],
  57. * info->typeinfo[types[k]]->numpoints,
  58. * info->typeinfo[types[k]]->pointoffset);
  59. */
  60. }
  61. }
  62. for (k = 0; k < typenum; k++) {
  63. /*LINE BOUNDARY */
  64. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  65. info->typeinfo[types[k]]->pointoffset = pointoffset;
  66. info->typeinfo[types[k]]->lineoffset = lineoffset;
  67. /*count the number of line_nodes and lines */
  68. Vect_rewind(Map);
  69. while (1) {
  70. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  71. break;
  72. if (type == -2) /* EOF */
  73. break;
  74. if (type == types[k]) {
  75. info->typeinfo[types[k]]->numpoints += Points->n_points;
  76. info->typeinfo[types[k]]->numlines++;
  77. }
  78. }
  79. pointoffset += info->typeinfo[types[k]]->numpoints;
  80. lineoffset += info->typeinfo[types[k]]->lineoffset;
  81. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  82. info->maxnumlinepoints += info->typeinfo[types[k]]->numpoints;
  83. info->maxnumlines += info->typeinfo[types[k]]->numlines;
  84. /*
  85. * printf("Lines Type %i Number %i offset %i\n", types[k],
  86. * info->typeinfo[types[k]]->numlines,
  87. * info->typeinfo[types[k]]->lineoffset);
  88. */
  89. }
  90. }
  91. for (k = 0; k < typenum; k++) {
  92. /*FACE */
  93. if (types[k] == GV_FACE) {
  94. info->typeinfo[types[k]]->pointoffset = pointoffset;
  95. info->typeinfo[types[k]]->polygonoffset = polygonoffset;
  96. /*count the number of line_nodes and lines */
  97. Vect_rewind(Map);
  98. while (1) {
  99. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  100. break;
  101. if (type == -2) /* EOF */
  102. break;
  103. if (type == types[k]) {
  104. info->typeinfo[types[k]]->numpoints += Points->n_points;
  105. info->typeinfo[types[k]]->numpolygons++;
  106. }
  107. }
  108. pointoffset += info->typeinfo[types[k]]->numpoints;
  109. polygonoffset += info->typeinfo[types[k]]->numpolygons;
  110. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  111. info->maxnumpolygonpoints += info->typeinfo[types[k]]->numpoints;
  112. info->maxnumpolygons += info->typeinfo[types[k]]->numpolygons;
  113. /*
  114. * printf("Polygons Type %i Number %i offset %i\n", types[k],
  115. * info->typeinfo[types[k]]->numpolygons,
  116. * info->typeinfo[types[k]]->polygonoffset);
  117. */
  118. }
  119. }
  120. for (k = 0; k < typenum; k++) {
  121. /*AREA */
  122. if (types[k] == GV_AREA) {
  123. info->typeinfo[types[k]]->numpolygons = Vect_get_num_areas(Map);
  124. info->typeinfo[types[k]]->pointoffset = pointoffset;
  125. info->typeinfo[types[k]]->polygonoffset = polygonoffset;
  126. /*Count the coordinate points */
  127. Vect_rewind(Map);
  128. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  129. centroid = Vect_get_area_centroid(Map, i);
  130. if (centroid > 0) {
  131. Vect_read_line(Map, NULL, Cats, centroid);
  132. }
  133. Vect_get_area_points(Map, i, Points);
  134. info->typeinfo[types[k]]->numpoints += Points->n_points;
  135. }
  136. pointoffset += info->typeinfo[types[k]]->numpoints;
  137. polygonoffset += info->typeinfo[types[k]]->numpolygons;
  138. info->maxnumpoints += info->typeinfo[types[k]]->numpoints;
  139. info->maxnumpolygonpoints += info->typeinfo[types[k]]->numpoints;
  140. info->maxnumpolygons += info->typeinfo[types[k]]->numpolygons;
  141. /*
  142. * printf("Polygons Type %i Number %i offset %i\n", types[k],
  143. * info->typeinfo[types[k]]->numpolygons,
  144. * info->typeinfo[types[k]]->polygonoffset);
  145. */
  146. }
  147. }
  148. /*
  149. * printf("Maxnum points %i \n", info->maxnumpoints);
  150. * printf("Maxnum vertices %i \n", info->maxnumvertices);
  151. * printf("Maxnum lines %i \n", info->maxnumlines);
  152. * printf("Maxnum line points %i \n", info->maxnumlinepoints);
  153. * printf("Maxnum polygons %i \n", info->maxnumpolygons);
  154. * printf("Maxnum polygon points %i \n", info->maxnumpolygonpoints);
  155. */
  156. /*break if nothing to generate */
  157. if (info->maxnumpoints == 0)
  158. G_fatal_error(_("No coordinates to generate the output! Maybe an empty vector type chosen?"));
  159. /************************************************/
  160. /*Write the coordinates into the vtk ascii file */
  161. /************************************************/
  162. fprintf(ascii, "POINTS %i float\n", info->maxnumpoints);
  163. /*For every available vector type */
  164. for (k = 0; k < typenum; k++) {
  165. /*POINT KERNEL CENTROID */
  166. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  167. types[k] == GV_CENTROID) {
  168. Vect_rewind(Map);
  169. /*Write the coordinates */
  170. cur = 0;
  171. while (1) {
  172. if (cur <= info->typeinfo[types[k]]->numpoints)
  173. G_percent(cur, info->typeinfo[types[k]]->numpoints, 2);
  174. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  175. break;
  176. if (type == -2) /* EOF */
  177. break;
  178. if (type == types[k]) {
  179. write_point_coordinates(Points, dp, scale, ascii);
  180. if (Cats->n_cats == 0)
  181. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  182. }
  183. cur++;
  184. }
  185. }
  186. }
  187. for (k = 0; k < typenum; k++) {
  188. /*LINE BOUNDARY */
  189. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  190. Vect_rewind(Map);
  191. cur = 0;
  192. while (1) {
  193. if (cur <= info->typeinfo[types[k]]->numlines)
  194. G_percent(cur, info->typeinfo[types[k]]->numlines, 2);
  195. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  196. break;
  197. if (type == -2) /* EOF */
  198. break;
  199. if (type == types[k]) {
  200. write_point_coordinates(Points, dp, scale, ascii);
  201. }
  202. cur++;
  203. }
  204. }
  205. }
  206. for (k = 0; k < typenum; k++) {
  207. /* FACE */
  208. if (types[k] == GV_FACE) {
  209. Vect_rewind(Map);
  210. cur = 0;
  211. while (1) {
  212. if (cur <= info->typeinfo[types[k]]->numpolygons)
  213. G_percent(cur, info->typeinfo[types[k]]->numpolygons, 2);
  214. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  215. break;
  216. if (type == -2) /* EOF */
  217. break;
  218. if (type == types[k]) {
  219. write_point_coordinates(Points, dp, scale, ascii);
  220. }
  221. cur++;
  222. }
  223. }
  224. }
  225. for (k = 0; k < typenum; k++) {
  226. /* AREA */
  227. if (types[k] == GV_AREA) {
  228. Vect_rewind(Map);
  229. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  230. centroid = Vect_get_area_centroid(Map, i);
  231. if (centroid > 0) {
  232. Vect_read_line(Map, NULL, Cats, centroid);
  233. }
  234. Vect_get_area_points(Map, i, Points);
  235. write_point_coordinates(Points, dp, scale, ascii);
  236. }
  237. }
  238. }
  239. return 1;
  240. }
  241. /* ************************************************************************* */
  242. /* This function writes the vtk cells ************************************** */
  243. /* ************************************************************************* */
  244. int write_vtk_cells(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  245. int *types, int typenum)
  246. {
  247. int type, i, j, k, centroid;
  248. static struct line_pnts *Points;
  249. struct line_cats *Cats;
  250. /*The keywords may only be written once! */
  251. int vertkeyword = 1;
  252. int linekeyword = 1;
  253. int polykeyword = 1;
  254. G_message("Writing vtk cells");
  255. Points = Vect_new_line_struct(); /* init line_pnts struct */
  256. Cats = Vect_new_cats_struct();
  257. /*For every available vector type */
  258. for (k = 0; k < typenum; k++) {
  259. /*POINT KERNEL CENTROID */
  260. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  261. types[k] == GV_CENTROID) {
  262. Vect_rewind(Map);
  263. /*Write the vertices */
  264. if (info->typeinfo[types[k]]->numpoints > 0) {
  265. if (vertkeyword) {
  266. fprintf(ascii, "VERTICES %i %i\n", info->maxnumvertices,
  267. info->maxnumvertices * 2 );
  268. vertkeyword = 0;
  269. }
  270. for (i = 0; i < info->typeinfo[types[k]]->numpoints; i++) {
  271. fprintf(ascii, "1 %i\n",
  272. i + info->typeinfo[types[k]]->pointoffset);
  273. }
  274. fprintf(ascii, "\n");
  275. }
  276. }
  277. }
  278. for (k = 0; k < typenum; k++) {
  279. /*LINE BOUNDARY */
  280. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  281. Vect_rewind(Map);
  282. if (info->maxnumlines > 0) {
  283. if (linekeyword) {
  284. fprintf(ascii, "LINES %i %i\n", info->maxnumlines,
  285. info->maxnumlinepoints + info->maxnumlines);
  286. linekeyword = 0;
  287. }
  288. Vect_rewind(Map);
  289. i = 0;
  290. while (1) {
  291. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  292. break;
  293. if (type == -2) /* EOF */
  294. break;
  295. if (type == types[k]) {
  296. /*Check for data generation */
  297. if (Cats->n_cats == 0)
  298. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  299. fprintf(ascii, "%i", Points->n_points);
  300. while (Points->n_points--) {
  301. fprintf(ascii, " %i",
  302. i + info->typeinfo[types[k]]->pointoffset);
  303. i++;
  304. }
  305. fprintf(ascii, "\n");
  306. }
  307. }
  308. }
  309. }
  310. }
  311. for (k = 0; k < typenum; k++) {
  312. /*LINE BOUNDARY FACE */
  313. if (types[k] == GV_FACE) {
  314. Vect_rewind(Map);
  315. if (info->maxnumpolygons > 0) {
  316. if (polykeyword) {
  317. fprintf(ascii, "POLYGONS %i %i\n",
  318. info->maxnumpolygons,
  319. info->maxnumpolygonpoints + info->maxnumpolygons);
  320. polykeyword = 0;
  321. }
  322. Vect_rewind(Map);
  323. i = 0;
  324. while (1) {
  325. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  326. break;
  327. if (type == -2) /* EOF */
  328. break;
  329. if (type == types[k]) {
  330. /*Check for data generation */
  331. if (Cats->n_cats == 0)
  332. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  333. fprintf(ascii, "%i", Points->n_points);
  334. while (Points->n_points--) {
  335. fprintf(ascii, " %i",
  336. i + info->typeinfo[types[k]]->pointoffset);
  337. i++;
  338. }
  339. fprintf(ascii, "\n");
  340. }
  341. }
  342. }
  343. }
  344. }
  345. for (k = 0; k < typenum; k++) {
  346. /*AREA */
  347. if (types[k] == GV_AREA) {
  348. Vect_rewind(Map);
  349. if (info->maxnumpolygons > 0) {
  350. if (polykeyword) {
  351. fprintf(ascii, "POLYGONS %i %i\n",
  352. info->maxnumpolygons,
  353. info->maxnumpolygonpoints + info->maxnumpolygons);
  354. polykeyword = 0;
  355. }
  356. j = 0;
  357. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  358. centroid = Vect_get_area_centroid(Map, i);
  359. if (centroid > 0) {
  360. Vect_read_line(Map, NULL, Cats, centroid);
  361. }
  362. Vect_get_area_points(Map, i, Points);
  363. /*Check for data generation */
  364. if (Cats->n_cats == 0)
  365. info->typeinfo[types[k]]->generatedata = 0; /*No data generation */
  366. fprintf(ascii, "%i", Points->n_points);
  367. while (Points->n_points--) {
  368. fprintf(ascii, " %i",
  369. j + info->typeinfo[types[k]]->pointoffset);
  370. j++;
  371. }
  372. fprintf(ascii, "\n");
  373. }
  374. }
  375. }
  376. }
  377. return 1;
  378. }
  379. /* ************************************************************************* */
  380. /* This function writes the categories as vtk cell data ******************** */
  381. /* ************************************************************************* */
  382. int write_vtk_cat_data(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  383. int layer, int *types, int typenum, int dp)
  384. {
  385. int type, cat, i, k, centroid;
  386. static struct line_pnts *Points;
  387. struct line_cats *Cats;
  388. /*The keywords may only be written once! */
  389. int numcelldata =
  390. info->maxnumvertices + info->maxnumlines + info->maxnumpolygons;
  391. Points = Vect_new_line_struct(); /* init line_pnts struct */
  392. Cats = Vect_new_cats_struct();
  393. G_message("Writing category cell data");
  394. if (numcelldata > 0) {
  395. /*Write the pointdata */
  396. fprintf(ascii, "CELL_DATA %i\n", numcelldata);
  397. fprintf(ascii, "SCALARS cat_%s int 1\n", Map->name);
  398. fprintf(ascii, "LOOKUP_TABLE default\n");
  399. /*For every available vector type */
  400. for (k = 0; k < typenum; k++) {
  401. /*POINT KERNEL CENTROID */
  402. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  403. types[k] == GV_CENTROID) {
  404. Vect_rewind(Map);
  405. while (1) {
  406. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  407. break;
  408. if (type == -2) /* EOF */
  409. break;
  410. if (type == types[k]) {
  411. Vect_cat_get(Cats, layer, &cat);
  412. fprintf(ascii, " %d", cat);
  413. }
  414. }
  415. }
  416. }
  417. for (k = 0; k < typenum; k++) {
  418. /*LINE BOUNDARY */
  419. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  420. Vect_rewind(Map);
  421. while (1) {
  422. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  423. break;
  424. if (type == -2) /* EOF */
  425. break;
  426. if (type == types[k]) {
  427. Vect_cat_get(Cats, layer, &cat);
  428. fprintf(ascii, " %d", cat);
  429. }
  430. }
  431. }
  432. }
  433. for (k = 0; k < typenum; k++) {
  434. /*FACE */
  435. if (types[k] == GV_FACE) {
  436. Vect_rewind(Map);
  437. while (1) {
  438. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  439. break;
  440. if (type == -2) /* EOF */
  441. break;
  442. if (type == types[k]) {
  443. Vect_cat_get(Cats, layer, &cat);
  444. fprintf(ascii, " %d", cat);
  445. }
  446. }
  447. }
  448. }
  449. for (k = 0; k < typenum; k++) {
  450. /*AREA */
  451. if (types[k] == GV_AREA) {
  452. Vect_rewind(Map);
  453. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  454. centroid = Vect_get_area_centroid(Map, i);
  455. if (centroid > 0) {
  456. Vect_read_line(Map, NULL, Cats, centroid);
  457. }
  458. Vect_cat_get(Cats, layer, &cat);
  459. fprintf(ascii, " %d", cat);
  460. }
  461. }
  462. }
  463. fprintf(ascii, "\n");
  464. }
  465. return 1;
  466. }
  467. /*
  468. Reads the attribute field "name" for current cat and returns the value as a string
  469. or NULL on error
  470. Memory for the result string is allocated by this function and must be free'd by
  471. the caller.
  472. */
  473. char *get_att ( char *name, int cat, struct field_info *Fi, dbDriver *Driver, int ncol ) {
  474. int j;
  475. char buf[2000];
  476. int more;
  477. dbTable *Table;
  478. dbString dbstring;
  479. dbColumn *Column;
  480. dbCursor cursor;
  481. char *retval;
  482. db_init_string(&dbstring);
  483. sprintf ( buf, "SELECT * FROM %s WHERE %s = %d", Fi->table, Fi->key, cat);
  484. db_set_string(&dbstring, buf);
  485. if ( db_open_select_cursor(Driver, &dbstring, &cursor, DB_SEQUENTIAL) != DB_OK ) {
  486. G_fatal_error ( _("Cannot select attributes for cat = %d"), cat);
  487. }
  488. if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
  489. G_fatal_error (_("Unable to fetch data from table"));
  490. Table = db_get_cursor_table (&cursor);
  491. for (j = 0; j < ncol; j++) {
  492. Column = db_get_table_column (Table, j);
  493. if ( !strcmp ( name, db_get_column_name(Column) ) ) {
  494. db_convert_column_value_to_string (Column, &dbstring);
  495. retval = G_malloc ( sizeof (char) * ( strlen ( db_get_string (&dbstring )) + 1 ) );
  496. retval = strdup ( db_get_string (&dbstring) );
  497. return (retval);
  498. }
  499. }
  500. return ( NULL );
  501. }
  502. /* ************************************************************************* */
  503. /* This function writes numerical attribute table fields as VTK scalars **** */
  504. /* ************************************************************************* */
  505. int write_vtk_db_data(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  506. int layer, int *types, int typenum, int dp)
  507. {
  508. int type, cat, i, k, centroid;
  509. static struct line_pnts *Points;
  510. struct line_cats *Cats;
  511. /*The keywords may only be written once! */
  512. int numcelldata =
  513. info->maxnumvertices + info->maxnumlines + info->maxnumpolygons;
  514. /* attribute table info */
  515. int ncol=0, colsqltype, colctype, num_atts, cur_att, progress;
  516. struct field_info *Fi=NULL;
  517. dbDriver *Driver=NULL;
  518. dbHandle handle;
  519. dbTable *Table;
  520. dbString dbstring;
  521. dbColumn *Column;
  522. char *valbuf;
  523. if ( layer < 1 ) {
  524. G_warning(_("Cannot export attribute table fields for layer < 1. Skipping export"));
  525. return 1;
  526. }
  527. /* attempt to open attribute table for selected layer */
  528. db_init_string(&dbstring);
  529. Fi = Vect_get_field( Map, layer);
  530. if ( Fi == NULL ) {
  531. G_fatal_error (_("No attribute table found"));
  532. }
  533. Driver = db_start_driver(Fi->driver);
  534. if (Driver == NULL)
  535. G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
  536. db_init_handle (&handle);
  537. db_set_handle (&handle, Fi->database, NULL);
  538. if (db_open_database(Driver, &handle) != DB_OK)
  539. G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver);
  540. db_set_string(&dbstring, Fi->table);
  541. if(db_describe_table (Driver, &dbstring, &Table) != DB_OK)
  542. G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
  543. /* analyse field structure */
  544. ncol = db_get_table_number_of_columns(Table);
  545. num_atts = 0;
  546. for (i = 0; i < ncol; i++) {
  547. Column = db_get_table_column (Table, i);
  548. colsqltype = db_get_column_sqltype(Column);
  549. colctype = db_sqltype_to_Ctype ( colsqltype );
  550. if ( ( colctype == DB_C_TYPE_INT ) || ( colctype == DB_C_TYPE_DOUBLE ) ) {
  551. /* we don't want to export the category field twice */
  552. if ( strcmp ( db_get_column_name(Column), "cat" ) ) {
  553. num_atts ++;
  554. /* fprintf ( stderr, "%i: %s\n", num_atts, db_get_column_name(Column) ); */
  555. }
  556. }
  557. }
  558. if ( num_atts < 1 ) {
  559. G_warning(_("No numerical attributes found. Skipping export"));
  560. db_close_database(Driver);
  561. db_shutdown_driver(Driver);
  562. return 1;
  563. }
  564. Points = Vect_new_line_struct(); /* init line_pnts struct */
  565. Cats = Vect_new_cats_struct();
  566. G_message("Writing %i scalar variables as cell data", num_atts );
  567. progress = 0;
  568. for ( cur_att = 0; cur_att < ncol; cur_att ++ ) {
  569. if (numcelldata > 0) {
  570. /*Write the pointdata */
  571. Column = db_get_table_column (Table, cur_att);
  572. colsqltype = db_get_column_sqltype(Column);
  573. colctype = db_sqltype_to_Ctype ( colsqltype );
  574. if ( ( strcmp ( "cat", db_get_column_name(Column) ) ) &&
  575. ( ( colctype == DB_C_TYPE_INT ) || ( colctype == DB_C_TYPE_DOUBLE )) ) {
  576. if ( colctype == DB_C_TYPE_INT ) {
  577. /* G_message(" Writing integer scalar %s", db_get_column_name(Column) ); */
  578. fprintf(ascii, "SCALARS %s int 1\n", db_get_column_name(Column) );
  579. }
  580. if ( colctype == DB_C_TYPE_DOUBLE ) {
  581. /* *G_message(" Writing double scalar %s", db_get_column_name(Column) ); */
  582. fprintf(ascii, "SCALARS %s double 1\n", db_get_column_name(Column) );
  583. }
  584. fprintf(ascii, "LOOKUP_TABLE default\n");
  585. progress ++;
  586. /*For every available vector type */
  587. for (k = 0; k < typenum; k++) {
  588. /*POINT KERNEL CENTROID */
  589. if (types[k] == GV_POINT || types[k] == GV_KERNEL ||
  590. types[k] == GV_CENTROID) {
  591. Vect_rewind(Map);
  592. while (1) {
  593. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  594. break;
  595. if (type == -2) /* EOF */
  596. break;
  597. if (type == types[k]) {
  598. Vect_cat_get(Cats, layer, &cat);
  599. valbuf = get_att ( (char*) db_get_column_name(Column), cat, Fi, Driver, ncol );
  600. if ( valbuf == NULL ) {
  601. db_close_database(Driver);
  602. db_shutdown_driver(Driver);
  603. G_fatal_error (_("Error reading value of attribute '%s'"), db_get_column_name(Column));
  604. }
  605. /* DEBUG
  606. fprintf ( stderr, "%s (%i) = %s\n", db_get_column_name(Column), cat, valbuf );
  607. */
  608. fprintf(ascii, " %s", valbuf);
  609. G_free ( valbuf );
  610. }
  611. }
  612. }
  613. }
  614. for (k = 0; k < typenum; k++) {
  615. /*LINE BOUNDARY */
  616. if (types[k] == GV_LINE || types[k] == GV_BOUNDARY) {
  617. Vect_rewind(Map);
  618. while (1) {
  619. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  620. break;
  621. if (type == -2) /* EOF */
  622. break;
  623. if (type == types[k]) {
  624. Vect_cat_get(Cats, layer, &cat);
  625. valbuf = get_att ( (char*) db_get_column_name(Column), cat, Fi, Driver, ncol );
  626. if ( valbuf == NULL ) {
  627. db_close_database(Driver);
  628. db_shutdown_driver(Driver);
  629. G_fatal_error (_("Error reading value of attribute '%s'"), db_get_column_name(Column));
  630. }
  631. /* DEBUG
  632. fprintf ( stderr, "%s (%i) = %s\n", db_get_column_name(Column), cat, valbuf );
  633. */
  634. fprintf(ascii, " %s", valbuf);
  635. G_free ( valbuf );
  636. }
  637. }
  638. }
  639. }
  640. for (k = 0; k < typenum; k++) {
  641. /*FACE */
  642. if (types[k] == GV_FACE) {
  643. Vect_rewind(Map);
  644. while (1) {
  645. if (-1 == (type = Vect_read_next_line(Map, Points, Cats)))
  646. break;
  647. if (type == -2) /* EOF */
  648. break;
  649. if (type == types[k]) {
  650. Vect_cat_get(Cats, layer, &cat);
  651. valbuf = get_att ( (char*) db_get_column_name(Column), cat, Fi, Driver, ncol );
  652. if ( valbuf == NULL ) {
  653. db_close_database(Driver);
  654. db_shutdown_driver(Driver);
  655. G_fatal_error (_("Error reading value of attribute '%s'"), db_get_column_name(Column));
  656. }
  657. /* DEBUG
  658. fprintf ( stderr, "%s (%i) = %s\n", db_get_column_name(Column), cat, valbuf );
  659. */
  660. fprintf(ascii, " %s", valbuf);
  661. G_free ( valbuf );
  662. }
  663. }
  664. }
  665. }
  666. for (k = 0; k < typenum; k++) {
  667. /*AREA */
  668. if (types[k] == GV_AREA) {
  669. Vect_rewind(Map);
  670. for (i = 1; i <= info->typeinfo[types[k]]->numpolygons; i++) {
  671. centroid = Vect_get_area_centroid(Map, i);
  672. if (centroid > 0) {
  673. Vect_read_line(Map, NULL, Cats, centroid);
  674. }
  675. Vect_cat_get(Cats, layer, &cat);
  676. valbuf = get_att ( (char*) db_get_column_name(Column), cat, Fi, Driver, ncol );
  677. if ( valbuf == NULL ) {
  678. db_close_database(Driver);
  679. db_shutdown_driver(Driver);
  680. G_fatal_error (_("Error reading value of attribute '%s'"), db_get_column_name(Column));
  681. }
  682. /* DEBUG
  683. fprintf ( stderr, "%s (%i) = %s\n", db_get_column_name(Column), cat, valbuf );
  684. */
  685. fprintf(ascii, " %s", valbuf);
  686. G_free ( valbuf );
  687. }
  688. }
  689. }
  690. fprintf(ascii, "\n");
  691. } /* END (do for all scalars != cat */
  692. }
  693. G_percent(progress,num_atts,1);
  694. } /* END (step through all numerical attributes) */
  695. fprintf ( stdout, "\n" );
  696. fflush ( stdout );
  697. db_close_database(Driver);
  698. db_shutdown_driver(Driver);
  699. return 1;
  700. }
  701. /* ************************************************************************* */
  702. /* This function writes attribute table fields as VTK labels **** */
  703. /* ************************************************************************* */
  704. int write_vtk_db_labels(FILE * ascii, struct Map_info *Map, VTKInfo * info,
  705. int layer, int *types, int typenum, int dp) {
  706. return 1;
  707. }
  708. /* ************************************************************************* */
  709. /* This function writes the point coordinates and the geometric feature **** */
  710. /* ************************************************************************* */
  711. int write_vtk(FILE * ascii, struct Map_info *Map, int layer, int *types,
  712. int typenum, int dp, double scale, int numatts, int labels )
  713. {
  714. VTKInfo *info;
  715. VTKTypeInfo **typeinfo;
  716. int i;
  717. int infonum =
  718. GV_POINT + GV_KERNEL + GV_CENTROID + GV_LINE + GV_BOUNDARY + GV_FACE +
  719. GV_AREA;
  720. /*Initiate the typeinfo structure for every supported type */
  721. typeinfo = (VTKTypeInfo **) calloc(infonum, sizeof(VTKTypeInfo *));
  722. for (i = 0; i < infonum; i++) {
  723. typeinfo[i] = (VTKTypeInfo *) calloc(1, sizeof(VTKTypeInfo));
  724. typeinfo[i]->numpoints = 0;
  725. typeinfo[i]->pointoffset = 0;
  726. typeinfo[i]->numvertices = 0;
  727. typeinfo[i]->verticesoffset = 0;
  728. typeinfo[i]->numlines = 0;
  729. typeinfo[i]->lineoffset = 0;
  730. typeinfo[i]->numpolygons = 0;
  731. typeinfo[i]->polygonoffset = 0;
  732. typeinfo[i]->generatedata = 1;
  733. }
  734. /*Initiate the info structure */
  735. info = (VTKInfo *) calloc(infonum, sizeof(VTKInfo));
  736. info->maxnumpoints = 0;
  737. info->maxnumvertices = 0;
  738. info->maxnumlines = 0;
  739. info->maxnumlinepoints = 0;
  740. info->maxnumpolygons = 0;
  741. info->maxnumpolygonpoints = 0;
  742. info->typeinfo = typeinfo;
  743. /*1. write the points */
  744. write_vtk_points(ascii, Map, info, types, typenum, dp, scale);
  745. /*2. write the cells */
  746. write_vtk_cells(ascii, Map, info, types, typenum);
  747. /*3. write the cat data */
  748. write_vtk_cat_data(ascii, Map, info, layer, types, typenum, dp);
  749. /*4. write the DB data: numerical attributes */
  750. if ( numatts ) {
  751. write_vtk_db_data(ascii, Map, info, layer, types, typenum, dp);
  752. }
  753. /*5. Write labels (not yet supported)
  754. if ( labels ) {
  755. write_vtk_db_labels(ascii, Map, info, layer, types, typenum, dp);
  756. }
  757. */
  758. /*Release the memory */
  759. for (i = 0; i < infonum; i++) {
  760. free(typeinfo[i]);
  761. }
  762. free(typeinfo);
  763. free(info);
  764. return 1;
  765. }
  766. /* ************************************************************************* */
  767. /* This function writes the point coordinates ****************************** */
  768. /* ************************************************************************* */
  769. void write_point_coordinates(struct line_pnts *Points, int dp, double scale, FILE * ascii)
  770. {
  771. char *xstring = NULL, *ystring = NULL, *zstring = NULL;
  772. double *xptr, *yptr, *zptr;
  773. xptr = Points->x;
  774. yptr = Points->y;
  775. zptr = Points->z;
  776. while (Points->n_points--) {
  777. G_asprintf(&xstring, "%.*f", dp, *xptr++ - x_extent);
  778. G_trim_decimal(xstring);
  779. G_asprintf(&ystring, "%.*f", dp, *yptr++ - y_extent);
  780. G_trim_decimal(ystring);
  781. G_asprintf(&zstring, "%.*f", dp, scale * (*zptr++));
  782. G_trim_decimal(zstring);
  783. fprintf(ascii, "%s %s %s \n", xstring, ystring, zstring);
  784. }
  785. return;
  786. }