open_nat.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*!
  2. \file open_nat.c
  3. \brief Vector library - open vector map (native format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  11. Update to GRASS 5.7 Radim Blazek and David D. Gray.
  12. \date 2001
  13. */
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <grass/Vect.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. static char name_buf[GPATH_MAX];
  21. static int check_coor(struct Map_info *Map);
  22. /**
  23. \brief Open existing vector map
  24. Map->name and Map->mapset must be set before.
  25. \param Map poiter to vector map
  26. \param update non-zero for write mode, otherwise read-only
  27. \return 0 success
  28. \return -1 error
  29. */
  30. int V1_open_old_nat(struct Map_info *Map, int update)
  31. {
  32. char buf[1000];
  33. G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
  34. Map->mapset);
  35. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  36. dig_file_init(&(Map->dig_fp));
  37. if (update)
  38. Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
  39. else
  40. Map->dig_fp.file =
  41. G_fopen_old(buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);
  42. if (Map->dig_fp.file == NULL)
  43. return -1;
  44. if (!(dig__read_head(Map)))
  45. return (-1);
  46. check_coor(Map);
  47. /* set conversion matrices */
  48. dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
  49. /* load to memory */
  50. if (!update)
  51. dig_file_load(&(Map->dig_fp));
  52. return (0);
  53. }
  54. /**
  55. \brief Open/Create new vector map.
  56. \param Map pointer to vector map
  57. \param name map name
  58. \param with_z 2D or 3D (unused?)
  59. \return 0 success
  60. \return -1 error
  61. */
  62. int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
  63. {
  64. char buf[1000];
  65. struct stat info;
  66. G_debug(1, "V1_open_new_nat(): name = %s", name);
  67. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);
  68. /* Set the 'coor' file version */
  69. Map->head.Version_Major = GV_COOR_VER_MAJOR;
  70. Map->head.Version_Minor = GV_COOR_VER_MINOR;
  71. Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
  72. Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;
  73. /* TODO open better */
  74. dig_file_init(&(Map->dig_fp));
  75. Map->dig_fp.file = G_fopen_new(buf, GRASS_VECT_COOR_ELEMENT);
  76. if (Map->dig_fp.file == NULL)
  77. return (-1);
  78. fclose(Map->dig_fp.file);
  79. dig_file_init(&(Map->dig_fp));
  80. Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
  81. if (Map->dig_fp.file == NULL)
  82. return (-1);
  83. /* check to see if dig_plus file exists and if so, remove it */
  84. G__file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
  85. if (stat(name_buf, &info) == 0) /* file exists? */
  86. unlink(name_buf);
  87. G__file_name(name_buf, buf, GRASS_VECT_COOR_ELEMENT, G_mapset());
  88. Map->head.size = 0;
  89. Map->head.head_size = GV_COOR_HEAD_SIZE;
  90. Vect__write_head(Map);
  91. /* set conversion matrices */
  92. dig_init_portable(&(Map->head.port), dig__byte_order_out());
  93. if (!(dig__write_head(Map)))
  94. return (-1);
  95. return 0;
  96. }
  97. /* check file size */
  98. int check_coor(struct Map_info *Map)
  99. {
  100. struct Coor_info CInfo;
  101. long dif;
  102. Vect_coor_info(Map, &CInfo);
  103. dif = CInfo.size - Map->head.size;
  104. G_debug(1, "coor size in head = %ld, real coor file size= %ld",
  105. Map->head.size, CInfo.size);
  106. if (dif > 0) {
  107. G_warning(_("Coor files of vector map <%s@%s> is larger than it should be "
  108. "(%ld bytes excess)"), Map->name, Map->mapset, dif);
  109. }
  110. else if (dif < 0) {
  111. G_warning(_("Coor files of vector <%s@%s> is shorter than it should be "
  112. "(%ld bytes missing)."), Map->name, Map->mapset, -dif);
  113. }
  114. return 1;
  115. }