raster_metadata.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*!
  2. \file lib/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. * \return dynamically-allocated string on success
  80. * \return NULL on error
  81. */
  82. static char *misc_read_line(const char *elem,
  83. const char *name, const char *mapset)
  84. {
  85. char buff[GNAME_MAX];
  86. FILE *fp;
  87. buff[0] = '\0';
  88. if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL)
  89. return NULL;
  90. fp = G_fopen_old_misc("cell_misc", elem, name, mapset);
  91. if (!fp) {
  92. G_warning(_("Unable to read <%s> for raster map <%s@%s>"),
  93. elem, name, mapset);
  94. return NULL;
  95. }
  96. if (G_getl2(buff, sizeof(buff) - 1, fp) == 0) {
  97. /* file is empty */
  98. *buff = '\0';
  99. }
  100. if (fclose(fp) != 0)
  101. G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
  102. elem, name, mapset);
  103. return *buff ? G_store(buff) : NULL;
  104. }
  105. /*!
  106. * \brief Write a line to a raster map metadata file
  107. *
  108. * Write (including overwrite) a string into a raster map's metadata file
  109. * found in in cell_misc/ in the current mapset.
  110. *
  111. * \param element metadata component filename
  112. * \param name
  113. * \param *str string containing data to be written
  114. */
  115. static void misc_write_line(const char *elem, const char *name, const char *str)
  116. {
  117. FILE *fp;
  118. fp = G_fopen_new_misc("cell_misc", elem, name);
  119. if (!fp)
  120. G_fatal_error(_("Unable to create <%s> metadata file for raster map <%s@%s>"),
  121. elem, name, G_mapset());
  122. fprintf(fp, "%s\n", str);
  123. if (fclose(fp) != 0)
  124. G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
  125. elem, name, G_mapset());
  126. }