main.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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-2006 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. #define MAIN
  17. #include <grass/glocale.h>
  18. #include "global.h"
  19. int main(int argc, char *argv[])
  20. {
  21. int n;
  22. struct Map_info Map;
  23. struct GModule *module;
  24. struct field_info *Fi;
  25. G_gisinit(argv[0]);
  26. module = G_define_module();
  27. module->keywords = _("vector, database, attribute table");
  28. module->description = _("Populate database values from vector features.");
  29. parse_command_line(argc, argv);
  30. G_begin_distance_calculations();
  31. G_begin_polygon_area_calculations();
  32. /* open map */
  33. Vect_set_open_level(2);
  34. Vect_open_old(&Map, options.name, options.mapset);
  35. Fi = Vect_get_field(&Map, options.field);
  36. if (!options.print && Fi == NULL) {
  37. G_fatal_error(_("Database connection not defined for layer %d. Use v.db.connect first."),
  38. options.field);
  39. }
  40. /* allocate array for values */
  41. /* (+ 1 is for cat -1 (no category) reported at the end ) */
  42. if (Vect_cidx_get_field_index(&Map, options.field) > -1) {
  43. n = Vect_cidx_get_num_unique_cats_by_index(&Map,
  44. Vect_cidx_get_field_index
  45. (&Map, options.field));
  46. }
  47. else {
  48. n = 0;
  49. }
  50. G_debug(2, "%d unique cats", n);
  51. Values = (VALUE *) G_calloc(n + 1, sizeof(VALUE));
  52. vstat.rcat = 0;
  53. /* Read values from map */
  54. if (options.option == O_QUERY) {
  55. query(&Map);
  56. }
  57. else if ((options.option == O_AREA) || (options.option == O_COMPACT) ||
  58. (options.option == O_PERIMETER) || (options.option == O_FD)) {
  59. read_areas(&Map);
  60. }
  61. else {
  62. read_lines(&Map);
  63. }
  64. conv_units();
  65. if (options.print) {
  66. report();
  67. }
  68. else {
  69. update(&Map);
  70. Vect_set_db_updated(&Map);
  71. }
  72. Vect_close(&Map);
  73. /* free list */
  74. G_free(Values);
  75. if (!(options.print && options.total)) {
  76. print_stat();
  77. }
  78. exit(EXIT_SUCCESS);
  79. }