GRASS GIS 7 Programmer's Manual  7.8.0(2019)-exported
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 <unistd.h>
13 #include <stdio.h>
14 #include <grass/gis.h>
15 #include <grass/glocale.h>
16 
17 #define PERMANENT "PERMANENT"
18 
19 /*!
20  \brief Gets units information for location
21 
22  Note: Allocated Key_Value structure should be freed by
23  G_free_key_value().
24 
25  Prints a warning if no units information available.
26 
27  \return pointer to Key_Value structure with key/value pairs
28  \return NULL on failure
29 */
30 struct Key_Value *G_get_projunits(void)
31 {
32  struct Key_Value *in_units_keys;
33  char path[GPATH_MAX];
34 
35  G_file_name(path, "", UNIT_FILE, PERMANENT);
36  if (access(path, 0) != 0) {
37  if (G_projection() != PROJECTION_XY) {
38  G_warning(_("<%s> file not found for location <%s>"),
39  UNIT_FILE, G_location());
40  }
41  return NULL;
42  }
43  in_units_keys = G_read_key_value_file(path);
44 
45  return in_units_keys;
46 }
47 
48 /*!
49  \brief Gets projection information for location
50 
51  Note: Allocated Key_Value structure should be freed by
52  G_free_key_value().
53 
54  Prints a warning if no projection information available.
55 
56  \return pointer to Key_Value structure with key/value pairs
57  \return NULL on failure
58 */
59 struct Key_Value *G_get_projinfo(void)
60 {
61  struct Key_Value *in_proj_keys, *in_epsg_keys;
62  char path[GPATH_MAX];
63 
64  G_file_name(path, "", PROJECTION_FILE, PERMANENT);
65  if (access(path, 0) != 0) {
66  if (G_projection() != PROJECTION_XY) {
67  G_warning(_("<%s> file not found for location <%s>"),
68  PROJECTION_FILE, G_location());
69  }
70  return NULL;
71  }
72  in_proj_keys = G_read_key_value_file(path);
73 
74  /* TODO: do not restrict to EPSG as the only authority */
75  if ((in_epsg_keys = G_get_projepsg()) != NULL) {
76  const char *epsgstr = G_find_key_value("epsg", in_epsg_keys);
77  char buf[4096];
78 
79  sprintf(buf, "EPSG:%s", epsgstr);
80  G_set_key_value("init", buf, in_proj_keys);
81  G_free_key_value(in_epsg_keys);
82  }
83 
84  return in_proj_keys;
85 }
86 
87 /*!
88  \brief Gets EPSG information for the current location
89 
90  Note: Allocated Key_Value structure should be freed by
91  G_free_key_value().
92 
93  \return pointer to Key_Value structure with key/value pairs
94  \return NULL when EPSG code is not defined for location
95 */
96 struct Key_Value *G_get_projepsg(void)
97 {
98  struct Key_Value *in_epsg_keys;
99  char path[GPATH_MAX];
100 
101  G_file_name(path, "", EPSG_FILE, PERMANENT);
102  if (access(path, 0) != 0) {
103  if (G_projection() != PROJECTION_XY) {
104  G_debug(1, "<%s> file not found for location <%s>",
105  EPSG_FILE, G_location());
106  }
107  return NULL;
108  }
109  in_epsg_keys = G_read_key_value_file(path);
110 
111  return in_epsg_keys;
112 }
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
#define PERMANENT
Definition: get_projinfo.c:17
struct Key_Value * G_get_projunits(void)
Gets units information for location.
Definition: get_projinfo.c:30
#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
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
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
Definition: get_projinfo.c:59
struct Key_Value * G_get_projepsg(void)
Gets EPSG information for the current location.
Definition: get_projinfo.c:96
Definition: path.h:16
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