create.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <errno.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. void create_location(const char *location, const char *epsg)
  7. {
  8. int ret;
  9. ret = G_make_location(location, &cellhd, projinfo, projunits);
  10. if (ret == 0)
  11. G_message(_("Location <%s> created"), location);
  12. else if (ret == -1)
  13. G_fatal_error(_("Unable to create location <%s>: %s"),
  14. location, strerror(errno));
  15. else if (ret == -2)
  16. G_fatal_error(_("Unable to create projection files: %s"),
  17. strerror(errno));
  18. else
  19. /* Shouldn't happen */
  20. G_fatal_error(_("Unable to create location <%s>"), location);
  21. /* create also PROJ_EPSG */
  22. if (epsg)
  23. create_epsg(location, epsg);
  24. G_message(_("You can switch to the new location by\n`%s=%s`"),
  25. "g.mapset mapset=PERMANENT location", location);
  26. }
  27. void modify_projinfo()
  28. {
  29. const char *mapset = G_mapset();
  30. struct Cell_head old_cellhd;
  31. if (strcmp(mapset, "PERMANENT") != 0)
  32. G_fatal_error(_("You must select the PERMANENT mapset before updating the "
  33. "current location's projection (current mapset is <%s>)"),
  34. mapset);
  35. /* Read projection information from current location first */
  36. G_get_default_window(&old_cellhd);
  37. char path[GPATH_MAX];
  38. /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
  39. if (projinfo != NULL) {
  40. G_file_name(path, "", "PROJ_INFO", "PERMANENT");
  41. G_write_key_value_file(path, projinfo);
  42. }
  43. if (projunits != NULL) {
  44. G_file_name(path, "", "PROJ_UNITS", "PERMANENT");
  45. G_write_key_value_file(path, projunits);
  46. }
  47. if ((old_cellhd.zone != cellhd.zone) ||
  48. (old_cellhd.proj != cellhd.proj)) {
  49. /* Recreate the default, and current window files if projection
  50. * number or zone have changed */
  51. G_put_element_window(&cellhd, "", "DEFAULT_WIND");
  52. G_put_element_window(&cellhd, "", "WIND");
  53. G_message(_("Default region was updated to the new projection, but if you have "
  54. "multiple mapsets `g.region -d` should be run in each to update the "
  55. "region from the default"));
  56. }
  57. G_important_message(_("Projection information updated"));
  58. }
  59. void create_epsg(const char *location, const char *epsg)
  60. {
  61. FILE *fp;
  62. char path[GPATH_MAX];
  63. /* if inputs were not clean it should of failed by now */
  64. if (location) {
  65. G_snprintf(path, sizeof(path), "%s%c%s%c%s%c%s", G_gisdbase(), HOST_DIRSEP,
  66. location, HOST_DIRSEP,
  67. "PERMANENT", HOST_DIRSEP, "PROJ_EPSG");
  68. path[sizeof(path)-1] = '\0';
  69. }
  70. else {
  71. G_file_name(path, "", "PROJ_EPSG", "PERMANENT");
  72. }
  73. fp = fopen(path, "w");
  74. if (!fp)
  75. G_fatal_error(_("Unable to create PROJ_EPSG file: %s"), strerror (errno));
  76. #ifdef HAVE_OGR
  77. fprintf(fp, "epsg: %s\n", epsg);
  78. #endif
  79. fclose(fp);
  80. }