main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. Vect_open_old(&Map, options.name, "");
  48. Vect_set_error_handler_io(&Map, NULL);
  49. Fi = Vect_get_field(&Map, options.field);
  50. if (!options.print && Fi == NULL) {
  51. G_fatal_error(_("Database connection not defined for layer %d. "
  52. "Use v.db.connect first."),
  53. options.field);
  54. }
  55. /* allocate array for values */
  56. /* (+ 1 is for cat -1 (no category) reported at the end ) */
  57. if (Vect_cidx_get_field_index(&Map, options.field) > -1) {
  58. n = Vect_cidx_get_num_unique_cats_by_index(&Map,
  59. Vect_cidx_get_field_index
  60. (&Map, options.field));
  61. }
  62. else {
  63. n = 0;
  64. }
  65. G_debug(2, "%d unique cats", n);
  66. Values = (struct value *) G_calloc(n + 1, sizeof(struct value));
  67. vstat.rcat = 0;
  68. /* Read values from map */
  69. if (options.option == O_QUERY) {
  70. query(&Map);
  71. }
  72. else if ((options.option == O_AREA) || (options.option == O_COMPACT) ||
  73. (options.option == O_PERIMETER) || (options.option == O_FD)) {
  74. read_areas(&Map);
  75. }
  76. else {
  77. read_lines(&Map);
  78. }
  79. conv_units();
  80. if (options.print || options.total) {
  81. report();
  82. }
  83. else {
  84. update(&Map);
  85. Vect_set_db_updated(&Map);
  86. }
  87. Vect_close(&Map);
  88. if (!(options.print || options.total)) {
  89. print_stat();
  90. }
  91. /* free list */
  92. G_free(Values);
  93. exit(EXIT_SUCCESS);
  94. }