GRASS Programmer's Manual  6.4.2(2012)
make_mapset.c
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  *
00004  * Project:  libgrass
00005  * Purpose:  Function to create a new mapset within an existing location
00006  * Author(s): Joel Pitt, joel.pitt@gmail.com
00007  *
00008  ******************************************************************************
00009  * Copyright (c) 2006, Joel Pitt
00010  *
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Library General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Library General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Library General Public
00022  * License along with this library; if not, write to the
00023  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00024  ******************************************************************************
00025  *
00026  */
00027 
00028 #include <grass/gis.h>
00029 
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <unistd.h>
00033 #include <sys/stat.h>
00034 
00035 /*
00036  * Returns 0 on success.
00037  * Returns -1 to indicate a system error (check errno).
00038  */
00039 
00040 
00041 int G__make_mapset(const char *gisdbase_name, const char *location_name,
00042                    const char *mapset_name)
00043 {
00044     char path[GPATH_MAX];
00045     struct Cell_head default_window;
00046 
00047     /* Get location */
00048     if (location_name == NULL)
00049         location_name = G_location();
00050 
00051     /* Get GISDBASE */
00052     if (gisdbase_name == NULL)
00053         gisdbase_name = G_gisdbase();
00054 
00055     /* TODO: Should probably check that user specified location and gisdbase are valid */
00056 
00057     /* Make the mapset. */
00058     sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
00059     if (G_mkdir(path) != 0)
00060         return -1;
00061 
00062     G__create_alt_env();
00063 
00064     /* Get PERMANENT default window */
00065     G__setenv("GISDBASE", gisdbase_name);
00066     G__setenv("LOCATION", location_name);
00067     G__setenv("MAPSET", "PERMANENT");
00068     G_get_default_window(&default_window);
00069 
00070     /* Change to the new mapset */
00071     G__setenv("MAPSET", mapset_name);
00072 
00073     /* Copy default window/regions to new mapset */
00074     G__put_window(&default_window, "", "WIND");
00075 
00076     /* And switch back to original environment */
00077     G__switch_env();
00078 
00079     return 0;
00080 }
00081 
00082 
00105 int G_make_mapset(const char *gisdbase_name, const char *location_name,
00106                   const char *mapset_name)
00107 {
00108     int err;
00109 
00110     err = G__make_mapset(gisdbase_name, location_name, mapset_name);
00111 
00112     if (err == 0)
00113         return 0;
00114 
00115     if (err == -1) {
00116         perror("G_make_mapset");
00117     }
00118 
00119     G_fatal_error("G_make_mapset failed.");
00120 
00121     return 1;
00122 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines