make_loc.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  *
00003  * Project:  libgrass
00004  * Purpose:  Function to create a new location automatically given a 
00005  *           "Cell_head", PROJ_INFO and PROJ_UNITS information.
00006  * Author:   Frank Warmerdam, warmerda@pobox.com
00007  *
00008  ******************************************************************************
00009  * Copyright (c) 2000, Frank Warmerdam
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 #include <math.h>
00036 
00037 #ifdef __MINGW32__
00038 # define mkdir(name, mode) ((mkdir) (name))
00039 #endif
00040 
00041 /*
00042  * Returns 0 on success.
00043  * Returns -1 to indicate a system error (check errno).
00044  */
00045  
00046 
00047 int G__make_location( 
00048     char *location_name,
00049     struct Cell_head *wind, 
00050     struct Key_Value *proj_info, 
00051     struct Key_Value *proj_units,
00052     FILE *report_file )
00053 
00054 {
00055     char        path[2048];
00056     int         out_stat;
00057 
00058     /* Try to create the location directory, under the gisdbase. */
00059     sprintf( path, "%s/%s", G_gisdbase(), location_name );
00060     if( mkdir( path, 0775 ) != 0 )
00061         return -1;
00062 
00063     /* Make the PERMANENT mapset. */
00064     sprintf( path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT" );
00065     if( mkdir( path, 0775 ) != 0 )
00066         return -1;
00067 
00068     /* make these the new current location and mapset */
00069     G__setenv( "LOCATION_NAME", location_name );
00070     G__setenv( "MAPSET", "PERMANENT" );
00071 
00072     /* Create the default, and current window files */
00073     G__put_window( wind, "", "DEFAULT_WIND" );
00074     G__put_window( wind, "", "WIND" );
00075 
00076     /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
00077     if( proj_info != NULL )
00078     {
00079         G__file_name( path, "", "PROJ_INFO", "PERMANENT" );
00080         G_write_key_value_file( path, proj_info, &out_stat );
00081         if( out_stat != 0 )
00082             return -2;
00083     }
00084 
00085     if( proj_units != NULL )
00086     {
00087         G__file_name( path, "", "PROJ_UNITS", "PERMANENT" );
00088         G_write_key_value_file( path, proj_units, &out_stat );
00089         if( out_stat != 0 )
00090             return -2;
00091     }
00092 
00093     return 0;
00094 }
00095 
00096 
00131 int G_make_location( 
00132     char *location_name,
00133     struct Cell_head *wind, 
00134     struct Key_Value *proj_info, 
00135     struct Key_Value *proj_units,
00136     FILE *report_file )
00137 
00138 {
00139     int err;
00140 
00141     err = G__make_location( location_name, wind, proj_info, proj_units, 
00142                             report_file );
00143 
00144     if( err == 0 )
00145         return 0;
00146 
00147     if( err == -1 )
00148     {
00149         perror( "G_make_location" );
00150     }
00151 
00152     G_fatal_error( "G_make_location failed." );
00153     
00154     return 1;
00155 }
00156 
00157 
00158 /************************************************************************/
00159 /*                       G_compare_projections()                        */
00160 /************************************************************************/
00161 
00175 int 
00176 G_compare_projections( struct Key_Value *proj_info1, 
00177                        struct Key_Value *proj_units1, 
00178                        struct Key_Value *proj_info2, 
00179                        struct Key_Value *proj_units2 )
00180 
00181 {
00182     char *proj1, *proj2;
00183     
00184     if( proj_info1 == NULL && proj_info2 == NULL )
00185         return TRUE;
00186 
00187 /* -------------------------------------------------------------------- */
00188 /*      Are they both in the same projection?                           */
00189 /* -------------------------------------------------------------------- */
00190     /* prevent seg fault in G_find_key_value */
00191     if( proj_info1 == NULL || proj_info2 == NULL)
00192         return -1;
00193 
00194     proj1 = G_find_key_value("proj", proj_info1);
00195     proj2 = G_find_key_value("proj", proj_info2);
00196 
00197     if( proj1 == NULL || proj2 == NULL || strcmp(proj1, proj2) )
00198         return -1;
00199 
00200 /* -------------------------------------------------------------------- */
00201 /*      Verify that the linear unit translation to meters is OK.        */
00202 /* -------------------------------------------------------------------- */
00203     /* prevent seg fault in G_find_key_value */
00204     if( proj_units1 == NULL && proj_units2 == NULL )
00205         return TRUE;
00206 
00207     if( proj_units1 == NULL || proj_units2 == NULL)
00208         return -2;
00209 
00210     {
00211         double a1=0, a2=0;
00212         if(G_find_key_value( "meters", proj_units1) != NULL)
00213            a1 = atof(G_find_key_value( "meters", proj_units1 ));
00214         if(G_find_key_value( "meters", proj_units2) != NULL)
00215            a2 = atof(G_find_key_value( "meters", proj_units2 ));
00216 
00217         if ( a1 && a2 && ( fabs(a2-a1) > 0.000001 ) )
00218             return -2;
00219     }
00220 
00221 /* -------------------------------------------------------------------- */
00222 /*      Do they both have the same ellipsoid?                           */
00223 /*      Lets just check the semi-major axis for now to keep it simple   */
00224 /* -------------------------------------------------------------------- */
00225     
00226     {
00227         double a1=0, a2=0;
00228         if(G_find_key_value( "a", proj_info1) != NULL)
00229            a1 = atof(G_find_key_value( "a", proj_info1 ));
00230         if(G_find_key_value( "a", proj_info2) != NULL)
00231            a2 = atof(G_find_key_value( "a", proj_info2 ));
00232 
00233         if ( a1 && a2 && ( fabs(a2-a1) > 0.000001 ) )
00234             return -4;
00235     }
00236 
00237 /* -------------------------------------------------------------------- */
00238 /*      Zone check specially for UTM                                    */
00239 /* -------------------------------------------------------------------- */
00240     if( !strcmp(proj1, "utm") && !strcmp(proj2, "utm")
00241         && atof(G_find_key_value( "zone", proj_info1 ))
00242            != atof(G_find_key_value( "zone", proj_info2 )) )
00243         return -5;
00244 
00245 /* -------------------------------------------------------------------- */
00246 /*      Do they both have the same false easting?                       */
00247 /* -------------------------------------------------------------------- */
00248     
00249     {
00250         char *x_0_1 = NULL, *x_0_2 = NULL;
00251        
00252         x_0_1 = G_find_key_value( "x_0", proj_info1);
00253         x_0_2 = G_find_key_value( "x_0", proj_info2);
00254 
00255         if ( x_0_1 && x_0_2 && 
00256              ( fabs(atof(x_0_1) - atof(x_0_2)) > 0.000001 ) )
00257             return -6;
00258     }
00259 
00260 /* -------------------------------------------------------------------- */
00261 /*      Do they both have the same false northing?                       */
00262 /* -------------------------------------------------------------------- */
00263     
00264     {
00265         char *y_0_1 = NULL, *y_0_2 = NULL;
00266        
00267         y_0_1 = G_find_key_value( "y_0", proj_info1);
00268         y_0_2 = G_find_key_value( "y_0", proj_info2);
00269 
00270         if ( y_0_1 && y_0_2 && 
00271              ( fabs(atof(y_0_1) - atof(y_0_2)) > 0.000001 ) )
00272             return -7;
00273     }
00274 
00275 /* -------------------------------------------------------------------- */
00276 /*      Add more details in later.                                      */
00277 /* -------------------------------------------------------------------- */
00278 
00279     return TRUE;
00280 }

Generated on Fri Nov 21 11:02:18 2008 for GRASS by  doxygen 1.5.1