GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
open_nat.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/open_nat.c
3
4 \brief Vector library - open vector map (native format) - level 1
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 (C) 2001-2009 by the GRASS Development Team
9
10 This program is free software under the GNU General Public License
11 (>=v2). Read the file COPYING that comes with GRASS for details.
12
13 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
14 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
15 */
16
17#include <inttypes.h>
18#include <unistd.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21
22#include <grass/vector.h>
23#include <grass/glocale.h>
24
25#include "local_proto.h"
26
27static int check_coor(struct Map_info *Map);
28
29/*!
30 \brief Open existing vector map (level 1)
31
32 Map->name and Map->mapset must be set before.
33
34 \param Map pointer to Map_info structure
35 \param update non-zero for write mode, otherwise read-only
36
37 \return 0 success
38 \return -1 error
39 */
40int V1_open_old_nat(struct Map_info *Map, int update)
41{
42 char path[GPATH_MAX];
43 struct Coor_info CInfo;
44
45 G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
46 Map->mapset);
47
49 dig_file_init(&(Map->dig_fp));
50 if (update)
51 Map->dig_fp.file = G_fopen_modify(path, GV_COOR_ELEMENT);
52 else
53 Map->dig_fp.file = G_fopen_old(path, GV_COOR_ELEMENT, Map->mapset);
54
55 if (Map->dig_fp.file == NULL) {
56 const char *map_name = Vect_get_full_name(Map);
57 G_warning(_("Unable to open coor file for vector map <%s>"), map_name);
58 G_free((void *)map_name);
59 return -1;
60 }
61
62 /* needed to determine file size, Map->head.size will be updated
63 by dig__read_head(Map) */
65 Map->head.size = CInfo.size;
66
67 if (!(dig__read_head(Map))) {
68 G_debug(1, "dig__read_head(): failed");
69 return -1;
70 }
71
72 /* compare coor size stored in head with real size */
73 /* check should catch if LFS is required but not available */
74 check_coor(Map);
75
76 /* set conversion matrices */
77 dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
78
79 /* load to memory */
80 if (!update)
82 &(Map->dig_fp)); /* has currently no effect, file never loaded */
83
84 return 0;
85}
86
87/*!
88 \brief Create new vector map (level 1)
89
90 \param[out] Map pointer to Map_info structure
91 \param name vector map name to be created
92 \param with_z 2D or 3D (unused?)
93
94 \return 0 success
95 \return -1 error
96 */
97int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
98{
99 char path[GPATH_MAX];
100
101 G_debug(1, "V1_open_new_nat(): name = %s with_z = %d is_tmp = %d", name,
102 with_z, Map->temporary);
103
104 /* Set the 'coor' file version */
105 Map->head.coor_version.major = GV_COOR_VER_MAJOR;
106 Map->head.coor_version.minor = GV_COOR_VER_MINOR;
107 Map->head.coor_version.back_major = GV_COOR_EARLIEST_MAJOR;
108 Map->head.coor_version.back_minor = GV_COOR_EARLIEST_MINOR;
109
111
112 /* TODO: open better */
113 dig_file_init(&(Map->dig_fp));
114 Map->dig_fp.file = G_fopen_new(path, GV_COOR_ELEMENT);
115 if (Map->dig_fp.file == NULL)
116 return -1;
117 fclose(Map->dig_fp.file);
118
119 dig_file_init(&(Map->dig_fp));
120 Map->dig_fp.file = G_fopen_modify(path, GV_COOR_ELEMENT);
121 if (Map->dig_fp.file == NULL)
122 return -1;
123
124 /* if overwrite OK, any existing files have already been deleted by
125 * Vect_open_new(): remove this check ? */
126 /* check to see if dig_plus file exists and if so, remove it */
128 if (access(path, F_OK) == 0)
129 unlink(path); /* remove topo file if exists */
130
131 /* set conversion matrices */
132 dig_init_portable(&(Map->head.port), dig__byte_order_out());
133
134 /* write coor header */
135 if (!(dig__write_head(Map)))
136 return -1;
137
138 return 0;
139}
140
141/* check file size */
142int check_coor(struct Map_info *Map)
143{
144 struct Coor_info CInfo;
145 off_t dif;
146
147 /* NOTE: coor file is open */
149 dif = CInfo.size - Map->head.size;
150 G_debug(1, "coor size in head = %lu, real coor file size= %lu",
151 (unsigned long)Map->head.size, (unsigned long)CInfo.size);
152
153 if (dif > 0) {
154 G_warning(
155 _("Coor file of vector map <%s@%s> is larger than it should be "
156 "(%" PRId64 " bytes excess)"),
157 Map->name, Map->mapset, dif);
158 }
159 else if (dif < 0) {
160 G_warning(_("Coor file of vector <%s@%s> is shorter than it should be "
161 "(%" PRId64 " bytes missing)."),
162 Map->name, Map->mapset, -dif);
163 }
164 return 1;
165}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
FILE * G_fopen_modify(const char *, const char *)
Open a database file for update (r+ mode)
Definition gis/open.c:313
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void G_warning(const char *,...) __attribute__((format(printf
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Definition gis/open.c:221
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition gis/open.c:253
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_coor_info(struct Map_info *, struct Coor_info *)
Update Coor_info structure.
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
#define GV_COOR_EARLIEST_MINOR
#define GV_COOR_VER_MAJOR
The latest versions of files known by current version of the library. Used for new files.
#define GV_COOR_ELEMENT
Native format, coordinates.
Definition dig_defines.h:12
#define GV_COOR_VER_MINOR
#define GV_TOPO_ELEMENT
Native format, topology file.
Definition dig_defines.h:20
#define GV_COOR_EARLIEST_MAJOR
The oldest versions of the library, which are capable to read the files created by the current versio...
int dig_file_load(struct gvfile *file)
Load opened struct gvfile to memory.
Definition file.c:188
int dig__read_head(struct Map_info *)
Definition head.c:87
int dig__byte_order_out(void)
Get byte order.
Definition portable.c:1008
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
Definition portable.c:900
int dig__write_head(struct Map_info *)
Definition head.c:23
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
Definition file.c:171
#define GPATH_MAX
Definition gis.h:199
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
int V1_open_old_nat(struct Map_info *Map, int update)
Open existing vector map (level 1)
Definition open_nat.c:40
int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
Create new vector map (level 1)
Definition open_nat.c:97
Coor file info.
Vector map info.
Definition path.h:15
#define unlink
Definition unistd.h:11
#define access
Definition unistd.h:7
#define F_OK
Definition unistd.h:22
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)
char * Vect__get_path(char *path, struct Map_info *Map)
Get map directory name (internal use only)