GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
open_nat.c
Go to the documentation of this file.
1 
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 
25 #include <grass/Vect.h>
26 #include <grass/gis.h>
27 #include <grass/glocale.h>
28 
29 static char name_buf[GPATH_MAX];
30 static int check_coor(struct Map_info *Map);
31 
43 int V1_open_old_nat(struct Map_info *Map, int update)
44 {
45  char buf[1000];
46 
47  G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
48  Map->mapset);
49 
50  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
51  dig_file_init(&(Map->dig_fp));
52  if (update)
53  Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
54  else
55  Map->dig_fp.file =
56  G_fopen_old(buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);
57 
58  if (Map->dig_fp.file == NULL)
59  return -1;
60 
61  if (!(dig__read_head(Map)))
62  return (-1);
63  check_coor(Map);
64 
65  /* set conversion matrices */
66  dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
67 
68  /* load to memory */
69  if (!update)
70  dig_file_load(&(Map->dig_fp));
71 
72  return (0);
73 }
74 
86 int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
87 {
88  char buf[1000];
89  struct stat info;
90 
91  G_debug(1, "V1_open_new_nat(): name = %s", name);
92 
93  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);
94 
95  /* Set the 'coor' file version */
96  Map->head.Version_Major = GV_COOR_VER_MAJOR;
97  Map->head.Version_Minor = GV_COOR_VER_MINOR;
98  Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
99  Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;
100 
101  /* TODO open better */
102  dig_file_init(&(Map->dig_fp));
103  Map->dig_fp.file = G_fopen_new(buf, GRASS_VECT_COOR_ELEMENT);
104  if (Map->dig_fp.file == NULL)
105  return (-1);
106  fclose(Map->dig_fp.file);
107 
108  dig_file_init(&(Map->dig_fp));
109  Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
110  if (Map->dig_fp.file == NULL)
111  return (-1);
112 
113  /* check to see if dig_plus file exists and if so, remove it */
114  G__file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
115  if (stat(name_buf, &info) == 0) /* file exists? */
116  unlink(name_buf);
117 
118  G__file_name(name_buf, buf, GRASS_VECT_COOR_ELEMENT, G_mapset());
119 
120  Map->head.size = 0;
121  Map->head.head_size = GV_COOR_HEAD_SIZE;
122  Vect__write_head(Map);
123 
124  /* set conversion matrices */
125  dig_init_portable(&(Map->head.port), dig__byte_order_out());
126 
127  if (!(dig__write_head(Map)))
128  return (-1);
129 
130  return 0;
131 }
132 
133 /* check file size */
134 int check_coor(struct Map_info *Map)
135 {
136  struct Coor_info CInfo;
137  long dif;
138 
139  Vect_coor_info(Map, &CInfo);
140  dif = CInfo.size - Map->head.size;
141  G_debug(1, "coor size in head = %ld, real coor file size= %ld",
142  Map->head.size, CInfo.size);
143 
144  if (dif > 0) {
145  G_warning(_("Coor files of vector map <%s@%s> is larger than it should be "
146  "(%ld bytes excess)"), Map->name, Map->mapset, dif);
147  }
148  else if (dif < 0) {
149  G_warning(_("Coor files of vector <%s@%s> is shorter than it should be "
150  "(%ld bytes missing)."), Map->name, Map->mapset, -dif);
151  }
152  return 1;
153 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
Open/Create new vector map.
Definition: open_nat.c:86
string name
Definition: render.py:1314
FILE * G_fopen_modify(const char *element, const char *name)
Open a database file for update (r+ mode)
Definition: gis/open.c:279
int dig__read_head(struct Map_info *Map)
Definition: head.c:61
void dig_init_portable(struct Port_info *port, int byte_order)
Definition: portable.c:568
int stat
Definition: g3dcolor.c:369
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:33
int Vect__write_head(struct Map_info *Map)
Writes head information to text file.
int Vect_coor_info(struct Map_info *Map, struct Coor_info *Info)
Update Coor_info structure.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:197
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void dig_file_init(GVFILE *file)
Initialize GVFILE.
Definition: file.c:168
fclose(fd)
int dig_file_load(GVFILE *file)
Load opened GVFILE to memory.
Definition: file.c:190
int dig__byte_order_out()
Definition: portable.c:646
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
int V1_open_old_nat(struct Map_info *Map, int update)
Open existing vector map.
Definition: open_nat.c:43
int dig__write_head(struct Map_info *Map)
Definition: head.c:22