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 "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_location( 
00043     char *location_name,
00044     struct Cell_head *wind, 
00045     struct Key_Value *proj_info, 
00046     struct Key_Value *proj_units,
00047     FILE *report_file )
00048 
00049 {
00050     char        path[2048];
00051     int         out_stat;
00052 
00053     /* Try to create the location directory, under the gisdbase. */
00054     sprintf( path, "%s/%s", G_gisdbase(), location_name );
00055     if( mkdir( path, 0775 ) != 0 )
00056         return -1;
00057 
00058     /* Make the PERMANENT mapset. */
00059     sprintf( path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT" );
00060     if( mkdir( path, 0775 ) != 0 )
00061         return -1;
00062 
00063     /* make these the new current location and mapset */
00064     G__setenv( "LOCATION_NAME", location_name );
00065     G__setenv( "MAPSET", "PERMANENT" );
00066 
00067     /* Create the default, and current window files */
00068     G__put_window( wind, "", "DEFAULT_WIND" );
00069     G__put_window( wind, "", "WIND" );
00070 
00071     /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
00072     if( proj_info != NULL )
00073     {
00074         G__file_name( path, "", "PROJ_INFO", "PERMANENT" );
00075         G_write_key_value_file( path, proj_info, &out_stat );
00076         if( out_stat != 0 )
00077             return -2;
00078     }
00079 
00080     if( proj_units != NULL )
00081     {
00082         G__file_name( path, "", "PROJ_UNITS", "PERMANENT" );
00083         G_write_key_value_file( path, proj_units, &out_stat );
00084         if( out_stat != 0 )
00085             return -2;
00086     }
00087 
00088     return 0;
00089 }
00090 
00091 int G_make_location( 
00092     char *location_name,
00093     struct Cell_head *wind, 
00094     struct Key_Value *proj_info, 
00095     struct Key_Value *proj_units,
00096     FILE *report_file )
00097 
00098 {
00099     int err;
00100 
00101     err = G__make_location( location_name, wind, proj_info, proj_units, 
00102                             report_file );
00103 
00104     if( err == 0 )
00105         return 0;
00106 
00107     if( err == -1 )
00108     {
00109         perror( "G_make_location" );
00110     }
00111 
00112     G_fatal_error( "G_make_location failed." );
00113     
00114     return 1;
00115 }
00116 
00117 
00118 /************************************************************************/
00119 /*                       G_compare_projections()                        */
00120 /************************************************************************/
00121 
00122 int 
00123 G_compare_projections( struct Key_Value *proj_info1, 
00124                        struct Key_Value *proj_units1, 
00125                        struct Key_Value *proj_info2, 
00126                        struct Key_Value *proj_units2 )
00127 
00128 {
00129     char  buf1[512], buf2[512];
00130     
00131     if( proj_info1 == NULL && proj_info2 == NULL )
00132         return TRUE;
00133     
00134 /* -------------------------------------------------------------------- */
00135 /*      Are they both in the same projection?                           */
00136 /* -------------------------------------------------------------------- */
00137     if( G_find_key_value( "proj", proj_info1 ) != NULL
00138         && G_find_key_value( "meters", proj_units1 ) != NULL
00139         && atof(G_find_key_value( "meters", proj_units1 ))
00140            != atof(G_find_key_value( "meters", proj_units2 )) )
00141         return -1;
00142 
00143 /* -------------------------------------------------------------------- */
00144 /*      Verify that the linear unit translation to meters is OK.        */
00145 /* -------------------------------------------------------------------- */
00146     if( proj_units1 != NULL && proj_units2 != NULL
00147         && G_find_key_value( "meters", proj_units1 ) != NULL
00148         && G_find_key_value( "meters", proj_units2 ) != NULL
00149         && atof(G_find_key_value( "meters", proj_units1 ))
00150            != atof(G_find_key_value( "meters", proj_units2 )) )
00151         return -2;
00152 
00153 /* -------------------------------------------------------------------- */
00154 /*      Do they both have the same ellipsoid?                           */
00155 /*      Lets just check the semi-major axis for now to keep it simple   */
00156 /* -------------------------------------------------------------------- */
00157     
00158     {
00159         double a1=0, a2=0;
00160         if(G_find_key_value( "a", proj_info1) != NULL)
00161            a1 = atof(G_find_key_value( "a", proj_info1 ));
00162         if(G_find_key_value( "a", proj_info2) != NULL)
00163            a2 = atof(G_find_key_value( "a", proj_info2 ));
00164 
00165         if ( a1 && a2 && ( abs(a2-a1) > 0.000001 ) )
00166             return -4;
00167     }
00168 
00169 /* -------------------------------------------------------------------- */
00170 /*      Zone check specially for UTM                                    */
00171 /* -------------------------------------------------------------------- */
00172     {   
00173         if(   G_find_key_value( "proj", proj_info1 ) == "utm"
00174            && G_find_key_value( "proj", proj_info2 ) == "utm"
00175            &&    atof(G_find_key_value( "zone", proj_info1 ))
00176               != atof(G_find_key_value( "zone", proj_info2 )) )
00177            return -5;
00178     }
00179 
00180 /* -------------------------------------------------------------------- */
00181 /*      Add more details in later.                                      */
00182 /* -------------------------------------------------------------------- */
00183 
00184     return TRUE;
00185 }

Generated on Sat Jul 22 22:06:15 2006 for GRASS by  doxygen 1.4.7