main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(_("database"));
  32. G_add_keyword(_("attribute table"));
  33. module->description = _("Populates database values from vector features.");
  34. parse_command_line(argc, argv);
  35. G_begin_distance_calculations();
  36. G_begin_polygon_area_calculations();
  37. /* open map */
  38. Vect_set_open_level(2);
  39. Vect_open_old(&Map, options.name, "");
  40. Fi = Vect_get_field(&Map, options.field);
  41. if (!options.print && Fi == NULL) {
  42. G_fatal_error(_("Database connection not defined for layer %d. "
  43. "Use v.db.connect first."),
  44. options.field);
  45. }
  46. /* allocate array for values */
  47. /* (+ 1 is for cat -1 (no category) reported at the end ) */
  48. if (Vect_cidx_get_field_index(&Map, options.field) > -1) {
  49. n = Vect_cidx_get_num_unique_cats_by_index(&Map,
  50. Vect_cidx_get_field_index
  51. (&Map, options.field));
  52. }
  53. else {
  54. n = 0;
  55. }
  56. G_debug(2, "%d unique cats", n);
  57. Values = (struct value *) G_calloc(n + 1, sizeof(struct value));
  58. vstat.rcat = 0;
  59. /* Read values from map */
  60. if (options.option == O_QUERY) {
  61. query(&Map);
  62. }
  63. else if ((options.option == O_AREA) || (options.option == O_COMPACT) ||
  64. (options.option == O_PERIMETER) || (options.option == O_FD)) {
  65. read_areas(&Map);
  66. }
  67. else {
  68. read_lines(&Map);
  69. }
  70. conv_units();
  71. if (options.print) {
  72. report();
  73. }
  74. else {
  75. update(&Map);
  76. Vect_set_db_updated(&Map);
  77. }
  78. Vect_close(&Map);
  79. if (!(options.print && options.total)) {
  80. print_stat();
  81. }
  82. /* free list */
  83. G_free(Values);
  84. exit(EXIT_SUCCESS);
  85. }