GRASS GIS 7 Programmer's Manual  7.9.dev(2020)-547ded6c0
get_projinfo.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/get_projinfo.c
3 
4  \brief GIS Library - Get projection info
5 
6  (C) 1999-2014 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 */
11 
12 #include <string.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <grass/gis.h>
17 #include <grass/glocale.h>
18 
19 #define PERMANENT "PERMANENT"
20 
21 /*!
22  \brief Gets units information for location
23 
24  Note: Allocated Key_Value structure should be freed by
25  G_free_key_value().
26 
27  Prints a warning if no units information available.
28 
29  \return pointer to Key_Value structure with key/value pairs
30  \return NULL on failure
31 */
33 {
34  struct Key_Value *in_units_keys;
35  char path[GPATH_MAX];
36 
37  G_file_name(path, "", UNIT_FILE, PERMANENT);
38  if (access(path, 0) != 0) {
39  if (G_projection() != PROJECTION_XY) {
40  G_warning(_("<%s> file not found for location <%s>"),
42  }
43  return NULL;
44  }
45  in_units_keys = G_read_key_value_file(path);
46 
47  return in_units_keys;
48 }
49 
50 /*!
51  \brief Gets projection information for location
52 
53  Note: Allocated Key_Value structure should be freed by
54  G_free_key_value().
55 
56  Prints a warning if no projection information available.
57 
58  \return pointer to Key_Value structure with key/value pairs
59  \return NULL on failure
60 */
62 {
63  struct Key_Value *in_proj_keys, *in_epsg_keys;
64  char path[GPATH_MAX];
65 
67  if (access(path, 0) != 0) {
68  if (G_projection() != PROJECTION_XY) {
69  G_warning(_("<%s> file not found for location <%s>"),
71  }
72  return NULL;
73  }
74  in_proj_keys = G_read_key_value_file(path);
75 
76  /* TODO: do not restrict to EPSG as the only authority */
77  if ((in_epsg_keys = G_get_projepsg()) != NULL) {
78  const char *epsgstr = G_find_key_value("epsg", in_epsg_keys);
79  char buf[4096];
80 
81  sprintf(buf, "EPSG:%s", epsgstr);
82  G_set_key_value("init", buf, in_proj_keys);
83  G_free_key_value(in_epsg_keys);
84  }
85 
86  return in_proj_keys;
87 }
88 
89 /*!
90  \brief Gets EPSG information for the current location
91 
92  Note: Allocated Key_Value structure should be freed by
93  G_free_key_value().
94 
95  \return pointer to Key_Value structure with key/value pairs
96  \return NULL when EPSG code is not defined for location
97 */
98 
99 /* superseded by G_get_projsrid(), keep for backwards compatibility */
101 {
102  struct Key_Value *in_epsg_keys;
103  char path[GPATH_MAX];
104 
105  G_file_name(path, "", EPSG_FILE, PERMANENT);
106  if (access(path, 0) != 0) {
107  if (G_projection() != PROJECTION_XY) {
108  G_debug(1, "<%s> file not found for location <%s>",
109  EPSG_FILE, G_location());
110  }
111  return NULL;
112  }
113  in_epsg_keys = G_read_key_value_file(path);
114 
115  return in_epsg_keys;
116 }
117 
118 /*!
119  \brief Get WKT information for the current location
120 
121  \return pointer to WKT string
122  \return NULL when WKT is not available for the current location
123 */
124 
125 char *G_get_projwkt(void)
126 {
127  char *wktstring = NULL;
128  char path[GPATH_MAX];
129  FILE *fp;
130  int n, nalloc;
131  int c;
132 
133  G_file_name(path, "", WKT_FILE, "PERMANENT");
134  if (access(path, 0) != 0) {
135  if (G_projection() != PROJECTION_XY) {
136  G_debug(1, "<%s> file not found for location <%s>",
137  WKT_FILE, G_location());
138  }
139  return NULL;
140  }
141 
142  fp = fopen(path, "r");
143  if (!fp)
144  G_fatal_error(_("Unable to open input file <%s>: %s"), path, strerror(errno));
145 
146  wktstring = G_malloc(1024 * sizeof(char));
147  nalloc = 1024;
148 
149  n = 0;
150  while (1) {
151  c = fgetc(fp);
152 
153  if (c == EOF) {
154  break;
155  }
156 
157  if (c == '\r') { /* DOS or MacOS9 */
158  c = fgetc(fp);
159  if (c != EOF) {
160  if (c != '\n') { /* MacOS9 - we have to return the char to stream */
161  ungetc(c, fp);
162  c = '\n';
163  }
164  }
165  else { /* MacOS9 - we have to return the char to stream */
166  ungetc(c, fp);
167  c = '\n';
168  }
169  }
170 
171  if (n == nalloc) {
172  wktstring = G_realloc(wktstring, nalloc + 1024);
173  nalloc += 1024;
174  }
175 
176  wktstring[n] = c;
177 
178  n++;
179  }
180 
181  if (n > 0) {
182  if (n == nalloc) {
183  wktstring = G_realloc(wktstring, nalloc + 1);
184  nalloc += 1;
185  }
186  wktstring[n] = '\0';
187  }
188  else {
189  G_free(wktstring);
190  wktstring = NULL;
191  }
192 
193  if (fclose(fp) != 0)
194  G_fatal_error(_("Error closing output file <%s>: %s"), path, strerror(errno));
195 
196  return wktstring;
197 }
198 
199 /*!
200  \brief Get srid (spatial reference id) for the current location
201 
202  Typically an srid will be of the form authority NAME:CODE,
203  e.g. EPSG:4326
204 
205  This srid is passed to proj_create() using PROJ or
206  OSRSetFromUserInput() using GDAL. Therefore various other forms of
207  srid are possible, e.g. in OSRSetFromUserInput():
208 
209  1. Well Known Text definition - passed on to importFromWkt().
210  2. "EPSG:n" - number passed on to importFromEPSG().
211  3. "EPSGA:n" - number passed on to importFromEPSGA().
212  4. "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
213  5. "urn:ogc:def:crs:EPSG::n" - ogc urns
214  6. PROJ.4 definitions - passed on to importFromProj4().
215  7. filename - file read for WKT, XML or PROJ.4 definition.
216  8. well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83, WGS84 or WGS72.
217  9. "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
218  10. PROJJSON (PROJ >= 6.2)
219 
220  \return pointer to srid string
221  \return NULL when srid is not available for the current location
222 */
223 
224 char *G_get_projsrid(void)
225 {
226  char *sridstring = NULL;
227  char path[GPATH_MAX];
228  FILE *fp;
229  int n, nalloc;
230  int c;
231 
232  G_file_name(path, "", SRID_FILE, "PERMANENT");
233  if (access(path, 0) != 0) {
234  if (G_projection() != PROJECTION_XY) {
235  G_debug(1, "<%s> file not found for location <%s>",
236  SRID_FILE, G_location());
237  }
238  return NULL;
239  }
240 
241  fp = fopen(path, "r");
242  if (!fp)
243  G_fatal_error(_("Unable to open input file <%s>: %s"), path, strerror(errno));
244 
245  sridstring = G_malloc(1024 * sizeof(char));
246  nalloc = 1024;
247 
248  n = 0;
249  while (1) {
250  c = fgetc(fp);
251 
252  if (c == EOF) {
253  break;
254  }
255 
256  if (c == '\r') { /* DOS or MacOS9 */
257  c = fgetc(fp);
258  if (c != EOF) {
259  if (c != '\n') { /* MacOS9 - we have to return the char to stream */
260  ungetc(c, fp);
261  c = '\n';
262  }
263  }
264  else { /* MacOS9 - we have to return the char to stream */
265  ungetc(c, fp);
266  c = '\n';
267  }
268  }
269 
270  if (n == nalloc) {
271  sridstring = G_realloc(sridstring, nalloc + 1024);
272  nalloc += 1024;
273  }
274 
275  sridstring[n] = c;
276 
277  n++;
278  }
279 
280  if (n > 0) {
281  if (n == nalloc) {
282  sridstring = G_realloc(sridstring, nalloc + 1);
283  nalloc += 1;
284  }
285  sridstring[n] = '\0';
286  }
287  else {
288  G_free(sridstring);
289  sridstring = NULL;
290  }
291 
292  if (fclose(fp) != 0)
293  G_fatal_error(_("Error closing output file <%s>: %s"), path, strerror(errno));
294 
295  return sridstring;
296 }
const char * G_location(void)
Get current location name.
Definition: location.c:32
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
#define PERMANENT
Definition: get_projinfo.c:19
struct Key_Value * G_get_projunits(void)
Gets units information for location.
Definition: get_projinfo.c:32
#define PROJECTION_XY
Projection code - XY coordinate system (unreferenced data)
Definition: gis.h:107
#define NULL
Definition: ccmath.h:32
void G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:103
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:160
struct Key_Value * G_read_key_value_file(const char *file)
Read key/values pairs from file.
Definition: key_value3.c:53
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:38
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
char * G_get_projwkt(void)
Get WKT information for the current location.
Definition: get_projinfo.c:125
#define GPATH_MAX
Definition: gis.h:167
char * G_get_projsrid(void)
Get srid (spatial reference id) for the current location.
Definition: get_projinfo.c:224
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
Definition: get_projinfo.c:61
Definition: gis.h:498
int nalloc
Definition: gis.h:501
struct Key_Value * G_get_projepsg(void)
Gets EPSG information for the current location.
Definition: get_projinfo.c:100
#define EPSG_FILE
Definition: gis.h:119
#define SRID_FILE
Definition: gis.h:121
Definition: path.h:16
#define WKT_FILE
Definition: gis.h:120
#define _(str)
Definition: glocale.h:13
#define UNIT_FILE
Definition: gis.h:118
void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:38
int G_projection(void)
Query cartographic projection.
Definition: proj1.c:32
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
#define PROJECTION_FILE
Definition: gis.h:117