make_loc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /******************************************************************************
  2. *
  3. * Project: libgrass
  4. * Purpose: Function to create a new location automatically given a
  5. * "Cell_head", PROJ_INFO and PROJ_UNITS information.
  6. * Author: Frank Warmerdam, warmerda@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 2000, Frank Warmerdam
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Library General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Library General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Library General Public
  22. * License along with this library; if not, write to the
  23. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. ******************************************************************************
  25. *
  26. */
  27. #include <grass/gis.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/stat.h>
  32. #include <math.h>
  33. /*
  34. * Returns 0 on success.
  35. * Returns -1 to indicate a system error (check errno).
  36. */
  37. int G__make_location(const char *location_name,
  38. struct Cell_head *wind,
  39. struct Key_Value *proj_info,
  40. struct Key_Value *proj_units, FILE * report_file)
  41. {
  42. char path[GPATH_MAX];
  43. /* Try to create the location directory, under the gisdbase. */
  44. sprintf(path, "%s/%s", G_gisdbase(), location_name);
  45. if (G_mkdir(path) != 0)
  46. return -1;
  47. /* Make the PERMANENT mapset. */
  48. sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT");
  49. if (G_mkdir(path) != 0)
  50. return -1;
  51. /* make these the new current location and mapset */
  52. G__setenv("LOCATION_NAME", location_name);
  53. G__setenv("MAPSET", "PERMANENT");
  54. /* Create the default, and current window files */
  55. G__put_window(wind, "", "DEFAULT_WIND");
  56. G__put_window(wind, "", "WIND");
  57. /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
  58. if (proj_info != NULL) {
  59. G_file_name(path, "", "PROJ_INFO", "PERMANENT");
  60. G_write_key_value_file(path, proj_info);
  61. }
  62. if (proj_units != NULL) {
  63. G_file_name(path, "", "PROJ_UNITS", "PERMANENT");
  64. G_write_key_value_file(path, proj_units);
  65. }
  66. return 0;
  67. }
  68. /*!
  69. * \brief create a new location
  70. *
  71. * This function creates a new location in the current database,
  72. * initializes the projection, default window and current window.
  73. *
  74. * \param location_name
  75. * The name of the new location. Should not include
  76. * the full path, the location will be created within
  77. * the current database.
  78. * \param wind
  79. * Contains the default window setting for the
  80. * new location. All fields should be set in this
  81. * structure, and care should be taken to ensure that
  82. * the proj/zone fields match the definition in the
  83. * proj_info parameter (see G_set_cellhd_from_projinfo()).
  84. *
  85. * \param proj_info
  86. * Projection definition suitable to write to the
  87. * PROJ_INFO file, or NULL for PROJECTION_XY.
  88. *
  89. * \param proj_units
  90. * Projection units suitable to write to the PROJ_UNITS
  91. * file, or NULL.
  92. *
  93. * \param report_file
  94. * File to which creation information should be written
  95. * (can be stdout). Currently not used.
  96. *
  97. * \return Returns 0 on success, or generates a fatal error on failure.
  98. * The G__make_location() function operates the same, but returns a
  99. * non-zero error code on failure, instead of terminating.
  100. */
  101. int G_make_location(const char *location_name,
  102. struct Cell_head *wind,
  103. struct Key_Value *proj_info,
  104. struct Key_Value *proj_units, FILE * report_file)
  105. {
  106. int err;
  107. err = G__make_location(location_name, wind, proj_info, proj_units,
  108. report_file);
  109. if (err == 0)
  110. return 0;
  111. if (err == -1) {
  112. perror("G_make_location");
  113. }
  114. G_fatal_error("G_make_location failed.");
  115. return 1;
  116. }
  117. /************************************************************************/
  118. /* G_compare_projections() */
  119. /************************************************************************/
  120. /*!
  121. * \brief compare projections
  122. *
  123. * \param proj_info1
  124. * \param proj_units1
  125. * \param proj_info2
  126. * \param proj_units2
  127. * \return -1 if not the same projection, -2 if linear unit translation to
  128. * meters fails, -4 if not the same ellipsoid, -5 if UTM zone differs
  129. * else TRUE if projections match.
  130. *
  131. */
  132. int
  133. G_compare_projections(const struct Key_Value *proj_info1,
  134. const struct Key_Value *proj_units1,
  135. const struct Key_Value *proj_info2,
  136. const struct Key_Value *proj_units2)
  137. {
  138. const char *proj1, *proj2;
  139. if (proj_info1 == NULL && proj_info2 == NULL)
  140. return TRUE;
  141. /* -------------------------------------------------------------------- */
  142. /* Are they both in the same projection? */
  143. /* -------------------------------------------------------------------- */
  144. /* prevent seg fault in G_find_key_value */
  145. if (proj_info1 == NULL || proj_info2 == NULL)
  146. return -1;
  147. proj1 = G_find_key_value("proj", proj_info1);
  148. proj2 = G_find_key_value("proj", proj_info2);
  149. if (proj1 == NULL || proj2 == NULL || strcmp(proj1, proj2))
  150. return -1;
  151. /* -------------------------------------------------------------------- */
  152. /* Verify that the linear unit translation to meters is OK. */
  153. /* -------------------------------------------------------------------- */
  154. /* prevent seg fault in G_find_key_value */
  155. if (proj_units1 == NULL && proj_units2 == NULL)
  156. return TRUE;
  157. if (proj_units1 == NULL || proj_units2 == NULL)
  158. return -2;
  159. {
  160. double a1 = 0, a2 = 0;
  161. if (G_find_key_value("meters", proj_units1) != NULL)
  162. a1 = atof(G_find_key_value("meters", proj_units1));
  163. if (G_find_key_value("meters", proj_units2) != NULL)
  164. a2 = atof(G_find_key_value("meters", proj_units2));
  165. if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
  166. return -2;
  167. }
  168. /* -------------------------------------------------------------------- */
  169. /* Do they both have the same ellipsoid? */
  170. /* Lets just check the semi-major axis for now to keep it simple */
  171. /* -------------------------------------------------------------------- */
  172. {
  173. double a1 = 0, a2 = 0;
  174. if (G_find_key_value("a", proj_info1) != NULL)
  175. a1 = atof(G_find_key_value("a", proj_info1));
  176. if (G_find_key_value("a", proj_info2) != NULL)
  177. a2 = atof(G_find_key_value("a", proj_info2));
  178. if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
  179. return -4;
  180. }
  181. /* -------------------------------------------------------------------- */
  182. /* Zone check specially for UTM */
  183. /* -------------------------------------------------------------------- */
  184. if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
  185. && atof(G_find_key_value("zone", proj_info1))
  186. != atof(G_find_key_value("zone", proj_info2)))
  187. return -5;
  188. /* -------------------------------------------------------------------- */
  189. /* Do they both have the same false easting? */
  190. /* -------------------------------------------------------------------- */
  191. {
  192. const char *x_0_1 = NULL, *x_0_2 = NULL;
  193. x_0_1 = G_find_key_value("x_0", proj_info1);
  194. x_0_2 = G_find_key_value("x_0", proj_info2);
  195. if (x_0_1 && x_0_2 && (fabs(atof(x_0_1) - atof(x_0_2)) > 0.000001))
  196. return -6;
  197. }
  198. /* -------------------------------------------------------------------- */
  199. /* Do they both have the same false northing? */
  200. /* -------------------------------------------------------------------- */
  201. {
  202. const char *y_0_1 = NULL, *y_0_2 = NULL;
  203. y_0_1 = G_find_key_value("y_0", proj_info1);
  204. y_0_2 = G_find_key_value("y_0", proj_info2);
  205. if (y_0_1 && y_0_2 && (fabs(atof(y_0_1) - atof(y_0_2)) > 0.000001))
  206. return -7;
  207. }
  208. /* -------------------------------------------------------------------- */
  209. /* Add more details in later. */
  210. /* -------------------------------------------------------------------- */
  211. return TRUE;
  212. }