GRASS Programmer's Manual
6.4.2(2012)
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 /* 00038 * Returns 0 on success. 00039 * Returns -1 to indicate a system error (check errno). 00040 */ 00041 00042 00043 int G__make_location(const char *location_name, 00044 struct Cell_head *wind, 00045 struct Key_Value *proj_info, 00046 struct Key_Value *proj_units, FILE * report_file) 00047 { 00048 char path[GPATH_MAX]; 00049 int out_stat; 00050 00051 /* Try to create the location directory, under the gisdbase. */ 00052 sprintf(path, "%s/%s", G_gisdbase(), location_name); 00053 if (G_mkdir(path) != 0) 00054 return -1; 00055 00056 /* Make the PERMANENT mapset. */ 00057 sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT"); 00058 if (G_mkdir(path) != 0) 00059 return -1; 00060 00061 /* make these the new current location and mapset */ 00062 G__setenv("LOCATION_NAME", location_name); 00063 G__setenv("MAPSET", "PERMANENT"); 00064 00065 /* Create the default, and current window files */ 00066 G__put_window(wind, "", "DEFAULT_WIND"); 00067 G__put_window(wind, "", "WIND"); 00068 00069 /* Write out the PROJ_INFO, and PROJ_UNITS if available. */ 00070 if (proj_info != NULL) { 00071 G__file_name(path, "", "PROJ_INFO", "PERMANENT"); 00072 G_write_key_value_file(path, proj_info, &out_stat); 00073 if (out_stat != 0) 00074 return -2; 00075 } 00076 00077 if (proj_units != NULL) { 00078 G__file_name(path, "", "PROJ_UNITS", "PERMANENT"); 00079 G_write_key_value_file(path, proj_units, &out_stat); 00080 if (out_stat != 0) 00081 return -2; 00082 } 00083 00084 return 0; 00085 } 00086 00087 00122 int G_make_location(const char *location_name, 00123 struct Cell_head *wind, 00124 struct Key_Value *proj_info, 00125 struct Key_Value *proj_units, FILE * report_file) 00126 { 00127 int err; 00128 00129 err = G__make_location(location_name, wind, proj_info, proj_units, 00130 report_file); 00131 00132 if (err == 0) 00133 return 0; 00134 00135 if (err == -1) { 00136 perror("G_make_location"); 00137 } 00138 00139 G_fatal_error("G_make_location failed."); 00140 00141 return 1; 00142 } 00143 00144 00145 /************************************************************************/ 00146 /* G_compare_projections() */ 00147 00148 /************************************************************************/ 00149 00163 int 00164 G_compare_projections(const struct Key_Value *proj_info1, 00165 const struct Key_Value *proj_units1, 00166 const struct Key_Value *proj_info2, 00167 const struct Key_Value *proj_units2) 00168 { 00169 const char *proj1, *proj2; 00170 00171 if (proj_info1 == NULL && proj_info2 == NULL) 00172 return TRUE; 00173 00174 /* -------------------------------------------------------------------- */ 00175 /* Are they both in the same projection? */ 00176 /* -------------------------------------------------------------------- */ 00177 /* prevent seg fault in G_find_key_value */ 00178 if (proj_info1 == NULL || proj_info2 == NULL) 00179 return -1; 00180 00181 proj1 = G_find_key_value("proj", proj_info1); 00182 proj2 = G_find_key_value("proj", proj_info2); 00183 00184 if (proj1 == NULL || proj2 == NULL || strcmp(proj1, proj2)) 00185 return -1; 00186 00187 /* -------------------------------------------------------------------- */ 00188 /* Verify that the linear unit translation to meters is OK. */ 00189 /* -------------------------------------------------------------------- */ 00190 /* prevent seg fault in G_find_key_value */ 00191 if (proj_units1 == NULL && proj_units2 == NULL) 00192 return TRUE; 00193 00194 if (proj_units1 == NULL || proj_units2 == NULL) 00195 return -2; 00196 00197 { 00198 double a1 = 0, a2 = 0; 00199 00200 if (G_find_key_value("meters", proj_units1) != NULL) 00201 a1 = atof(G_find_key_value("meters", proj_units1)); 00202 if (G_find_key_value("meters", proj_units2) != NULL) 00203 a2 = atof(G_find_key_value("meters", proj_units2)); 00204 00205 if (a1 && a2 && (fabs(a2 - a1) > 0.000001)) 00206 return -2; 00207 } 00208 00209 /* -------------------------------------------------------------------- */ 00210 /* Do they both have the same ellipsoid? */ 00211 /* Lets just check the semi-major axis for now to keep it simple */ 00212 /* -------------------------------------------------------------------- */ 00213 00214 { 00215 double a1 = 0, a2 = 0; 00216 00217 if (G_find_key_value("a", proj_info1) != NULL) 00218 a1 = atof(G_find_key_value("a", proj_info1)); 00219 if (G_find_key_value("a", proj_info2) != NULL) 00220 a2 = atof(G_find_key_value("a", proj_info2)); 00221 00222 if (a1 && a2 && (fabs(a2 - a1) > 0.000001)) 00223 return -4; 00224 } 00225 00226 /* -------------------------------------------------------------------- */ 00227 /* Zone check specially for UTM */ 00228 /* -------------------------------------------------------------------- */ 00229 if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm") 00230 && atof(G_find_key_value("zone", proj_info1)) 00231 != atof(G_find_key_value("zone", proj_info2))) 00232 return -5; 00233 00234 /* -------------------------------------------------------------------- */ 00235 /* Do they both have the same false easting? */ 00236 /* -------------------------------------------------------------------- */ 00237 00238 { 00239 char *x_0_1 = NULL, *x_0_2 = NULL; 00240 00241 x_0_1 = G_find_key_value("x_0", proj_info1); 00242 x_0_2 = G_find_key_value("x_0", proj_info2); 00243 00244 if (x_0_1 && x_0_2 && (fabs(atof(x_0_1) - atof(x_0_2)) > 0.000001)) 00245 return -6; 00246 } 00247 00248 /* -------------------------------------------------------------------- */ 00249 /* Do they both have the same false northing? */ 00250 /* -------------------------------------------------------------------- */ 00251 00252 { 00253 char *y_0_1 = NULL, *y_0_2 = NULL; 00254 00255 y_0_1 = G_find_key_value("y_0", proj_info1); 00256 y_0_2 = G_find_key_value("y_0", proj_info2); 00257 00258 if (y_0_1 && y_0_2 && (fabs(atof(y_0_1) - atof(y_0_2)) > 0.000001)) 00259 return -7; 00260 } 00261 00262 /* -------------------------------------------------------------------- */ 00263 /* Add more details in later. */ 00264 /* -------------------------------------------------------------------- */ 00265 00266 return TRUE; 00267 }