• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

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., 59 Temple Place - Suite 330,
00024  * Boston, MA 02111-1307, USA.
00025  ******************************************************************************
00026  *
00027  */
00028 
00029 #include <grass/gis.h>
00030 
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <sys/stat.h>
00035 
00036 /*
00037  * Returns 0 on success.
00038  * Returns -1 to indicate a system error (check errno).
00039  */
00040 
00041 
00042 int G__make_mapset(const char *gisdbase_name, const char *location_name,
00043                    const char *mapset_name)
00044 {
00045     char path[GPATH_MAX];
00046     struct Cell_head default_window;
00047 
00048     /* Get location */
00049     if (location_name == NULL)
00050         location_name = G_location();
00051 
00052     /* Get GISDBASE */
00053     if (gisdbase_name == NULL)
00054         gisdbase_name = G_gisdbase();
00055 
00056     /* TODO: Should probably check that user specified location and gisdbase are valid */
00057 
00058     /* Make the mapset. */
00059     sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
00060     if (G_mkdir(path) != 0)
00061         return -1;
00062 
00063     G__create_alt_env();
00064 
00065     /* Get PERMANENT default window */
00066     G__setenv("GISDBASE", gisdbase_name);
00067     G__setenv("LOCATION", location_name);
00068     G__setenv("MAPSET", "PERMANENT");
00069     G_get_default_window(&default_window);
00070 
00071     /* Change to the new mapset */
00072     G__setenv("MAPSET", mapset_name);
00073 
00074     /* Copy default window/regions to new mapset */
00075     G__put_window(&default_window, "", "WIND");
00076 
00077     /* And switch back to original environment */
00078     G__switch_env();
00079 
00080     return 0;
00081 }
00082 
00083 
00106 int G_make_mapset(const char *gisdbase_name, const char *location_name,
00107                   const char *mapset_name)
00108 {
00109     int err;
00110 
00111     err = G__make_mapset(gisdbase_name, location_name, mapset_name);
00112 
00113     if (err == 0)
00114         return 0;
00115 
00116     if (err == -1) {
00117         perror("G_make_mapset");
00118     }
00119 
00120     G_fatal_error("G_make_mapset failed.");
00121 
00122     return 1;
00123 }

Generated on Wed Oct 13 2010 12:09:29 for GRASS Programmer's Manual by  doxygen 1.7.1