GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
make_loc.c
Go to the documentation of this file.
1 
17 #include <grass/gis.h>
18 
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <math.h>
24 
51 int G_make_location(const char *location_name,
52  struct Cell_head *wind,
53  const struct Key_Value *proj_info,
54  const struct Key_Value *proj_units)
55 {
56  char path[GPATH_MAX];
57 
58  /* check if location name is legal */
59  if (G_legal_filename(location_name) != 1)
60  return -3;
61 
62  /* Try to create the location directory, under the gisdbase. */
63  sprintf(path, "%s/%s", G_gisdbase(), location_name);
64  if (G_mkdir(path) != 0)
65  return -1;
66 
67  /* Make the PERMANENT mapset. */
68  sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT");
69  if (G_mkdir(path) != 0) {
70  return -1;
71  }
72 
73  /* make these the new current location and mapset */
74  G_setenv_nogisrc("LOCATION_NAME", location_name);
75  G_setenv_nogisrc("MAPSET", "PERMANENT");
76 
77  /* Create the default, and current window files */
78  G_put_element_window(wind, "", "DEFAULT_WIND");
79  G_put_element_window(wind, "", "WIND");
80 
81  /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
82  if (proj_info != NULL) {
83  G_file_name(path, "", "PROJ_INFO", "PERMANENT");
84  G_write_key_value_file(path, proj_info);
85  }
86 
87  if (proj_units != NULL) {
88  G_file_name(path, "", "PROJ_UNITS", "PERMANENT");
89  G_write_key_value_file(path, proj_units);
90  }
91 
92  return 0;
93 }
94 
112 int G_compare_projections(const struct Key_Value *proj_info1,
113  const struct Key_Value *proj_units1,
114  const struct Key_Value *proj_info2,
115  const struct Key_Value *proj_units2)
116 {
117  const char *proj1, *proj2;
118 
119  if (proj_info1 == NULL && proj_info2 == NULL)
120  return TRUE;
121 
122  /* -------------------------------------------------------------------- */
123  /* Are they both in the same projection? */
124  /* -------------------------------------------------------------------- */
125  /* prevent seg fault in G_find_key_value */
126  if (proj_info1 == NULL || proj_info2 == NULL)
127  return -1;
128 
129  proj1 = G_find_key_value("proj", proj_info1);
130  proj2 = G_find_key_value("proj", proj_info2);
131 
132  if (proj1 == NULL || proj2 == NULL || strcmp(proj1, proj2))
133  return -1;
134 
135  /* -------------------------------------------------------------------- */
136  /* Verify that the linear unit translation to meters is OK. */
137  /* -------------------------------------------------------------------- */
138  /* prevent seg fault in G_find_key_value */
139  if (proj_units1 == NULL && proj_units2 == NULL)
140  return 1;
141 
142  if (proj_units1 == NULL || proj_units2 == NULL)
143  return -2;
144 
145  {
146  double a1 = 0, a2 = 0;
147 
148  if (G_find_key_value("meters", proj_units1) != NULL)
149  a1 = atof(G_find_key_value("meters", proj_units1));
150  if (G_find_key_value("meters", proj_units2) != NULL)
151  a2 = atof(G_find_key_value("meters", proj_units2));
152 
153  if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
154  return -2;
155  }
156 
157  /* -------------------------------------------------------------------- */
158  /* Do they both have the same ellipsoid? */
159  /* Lets just check the semi-major axis for now to keep it simple */
160  /* -------------------------------------------------------------------- */
161 
162  {
163  double a1 = 0, a2 = 0;
164 
165  if (G_find_key_value("a", proj_info1) != NULL)
166  a1 = atof(G_find_key_value("a", proj_info1));
167  if (G_find_key_value("a", proj_info2) != NULL)
168  a2 = atof(G_find_key_value("a", proj_info2));
169 
170  if (a1 && a2 && (fabs(a2 - a1) > 0.000001))
171  return -4;
172  }
173 
174  /* -------------------------------------------------------------------- */
175  /* Zone check specially for UTM */
176  /* -------------------------------------------------------------------- */
177  if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
178  && atof(G_find_key_value("zone", proj_info1))
179  != atof(G_find_key_value("zone", proj_info2)))
180  return -5;
181 
182  /* -------------------------------------------------------------------- */
183  /* Hemisphere check specially for UTM */
184  /* -------------------------------------------------------------------- */
185  if (!strcmp(proj1, "utm") && !strcmp(proj2, "utm")
186  && !!G_find_key_value("south", proj_info1)
187  != !!G_find_key_value("south", proj_info2))
188  return -6;
189 
190  /* -------------------------------------------------------------------- */
191  /* Do they both have the same false easting? */
192  /* -------------------------------------------------------------------- */
193 
194  {
195  const char *x_0_1 = NULL, *x_0_2 = NULL;
196 
197  x_0_1 = G_find_key_value("x_0", proj_info1);
198  x_0_2 = G_find_key_value("x_0", proj_info2);
199 
200  if (x_0_1 && x_0_2 && (fabs(atof(x_0_1) - atof(x_0_2)) > 0.000001))
201  return -7;
202  }
203 
204  /* -------------------------------------------------------------------- */
205  /* Do they both have the same false northing? */
206  /* -------------------------------------------------------------------- */
207 
208  {
209  const char *y_0_1 = NULL, *y_0_2 = NULL;
210 
211  y_0_1 = G_find_key_value("y_0", proj_info1);
212  y_0_2 = G_find_key_value("y_0", proj_info2);
213 
214  if (y_0_1 && y_0_2 && (fabs(atof(y_0_1) - atof(y_0_2)) > 0.000001))
215  return -8;
216  }
217 
218  /* -------------------------------------------------------------------- */
219  /* Add more details in later. */
220  /* -------------------------------------------------------------------- */
221 
222  return 1;
223 }
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:27
#define NULL
Definition: ccmath.h:32
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 including units.
Definition: make_loc.c:112
void G_write_key_value_file(const char *file, const struct Key_Value *kv)
Write key/value pairs to file.
Definition: key_value3.c:28
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
void G_setenv_nogisrc(const char *name, const char *value)
Set environment name to value (doesn&#39;t update .gisrc)
Definition: env.c:448
int G_put_element_window(const struct Cell_head *window, const char *dir, const char *name)
Write the region.
Definition: put_window.c:74
const char * G_gisdbase(void)
Get name of top level database directory.
Definition: gisdbase.c:26
Definition: path.h:16
int G_make_location(const char *location_name, struct Cell_head *wind, const struct Key_Value *proj_info, const struct Key_Value *proj_units)
Create a new location.
Definition: make_loc.c:51