GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
make_mapset.c
Go to the documentation of this file.
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
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
23
/*!
24
* \brief Create a new mapset
25
*
26
* This function creates a new mapset in the given location,
27
* initializes default window and the current window.
28
*
29
* Calls G_fatal_error() if location doesn't exist.
30
*
31
* \param gisdbase_name full path of GISDBASE to create mapset in
32
* (NULL for the current GISDBASE)
33
* \param location_name name of location to create mapset in
34
* (NULL for the current location)
35
* \param mapset_name Name of the new mapset. Should not include
36
* the full path, the mapset will be created within
37
* the specified database and location.
38
*
39
* \return 0 on success
40
* \return -1 to indicate a system error (check errno).
41
* \return -2 illegal name
42
*/
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
58
* valid */
59
60
/* check if mapset name is legal */
61
if
(
G_legal_filename
(mapset_name) != 1)
62
return
-2;
63
64
/* Check if location exists */
65
snprintf(
path
,
sizeof
(
path
),
"%s/%s"
, gisdbase_name, location_name);
66
if
(access(
path
, F_OK) == -1)
67
G_fatal_error
(_(
"Location <%s> doesn't exist"
), location_name);
68
69
/* Make the mapset */
70
snprintf(
path
,
sizeof
(
path
),
"%s/%s/%s"
, gisdbase_name, location_name,
71
mapset_name);
72
if
(
G_mkdir
(
path
) != 0) {
73
perror(
"G_make_mapset"
);
74
return
-1;
75
}
76
G_create_alt_env
();
77
78
/* Get PERMANENT default window */
79
G_setenv_nogisrc
(
"GISDBASE"
, gisdbase_name);
80
G_setenv_nogisrc
(
"LOCATION_NAME"
, location_name);
81
G_setenv_nogisrc
(
"MAPSET"
,
"PERMANENT"
);
82
G_get_default_window
(&default_window);
83
84
/* Change to the new mapset */
85
G_setenv_nogisrc
(
"MAPSET"
, mapset_name);
86
87
/* Copy default window/regions to new mapset */
88
G_put_element_window
(&default_window,
""
,
"WIND"
);
89
90
/* And switch back to original environment */
91
G_switch_env
();
92
93
return
0;
94
}
NULL
#define NULL
Definition
ccmath.h:32
G_switch_env
void G_switch_env(void)
Switch environments.
Definition
env.c:591
G_setenv_nogisrc
void G_setenv_nogisrc(const char *name, const char *value)
Set environment name to value (doesn't update .gisrc).
Definition
env.c:472
G_create_alt_env
void G_create_alt_env(void)
Set up alternative environment variables.
Definition
env.c:569
G_get_default_window
void G_get_default_window(struct Cell_head *window)
Get the default region.
Definition
get_window.c:99
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition
gis/error.c:159
G_gisdbase
const char * G_gisdbase(void)
Get name of top level database directory.
Definition
gisdbase.c:26
G_legal_filename
int G_legal_filename(const char *s)
Check for legal database file name.
Definition
legal_name.c:34
G_location
const char * G_location(void)
Get current location name.
Definition
location.c:32
G_make_mapset
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
G_mkdir
int G_mkdir(const char *path)
Creates a new directory.
Definition
paths.c:27
G_put_element_window
int G_put_element_window(const struct Cell_head *window, const char *dir, const char *name)
Write the region.
Definition
put_window.c:74
path
Definition
path.h:15
gis
make_mapset.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0