GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vector/Vlib/header.c
Go to the documentation of this file.
1 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <grass/gis.h>
24 #include <grass/Vect.h>
25 #include <grass/glocale.h>
26 
27 static int lookup(const char *file, const char *key, char *value, size_t len);
28 
29 
37 int Vect_print_header(struct Map_info *Map)
38 {
39  fprintf(stdout, "\nSelected information from dig header\n");
40  fprintf(stdout, " Organization: %s\n", Vect_get_organization(Map));
41  fprintf(stdout, " Map Name: %s\n", Vect_get_map_name(Map));
42  fprintf(stdout, " Source Date: %s\n", Vect_get_map_date(Map));
43  fprintf(stdout, " Orig. Scale: %d\n", Vect_get_scale(Map));
44 
45  return 0;
46 }
47 
48 
56 int Vect_read_header(struct Map_info *Map)
57 {
58  Vect__read_head(Map);
59  return 0;
60 }
61 
62 
70 int Vect_write_header(struct Map_info *Map)
71 {
72  /* do some sanity checking here */
73  Vect__write_head(Map);
74  return 0;
75 }
76 
77 
86 int Vect__write_head(struct Map_info *Map)
87 {
88  char buf[200];
89  FILE *head_fp;
90 
91  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
92 
93  head_fp = G_fopen_new(buf, GRASS_VECT_HEAD_ELEMENT);
94  if (head_fp == NULL) {
95  G_warning(_("Unable to open header file of vector <%s>"),
96  Vect_get_full_name(Map));
97  return (GRASS_ERR);
98  }
99 
100  fprintf(head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map));
101  fprintf(head_fp, "DIGIT DATE: %s\n", Vect_get_date(Map));
102  fprintf(head_fp, "DIGIT NAME: %s\n", Vect_get_person(Map));
103  fprintf(head_fp, "MAP NAME: %s\n", Vect_get_map_name(Map));
104  fprintf(head_fp, "MAP DATE: %s\n", Vect_get_map_date(Map));
105  fprintf(head_fp, "MAP SCALE: %d\n", Vect_get_scale(Map));
106  fprintf(head_fp, "OTHER INFO: %s\n", Vect_get_comment(Map));
107  fprintf(head_fp, "ZONE: %d\n", Vect_get_zone(Map));
108  fprintf(head_fp, "MAP THRESH: %f\n", Vect_get_thresh(Map));
109 
110  fclose(head_fp);
111  return (GRASS_OK);
112 }
113 
122 int Vect__read_head(struct Map_info *Map)
123 {
124  FILE *head_fp;
125  char buff[2001];
126  char *ptr;
127 
128  /* Reset / init */
129  Vect_set_organization(Map, "");
130  Vect_set_date(Map, "");
131  Vect_set_person(Map, "");
132  Vect_set_map_name(Map, "");
133  Vect_set_map_date(Map, "");
134  Vect_set_scale(Map, 1);
135  Vect_set_comment(Map, "");
136  Vect_set_zone(Map, 0);
137  Vect_set_thresh(Map, 0.);
138 
139  G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
140  sprintf(buff, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
141  head_fp = G_fopen_old(buff, GRASS_VECT_HEAD_ELEMENT, Map->mapset);
142  if (head_fp == NULL) {
143  G_warning(_("Unable to open header file of vector <%s>"),
144  Vect_get_full_name(Map));
145  return (GRASS_ERR);
146  }
147 
148  while (G_getl2(buff, 2000, head_fp)) {
149 
150  if (!(ptr = G_index(buff, ':'))) {
151  G_warning(_("Corrupted row in head: %s"), buff);
152  continue;
153  }
154 
155  ptr++; /* Search for the start of text */
156  while (*ptr == ' ')
157  ptr++;
158 
159  if (strncmp(buff, "ORGANIZATION:", sizeof(char) * 12) == 0)
160  Vect_set_organization(Map, ptr);
161  else if (strncmp(buff, "DIGIT DATE:", sizeof(char) * 11) == 0)
162  Vect_set_date(Map, ptr);
163  else if (strncmp(buff, "DIGIT NAME:", sizeof(char) * 11) == 0)
164  Vect_set_person(Map, ptr);
165  else if (strncmp(buff, "MAP NAME:", sizeof(char) * 9) == 0)
166  Vect_set_map_name(Map, ptr);
167  else if (strncmp(buff, "MAP DATE:", sizeof(char) * 9) == 0)
168  Vect_set_map_date(Map, ptr);
169  else if (strncmp(buff, "MAP SCALE:", sizeof(char) * 10) == 0)
170  Vect_set_scale(Map, atoi(ptr));
171  else if (strncmp(buff, "OTHER INFO:", sizeof(char) * 11) == 0)
172  Vect_set_comment(Map, ptr);
173  else if (strncmp(buff, "PROJ:", sizeof(char) * 5) == 0)
174  G_debug(1, "Projection code for map is %s", ptr);
175  else if (strncmp(buff, "ZONE:", sizeof(char) * 5) == 0 ||
176  strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0)
177  Vect_set_zone(Map, atoi(ptr));
178  else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) {
179  }
180  else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) {
181  }
182  else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) {
183  }
184  else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) {
185  }
186  else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
187  Vect_set_thresh(Map, atof(ptr));
188  else
189  G_warning(_("Unknown keyword %s in vector head"), buff);
190  }
191 
192  fclose(head_fp);
193  return (GRASS_OK);
194 }
195 
203 const char *Vect_get_name(struct Map_info *Map)
204 {
205  return (Map->name);
206 }
207 
215 const char *Vect_get_mapset(struct Map_info *Map)
216 {
217  return (Map->mapset);
218 }
219 
227 const char *Vect_get_full_name(struct Map_info *Map)
228 {
229  char *ptr;
230 
231  ptr = (char *)G_malloc(strlen(Map->name) + strlen(Map->mapset) + 2);
232  sprintf(ptr, "%s@%s", Map->name, Map->mapset);
233  return (ptr);
234 }
235 
244 int Vect_is_3d(struct Map_info *Map)
245 {
246  return (Map->head.with_z);
247 }
248 
257 int Vect_set_organization(struct Map_info *Map, const char *str)
258 {
259  G_free(Map->head.organization);
260  Map->head.organization = G_store(str);
261 
262  return 0;
263 }
264 
272 const char *Vect_get_organization(struct Map_info *Map)
273 {
274  return (Map->head.organization);
275 }
276 
288 int Vect_set_date(struct Map_info *Map, const char *str)
289 {
290  G_free(Map->head.date);
291  Map->head.date = G_store(str);
292  return (0);
293 }
294 
305 const char *Vect_get_date(struct Map_info *Map)
306 {
307  return (Map->head.date);
308 }
309 
318 int Vect_set_person(struct Map_info *Map, const char *str)
319 {
320  G_free(Map->head.your_name);
321  Map->head.your_name = G_store(str);
322  return (0);
323 }
324 
332 const char *Vect_get_person(struct Map_info *Map)
333 {
334  return (Map->head.your_name);
335 }
336 
345 int Vect_set_map_name(struct Map_info *Map, const char *str)
346 {
347  G_free(Map->head.map_name);
348  Map->head.map_name = G_store(str);
349  return (0);
350 }
351 
359 const char *Vect_get_map_name(struct Map_info *Map)
360 {
361  return (Map->head.map_name);
362 }
363 
372 int Vect_set_map_date(struct Map_info *Map, const char *str)
373 {
374  G_free(Map->head.source_date);
375  Map->head.source_date = G_store(str);
376  return (0);
377 }
378 
386 const char *Vect_get_map_date(struct Map_info *Map)
387 {
388  return (Map->head.source_date);
389 }
390 
399 int Vect_set_scale(struct Map_info *Map, int scale)
400 {
401  Map->head.orig_scale = scale;
402  return (0);
403 }
404 
412 int Vect_get_scale(struct Map_info *Map)
413 {
414  return ((int)Map->head.orig_scale);
415 }
416 
425 int Vect_set_comment(struct Map_info *Map, const char *str)
426 {
427  G_free(Map->head.line_3);
428  Map->head.line_3 = G_store(str);
429  return (0);
430 }
431 
439 const char *Vect_get_comment(struct Map_info *Map)
440 {
441  return (Map->head.line_3);
442 }
443 
452 int Vect_set_zone(struct Map_info *Map, int zone)
453 {
454  Map->head.plani_zone = zone;
455  return (0);
456 }
457 
458 
466 int Vect_get_zone(struct Map_info *Map)
467 {
468  return (Map->head.plani_zone);
469 }
470 
481 int Vect_get_proj(struct Map_info *Map)
482 {
483  return (Map->proj);
484 }
485 
486 
499 const char *Vect_get_proj_name(struct Map_info *Map)
500 {
501  char name[256];
502  int n;
503 
504  switch (n = Vect_get_proj(Map)) {
505  case PROJECTION_XY:
506  case PROJECTION_UTM:
507  case PROJECTION_LL:
508  case PROJECTION_SP:
509  return G__projection_name(n);
510  }
511 
512  /* Vect_get_proj() didn't return a useful result,
513  fallback to G_database_projection_name() */
514  /* (is this behavior desirable?) */
515  if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
516  strcpy(name, _("Unknown projection"));
517 
518  return G_store(name);
519 }
520 
529 int Vect_set_thresh(struct Map_info *Map, double thresh)
530 {
531  G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
532  Map->head.digit_thresh = thresh;
533  return (0);
534 }
535 
543 double Vect_get_thresh(struct Map_info *Map)
544 {
545  G_debug(1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh);
546  return (Map->head.digit_thresh);
547 }
548 
549 
550 /* from lib/gis/proj3.c */
551 static int lookup(const char *file, const char *key, char *value, size_t len)
552 {
553  char path[GPATH_MAX];
554 
555  G__file_name(path, "", file, "PERMANENT");
556  return G_lookup_key_value_from_file(path, key, value, (int)len) == 1;
557 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int Vect_get_scale(struct Map_info *Map)
Get map scale from map header.
const char * Vect_get_proj_name(struct Map_info *Map)
Query cartographic projection name of vector map.
int Vect_set_organization(struct Map_info *Map, const char *str)
Set organization string in map header.
int Vect_set_scale(struct Map_info *Map, int scale)
Set map scale in map header.
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
const char * Vect_get_name(struct Map_info *Map)
Get map name.
string name
Definition: render.py:1314
char * G_index(const char *str, int delim)
delimiter
Definition: gis/index.c:16
int Vect_read_header(struct Map_info *Map)
Read vector map header from map head file.
int Vect_get_proj(struct Map_info *Map)
Get projection from map header.
int Vect_write_header(struct Map_info *Map)
Write vector map header to map head file.
int Vect_set_date(struct Map_info *Map, const char *str)
Set date of digitization string 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_full_name(struct Map_info *Map)
Get full map name.
int G_lookup_key_value_from_file(const char *file, const char *key, char value[], int n)
Look up for key in file.
Definition: key_value4.c:64
char buff[1024]
Definition: g3dcats.c:89
int Vect_is_3d(struct Map_info *Map)
Check if vector map is 3D (with z)
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
Definition: getl.c:52
int Vect_set_zone(struct Map_info *Map, int zone)
Set projection zone in map header.
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
const char * Vect_get_date(struct Map_info *Map)
Get date of digitization string from map header.
int Vect_set_map_date(struct Map_info *Map, const char *str)
Set date string when the source map was originally produced in map header.
int Vect_set_map_name(struct Map_info *Map, const char *str)
Set map name string in map header.
const char * Vect_get_map_date(struct Map_info *Map)
Get date string when the source map was originally produced in map header.
char * value
Definition: env.c:30
int Vect__read_head(struct Map_info *Map)
Reads head information from text file (GRASS_VECT_HEAD_ELEMENT).
int Vect__write_head(struct Map_info *Map)
Writes head information to text file.
double Vect_get_thresh(struct Map_info *Map)
Get threshold used for digitization from map header.
int Vect_get_zone(struct Map_info *Map)
Get projection zone from map header.
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
fclose(fd)
#define file
char * G__projection_name(int n)
Definition: proj2.c:36
const char * Vect_get_person(struct Map_info *Map)
Get user name string who digitized the map from map header.
const char * Vect_get_mapset(struct Map_info *Map)
Get mapset name.
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 Vect_set_thresh(struct Map_info *Map, double thresh)
Set threshold used for digitization in map header.
int n
Definition: dataquad.c:291
int Vect_set_comment(struct Map_info *Map, const char *str)
Set comment or other info string in map header.
const char * Vect_get_map_name(struct Map_info *Map)
Get map name string in map header.
int Vect_set_person(struct Map_info *Map, const char *str)
Set user name string who digitized the map in map header.
int Vect_print_header(struct Map_info *Map)
Print vector map header.
const char * Vect_get_organization(struct Map_info *Map)
Get organization string from map header.