GRASS Programmer's Manual 6.4.1(2011)
make_loc.c
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  *
00004  * Project:  libgrass
00005  * Purpose:  Function to create a new location automatically given a 
00006  *           "Cell_head", PROJ_INFO and PROJ_UNITS information.
00007  * Author:   Frank Warmerdam, warmerda@pobox.com
00008  *
00009  ******************************************************************************
00010  * Copyright (c) 2000, Frank Warmerdam
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Library General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Library General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Library General Public
00023  * License along with this library; if not, write to the
00024  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00025  * Boston, MA 02111-1307, USA.
00026  ******************************************************************************
00027  *
00028  */
00029 
00030 #include <grass/gis.h>
00031 
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include <sys/stat.h>
00036 #include <math.h>
00037 
00038 /*
00039  * Returns 0 on success.
00040  * Returns -1 to indicate a system error (check errno).
00041  */
00042 
00043 
00044 int G__make_location(const char *location_name,
00045                      struct Cell_head *wind,
00046                      struct Key_Value *proj_info,
00047                      struct Key_Value *proj_units, FILE * report_file)
00048 {
00049     char path[GPATH_MAX];
00050     int out_stat;
00051 
00052     /* Try to create the location directory, under the gisdbase. */
00053     sprintf(path, "%s/%s", G_gisdbase(), location_name);
00054     if (G_mkdir(path) != 0)
00055         return -1;
00056 
00057     /* Make the PERMANENT mapset. */
00058     sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT");
00059     if (G_mkdir(path) != 0)
00060         return -1;
00061 
00062     /* make these the new current location and mapset */
00063     G__setenv("LOCATION_NAME", location_name);
00064     G__setenv("MAPSET", "PERMANENT");
00065 
00066     /* Create the default, and current window files */
00067     G__put_window(wind, "", "DEFAULT_WIND");
00068     G__put_window(wind, "", "WIND");
00069 
00070     /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
00071     if (proj_info != NULL) {
00072         G__file_name(path, "", "PROJ_INFO", "PERMANENT");
00073         G_write_key_value_file(path, proj_info, &out_stat);
00074         if (out_stat != 0)
00075             return -2;
00076     }
00077 
00078     if (proj_units != NULL) {
00079         G__file_name(path, "", "PROJ_UNITS", "PERMANENT");
00080         G_write_key_value_file(path, proj_units, &out_stat);
00081         if (out_stat != 0)
00082             return -2;
00083     }
00084 
00085     return 0;
00086 }
00087 
00088 
00123 int G_make_location(const char *location_name,
00124                     struct Cell_head *wind,
00125                     struct Key_Value *proj_info,
00126                     struct Key_Value *proj_units, FILE * report_file)
00127 {
00128     int err;
00129 
00130     err = G__make_location(location_name, wind, proj_info, proj_units,
00131                            report_file);
00132 
00133     if (err == 0)
00134         return 0;
00135 
00136     if (err == -1) {
00137         perror("G_make_location");
00138     }
00139 
00140     G_fatal_error("G_make_location failed.");
00141 
00142     return 1;
00143 }
00144 
00145 
00146 /************************************************************************/
00147 /*                       G_compare_projections()                        */
00148 
00149 /************************************************************************/
00150 
00164 int
00165 G_compare_projections(const struct Key_Value *proj_info1,
00166                       const struct Key_Value *proj_units1,
00167                       const struct Key_Value *proj_info2,
00168                       const struct Key_Value *proj_units2)
00169 {
00170     const char *proj1, *proj2;
00171 
00172     if (proj_info1 == NULL && proj_info2 == NULL)
00173         return TRUE;
00174 
00175     /* -------------------------------------------------------------------- */
00176     /*      Are they both in the same projection?                           */
00177     /* -------------------------------------------------------------------- */
00178     /* prevent seg fault in G_find_key_value */
00179     if (proj_info1 == NULL || proj_info2 == NULL)
00180         return -1;
00181 
00182     proj1 = G_find_key_value("proj", proj_info1);
00183     proj2 = G_find_key_value("proj", proj_info2);
00184 
00185     if (proj1 == NULL || proj2 == NULL || strcmp(proj1, proj2))
00186         return -1;
00187 
00188     /* -------------------------------------------------------------------- */
00189     /*      Verify that the linear unit translation to meters is OK.        */
00190     /* -------------------------------------------------------------------- */
00191     /* prevent seg fault in G_find_key_value */
00192     if (proj_units1 == NULL && proj_units2 == NULL)
00193         return TRUE;
00194 
00195     if (proj_units1 == NULL || proj_units2 == NULL)
00196         return -2;
00197 
00198     {
00199         double a1 = 0, a2 = 0;
00200 
00201         if (G_find_key_value("meters", proj_units1) != NULL)
00202             a1 = atof(G_find_key_value("meters", proj_units1));
00203         if (G_find_key_value("meters", proj_units2) != NULL)
00204             a2 = atof(G_find_key_value("meters", proj_units2));
00205 
00206         if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
00207             return -2;
00208     }
00209 
00210     /* -------------------------------------------------------------------- */
00211     /*      Do they both have the same ellipsoid?                           */
00212     /*      Lets just check the semi-major axis for now to keep it simple   */
00213     /* -------------------------------------------------------------------- */
00214 
00215     {
00216         double a1 = 0, a2 = 0;
00217 
00218         if (G_find_key_value("a", proj_info1) != NULL)
00219             a1 = atof(G_find_key_value("a", proj_info1));
00220         if (G_find_key_value("a", proj_info2) != NULL)
00221             a2 = atof(G_find_key_value("a", proj_info2));
00222 
00223         if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
00224             return -4;
00225     }
00226 
00227     /* -------------------------------------------------------------------- */
00228     /*      Zone check specially for UTM                                    */
00229     /* -------------------------------------------------------------------- */
00230     if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
00231         && atof(G_find_key_value("zone", proj_info1))
00232         != atof(G_find_key_value("zone", proj_info2)))
00233         return -5;
00234 
00235     /* -------------------------------------------------------------------- */
00236     /*      Do they both have the same false easting?                       */
00237     /* -------------------------------------------------------------------- */
00238 
00239     {
00240         char *x_0_1 = NULL, *x_0_2 = NULL;
00241 
00242         x_0_1 = G_find_key_value("x_0", proj_info1);
00243         x_0_2 = G_find_key_value("x_0", proj_info2);
00244 
00245         if (x_0_1 && x_0_2 && (fabs(atof(x_0_1) - atof(x_0_2)) > 0.000001))
00246             return -6;
00247     }
00248 
00249     /* -------------------------------------------------------------------- */
00250     /*      Do they both have the same false northing?                       */
00251     /* -------------------------------------------------------------------- */
00252 
00253     {
00254         char *y_0_1 = NULL, *y_0_2 = NULL;
00255 
00256         y_0_1 = G_find_key_value("y_0", proj_info1);
00257         y_0_2 = G_find_key_value("y_0", proj_info2);
00258 
00259         if (y_0_1 && y_0_2 && (fabs(atof(y_0_1) - atof(y_0_2)) > 0.000001))
00260             return -7;
00261     }
00262 
00263     /* -------------------------------------------------------------------- */
00264     /*      Add more details in later.                                      */
00265     /* -------------------------------------------------------------------- */
00266 
00267     return TRUE;
00268 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines