GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
make_loc.c
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * Project: libgrass
5  * Purpose: Function to create a new location automatically given a
6  * "Cell_head", PROJ_INFO and PROJ_UNITS information.
7  * Author: Frank Warmerdam, warmerda@pobox.com
8  *
9  ******************************************************************************
10  * Copyright (c) 2000, Frank Warmerdam
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the
24  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  ******************************************************************************
26  *
27  */
28 
29 #include <grass/gis.h>
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <math.h>
36 
37 /*
38  * Returns 0 on success.
39  * Returns -1 to indicate a system error (check errno).
40  */
41 
42 
43 int G__make_location(const char *location_name,
44  struct Cell_head *wind,
45  struct Key_Value *proj_info,
46  struct Key_Value *proj_units, FILE * report_file)
47 {
48  char path[GPATH_MAX];
49  int out_stat;
50 
51  /* Try to create the location directory, under the gisdbase. */
52  sprintf(path, "%s/%s", G_gisdbase(), location_name);
53  if (G_mkdir(path) != 0)
54  return -1;
55 
56  /* Make the PERMANENT mapset. */
57  sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT");
58  if (G_mkdir(path) != 0)
59  return -1;
60 
61  /* make these the new current location and mapset */
62  G__setenv("LOCATION_NAME", location_name);
63  G__setenv("MAPSET", "PERMANENT");
64 
65  /* Create the default, and current window files */
66  G__put_window(wind, "", "DEFAULT_WIND");
67  G__put_window(wind, "", "WIND");
68 
69  /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
70  if (proj_info != NULL) {
71  G__file_name(path, "", "PROJ_INFO", "PERMANENT");
72  G_write_key_value_file(path, proj_info, &out_stat);
73  if (out_stat != 0)
74  return -2;
75  }
76 
77  if (proj_units != NULL) {
78  G__file_name(path, "", "PROJ_UNITS", "PERMANENT");
79  G_write_key_value_file(path, proj_units, &out_stat);
80  if (out_stat != 0)
81  return -2;
82  }
83 
84  return 0;
85 }
86 
87 
122 int G_make_location(const char *location_name,
123  struct Cell_head *wind,
124  struct Key_Value *proj_info,
125  struct Key_Value *proj_units, FILE * report_file)
126 {
127  int err;
128 
129  err = G__make_location(location_name, wind, proj_info, proj_units,
130  report_file);
131 
132  if (err == 0)
133  return 0;
134 
135  if (err == -1) {
136  perror("G_make_location");
137  }
138 
139  G_fatal_error("G_make_location failed.");
140 
141  return 1;
142 }
143 
144 
145 /************************************************************************/
146 /* G_compare_projections() */
147 
148 /************************************************************************/
149 
165 int
166 G_compare_projections(const struct Key_Value *proj_info1,
167  const struct Key_Value *proj_units1,
168  const struct Key_Value *proj_info2,
169  const struct Key_Value *proj_units2)
170 {
171  const char *proj1, *proj2;
172 
173  if (proj_info1 == NULL && proj_info2 == NULL)
174  return TRUE;
175 
176  /* -------------------------------------------------------------------- */
177  /* Are they both in the same projection? */
178  /* -------------------------------------------------------------------- */
179  /* prevent seg fault in G_find_key_value */
180  if (proj_info1 == NULL || proj_info2 == NULL)
181  return -1;
182 
183  proj1 = G_find_key_value("proj", proj_info1);
184  proj2 = G_find_key_value("proj", proj_info2);
185 
186  if (proj1 == NULL || proj2 == NULL || strcmp(proj1, proj2))
187  return -1;
188 
189  /* -------------------------------------------------------------------- */
190  /* Verify that the linear unit translation to meters is OK. */
191  /* -------------------------------------------------------------------- */
192  /* prevent seg fault in G_find_key_value */
193  if (proj_units1 == NULL && proj_units2 == NULL)
194  return TRUE;
195 
196  if (proj_units1 == NULL || proj_units2 == NULL)
197  return -2;
198 
199  {
200  double a1 = 0, a2 = 0;
201 
202  if (G_find_key_value("meters", proj_units1) != NULL)
203  a1 = atof(G_find_key_value("meters", proj_units1));
204  if (G_find_key_value("meters", proj_units2) != NULL)
205  a2 = atof(G_find_key_value("meters", proj_units2));
206 
207  if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
208  return -2;
209  }
210 
211  /* -------------------------------------------------------------------- */
212  /* Do they both have the same ellipsoid? */
213  /* Lets just check the semi-major axis for now to keep it simple */
214  /* -------------------------------------------------------------------- */
215 
216  {
217  double a1 = 0, a2 = 0;
218 
219  if (G_find_key_value("a", proj_info1) != NULL)
220  a1 = atof(G_find_key_value("a", proj_info1));
221  if (G_find_key_value("a", proj_info2) != NULL)
222  a2 = atof(G_find_key_value("a", proj_info2));
223 
224  if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
225  return -4;
226  }
227 
228  /* -------------------------------------------------------------------- */
229  /* Zone check specially for UTM */
230  /* -------------------------------------------------------------------- */
231  if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
232  && atof(G_find_key_value("zone", proj_info1))
233  != atof(G_find_key_value("zone", proj_info2)))
234  return -5;
235 
236  /* -------------------------------------------------------------------- */
237  /* Hemisphere check specially for UTM */
238  /* -------------------------------------------------------------------- */
239  if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
240  && !!G_find_key_value("south", proj_info1)
241  != !!G_find_key_value("south", proj_info2))
242  return -6;
243 
244  /* -------------------------------------------------------------------- */
245  /* Do they both have the same false easting? */
246  /* -------------------------------------------------------------------- */
247 
248  {
249  char *x_0_1 = NULL, *x_0_2 = NULL;
250 
251  x_0_1 = G_find_key_value("x_0", proj_info1);
252  x_0_2 = G_find_key_value("x_0", proj_info2);
253 
254  if (x_0_1 && x_0_2 && (fabs(atof(x_0_1) - atof(x_0_2)) > 0.000001))
255  return -7;
256  }
257 
258  /* -------------------------------------------------------------------- */
259  /* Do they both have the same false northing? */
260  /* -------------------------------------------------------------------- */
261 
262  {
263  char *y_0_1 = NULL, *y_0_2 = NULL;
264 
265  y_0_1 = G_find_key_value("y_0", proj_info1);
266  y_0_2 = G_find_key_value("y_0", proj_info2);
267 
268  if (y_0_1 && y_0_2 && (fabs(atof(y_0_1) - atof(y_0_2)) > 0.000001))
269  return -8;
270  }
271 
272  /* -------------------------------------------------------------------- */
273  /* Add more details in later. */
274  /* -------------------------------------------------------------------- */
275 
276  return TRUE;
277 }
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
Definition: key_value1.c:128
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:17
const char * err
Definition: g3dcolor.c:50
int G_compare_projections(const struct Key_Value *proj_info1, const struct Key_Value *proj_units1, const struct Key_Value *proj_info2, const struct Key_Value *proj_units2)
compare projections
Definition: make_loc.c:166
int G__make_location(const char *location_name, struct Cell_head *wind, struct Key_Value *proj_info, struct Key_Value *proj_units, FILE *report_file)
Definition: make_loc.c:43
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
#define TRUE
Definition: dbfopen.c:118
char * G_gisdbase(void)
Get name of top level database directory.
Definition: gisdbase.c:29
return NULL
Definition: dbfopen.c:1394
int G_write_key_value_file(const char *file, const struct Key_Value *kv, int *stat)
Write key/value pairs to file.
Definition: key_value3.c:29
int G__setenv(const char *name, const char *value)
Set environment name to value.
Definition: env.c:388
int G__put_window(const struct Cell_head *window, char *dir, char *name)
Definition: put_window.c:40
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G_make_location(const char *location_name, struct Cell_head *wind, struct Key_Value *proj_info, struct Key_Value *proj_units, FILE *report_file)
create a new location
Definition: make_loc.c:122