make_mapset.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*!
  2. * \file lib/gis/make_mapset.c
  3. *
  4. * \brief GIS Library - Functions to create a new mapset within an
  5. * existing location
  6. *
  7. * (C) 2006-2013 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public License
  10. * (>=v2). Read the file COPYING that comes with GRASS for details.
  11. *
  12. * \author Joel Pitt, joel.pitt@gmail.com
  13. */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/stat.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. /*!
  21. * \brief Create a new mapset
  22. *
  23. * This function creates a new mapset in the given location,
  24. * initializes default window and the current window.
  25. *
  26. * \param gisdbase_name full path of GISDBASE to create mapset in
  27. * (NULL for the current GISDBASE)
  28. * \param location_name name of location to create mapset in
  29. * (NULL for the current location)
  30. * \param mapset_name Name of the new mapset. Should not include
  31. * the full path, the mapset will be created within
  32. * the specified database and location.
  33. *
  34. * \return 0 on success
  35. * \return -1 to indicate a system error (check errno).
  36. * \return -2 illegal name
  37. */
  38. int G_make_mapset(const char *gisdbase_name, const char *location_name,
  39. const char *mapset_name)
  40. {
  41. char path[GPATH_MAX];
  42. struct Cell_head default_window;
  43. /* Get location */
  44. if (location_name == NULL)
  45. location_name = G_location();
  46. /* Get GISDBASE */
  47. if (gisdbase_name == NULL)
  48. gisdbase_name = G_gisdbase();
  49. /* TODO: Should probably check that user specified location and gisdbase are valid */
  50. /* check if mapset name is legal */
  51. if (G_legal_filename(mapset_name) != 1)
  52. return -2;
  53. /* Make the mapset. */
  54. sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
  55. if (G_mkdir(path) != 0) {
  56. perror("G_make_mapset");
  57. return -1;
  58. }
  59. G__create_alt_env();
  60. /* Get PERMANENT default window */
  61. G__setenv("GISDBASE", gisdbase_name);
  62. G__setenv("LOCATION", location_name);
  63. G__setenv("MAPSET", "PERMANENT");
  64. G_get_default_window(&default_window);
  65. /* Change to the new mapset */
  66. G__setenv("MAPSET", mapset_name);
  67. /* Copy default window/regions to new mapset */
  68. G__put_window(&default_window, "", "WIND");
  69. /* And switch back to original environment */
  70. G__switch_env();
  71. return 0;
  72. }