GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
vector/Vlib/header.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/header.c
3
4 \brief Vector library - header manipulation
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 SPDX-FileCopyrightText: 2001-2010 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
12 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
13 \author Update to GRASS 7 (OGR support) by Martin Landa <landa.martin
14 gmail.com>
15 */
16
17#include <stdlib.h>
18#include <string.h>
19#include <grass/vector.h>
20#include <grass/glocale.h>
21
22#include "local_proto.h"
23
24/*!
25 \brief Print vector map header to stdout
26
27 \param Map pointer to Map_info structure
28
29 \return 0 on success
30 */
32{
33 fprintf(stdout, "\nSelected information from dig header\n");
34 fprintf(stdout, " Organization: %s\n", Vect_get_organization(Map));
35 fprintf(stdout, " Map Name: %s\n", Vect_get_map_name(Map));
36 fprintf(stdout, " Source Date: %s\n", Vect_get_map_date(Map));
37 fprintf(stdout, " Orig. Scale: %d\n", Vect_get_scale(Map));
38
39 return 0;
40}
41
42/*!
43 \brief Read vector map header from map head file
44
45 \param Map pointrt to Map_info structure
46
47 \return 0
48 */
50{
52 return 0;
53}
54
55/*!
56 \brief Write vector map header to map head file
57
58 \param Map pointer to Map_info structure
59
60 \return 0
61 */
63{
64 /* do some sanity checking here */
66 return 0;
67}
68
69/*!
70 \brief Writes head information to text file (GV_HEAD_ELEMENT)
71
72 \param Map pointer to Map_info structure
73
74 \return 0 on success
75 \return -1 on error
76 */
78{
79 char path[GPATH_MAX];
81
84 if (head_fp == NULL) {
85 const char *map_name = Vect_get_full_name(Map);
86 G_warning(_("Unable to create header file for vector map <%s>"),
87 map_name);
88 G_free((void *)map_name);
89 return -1;
90 }
91
92 fprintf(head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map));
93 fprintf(head_fp, "DIGIT DATE: %s\n", Vect_get_date(Map));
94 fprintf(head_fp, "DIGIT NAME: %s\n", Vect_get_person(Map));
95 fprintf(head_fp, "MAP NAME: %s\n", Vect_get_map_name(Map));
96 fprintf(head_fp, "MAP DATE: %s\n", Vect_get_map_date(Map));
97 fprintf(head_fp, "MAP SCALE: %d\n", Vect_get_scale(Map));
98 fprintf(head_fp, "OTHER INFO: %s\n", Vect_get_comment(Map));
99 if (Vect_get_proj(Map) > 0)
100 fprintf(head_fp, "PROJ: %d\n", Vect_get_proj(Map));
101 fprintf(head_fp, "ZONE: %d\n", Vect_get_zone(Map));
102 fprintf(head_fp, "MAP THRESH: %f\n", Vect_get_thresh(Map));
103
105
106 return 0;
107}
108
109/*!
110 \brief Reads head information from text file (GV_HEAD_ELEMENT) - for internal
111 use only
112
113 \param Map pointer to Map_info structure
114
115 \return 0 on success
116 \return -1 on error
117 */
119{
120 FILE *head_fp;
121 char buff[2000];
122 char path[GPATH_MAX], *ptr;
123
124 /* Reset / init */
126
127 G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
130 if (head_fp == NULL) {
131 const char *map_name = Vect_get_full_name(Map);
132 G_warning(_("Unable to open header file of vector <%s>"), map_name);
133 G_free((void *)map_name);
134 return -1;
135 }
136
137 while (G_getl2(buff, 2000, head_fp)) {
138
139 if (!(ptr = strchr(buff, ':'))) {
140 G_warning(_("Corrupted row in head: %s"), buff);
141 continue;
142 }
143
144 ptr++; /* Search for the start of text */
145 while (*ptr == ' ')
146 ptr++;
147
148 if (strncmp(buff, "ORGANIZATION:", sizeof(char) * 13) == 0)
150 else if (strncmp(buff, "DIGIT DATE:", sizeof(char) * 11) == 0)
151 Vect_set_date(Map, ptr);
152 else if (strncmp(buff, "DIGIT NAME:", sizeof(char) * 11) == 0)
153 Vect_set_person(Map, ptr);
154 else if (strncmp(buff, "MAP NAME:", sizeof(char) * 9) == 0)
156 else if (strncmp(buff, "MAP DATE:", sizeof(char) * 9) == 0)
158 else if (strncmp(buff, "MAP SCALE:", sizeof(char) * 10) == 0)
159 Vect_set_scale(Map, atoi(ptr));
160 else if (strncmp(buff, "OTHER INFO:", sizeof(char) * 11) == 0)
161 Vect_set_comment(Map, ptr);
162 else if (strncmp(buff, "PROJ:", sizeof(char) * 5) == 0)
163 Vect_set_proj(Map, atoi(ptr));
164 else if (strncmp(buff, "ZONE:", sizeof(char) * 5) == 0 ||
165 strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0)
166 Vect_set_zone(Map, atoi(ptr));
167 else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) {
168 }
169 else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) {
170 }
171 else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) {
172 }
173 else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) {
174 }
175 else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
176 Vect_set_thresh(Map, atof(ptr));
177 else
178 G_warning(_("Unknown keyword '%s' in vector head"), buff);
179 }
180
182
183 return 0;
184}
185
186/*!
187 \brief Get name of vector map
188
189 \param Map pointer to Map_info structure
190
191 \return string containing name
192 */
193const char *Vect_get_name(struct Map_info *Map)
194{
195 return Map->name;
196}
197
198/*!
199 \brief Get name of mapset where vector map lives
200
201 \param Map pointer to Map_info structure
202
203 \return string containing mapset name
204 */
205const char *Vect_get_mapset(struct Map_info *Map)
206{
207 return Map->mapset;
208}
209
210/*!
211 \brief Get fully qualified name of vector map
212
213 - for GV_FORMAT_NATIVE and GV_FORMAT_OGR returns "map@mapset"
214 - for GV_FORMAT_OGR_DIRECT returns "layer@datasourse"
215
216 Allocated string should be freed by G_free().
217
218 \param Map pointer to Map_info structure
219
220 \return allocated string "name@mapset"
221 */
222const char *Vect_get_full_name(struct Map_info *Map)
223{
224 char *ptr;
225 size_t len;
226
227 if (Map->format == GV_FORMAT_OGR_DIRECT && Map->fInfo.ogr.dsn &&
228 Map->fInfo.ogr.layer_name) {
229 len =
230 strlen(Map->fInfo.ogr.layer_name) + strlen(Map->fInfo.ogr.dsn) + 2;
231 ptr = (char *)G_malloc(len);
232 snprintf(ptr, len, "%s@%s", Map->fInfo.ogr.layer_name,
233 Map->fInfo.ogr.dsn);
234
235 return ptr;
236 }
237
238 len = strlen(Map->name) + strlen(Map->mapset) + 2;
239 ptr = (char *)G_malloc(len);
240 if (strlen(Map->mapset) > 0) {
241 snprintf(ptr, len, "%s@%s", Map->name, Map->mapset);
242 }
243 else {
244 snprintf(ptr, len, "%s", Map->name);
245 }
246
247 return ptr;
248}
249
250/*!
251 \brief Check if vector map is 3D
252
253 Check vector map header.
254
255 \param Map pointer to Map_info structure
256
257 \return TRUE vector map is 3D
258 \return FALSE vector map is not 3D
259 */
261{
262 return Map->head.with_z;
263}
264
265/*!
266 \brief Set organization string in map header
267
268 \param Map pointer to Map_info structure
269 \param str organization name
270
271 \return 0
272 */
273int Vect_set_organization(struct Map_info *Map, const char *str)
274{
275 G_free(Map->head.organization);
276 Map->head.organization = G_store(str);
277
278 return 0;
279}
280
281/*!
282 \brief Get organization string from map header
283
284 \param Map pointer to Map_info structure
285
286 \return string containing organization name
287 */
289{
290 return Map->head.organization;
291}
292
293/*!
294 \brief Set date of digitization in map header
295
296 \todo This should be coupled to DateTime functions to support
297 time series
298
299 \param Map pointer to Map_info structure
300 \param str date given as string
301
302 \return 0
303 */
304int Vect_set_date(struct Map_info *Map, const char *str)
305{
306 G_free(Map->head.date);
307 Map->head.date = G_store(str);
308
309 return 0;
310}
311
312/*!
313 \brief Get date of digitization from map header
314
315 \param Map pointer to Map_info structure
316
317 \return date of digitization string
318 */
319const char *Vect_get_date(struct Map_info *Map)
320{
321 return (Map->head.date);
322}
323
324/*!
325 \brief Set name of user who digitized the map in map header
326
327 \param Map pointer to Map_info structure
328 \param str user name
329
330 \return 0
331 */
332int Vect_set_person(struct Map_info *Map, const char *str)
333{
334 G_free(Map->head.user_name);
335 Map->head.user_name = G_store(str);
336
337 return 0;
338}
339
340/*!
341 \brief Get user name string who digitized the map from map header
342
343 \param Map pointer to Map_info structure
344
345 \return string containing user name
346 */
347const char *Vect_get_person(struct Map_info *Map)
348{
349 return (Map->head.user_name);
350}
351
352/*!
353 \brief Set map name in map header
354
355 \param Map pointer to Map_info structure
356 \param str map name to be set
357
358 \return 0
359 */
360int Vect_set_map_name(struct Map_info *Map, const char *str)
361{
362 G_free(Map->head.map_name);
363 Map->head.map_name = G_store(str);
364
365 return 0;
366}
367
368/*!
369 \brief Get map name from map header
370
371 \param Map pointer to Map_info structure
372
373 \return string containing map name
374 */
375const char *Vect_get_map_name(struct Map_info *Map)
376{
377 return Map->head.map_name;
378}
379
380/*!
381 \brief Set date when the source map was originally produced in map header
382
383 \param Map pointer to Map_info structure
384 \param str date given as a string
385
386 \return 0
387 */
388int Vect_set_map_date(struct Map_info *Map, const char *str)
389{
390 G_free(Map->head.source_date);
391 Map->head.source_date = G_store(str);
392
393 return 0;
394}
395
396/*!
397 \brief Get date when the source map was originally produced from map header
398
399 \param Map pointer to Map_info structure
400
401 \return string containing a date
402 */
403const char *Vect_get_map_date(struct Map_info *Map)
404{
405 return Map->head.source_date;
406}
407
408/*!
409 \brief Set map scale in map header
410
411 \param Map pointer to Map_info structure
412 \param scale map scale
413
414 \return 0
415 */
416int Vect_set_scale(struct Map_info *Map, int scale)
417{
418 Map->head.orig_scale = scale;
419
420 return 0;
421}
422
423/*!
424 \brief Get map scale from map header
425
426 \param Map pointer to Map_info structure
427
428 \return map scale
429 */
431{
432 return (int)Map->head.orig_scale;
433}
434
435/*!
436 \brief Set comment or other info string in map header
437
438 \param Map pointer to Map_info structure
439 \param str comment or other info string
440
441 \return 0
442 */
443int Vect_set_comment(struct Map_info *Map, const char *str)
444{
445 G_free(Map->head.comment);
446 Map->head.comment = G_store(str);
447
448 return 0;
449}
450
451/*!
452 \brief Get comment or other info string from map header
453
454 \param Map pointer to Map_info structure
455
456 \return comment or other info string
457 */
458const char *Vect_get_comment(struct Map_info *Map)
459{
460 return (Map->head.comment);
461}
462
463/*!
464 \brief Set projection zone in map header
465
466 \param Map pointer to Map_info structure
467 \param zone projection zone
468
469 \return 0
470 */
471int Vect_set_zone(struct Map_info *Map, int zone)
472{
473 Map->head.plani_zone = zone;
474
475 return 0;
476}
477
478/*!
479 \brief Get projection zone from map header
480
481 \param Map pointer to Map_info structure (unused, returns the zone for
482 the active region)
483
484 \return projection zone
485 */
487{
488 /* return Map->head.plani_zone; */
489
490 /* use utm zone of current location,
491 * a vector in a given location can not be in a different CRS */
492 return G_zone();
493}
494
495/*!
496 \brief Set projection in map header
497
498 Supported projections:
499 - PROJECTION_XY 0 - x,y (Raw imagery),
500 - PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
501 - PROJECTION_LL 3 - Latitude-Longitude
502
503 \param Map pointer to Map_info structure
504 \param proj projection code
505
506 \return 0
507 */
508int Vect_set_proj(struct Map_info *Map, int proj)
509{
510 Map->head.proj = proj;
511
512 return 0;
513}
514
515/*!
516 \brief Get projection from map header
517
518 \param Map pointer to Map_info structure
519
520 \return PROJECTION_XY 0 - x,y (Raw imagery),
521 \return PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
522 \return PROJECTION_LL 3 - Latitude-Longitude
523 */
525{
526 return (Map->head.proj);
527}
528
529/*!
530 \brief Query cartographic projection name of pointer to Map_info structure
531
532 Returns a pointer to a string which is a printable name for
533 projection code <em>proj</em> (as returned by Vect_get_proj()).
534
535 \param Map pointer to Map_info structure
536
537 \return allocated string containing projection name
538 \return NULL if <em>proj</em> is not a valid projection
539 */
540const char *Vect_get_proj_name(struct Map_info *Map)
541{
542 char name[256];
543 int n;
544
545 switch (n = Vect_get_proj(Map)) {
546 case PROJECTION_XY:
547 case PROJECTION_UTM:
548 case PROJECTION_LL:
549 return G_projection_name(n);
550 case PROJECTION_OTHER:
551 /* this won't protect against differing "other" projections, so
552 better to just include P_OTHER in the above list so we return the
553 strictly more correct, but less nice, string: "Other projection" ? */
555 default:
556 G_debug(1,
557 "Vect_get_proj_name(): "
558 "Vect_get_proj() returned an invalid result (%d)",
559 n);
560 break;
561 }
562
563 strcpy(name, _("Unknown projection"));
564 return G_store(name);
565}
566
567/*!
568 \brief Set threshold used for digitization in map header
569
570 \param Map pointer to Map_info structure
571 \param thresh threshold used for digitization
572
573 \return 0
574 */
575int Vect_set_thresh(struct Map_info *Map, double thresh)
576{
577 G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
578 Map->head.digit_thresh = thresh;
579 return 0;
580}
581
582/*!
583 \brief Get threshold used for digitization from map header
584
585 \param Map pointer to Map_info structure
586
587 \return threshold used for digitization
588 */
590{
591 return Map->head.digit_thresh;
592}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition getl.c:58
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void G_warning(const char *,...) __attribute__((format(printf
const char * G_projection_name(int)
Get projection name.
Definition proj2.c:53
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Definition gis/open.c:218
#define G_malloc(n)
Definition defs/gis.h:136
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition gis/open.c:250
int G_zone(void)
Query cartographic zone.
Definition zone.c:23
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_database_projection_name(void)
Query cartographic projection for the current location.
Definition proj3.c:116
void Vect__init_head(struct Map_info *)
Initialize Map_info head structure (dig_head)
Definition init_head.c:27
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
Definition dig_defines.h:87
#define GV_HEAD_ELEMENT
Native format, header information.
Definition dig_defines.h:14
#define PROJECTION_OTHER
Projection code - other projection (other then noted above)
Definition gis.h:128
#define PROJECTION_XY
Projection code - XY coordinate system (unreferenced data)
Definition gis.h:120
#define GPATH_MAX
Definition gis.h:196
#define PROJECTION_UTM
Projection code - UTM.
Definition gis.h:122
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition gis.h:126
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
#define strcpy
Definition parson.c:66
Vector map info.
Definition path.h:15
int Vect_set_person(struct Map_info *Map, const char *str)
Set name of user who digitized the map in map header.
const char * Vect_get_comment(struct Map_info *Map)
Get comment or other info string from map header.
const char * Vect_get_map_date(struct Map_info *Map)
Get date when the source map was originally produced from map header.
int Vect_print_header(struct Map_info *Map)
Print vector map header to stdout.
int Vect_set_map_date(struct Map_info *Map, const char *str)
Set date when the source map was originally produced in map header.
int Vect_is_3d(struct Map_info *Map)
Check if vector map is 3D.
const char * Vect_get_person(struct Map_info *Map)
Get user name string who digitized the map from map header.
int Vect_set_organization(struct Map_info *Map, const char *str)
Set organization string in map header.
const char * Vect_get_name(struct Map_info *Map)
Get name of vector map.
int Vect_set_thresh(struct Map_info *Map, double thresh)
Set threshold used for digitization in map header.
const char * Vect_get_date(struct Map_info *Map)
Get date of digitization from map header.
int Vect__read_head(struct Map_info *Map)
Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only.
int Vect_set_map_name(struct Map_info *Map, const char *str)
Set map name in map header.
const char * Vect_get_mapset(struct Map_info *Map)
Get name of mapset where vector map lives.
int Vect_set_comment(struct Map_info *Map, const char *str)
Set comment or other info string in map header.
int Vect_get_proj(struct Map_info *Map)
Get projection from map header.
const char * Vect_get_organization(struct Map_info *Map)
Get organization string from map header.
int Vect_set_scale(struct Map_info *Map, int scale)
Set map scale in map header.
int Vect_set_zone(struct Map_info *Map, int zone)
Set projection zone in map header.
const char * Vect_get_full_name(struct Map_info *Map)
Get fully qualified name of vector map.
int Vect_set_proj(struct Map_info *Map, int proj)
Set projection in map header.
int Vect_read_header(struct Map_info *Map)
Read vector map header from map head file.
int Vect_set_date(struct Map_info *Map, const char *str)
Set date of digitization in map header.
int Vect_write_header(struct Map_info *Map)
Write vector map header to map head file.
int Vect_get_zone(struct Map_info *Map)
Get projection zone from map header.
int Vect__write_head(struct Map_info *Map)
Writes head information to text file (GV_HEAD_ELEMENT)
const char * Vect_get_proj_name(struct Map_info *Map)
Query cartographic projection name of pointer to Map_info structure.
const char * Vect_get_map_name(struct Map_info *Map)
Get map name from map header.
double Vect_get_thresh(struct Map_info *Map)
Get threshold used for digitization from map header.
int Vect_get_scale(struct Map_info *Map)
Get map scale from map header.
char * Vect__get_path(char *path, struct Map_info *Map)
Get map directory name (internal use only)