main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /****************************************************************************
  2. *
  3. * MODULE: v.to.db
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Wolf Bergenheim <wolf+grass bergenheim net>,
  6. * Glynn Clements <glynn gclements.plus.com>,
  7. * Markus Neteler <neteler itc.it>
  8. * PURPOSE: load values from vector to database
  9. * COPYRIGHT: (C) 2000-2010 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <grass/glocale.h>
  18. #include "global.h"
  19. struct value *Values;
  20. struct options options;
  21. struct vstat vstat;
  22. int main(int argc, char *argv[])
  23. {
  24. int n;
  25. struct Map_info Map;
  26. struct GModule *module;
  27. struct field_info *Fi;
  28. G_gisinit(argv[0]);
  29. module = G_define_module();
  30. G_add_keyword(_("vector"));
  31. G_add_keyword(_("attribute table"));
  32. G_add_keyword(_("database"));
  33. module->description = _("Populates attribute values from vector features.");
  34. parse_command_line(argc, argv);
  35. if (!options.print && !options.total) {
  36. const char *mapset;
  37. mapset = G_find_vector2(options.name, "");
  38. if (!mapset || (strcmp(mapset, G_mapset()) != 0))
  39. G_fatal_error(_("Vector map <%s> not found in the current mapset. "
  40. "Unable to modify vector maps from different mapsets."),
  41. options.name);
  42. }
  43. G_begin_distance_calculations();
  44. G_begin_polygon_area_calculations();
  45. /* open map */
  46. Vect_set_open_level(2);
  47. if (Vect_open_old(&Map, options.name, "") < 0)
  48. G_fatal_error(_("Unable to open vector map <%s>"), options.name);
  49. Vect_set_error_handler_io(&Map, NULL);
  50. Fi = Vect_get_field(&Map, options.field);
  51. if (!options.print && Fi == NULL) {
  52. G_fatal_error(_("Database connection not defined for layer %d. "
  53. "Use v.db.connect first."),
  54. options.field);
  55. }
  56. /* allocate array for values */
  57. /* (+ 1 is for cat -1 (no category) reported at the end ) */
  58. if (Vect_cidx_get_field_index(&Map, options.field) > -1) {
  59. n = Vect_cidx_get_num_unique_cats_by_index(&Map,
  60. Vect_cidx_get_field_index
  61. (&Map, options.field));
  62. }
  63. else {
  64. n = 0;
  65. }
  66. G_debug(2, "%d unique cats", n);
  67. Values = (struct value *) G_calloc(n + 1, sizeof(struct value));
  68. vstat.rcat = 0;
  69. /* Read values from map */
  70. if (options.option == O_QUERY) {
  71. query(&Map);
  72. }
  73. else if ((options.option == O_AREA) || (options.option == O_COMPACT) ||
  74. (options.option == O_PERIMETER) || (options.option == O_FD)) {
  75. read_areas(&Map);
  76. }
  77. else {
  78. read_lines(&Map);
  79. }
  80. conv_units();
  81. if (options.print || options.total) {
  82. report();
  83. }
  84. else {
  85. update(&Map);
  86. Vect_set_db_updated(&Map);
  87. }
  88. Vect_close(&Map);
  89. if (!(options.print || options.total)) {
  90. print_stat();
  91. }
  92. /* free list */
  93. G_free(Values);
  94. exit(EXIT_SUCCESS);
  95. }