make_mapset.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /******************************************************************************
  2. *
  3. * Project: libgrass
  4. * Purpose: Function to create a new mapset within an existing location
  5. * Author(s): Joel Pitt, joel.pitt@gmail.com
  6. *
  7. ******************************************************************************
  8. * Copyright (c) 2006, Joel Pitt
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Library General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Library General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Library General Public
  21. * License along with this library; if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. ******************************************************************************
  24. *
  25. */
  26. #include <grass/gis.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/stat.h>
  31. /*
  32. * Returns 0 on success.
  33. * Returns -1 to indicate a system error (check errno).
  34. */
  35. int G__make_mapset(const char *gisdbase_name, const char *location_name,
  36. const char *mapset_name)
  37. {
  38. char path[GPATH_MAX];
  39. struct Cell_head default_window;
  40. /* Get location */
  41. if (location_name == NULL)
  42. location_name = G_location();
  43. /* Get GISDBASE */
  44. if (gisdbase_name == NULL)
  45. gisdbase_name = G_gisdbase();
  46. /* TODO: Should probably check that user specified location and gisdbase are valid */
  47. /* Make the mapset. */
  48. sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
  49. if (G_mkdir(path) != 0)
  50. return -1;
  51. G__create_alt_env();
  52. /* Get PERMANENT default window */
  53. G__setenv("GISDBASE", gisdbase_name);
  54. G__setenv("LOCATION", location_name);
  55. G__setenv("MAPSET", "PERMANENT");
  56. G_get_default_window(&default_window);
  57. /* Change to the new mapset */
  58. G__setenv("MAPSET", mapset_name);
  59. /* Copy default window/regions to new mapset */
  60. G__put_window(&default_window, "", "WIND");
  61. /* And switch back to original environment */
  62. G__switch_env();
  63. return 0;
  64. }
  65. /*!
  66. * \brief create a new mapset
  67. *
  68. * This function creates a new mapset in the current location,
  69. * initializes default window and current window.
  70. *
  71. * \param gisdbase_name
  72. * The full path of GISDBASE to create mapset in.
  73. * If NULL then current GISDBASE is used.
  74. * \param location_name
  75. * The name location to create mapset in.
  76. * If NULL then current location is used.
  77. * \param mapset_name
  78. * The name of the new mapset. Should not include
  79. * the full path, the mapset will be created within
  80. * the current database and location.
  81. *
  82. * \return Returns 0 on success, or generates a fatal error on failure.
  83. * The G__make_mapset() function operates the same, but returns a
  84. * non-zero error code on failure, instead of terminating.
  85. */
  86. int G_make_mapset(const char *gisdbase_name, const char *location_name,
  87. const char *mapset_name)
  88. {
  89. int err;
  90. err = G__make_mapset(gisdbase_name, location_name, mapset_name);
  91. if (err == 0)
  92. return 0;
  93. if (err == -1) {
  94. perror("G_make_mapset");
  95. }
  96. G_fatal_error("G_make_mapset failed.");
  97. return 1;
  98. }