GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
make_mapset.c
Go to the documentation of this file.
1 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 
20 #include <grass/gis.h>
21 #include <grass/glocale.h>
22 
43 int G_make_mapset(const char *gisdbase_name, const char *location_name,
44  const char *mapset_name)
45 {
46  char path[GPATH_MAX];
47  struct Cell_head default_window;
48 
49  /* Get location */
50  if (location_name == NULL)
51  location_name = G_location();
52 
53  /* Get GISDBASE */
54  if (gisdbase_name == NULL)
55  gisdbase_name = G_gisdbase();
56 
57  /* TODO: Should probably check that user specified location and gisdbase are valid */
58 
59  /* check if mapset name is legal */
60  if (G_legal_filename(mapset_name) != 1)
61  return -2;
62 
63  /* Check if location exists */
64  sprintf(path, "%s/%s", gisdbase_name, location_name);
65  if (access(path, F_OK ) == -1)
66  G_fatal_error(_("Location <%s> doesn't exist"), location_name);
67 
68  /* Make the mapset */
69  sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
70  if (G_mkdir(path) != 0) {
71  perror("G_make_mapset");
72  return -1;
73  }
75 
76  /* Get PERMANENT default window */
77  G_setenv_nogisrc("GISDBASE", gisdbase_name);
78  G_setenv_nogisrc("LOCATION", location_name);
79  G_setenv_nogisrc("MAPSET", "PERMANENT");
80  G_get_default_window(&default_window);
81 
82  /* Change to the new mapset */
83  G_setenv_nogisrc("MAPSET", mapset_name);
84 
85  /* Copy default window/regions to new mapset */
86  G_put_element_window(&default_window, "", "WIND");
87 
88  /* And switch back to original environment */
89  G_switch_env();
90 
91  return 0;
92 }
93 
const char * G_location(void)
Get current location name.
Definition: location.c:32
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:27
#define NULL
Definition: ccmath.h:32
void G_create_alt_env(void)
Set up alternative environment variables.
Definition: env.c:544
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
void G_setenv_nogisrc(const char *name, const char *value)
Set environment name to value (doesn&#39;t update .gisrc)
Definition: env.c:448
int G_put_element_window(const struct Cell_head *window, const char *dir, const char *name)
Write the region.
Definition: put_window.c:74
void G_get_default_window(struct Cell_head *window)
Get the default region.
Definition: get_window.c:93
int G_make_mapset(const char *gisdbase_name, const char *location_name, const char *mapset_name)
Create a new mapset.
Definition: make_mapset.c:43
const char * G_gisdbase(void)
Get name of top level database directory.
Definition: gisdbase.c:26
Definition: path.h:16
void G_switch_env(void)
Switch environments.
Definition: env.c:565