main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 atributes
  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, GV_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. /* Open new vector for reading/writing */
  81. if (0 > Vect_open_new(&Out, new->answer, open3d)) {
  82. Vect_close(&In);
  83. G_fatal_error(_("Unable to create vector map <%s>"), new->answer);
  84. }
  85. /* Let's get vector layers db connections information */
  86. Fi = Vect_get_field(&In, 1);
  87. if (!Fi) {
  88. Vect_close(&In);
  89. G_fatal_error(_("Database connection not defined for layer %d"), 1);
  90. }
  91. /* Output information useful for debuging
  92. include/vect/dig_structs.h
  93. */
  94. G_debug(1,
  95. "Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
  96. Fi->number, Fi->name, Fi->driver, Fi->database, Fi->table,
  97. Fi->key);
  98. /* Prepeare strings for use in db_* calls */
  99. db_init_string(&dbsql);
  100. db_init_string(&valstr);
  101. db_init_string(&table_name);
  102. db_init_handle(&handle);
  103. /* Prepare database for use */
  104. driver = db_start_driver(Fi->driver);
  105. if (driver == NULL) {
  106. Vect_close(&In);
  107. G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
  108. }
  109. db_set_handle(&handle, Fi->database, NULL);
  110. if (db_open_database(driver, &handle) != DB_OK) {
  111. Vect_close(&In);
  112. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  113. Fi->database, driver);
  114. }
  115. db_set_string(&table_name, Fi->table);
  116. if (db_describe_table(driver, &table_name, &table) != DB_OK) {
  117. Vect_close(&In);
  118. G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
  119. }
  120. ncols = db_get_table_number_of_columns(table);
  121. /* Copy header and history data from old to new map */
  122. Vect_copy_head_data(&In, &Out);
  123. Vect_hist_copy(&In, &Out);
  124. Vect_hist_command(&Out);
  125. i = 1;
  126. /* Let's do something with every vector feature in input map... */
  127. /* Read in single line and get it's type */
  128. while ((type = Vect_read_next_line(&In, Points, Cats)) > 0) {
  129. /* If Points contain line... */
  130. if (type == GV_LINE || type == GV_POINT || type == GV_CENTROID) {
  131. if (Vect_cat_get(Cats, 1, &cat) == 0) {
  132. Vect_cat_set(Cats, 1, i);
  133. i++;
  134. }
  135. }
  136. if (type == GV_POINT) {
  137. /* Print out point coordinates */
  138. printf("No:%d\tX:%f\tY:%f\tZ:%f\tCAT:%d\n", i, *Points->x,
  139. *Points->y, *Points->z, cat);
  140. /* Prepeare SQL query to get point attribute data */
  141. sprintf(sql, "select * from %s where %s=%d", Fi->table, Fi->key,
  142. cat);
  143. G_debug(1, "SQL: \"%s\"", sql);
  144. db_set_string(&dbsql, sql);
  145. /* Now execute query */
  146. if (db_open_select_cursor(driver, &dbsql, &cursor, DB_SEQUENTIAL)
  147. != DB_OK)
  148. G_warning(_("Unable to get attribute data for cat %d"), cat);
  149. else {
  150. /* Result count */
  151. nrows = db_get_num_rows(&cursor);
  152. G_debug(1, "Result count: %d", nrows);
  153. table = db_get_cursor_table(&cursor);
  154. /* Let's output every columns name and value */
  155. while (1) {
  156. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
  157. G_warning(_("Error while retrieving database record for cat %d"),
  158. cat);
  159. break;
  160. }
  161. if (!more)
  162. break;
  163. for (col = 0; col < ncols; col++) {
  164. column = db_get_table_column(table, col);
  165. db_convert_column_value_to_string(column, &valstr);
  166. printf("%s: %s\t", db_get_column_name(column),
  167. db_get_string(&valstr));
  168. }
  169. printf("\n");
  170. }
  171. db_close_cursor(&cursor);
  172. }
  173. }
  174. /* Only now we write data into new vector */
  175. Vect_write_line(&Out, type, Points, Cats);
  176. }
  177. /* Create database for new vector map */
  178. Fin = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
  179. driver = db_start_driver_open_database(Fin->driver, Fin->database);
  180. G_debug(1,
  181. "Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
  182. Fin->number, Fin->name, Fin->driver, Fin->database, Fin->table,
  183. Fin->key);
  184. /* Let's copy attribute table data */
  185. if (db_copy_table(Fi->driver, Fi->database, Fi->table,
  186. Fin->driver, Vect_subst_var(Fin->database, &Out),
  187. Fin->table) == DB_FAILED)
  188. G_warning(_("Unable to copy attribute table to vector map <%s>"),
  189. new->answer);
  190. else
  191. Vect_map_add_dblink(&Out, Fin->number, Fin->name, Fin->table, Fi->key,
  192. Fin->database, Fin->driver);
  193. /* Build topology for vector map and close them */
  194. Vect_build(&Out);
  195. Vect_close(&In);
  196. Vect_close(&Out);
  197. db_close_database_shutdown_driver(driver);
  198. /* Don't forget to report to caller sucessfull end of data processing :) */
  199. exit(EXIT_SUCCESS);
  200. }