GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 1999-2014 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10#include <string.h>
11#include <errno.h>
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 */
31{
33 char path[GPATH_MAX];
34
36 if (access(path, 0) != 0) {
37 if (G_projection() != PROJECTION_XY) {
38 G_warning(_("<%s> file not found for location <%s>"), UNIT_FILE,
39 G_location());
40 }
41 return NULL;
42 }
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 */
60{
62 char path[GPATH_MAX];
63
65 if (access(path, 0) != 0) {
66 if (G_projection() != PROJECTION_XY) {
67 G_warning(_("<%s> file not found for location <%s>"),
69 }
70 return NULL;
71 }
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 snprintf(buf, sizeof(buf), "EPSG:%s", epsgstr);
80 G_set_key_value("init", buf, in_proj_keys);
82 }
83
84 return in_proj_keys;
85}
86
87/*!
88 \brief Gets EPSG information for the current location
89
90 DEPRECATED: Use G_get_projsrid() instead.
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
106 if (access(path, 0) != 0) {
107 if (G_projection() != PROJECTION_XY) {
108 G_debug(1, "<%s> file not found for location <%s>", EPSG_FILE,
109 G_location());
110 }
111 return NULL;
112 }
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 */
124char *G_get_projwkt(void)
125{
126 char *wktstring = NULL;
127 char path[GPATH_MAX];
128 FILE *fp;
129 int n, nalloc;
130 int c;
131
132 G_file_name(path, "", WKT_FILE, "PERMANENT");
133 if (access(path, 0) != 0) {
134 if (G_projection() != PROJECTION_XY) {
135 G_debug(1, "<%s> file not found for location <%s>", WKT_FILE,
136 G_location());
137 }
138 return NULL;
139 }
140
141 fp = fopen(path, "r");
142 if (!fp)
143 G_fatal_error(_("Unable to open input file <%s>: %s"), path,
144 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 !=
161 '\n') { /* MacOS9 - we have to return the char to stream */
162 ungetc(c, fp);
163 c = '\n';
164 }
165 }
166 else { /* MacOS9 - we have to return the char to stream */
167 ungetc(c, fp);
168 c = '\n';
169 }
170 }
171
172 if (n == nalloc) {
174 nalloc += 1024;
175 }
176
177 wktstring[n] = c;
178
179 n++;
180 }
181
182 if (n > 0) {
183 if (n == nalloc) {
185 nalloc += 1;
186 }
187 wktstring[n] = '\0';
188 }
189 else {
191 wktstring = NULL;
192 }
193
194 if (fclose(fp) != 0)
195 G_fatal_error(_("Error closing output file <%s>: %s"), path,
196 strerror(errno));
197
198 if (wktstring && *wktstring)
200 if (wktstring && *wktstring == '\0') {
202 wktstring = NULL;
203 }
204
205 return wktstring;
206}
207
208/*!
209 \brief Get srid (spatial reference id) for the current location
210
211 Typically an srid will be of the form authority NAME:CODE,
212 e.g. EPSG:4326
213
214 This srid is passed to proj_create() using PROJ or
215 OSRSetFromUserInput() using GDAL. Therefore various other forms of
216 srid are possible, e.g. in OSRSetFromUserInput():
217
218 1. Well Known Text definition - passed on to importFromWkt().
219 2. "EPSG:n" - number passed on to importFromEPSG().
220 3. "EPSGA:n" - number passed on to importFromEPSGA().
221 4. "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
222 5. "urn:ogc:def:crs:EPSG::n" - ogc urns
223 6. PROJ.4 definitions - passed on to importFromProj4().
224 7. filename - file read for WKT, XML or PROJ.4 definition.
225 8. well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
226 WGS84 or WGS72.
227 9. "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
228 10. PROJJSON (PROJ >= 6.2)
229
230 \return pointer to srid string
231 \return NULL when srid is not available for the current location
232 */
233char *G_get_projsrid(void)
234{
235 char *sridstring = NULL;
236 char path[GPATH_MAX];
237 FILE *fp;
238 int n, nalloc;
239 int c;
240
241 G_file_name(path, "", SRID_FILE, "PERMANENT");
242 if (access(path, 0) != 0) {
243 if (G_projection() != PROJECTION_XY) {
244 struct Key_Value *projepsg;
245 const char *epsg_num;
246
247 G_debug(1, "<%s> file not found for location <%s>", SRID_FILE,
248 G_location());
249
250 /* for backwards compatibility, check if PROJ_EPSG exists */
251 if ((projepsg = G_get_projepsg()) != NULL) {
252 epsg_num = G_find_key_value("epsg", projepsg);
253 if (*epsg_num) {
254 G_debug(1, "Using <%s> file instead for location <%s>",
256 G_asprintf(&sridstring, "EPSG:%s", epsg_num);
257 G_free_key_value(projepsg);
258
259 return sridstring;
260 }
261 }
262 }
263 return NULL;
264 }
265
266 fp = fopen(path, "r");
267 if (!fp)
268 G_fatal_error(_("Unable to open input file <%s>: %s"), path,
269 strerror(errno));
270
271 sridstring = G_malloc(1024 * sizeof(char));
272 nalloc = 1024;
273
274 n = 0;
275 while (1) {
276 c = fgetc(fp);
277
278 if (c == EOF) {
279 break;
280 }
281
282 if (c == '\r') { /* DOS or MacOS9 */
283 c = fgetc(fp);
284 if (c != EOF) {
285 if (c !=
286 '\n') { /* MacOS9 - we have to return the char to stream */
287 ungetc(c, fp);
288 c = '\n';
289 }
290 }
291 else { /* MacOS9 - we have to return the char to stream */
292 ungetc(c, fp);
293 c = '\n';
294 }
295 }
296
297 if (n == nalloc) {
299 nalloc += 1024;
300 }
301
302 sridstring[n] = c;
303
304 n++;
305 }
306
307 if (n > 0) {
308 if (n == nalloc) {
310 nalloc += 1;
311 }
312 sridstring[n] = '\0';
313 }
314 else {
317 }
318
319 if (fclose(fp) != 0)
320 G_fatal_error(_("Error closing output file <%s>: %s"), path,
321 strerror(errno));
322
323 if (sridstring && *sridstring)
325 if (sridstring && *sridstring == '\0') {
328 }
329
330 return sridstring;
331}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:59
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
Definition key_value1.c:102
void G_set_key_value(const char *, const char *, struct Key_Value *)
Set value for given key.
Definition key_value1.c:37
struct Key_Value * G_read_key_value_file(const char *)
Read key/values pairs from file.
Definition key_value3.c:53
const char * G_location(void)
Get current location name.
Definition location.c:30
int G_asprintf(char **, const char *,...) __attribute__((format(printf
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
Definition key_value1.c:83
char * G_chop(char *)
Chop leading and trailing white spaces.
Definition strings.c:330
int G_debug(int, const char *,...) __attribute__((format(printf
int G_projection(void)
Query cartographic projection.
Definition proj1.c:30
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
struct Key_Value * G_get_projepsg(void)
Gets EPSG information for the current location.
char * G_get_projwkt(void)
Get WKT information for the current location.
#define PERMANENT
char * G_get_projsrid(void)
Get srid (spatial reference id) for the current location.
struct Key_Value * G_get_projunits(void)
Gets units information for location.
#define PROJECTION_XY
Projection code - XY coordinate system (unreferenced data)
Definition gis.h:120
#define WKT_FILE
Definition gis.h:133
#define PROJECTION_FILE
Definition gis.h:130
#define EPSG_FILE
Definition gis.h:132
#define GPATH_MAX
Definition gis.h:196
#define SRID_FILE
Definition gis.h:134
#define UNIT_FILE
Definition gis.h:131
#define _(str)
Definition glocale.h:10
int nalloc
Definition gis.h:533
Definition path.h:15
#define access
Definition unistd.h:7