raster_metadata.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*!
  2. \file raster/raster_metadata.c
  3. \brief Raster library - Functions to read and write raster "units"
  4. and "vertical datum" meta-data info
  5. (C) 2007-2009 by Hamish Bowman, and 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 Hamish Bowman
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/raster.h>
  14. #include <grass/glocale.h>
  15. static char *misc_read_line(const char *, const char *, const char *);
  16. static void misc_write_line(const char *, const char *, const char *);
  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 raster map name
  23. * \param mapset mapset name
  24. *
  25. * \return string representing units on success
  26. * \return NULL on error
  27. */
  28. char *Rast_read_units(const char *name, const char *mapset)
  29. {
  30. return misc_read_line("units", name, mapset);
  31. }
  32. /*!
  33. * \brief Write a string to a raster map's units metadata file
  34. *
  35. * Raster map must exist in the current mapset.
  36. *
  37. * \param name raster map name
  38. * \param str string containing data to be written
  39. */
  40. void Rast_write_units(const char *name, const char *str)
  41. {
  42. misc_write_line("units", name, str);
  43. }
  44. /*!
  45. * \brief Get a raster map's vertical datum metadata string
  46. *
  47. * Read the raster's vertical datum metadata file and put string in str
  48. *
  49. * \param name raster map name
  50. * \param mapset mapset name
  51. *
  52. * \return string representing vertical datum on success
  53. * \return NULL on error
  54. */
  55. char *Rast_read_vdatum(const char *name, const char *mapset)
  56. {
  57. return misc_read_line("vertical_datum", name, mapset);
  58. }
  59. /*!
  60. * \brief Write a string into a raster's vertical datum metadata file
  61. *
  62. * Raster map must exist in the current mapset.
  63. *
  64. * \param name raster map name
  65. * \param str string containing data to be written
  66. */
  67. void Rast_write_vdatum(const char *name, const char *str)
  68. {
  69. misc_write_line("vertical_datum", name, str);
  70. }
  71. /*!
  72. * \brief Read the first line of a file in cell_misc/
  73. *
  74. * Read the first line of data from a cell_misc/ meta-data file.
  75. *
  76. * \param element metadata component filename
  77. * \param name
  78. * \param mapset
  79. * \param *str string to be populated with data
  80. * \return dynamically-allocated string on success
  81. * \return NULL on error
  82. */
  83. static char *misc_read_line(const char *elem,
  84. const char *name, const char *mapset)
  85. {
  86. char buff[GNAME_MAX];
  87. FILE *fp;
  88. buff[0] = '\0';
  89. if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL)
  90. return NULL;
  91. fp = G_fopen_old_misc("cell_misc", elem, name, mapset);
  92. if (!fp) {
  93. G_warning(_("Unable to read <%s> for raster map <%s@%s>"),
  94. elem, name, mapset);
  95. return NULL;
  96. }
  97. if (G_getl2(buff, sizeof(buff) - 1, fp) == 0) {
  98. /* file is empty */
  99. *buff = '\0';
  100. }
  101. if (fclose(fp) != 0)
  102. G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
  103. elem, name, mapset);
  104. return *buff ? G_store(buff) : NULL;
  105. }
  106. /*!
  107. * \brief Write a line to a raster map metadata file
  108. *
  109. * Write (including overwrite) a string into a raster map's metadata file
  110. * found in in cell_misc/ in the current mapset.
  111. *
  112. * \param element metadata component filename
  113. * \param name
  114. * \param *str string containing data to be written
  115. */
  116. static void misc_write_line(const char *elem, const char *name, const char *str)
  117. {
  118. FILE *fp;
  119. fp = G_fopen_new_misc("cell_misc", elem, name);
  120. if (!fp)
  121. G_fatal_error(_("Unable to create <%s> metadata file for raster map <%s@%s>"),
  122. elem, name, G_mapset());
  123. fprintf(fp, "%s\n", str);
  124. if (fclose(fp) != 0)
  125. G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
  126. elem, name, G_mapset());
  127. }