main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /****************************************************************
  2. *
  3. * MODULE: v.example
  4. *
  5. * AUTHOR(S): GRASS Development Team, Radim Blazek, Maris Nartiss
  6. *
  7. * PURPOSE: copies vector data from source map to destination map
  8. * prints out all point coordinates and attributes
  9. *
  10. * COPYRIGHT: (C) 2002-2009 by the GRASS Development Team
  11. *
  12. * This program is free software under the
  13. * GNU General Public License (>=v2).
  14. * Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. ****************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <grass/gis.h>
  21. #include <grass/vector.h>
  22. #include <grass/dbmi.h>
  23. #include <grass/glocale.h>
  24. int main(int argc, char *argv[])
  25. {
  26. struct Map_info In, Out;
  27. static struct line_pnts *Points;
  28. struct line_cats *Cats;
  29. int i, type, cat, ncols, nrows, col, more, open3d;
  30. char *mapset, sql[200];
  31. struct GModule *module; /* GRASS module for parsing arguments */
  32. struct Option *old, *new;
  33. dbDriver *driver;
  34. dbHandle handle;
  35. dbCursor cursor;
  36. dbTable *table;
  37. dbColumn *column;
  38. dbString table_name, dbsql, valstr;
  39. struct field_info *Fi, *Fin;
  40. /* initialize GIS environment */
  41. /* reads grass env, stores program name to G_program_name() */
  42. G_gisinit(argv[0]);
  43. /* initialize module */
  44. module = G_define_module();
  45. G_add_keyword(_("vector"));
  46. G_add_keyword(_("keyword2"));
  47. G_add_keyword(_("keyword3"));
  48. module->description = _("My first vector module");
  49. /* Define the different options as defined in gis.h */
  50. old = G_define_standard_option(G_OPT_V_INPUT);
  51. new = G_define_standard_option(G_OPT_V_OUTPUT);
  52. /* options and flags parser */
  53. if (G_parser(argc, argv))
  54. exit(EXIT_FAILURE);
  55. /* Create and initialize struct's where to store points/lines and categories */
  56. Points = Vect_new_line_struct();
  57. Cats = Vect_new_cats_struct();
  58. /* Check 1) output is legal vector name; 2) if can find input map;
  59. 3) if input was found in current mapset, check if input != output.
  60. lib/vector/Vlib/legal_vname.c
  61. */
  62. Vect_check_input_output_name(old->answer, new->answer, G_FATAL_EXIT);
  63. if ((mapset = (char *)G_find_vector2(old->answer, "")) == NULL)
  64. G_fatal_error(_("Vector map <%s> not found"), old->answer);
  65. /* Predetermine level at which a map will be opened for reading
  66. lib/vector/Vlib/open.c
  67. */
  68. if (Vect_set_open_level(2))
  69. G_fatal_error(_("Unable to set predetermined vector open level"));
  70. /* Open existing vector for reading
  71. lib/vector/Vlib/open.c
  72. */
  73. if (1 > Vect_open_old(&In, old->answer, mapset))
  74. G_fatal_error(_("Unable to open vector map <%s>"), old->answer);
  75. /* Check if old vector is 3D. We should preserve 3D data. */
  76. if (Vect_is_3d(&In))
  77. open3d = WITH_Z;
  78. else
  79. open3d = WITHOUT_Z;
  80. /* Set error handler for input vector map */
  81. Vect_set_error_handler_io(&In, NULL);
  82. /* Open new vector for reading/writing */
  83. if (0 > Vect_open_new(&Out, new->answer, open3d)) {
  84. G_fatal_error(_("Unable to create vector map <%s>"), new->answer);
  85. }
  86. /* Set error handler for output vector map */
  87. Vect_set_error_handler_io(NULL, &Out);
  88. /* Let's get vector layers db connections information */
  89. Fi = Vect_get_field(&In, 1);
  90. if (!Fi) {
  91. G_fatal_error(_("Database connection not defined for layer %d"), 1);
  92. }
  93. /* Output information useful for debuging
  94. include/vect/dig_structs.h
  95. */
  96. G_debug(1,
  97. "Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
  98. Fi->number, Fi->name, Fi->driver, Fi->database, Fi->table,
  99. Fi->key);
  100. /* Prepeare strings for use in db_* calls */
  101. db_init_string(&dbsql);
  102. db_init_string(&valstr);
  103. db_init_string(&table_name);
  104. db_init_handle(&handle);
  105. /* Prepare database for use */
  106. driver = db_start_driver(Fi->driver);
  107. if (driver == NULL) {
  108. G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
  109. }
  110. /* Set error handler for DB driver */
  111. db_set_error_handler_driver(driver);
  112. db_set_handle(&handle, Fi->database, NULL);
  113. if (db_open_database(driver, &handle) != DB_OK) {
  114. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  115. Fi->database, Fi->driver);
  116. }
  117. db_set_string(&table_name, Fi->table);
  118. if (db_describe_table(driver, &table_name, &table) != DB_OK) {
  119. G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
  120. }
  121. ncols = db_get_table_number_of_columns(table);
  122. /* Copy header and history data from old to new map */
  123. Vect_copy_head_data(&In, &Out);
  124. Vect_hist_copy(&In, &Out);
  125. Vect_hist_command(&Out);
  126. i = 1;
  127. /* Let's do something with every vector feature in input map... */
  128. /* Read in single line and get it's type */
  129. while ((type = Vect_read_next_line(&In, Points, Cats)) > 0) {
  130. /* If Points contain line... */
  131. if (type == GV_LINE || type == GV_POINT || type == GV_CENTROID) {
  132. if (Vect_cat_get(Cats, 1, &cat) == 0) {
  133. Vect_cat_set(Cats, 1, i);
  134. i++;
  135. }
  136. }
  137. if (type == GV_POINT) {
  138. /* Print out point coordinates */
  139. printf("No:%d\tX:%f\tY:%f\tZ:%f\tCAT:%d\n", i, *Points->x,
  140. *Points->y, *Points->z, cat);
  141. /* Prepeare SQL query to get point attribute data */
  142. sprintf(sql, "select * from %s where %s=%d", Fi->table, Fi->key,
  143. cat);
  144. G_debug(1, "SQL: \"%s\"", sql);
  145. db_set_string(&dbsql, sql);
  146. /* Now execute query */
  147. if (db_open_select_cursor(driver, &dbsql, &cursor, DB_SEQUENTIAL)
  148. != DB_OK)
  149. G_warning(_("Unable to get attribute data for cat %d"), cat);
  150. else {
  151. /* Result count */
  152. nrows = db_get_num_rows(&cursor);
  153. G_debug(1, "Result count: %d", nrows);
  154. table = db_get_cursor_table(&cursor);
  155. /* Let's output every columns name and value */
  156. while (1) {
  157. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
  158. G_warning(_("Error while retrieving database record for cat %d"),
  159. cat);
  160. break;
  161. }
  162. if (!more)
  163. break;
  164. for (col = 0; col < ncols; col++) {
  165. column = db_get_table_column(table, col);
  166. db_convert_column_value_to_string(column, &valstr);
  167. printf("%s: %s\t", db_get_column_name(column),
  168. db_get_string(&valstr));
  169. }
  170. printf("\n");
  171. }
  172. db_close_cursor(&cursor);
  173. }
  174. }
  175. /* Only now we write data into new vector */
  176. Vect_write_line(&Out, type, Points, Cats);
  177. }
  178. /* Create database for new vector map */
  179. Fin = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
  180. driver = db_start_driver_open_database(Fin->driver, Fin->database);
  181. G_debug(1,
  182. "Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
  183. Fin->number, Fin->name, Fin->driver, Fin->database, Fin->table,
  184. Fin->key);
  185. /* Let's copy attribute table data */
  186. if (db_copy_table(Fi->driver, Fi->database, Fi->table,
  187. Fin->driver, Vect_subst_var(Fin->database, &Out),
  188. Fin->table) == DB_FAILED)
  189. G_warning(_("Unable to copy attribute table to vector map <%s>"),
  190. new->answer);
  191. else
  192. Vect_map_add_dblink(&Out, Fin->number, Fin->name, Fin->table, Fi->key,
  193. Fin->database, Fin->driver);
  194. /* Build topology for vector map and close them */
  195. Vect_build(&Out);
  196. Vect_close(&In);
  197. Vect_close(&Out);
  198. db_close_database_shutdown_driver(driver);
  199. /* Don't forget to report to caller successful end of data processing :) */
  200. exit(EXIT_SUCCESS);
  201. }