make_mapset.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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., 59 Temple Place - Suite 330,
  23. * Boston, MA 02111-1307, 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. /*
  33. * Returns 0 on success.
  34. * Returns -1 to indicate a system error (check errno).
  35. */
  36. int G__make_mapset(const char *gisdbase_name, const char *location_name,
  37. const char *mapset_name)
  38. {
  39. char path[GPATH_MAX];
  40. struct Cell_head default_window;
  41. /* Get location */
  42. if (location_name == NULL)
  43. location_name = G_location();
  44. /* Get GISDBASE */
  45. if (gisdbase_name == NULL)
  46. gisdbase_name = G_gisdbase();
  47. /* TODO: Should probably check that user specified location and gisdbase are valid */
  48. /* Make the mapset. */
  49. sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
  50. if (G_mkdir(path) != 0)
  51. return -1;
  52. G__create_alt_env();
  53. /* Get PERMANENT default window */
  54. G__setenv("GISDBASE", gisdbase_name);
  55. G__setenv("LOCATION", location_name);
  56. G__setenv("MAPSET", "PERMANENT");
  57. G_get_default_window(&default_window);
  58. /* Change to the new mapset */
  59. G__setenv("MAPSET", mapset_name);
  60. /* Copy default window/regions to new mapset */
  61. G__put_window(&default_window, "", "WIND");
  62. /* And switch back to original environment */
  63. G__switch_env();
  64. return 0;
  65. }
  66. /*!
  67. * \brief create a new mapset
  68. *
  69. * This function creates a new mapset in the current location,
  70. * initializes default window and current window.
  71. *
  72. * \param char * gisdbase_name
  73. * The full path of GISDBASE to create mapset in.
  74. * If NULL then current GISDBASE is used.
  75. * \param char * location_name
  76. * The name location to create mapset in.
  77. * If NULL then current location is used.
  78. * \param char * mapset_name
  79. * The name of the new mapset. Should not include
  80. * the full path, the mapset will be created within
  81. * the current database and location.
  82. *
  83. * \return Returns 0 on success, or generates a fatal error on failure.
  84. * The G__make_mapset() function operates the same, but returns a
  85. * non-zero error code on failure, instead of terminating.
  86. */
  87. int G_make_mapset(const char *gisdbase_name, const char *location_name,
  88. const char *mapset_name)
  89. {
  90. int err;
  91. err = G__make_mapset(gisdbase_name, location_name, mapset_name);
  92. if (err == 0)
  93. return 0;
  94. if (err == -1) {
  95. perror("G_make_mapset");
  96. }
  97. G_fatal_error("G_make_mapset failed.");
  98. return 1;
  99. }