GRASS Programmer's Manual  6.4.2(2012)
vector/Vlib/header.c
Go to the documentation of this file.
00001 
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <grass/gis.h>
00024 #include <grass/Vect.h>
00025 #include <grass/glocale.h>
00026 
00027 static int lookup(const char *file, const char *key, char *value, size_t len);
00028 
00029 
00037 int Vect_print_header(struct Map_info *Map)
00038 {
00039     fprintf(stdout, "\nSelected information from dig header\n");
00040     fprintf(stdout, " Organization:  %s\n", Vect_get_organization(Map));
00041     fprintf(stdout, " Map Name:      %s\n", Vect_get_map_name(Map));
00042     fprintf(stdout, " Source Date:   %s\n", Vect_get_map_date(Map));
00043     fprintf(stdout, " Orig. Scale:   %d\n", Vect_get_scale(Map));
00044 
00045     return 0;
00046 }
00047 
00048 
00056 int Vect_read_header(struct Map_info *Map)
00057 {
00058     Vect__read_head(Map);
00059     return 0;
00060 }
00061 
00062 
00070 int Vect_write_header(struct Map_info *Map)
00071 {
00072     /* do some sanity checking here */
00073     Vect__write_head(Map);
00074     return 0;
00075 }
00076 
00077 
00086 int Vect__write_head(struct Map_info *Map)
00087 {
00088     char buf[200];
00089     FILE *head_fp;
00090 
00091     sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00092 
00093     head_fp = G_fopen_new(buf, GRASS_VECT_HEAD_ELEMENT);
00094     if (head_fp == NULL) {
00095         G_warning(_("Unable to open header file of vector <%s>"),
00096                   Vect_get_full_name(Map));
00097         return (GRASS_ERR);
00098     }
00099 
00100     fprintf(head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map));
00101     fprintf(head_fp, "DIGIT DATE:   %s\n", Vect_get_date(Map));
00102     fprintf(head_fp, "DIGIT NAME:   %s\n", Vect_get_person(Map));
00103     fprintf(head_fp, "MAP NAME:     %s\n", Vect_get_map_name(Map));
00104     fprintf(head_fp, "MAP DATE:     %s\n", Vect_get_map_date(Map));
00105     fprintf(head_fp, "MAP SCALE:    %d\n", Vect_get_scale(Map));
00106     fprintf(head_fp, "OTHER INFO:   %s\n", Vect_get_comment(Map));
00107     fprintf(head_fp, "ZONE:         %d\n", Vect_get_zone(Map));
00108     fprintf(head_fp, "MAP THRESH:   %f\n", Vect_get_thresh(Map));
00109 
00110     fclose(head_fp);
00111     return (GRASS_OK);
00112 }
00113 
00122 int Vect__read_head(struct Map_info *Map)
00123 {
00124     FILE *head_fp;
00125     char buff[2001];
00126     char *ptr;
00127 
00128     /* Reset / init */
00129     Vect_set_organization(Map, "");
00130     Vect_set_date(Map, "");
00131     Vect_set_person(Map, "");
00132     Vect_set_map_name(Map, "");
00133     Vect_set_map_date(Map, "");
00134     Vect_set_scale(Map, 1);
00135     Vect_set_comment(Map, "");
00136     Vect_set_zone(Map, 0);
00137     Vect_set_thresh(Map, 0.);
00138 
00139     G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
00140     sprintf(buff, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00141     head_fp = G_fopen_old(buff, GRASS_VECT_HEAD_ELEMENT, Map->mapset);
00142     if (head_fp == NULL) {
00143         G_warning(_("Unable to open header file of vector <%s>"),
00144                   Vect_get_full_name(Map));
00145         return (GRASS_ERR);
00146     }
00147 
00148     while (G_getl2(buff, 2000, head_fp)) {
00149 
00150         if (!(ptr = G_index(buff, ':'))) {
00151             G_warning(_("Corrupted row in head: %s"), buff);
00152             continue;
00153         }
00154 
00155         ptr++;                  /* Search for the start of text */
00156         while (*ptr == ' ')
00157             ptr++;
00158 
00159         if (strncmp(buff, "ORGANIZATION:", sizeof(char) * 12) == 0)
00160             Vect_set_organization(Map, ptr);
00161         else if (strncmp(buff, "DIGIT DATE:", sizeof(char) * 11) == 0)
00162             Vect_set_date(Map, ptr);
00163         else if (strncmp(buff, "DIGIT NAME:", sizeof(char) * 11) == 0)
00164             Vect_set_person(Map, ptr);
00165         else if (strncmp(buff, "MAP NAME:", sizeof(char) * 9) == 0)
00166             Vect_set_map_name(Map, ptr);
00167         else if (strncmp(buff, "MAP DATE:", sizeof(char) * 9) == 0)
00168             Vect_set_map_date(Map, ptr);
00169         else if (strncmp(buff, "MAP SCALE:", sizeof(char) * 10) == 0)
00170             Vect_set_scale(Map, atoi(ptr));
00171         else if (strncmp(buff, "OTHER INFO:", sizeof(char) * 11) == 0)
00172             Vect_set_comment(Map, ptr);
00173         else if (strncmp(buff, "PROJ:", sizeof(char) * 5) == 0)
00174             G_debug(1, "Projection code for map is %s", ptr);
00175         else if (strncmp(buff, "ZONE:", sizeof(char) * 5) == 0 ||
00176                  strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0)
00177             Vect_set_zone(Map, atoi(ptr));
00178         else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) {
00179         }
00180         else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) {
00181         }
00182         else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) {
00183         }
00184         else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) {
00185         }
00186         else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
00187             Vect_set_thresh(Map, atof(ptr));
00188         else
00189             G_warning(_("Unknown keyword %s in vector head"), buff);
00190     }
00191 
00192     fclose(head_fp);
00193     return (GRASS_OK);
00194 }
00195 
00203 const char *Vect_get_name(struct Map_info *Map)
00204 {
00205     return (Map->name);
00206 }
00207 
00215 const char *Vect_get_mapset(struct Map_info *Map)
00216 {
00217     return (Map->mapset);
00218 }
00219 
00227 const char *Vect_get_full_name(struct Map_info *Map)
00228 {
00229     char *ptr;
00230 
00231     ptr = (char *)G_malloc(strlen(Map->name) + strlen(Map->mapset) + 2);
00232     sprintf(ptr, "%s@%s", Map->name, Map->mapset);
00233     return (ptr);
00234 }
00235 
00244 int Vect_is_3d(struct Map_info *Map)
00245 {
00246     return (Map->head.with_z);
00247 }
00248 
00257 int Vect_set_organization(struct Map_info *Map, const char *str)
00258 {
00259     G_free(Map->head.organization);
00260     Map->head.organization = G_store(str);
00261 
00262     return 0;
00263 }
00264 
00272 const char *Vect_get_organization(struct Map_info *Map)
00273 {
00274     return (Map->head.organization);
00275 }
00276 
00288 int Vect_set_date(struct Map_info *Map, const char *str)
00289 {
00290     G_free(Map->head.date);
00291     Map->head.date = G_store(str);
00292     return (0);
00293 }
00294 
00305 const char *Vect_get_date(struct Map_info *Map)
00306 {
00307     return (Map->head.date);
00308 }
00309 
00318 int Vect_set_person(struct Map_info *Map, const char *str)
00319 {
00320     G_free(Map->head.your_name);
00321     Map->head.your_name = G_store(str);
00322     return (0);
00323 }
00324 
00332 const char *Vect_get_person(struct Map_info *Map)
00333 {
00334     return (Map->head.your_name);
00335 }
00336 
00345 int Vect_set_map_name(struct Map_info *Map, const char *str)
00346 {
00347     G_free(Map->head.map_name);
00348     Map->head.map_name = G_store(str);
00349     return (0);
00350 }
00351 
00359 const char *Vect_get_map_name(struct Map_info *Map)
00360 {
00361     return (Map->head.map_name);
00362 }
00363 
00372 int Vect_set_map_date(struct Map_info *Map, const char *str)
00373 {
00374     G_free(Map->head.source_date);
00375     Map->head.source_date = G_store(str);
00376     return (0);
00377 }
00378 
00386 const char *Vect_get_map_date(struct Map_info *Map)
00387 {
00388     return (Map->head.source_date);
00389 }
00390 
00399 int Vect_set_scale(struct Map_info *Map, int scale)
00400 {
00401     Map->head.orig_scale = scale;
00402     return (0);
00403 }
00404 
00412 int Vect_get_scale(struct Map_info *Map)
00413 {
00414     return ((int)Map->head.orig_scale);
00415 }
00416 
00425 int Vect_set_comment(struct Map_info *Map, const char *str)
00426 {
00427     G_free(Map->head.line_3);
00428     Map->head.line_3 = G_store(str);
00429     return (0);
00430 }
00431 
00439 const char *Vect_get_comment(struct Map_info *Map)
00440 {
00441     return (Map->head.line_3);
00442 }
00443 
00452 int Vect_set_zone(struct Map_info *Map, int zone)
00453 {
00454     Map->head.plani_zone = zone;
00455     return (0);
00456 }
00457 
00458 
00466 int Vect_get_zone(struct Map_info *Map)
00467 {
00468     return (Map->head.plani_zone);
00469 }
00470 
00481 int Vect_get_proj(struct Map_info *Map)
00482 {
00483     return (Map->proj);
00484 }
00485 
00486 
00499 const char *Vect_get_proj_name(struct Map_info *Map)
00500 {
00501     char name[256];
00502     int n;
00503 
00504     switch (n = Vect_get_proj(Map)) {
00505     case PROJECTION_XY:
00506     case PROJECTION_UTM:
00507     case PROJECTION_LL:
00508     case PROJECTION_SP:
00509         return G__projection_name(n);
00510     }
00511     if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
00512         strcpy(name, _("Unknown projection"));
00513     return G_store(name);
00514 }
00515 
00524 int Vect_set_thresh(struct Map_info *Map, double thresh)
00525 {
00526     G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
00527     Map->head.digit_thresh = thresh;
00528     return (0);
00529 }
00530 
00538 double Vect_get_thresh(struct Map_info *Map)
00539 {
00540     G_debug(1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh);
00541     return (Map->head.digit_thresh);
00542 }
00543 
00544 
00545 /* from lib/gis/proj3.c */
00546 static int lookup(const char *file, const char *key, char *value, size_t len)
00547 {
00548     char path[GPATH_MAX];
00549 
00550     G__file_name(path, "", file, "PERMANENT");
00551     return G_lookup_key_value_from_file(path, key, value, (int)len) == 1;
00552 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines