raster_metadata.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* raster_metadata.c
  2. *
  3. * PURPOSE: functions to read and write raster "units" and "vertical datum"
  4. * meta-data info
  5. *
  6. * Copyright (C) 2007 by Hamish Bowman, and the GRASS Development Team
  7. * Author(s): Hamish Bowman, Dunedin, New Zealand
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. * \brief Get a raster map's units metadata string
  19. *
  20. * Read the raster's units metadata file and put string in str
  21. *
  22. * \param name
  23. * \param mapset
  24. * \param *str string to be populated with data
  25. * \return 0 on success
  26. * \return -1, EOF (fclose() result) on error
  27. */
  28. int G_read_raster_units(const char *name, const char *mapset, char *str)
  29. {
  30. return G__raster_misc_read_line("units", name, mapset, str);
  31. }
  32. /*!
  33. * \brief Write a string to a raster map's units metadata file
  34. *
  35. * Map must exist in the current mapset.
  36. *
  37. * \param name
  38. * \param *str string containing data to be written
  39. * \return 0 on success
  40. * \return -1, EOF (fclose() result) on error
  41. */
  42. int G_write_raster_units(const char *name, const char *str)
  43. {
  44. return G__raster_misc_write_line("units", name, str);
  45. }
  46. /*!
  47. * \brief Get a raster map's vertical datum metadata string
  48. *
  49. * Read the raster's vertical datum metadata file and put string in str
  50. *
  51. * \param name
  52. * \param mapset
  53. * \param *str string to be populated with data
  54. * \return 0 on success
  55. * \return -1, EOF (fclose() result) on error
  56. */
  57. int G_read_raster_vdatum(const char *name, const char *mapset, char *str)
  58. {
  59. return G__raster_misc_read_line("vertical_datum", name, mapset, str);
  60. }
  61. /*!
  62. * \brief Write a string into a raster's vertical datum metadata file
  63. *
  64. * Map must exist in the current mapset.
  65. *
  66. * \param name
  67. * \param *str string containing data to be written
  68. * \return 0 on success
  69. * \return -1, EOF (fclose() result) on error
  70. */
  71. int G_write_raster_vdatum(const char *name, const char *str)
  72. {
  73. return G__raster_misc_write_line("vertical_datum", name, str);
  74. }
  75. /*!
  76. * \brief Read the first line of a file in cell_misc/
  77. *
  78. * Read the first line of data from a cell_misc/ meta-data file.
  79. *
  80. * \param element metadata component filename
  81. * \param name
  82. * \param mapset
  83. * \param *str string to be populated with data
  84. * \return 0 on success
  85. * \return -1, EOF (fclose() result) on error
  86. */
  87. int G__raster_misc_read_line(const char *elem, const char *name,
  88. const char *mapset, char *str)
  89. {
  90. FILE *fd;
  91. char buff[GNAME_MAX];
  92. buff[0] = '\0';
  93. if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL)
  94. return -1;
  95. fd = G_fopen_old_misc("cell_misc", elem, name, mapset);
  96. if (!fd) {
  97. G_warning(_("Can't read %s for [%s in %s]"), elem, name, mapset);
  98. return -1;
  99. }
  100. if (G_getl2(buff, sizeof(buff) - 1, fd) == 0) {
  101. /* file is empty */
  102. return fclose(fd);
  103. }
  104. strcpy(str, buff);
  105. return fclose(fd);
  106. }
  107. /*!
  108. * \brief Write a line to a raster map metadata file
  109. *
  110. * Write (including overwrite) a string into a raster map's metadata file
  111. * found in in cell_misc/ in the current mapset.
  112. *
  113. * \param element metadata component filename
  114. * \param name
  115. * \param *str string containing data to be written
  116. * \return 0 on success
  117. * \return -1, EOF (fclose() result) on error
  118. */
  119. int G__raster_misc_write_line(const char *elem, const char *name,
  120. const char *str)
  121. {
  122. FILE *fd;
  123. fd = G_fopen_new_misc("cell_misc", elem, name);
  124. if (fd == NULL) {
  125. G_warning(_("Can't create %s metadata file for [%s in %s]"),
  126. elem, name, G_mapset());
  127. return -1;
  128. }
  129. fprintf(fd, "%s", str);
  130. return fclose(fd);
  131. }