make_loc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*!
  2. * \file lib/gis/make_loc.c
  3. *
  4. * \brief GIS Library - Functions to create a new location
  5. *
  6. * Creates a new location automatically given a "Cell_head", PROJ_INFO
  7. * and PROJ_UNITS information.
  8. *
  9. * (C) 2000-2013 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public License
  12. * (>=v2). Read the file COPYING that comes with GRASS for details.
  13. *
  14. * \author Frank Warmerdam
  15. */
  16. #include <grass/gis.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <sys/stat.h>
  21. #include <math.h>
  22. /*!
  23. * \brief Create a new location
  24. *
  25. * This function creates a new location in the current database,
  26. * initializes the projection, default window and current window.
  27. *
  28. * \param location_name Name of the new location. Should not include
  29. * the full path, the location will be created within
  30. * the current database.
  31. * \param wind default window setting for the new location.
  32. * All fields should be set in this
  33. * structure, and care should be taken to ensure that
  34. * the proj/zone fields match the definition in the
  35. * proj_info parameter(see G_set_cellhd_from_projinfo()).
  36. *
  37. * \param proj_info projection definition suitable to write to the
  38. * PROJ_INFO file, or NULL for PROJECTION_XY.
  39. *
  40. * \param proj_units projection units suitable to write to the PROJ_UNITS
  41. * file, or NULL.
  42. *
  43. * \return 0 on success
  44. * \return -1 to indicate a system error (check errno).
  45. * \return -2 failed to create projection file (currently not used)
  46. * \return -3 illegal name
  47. */
  48. int G_make_location(const char *location_name,
  49. struct Cell_head *wind,
  50. const struct Key_Value *proj_info,
  51. const struct Key_Value *proj_units)
  52. {
  53. char path[GPATH_MAX];
  54. /* check if location name is legal */
  55. if (G_legal_filename(location_name) != 1)
  56. return -3;
  57. /* Try to create the location directory, under the gisdbase. */
  58. sprintf(path, "%s/%s", G_gisdbase(), location_name);
  59. if (G_mkdir(path) != 0)
  60. return -1;
  61. /* Make the PERMANENT mapset. */
  62. sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT");
  63. if (G_mkdir(path) != 0) {
  64. return -1;
  65. }
  66. /* make these the new current location and mapset */
  67. G_setenv_nogisrc("LOCATION_NAME", location_name);
  68. G_setenv_nogisrc("MAPSET", "PERMANENT");
  69. /* Create the default, and current window files */
  70. G_put_element_window(wind, "", "DEFAULT_WIND");
  71. G_put_element_window(wind, "", "WIND");
  72. /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
  73. if (proj_info != NULL) {
  74. G_file_name(path, "", "PROJ_INFO", "PERMANENT");
  75. G_write_key_value_file(path, proj_info);
  76. }
  77. if (proj_units != NULL) {
  78. G_file_name(path, "", "PROJ_UNITS", "PERMANENT");
  79. G_write_key_value_file(path, proj_units);
  80. }
  81. return 0;
  82. }
  83. /*!
  84. * \brief Create a new location
  85. *
  86. * This function creates a new location in the current database,
  87. * initializes the projection, default window and current window,
  88. * and sets the EPSG code if present
  89. *
  90. * \param location_name Name of the new location. Should not include
  91. * the full path, the location will be created within
  92. * the current database.
  93. * \param wind default window setting for the new location.
  94. * All fields should be set in this
  95. * structure, and care should be taken to ensure that
  96. * the proj/zone fields match the definition in the
  97. * proj_info parameter(see G_set_cellhd_from_projinfo()).
  98. *
  99. * \param proj_info projection definition suitable to write to the
  100. * PROJ_INFO file, or NULL for PROJECTION_XY.
  101. *
  102. * \param proj_units projection units suitable to write to the PROJ_UNITS
  103. * file, or NULL.
  104. *
  105. * \param proj_epsg EPSG code suitable to write to the PROJ_EPSG
  106. * file, or NULL.
  107. *
  108. * \return 0 on success
  109. * \return -1 to indicate a system error (check errno).
  110. * \return -2 failed to create projection file (currently not used)
  111. * \return -3 illegal name
  112. */
  113. int G_make_location_epsg(const char *location_name,
  114. struct Cell_head *wind,
  115. const struct Key_Value *proj_info,
  116. const struct Key_Value *proj_units,
  117. const struct Key_Value *proj_epsg)
  118. {
  119. int ret;
  120. char path[GPATH_MAX];
  121. ret = G_make_location(location_name, wind, proj_info, proj_units);
  122. if (ret != 0)
  123. return ret;
  124. /* Write out the PROJ_EPSG if available. */
  125. if (proj_epsg != NULL) {
  126. G_file_name(path, "", "PROJ_EPSG", "PERMANENT");
  127. G_write_key_value_file(path, proj_epsg);
  128. }
  129. return 0;
  130. }
  131. /*!
  132. * \brief Compare projections including units
  133. *
  134. * \param proj_info1 projection info to compare
  135. * \param proj_units1 projection units to compare
  136. * \param proj_info2 projection info to compare
  137. * \param proj_units2 projection units to compare
  138. * \return -1 if not the same projection
  139. * \return -2 if linear unit translation to meters fails
  140. * \return -3 if not the same datum,
  141. * \return -4 if not the same ellipsoid,
  142. * \return -5 if UTM zone differs
  143. * \return -6 if UTM hemisphere differs,
  144. * \return -7 if false easting differs
  145. * \return -8 if false northing differs,
  146. * \return -9 if center longitude differs,
  147. * \return -10 if center latitude differs,
  148. * \return -11 if standard parallels differ,
  149. * \return 1 if projections match.
  150. */
  151. int G_compare_projections(const struct Key_Value *proj_info1,
  152. const struct Key_Value *proj_units1,
  153. const struct Key_Value *proj_info2,
  154. const struct Key_Value *proj_units2)
  155. {
  156. const char *proj1, *proj2;
  157. if (proj_info1 == NULL && proj_info2 == NULL)
  158. return TRUE;
  159. /* -------------------------------------------------------------------- */
  160. /* Are they both in the same projection? */
  161. /* -------------------------------------------------------------------- */
  162. /* prevent seg fault in G_find_key_value */
  163. if (proj_info1 == NULL || proj_info2 == NULL)
  164. return -1;
  165. proj1 = G_find_key_value("proj", proj_info1);
  166. proj2 = G_find_key_value("proj", proj_info2);
  167. if (proj1 == NULL || proj2 == NULL || strcmp(proj1, proj2))
  168. return -1;
  169. /* -------------------------------------------------------------------- */
  170. /* Verify that the linear unit translation to meters is OK. */
  171. /* -------------------------------------------------------------------- */
  172. /* prevent seg fault in G_find_key_value */
  173. if (proj_units1 == NULL && proj_units2 == NULL)
  174. return 1;
  175. if (proj_units1 == NULL || proj_units2 == NULL)
  176. return -2;
  177. {
  178. double a1 = 0, a2 = 0;
  179. if (G_find_key_value("meters", proj_units1) != NULL)
  180. a1 = atof(G_find_key_value("meters", proj_units1));
  181. if (G_find_key_value("meters", proj_units2) != NULL)
  182. a2 = atof(G_find_key_value("meters", proj_units2));
  183. if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
  184. return -2;
  185. }
  186. /* compare unit name only if there is no to meter conversion factor */
  187. if (G_find_key_value("meters", proj_units1) == NULL ||
  188. G_find_key_value("meters", proj_units2) == NULL) {
  189. const char *u_1 = NULL, *u_2 = NULL;
  190. u_1 = G_find_key_value("unit", proj_units1);
  191. u_2 = G_find_key_value("unit", proj_units2);
  192. if ((u_1 && !u_2) || (!u_1 && u_2))
  193. return -2;
  194. /* the unit name can be arbitrary: the following can be the same
  195. * us-ft (proj.4 keyword)
  196. * U.S. Surveyor's Foot (proj.4 name)
  197. * US survey foot (WKT)
  198. * Foot_US (WKT)
  199. */
  200. if (u_1 && u_2 && G_strcasecmp(u_1, u_2))
  201. return -2;
  202. }
  203. /* -------------------------------------------------------------------- */
  204. /* Do they both have the same datum? */
  205. /* -------------------------------------------------------------------- */
  206. {
  207. const char *d_1 = NULL, *d_2 = NULL;
  208. d_1 = G_find_key_value("datum", proj_info1);
  209. d_2 = G_find_key_value("datum", proj_info2);
  210. if ((d_1 && !d_2) || (!d_1 && d_2))
  211. return -3;
  212. if (d_1 && d_2 && strcmp(d_1, d_2)) {
  213. /* different datum short names can mean the same datum,
  214. * see lib/gis/datum.table */
  215. G_debug(1, "Different datum names");
  216. }
  217. }
  218. /* -------------------------------------------------------------------- */
  219. /* Do they both have the same ellipsoid? */
  220. /* -------------------------------------------------------------------- */
  221. {
  222. const char *e_1 = NULL, *e_2 = NULL;
  223. e_1 = G_find_key_value("ellps", proj_info1);
  224. e_2 = G_find_key_value("ellps", proj_info2);
  225. if (e_1 && e_2 && strcmp(e_1, e_2))
  226. return -4;
  227. if (e_1 == NULL || e_2 == NULL) {
  228. double a1 = 0, a2 = 0;
  229. double es1 = 0, es2 = 0;
  230. /* it may happen that one proj_info has ellps,
  231. * while the other has a, es: translate ellps to a, es */
  232. if (e_1)
  233. G_get_ellipsoid_by_name(e_1, &a1, &es1);
  234. else {
  235. if (G_find_key_value("a", proj_info1) != NULL)
  236. a1 = atof(G_find_key_value("a", proj_info1));
  237. if (G_find_key_value("es", proj_info1) != NULL)
  238. es1 = atof(G_find_key_value("es", proj_info1));
  239. }
  240. if (e_2)
  241. G_get_ellipsoid_by_name(e_2, &a2, &es2);
  242. else {
  243. if (G_find_key_value("a", proj_info2) != NULL)
  244. a2 = atof(G_find_key_value("a", proj_info2));
  245. if (G_find_key_value("es", proj_info2) != NULL)
  246. es2 = atof(G_find_key_value("es", proj_info2));
  247. }
  248. /* it should be an error if a = 0 */
  249. if ((a1 == 0 && a2 != 0) || (a1 != 0 && a2 == 0))
  250. return -4;
  251. if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
  252. return -4;
  253. if ((es1 == 0 && es2 != 0) || (es1 != 0 && es2 == 0))
  254. return -4;
  255. if (es1 && es2 && (fabs(es2 - es1) > 0.000001))
  256. return -4;
  257. }
  258. }
  259. /* -------------------------------------------------------------------- */
  260. /* Zone check specially for UTM */
  261. /* -------------------------------------------------------------------- */
  262. if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
  263. && atof(G_find_key_value("zone", proj_info1))
  264. != atof(G_find_key_value("zone", proj_info2)))
  265. return -5;
  266. /* -------------------------------------------------------------------- */
  267. /* Hemisphere check specially for UTM */
  268. /* -------------------------------------------------------------------- */
  269. if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
  270. && !!G_find_key_value("south", proj_info1)
  271. != !!G_find_key_value("south", proj_info2))
  272. return -6;
  273. /* -------------------------------------------------------------------- */
  274. /* Do they both have the same false easting? */
  275. /* -------------------------------------------------------------------- */
  276. {
  277. const char *x_0_1 = NULL, *x_0_2 = NULL;
  278. x_0_1 = G_find_key_value("x_0", proj_info1);
  279. x_0_2 = G_find_key_value("x_0", proj_info2);
  280. if ((x_0_1 && !x_0_2) || (!x_0_1 && x_0_2))
  281. return -7;
  282. if (x_0_1 && x_0_2 && (fabs(atof(x_0_1) - atof(x_0_2)) > 0.000001))
  283. return -7;
  284. }
  285. /* -------------------------------------------------------------------- */
  286. /* Do they both have the same false northing? */
  287. /* -------------------------------------------------------------------- */
  288. {
  289. const char *y_0_1 = NULL, *y_0_2 = NULL;
  290. y_0_1 = G_find_key_value("y_0", proj_info1);
  291. y_0_2 = G_find_key_value("y_0", proj_info2);
  292. if ((y_0_1 && !y_0_2) || (!y_0_1 && y_0_2))
  293. return -8;
  294. if (y_0_1 && y_0_2 && (fabs(atof(y_0_1) - atof(y_0_2)) > 0.000001))
  295. return -8;
  296. }
  297. /* -------------------------------------------------------------------- */
  298. /* Do they have the same center longitude? */
  299. /* -------------------------------------------------------------------- */
  300. {
  301. const char *l_1 = NULL, *l_2 = NULL;
  302. l_1 = G_find_key_value("lon_0", proj_info1);
  303. l_2 = G_find_key_value("lon_0", proj_info2);
  304. if ((l_1 && !l_2) || (!l_1 && l_2))
  305. return -9;
  306. if (l_1 && l_2 && (fabs(atof(l_1) - atof(l_2)) > 0.000001))
  307. return -9;
  308. /* -------------------------------------------------------------------- */
  309. /* Do they have the same center latitude? */
  310. /* -------------------------------------------------------------------- */
  311. l_1 = l_2 = NULL;
  312. l_1 = G_find_key_value("lat_0", proj_info1);
  313. l_2 = G_find_key_value("lat_0", proj_info2);
  314. if ((l_1 && !l_2) || (!l_1 && l_2))
  315. return -10;
  316. if (l_1 && l_2 && (fabs(atof(l_1) - atof(l_2)) > 0.000001))
  317. return -10;
  318. /* -------------------------------------------------------------------- */
  319. /* Do they have the same standard parallels? */
  320. /* -------------------------------------------------------------------- */
  321. l_1 = l_2 = NULL;
  322. l_1 = G_find_key_value("lat_1", proj_info1);
  323. l_2 = G_find_key_value("lat_1", proj_info2);
  324. if ((l_1 && !l_2) || (!l_1 && l_2))
  325. return -11;
  326. if (l_1 && l_2 && (fabs(atof(l_1) - atof(l_2)) > 0.000001)) {
  327. /* lat_1 differ */
  328. /* check for swapped lat_1, lat_2 */
  329. l_2 = G_find_key_value("lat_2", proj_info2);
  330. if (!l_2)
  331. return -11;
  332. if (l_1 && l_2 && (fabs(atof(l_1) - atof(l_2)) > 0.000001)) {
  333. return -11;
  334. }
  335. }
  336. l_1 = l_2 = NULL;
  337. l_1 = G_find_key_value("lat_2", proj_info1);
  338. l_2 = G_find_key_value("lat_2", proj_info2);
  339. if ((l_1 && !l_2) || (!l_1 && l_2))
  340. return -11;
  341. if (l_1 && l_2 && (fabs(atof(l_1) - atof(l_2)) > 0.000001)) {
  342. /* lat_2 differ */
  343. /* check for swapped lat_1, lat_2 */
  344. l_2 = G_find_key_value("lat_1", proj_info2);
  345. if (!l_2)
  346. return -11;
  347. if (l_1 && l_2 && (fabs(atof(l_1) - atof(l_2)) > 0.000001)) {
  348. return -11;
  349. }
  350. }
  351. }
  352. /* towgs84 ? */
  353. /* -------------------------------------------------------------------- */
  354. /* Add more details in later. */
  355. /* -------------------------------------------------------------------- */
  356. return 1;
  357. }