open_nat.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*!
  2. \file vector/Vlib/open_nat.c
  3. \brief Vector library - open vector map (native format) - level 1
  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 (level 1)
  22. Map->name and Map->mapset must be set before.
  23. \param Map poiter to Map_info structure
  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>"),
  43. Vect_get_full_name(Map));
  44. return -1;
  45. }
  46. /* needed to determine file size, Map->head.size will be updated by dig__read_head(Map) */
  47. Vect_coor_info(Map, &CInfo);
  48. Map->head.size = CInfo.size;
  49. if (!(dig__read_head(Map)))
  50. return (-1);
  51. /* compare coor size stored in head with real size */
  52. /* check should catch if LFS is required but not available */
  53. check_coor(Map);
  54. /* set conversion matrices */
  55. dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
  56. /* load to memory */
  57. if (!update)
  58. dig_file_load(&(Map->dig_fp)); /* has currently no effect, file never loaded */
  59. return 0;
  60. }
  61. /*!
  62. \brief Create new vector map (level 1)
  63. \param[out] Map pointer to Map_info structure
  64. \param name vector map name to be created
  65. \param with_z 2D or 3D (unused?)
  66. \return 0 success
  67. \return -1 error
  68. */
  69. int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
  70. {
  71. char buf[1000];
  72. struct stat info;
  73. G_debug(1, "V1_open_new_nat(): name = %s", name);
  74. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  75. /* Set the 'coor' file version */
  76. Map->head.Version_Major = GV_COOR_VER_MAJOR;
  77. Map->head.Version_Minor = GV_COOR_VER_MINOR;
  78. Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
  79. Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;
  80. /* TODO: open better */
  81. dig_file_init(&(Map->dig_fp));
  82. Map->dig_fp.file = G_fopen_new(buf, GV_COOR_ELEMENT);
  83. if (Map->dig_fp.file == NULL)
  84. return (-1);
  85. fclose(Map->dig_fp.file);
  86. dig_file_init(&(Map->dig_fp));
  87. Map->dig_fp.file = G_fopen_modify(buf, GV_COOR_ELEMENT);
  88. if (Map->dig_fp.file == NULL)
  89. return (-1);
  90. /* if overwrite OK, any existing files have already been deleted by
  91. * Vect_open_new(): remove this check ? */
  92. /* check to see if dig_plus file exists and if so, remove it */
  93. G__file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
  94. if (stat(name_buf, &info) == 0) /* file exists? */
  95. unlink(name_buf);
  96. G__file_name(name_buf, buf, GV_COOR_ELEMENT, G_mapset());
  97. Map->head.size = 0;
  98. Map->head.head_size = GV_COOR_HEAD_SIZE + 4;
  99. Vect__write_head(Map);
  100. /* set conversion matrices */
  101. dig_init_portable(&(Map->head.port), dig__byte_order_out());
  102. if (!(dig__write_head(Map)))
  103. return (-1);
  104. return 0;
  105. }
  106. /* check file size */
  107. int check_coor(struct Map_info *Map)
  108. {
  109. struct Coor_info CInfo;
  110. off_t dif;
  111. /* NOTE: coor file is open */
  112. Vect_coor_info(Map, &CInfo);
  113. dif = CInfo.size - Map->head.size;
  114. G_debug(1, "coor size in head = %lu, real coor file size= %lu",
  115. (unsigned long) Map->head.size, (unsigned long) CInfo.size);
  116. if (dif > 0) {
  117. G_warning(_("Coor file of vector map <%s@%s> is larger than it should be "
  118. "(%ld bytes excess)"), Map->name, Map->mapset, dif);
  119. }
  120. else if (dif < 0) {
  121. G_warning(_("Coor file of vector <%s@%s> is shorter than it should be "
  122. "(%ld bytes missing)."), Map->name, Map->mapset, -dif);
  123. }
  124. return 1;
  125. }