main.c 2.7 KB

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