open_nat.c 4.1 KB

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