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
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
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++;
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, "ZONE:", sizeof(char) * 5) == 0 ||
00174 strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0)
00175 Vect_set_zone(Map, atoi(ptr));
00176 else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) {
00177 }
00178 else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) {
00179 }
00180 else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) {
00181 }
00182 else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) {
00183 }
00184 else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
00185 Vect_set_thresh(Map, atof(ptr));
00186 else
00187 G_warning(_("Unknown keyword %s in vector head"), buff);
00188 }
00189
00190 fclose(head_fp);
00191 return (GRASS_OK);
00192 }
00193
00201 const char *Vect_get_name(struct Map_info *Map)
00202 {
00203 return (Map->name);
00204 }
00205
00213 const char *Vect_get_mapset(struct Map_info *Map)
00214 {
00215 return (Map->mapset);
00216 }
00217
00225 const char *Vect_get_full_name(struct Map_info *Map)
00226 {
00227 char *ptr;
00228
00229 ptr = (char *)G_malloc(strlen(Map->name) + strlen(Map->mapset) + 2);
00230 sprintf(ptr, "%s@%s", Map->name, Map->mapset);
00231 return (ptr);
00232 }
00233
00242 int Vect_is_3d(struct Map_info *Map)
00243 {
00244 return (Map->head.with_z);
00245 }
00246
00255 int Vect_set_organization(struct Map_info *Map, const char *str)
00256 {
00257 G_free(Map->head.organization);
00258 Map->head.organization = G_store(str);
00259
00260 return 0;
00261 }
00262
00270 const char *Vect_get_organization(struct Map_info *Map)
00271 {
00272 return (Map->head.organization);
00273 }
00274
00286 int Vect_set_date(struct Map_info *Map, const char *str)
00287 {
00288 G_free(Map->head.date);
00289 Map->head.date = G_store(str);
00290 return (0);
00291 }
00292
00303 const char *Vect_get_date(struct Map_info *Map)
00304 {
00305 return (Map->head.date);
00306 }
00307
00316 int Vect_set_person(struct Map_info *Map, const char *str)
00317 {
00318 G_free(Map->head.your_name);
00319 Map->head.your_name = G_store(str);
00320 return (0);
00321 }
00322
00330 const char *Vect_get_person(struct Map_info *Map)
00331 {
00332 return (Map->head.your_name);
00333 }
00334
00343 int Vect_set_map_name(struct Map_info *Map, const char *str)
00344 {
00345 G_free(Map->head.map_name);
00346 Map->head.map_name = G_store(str);
00347 return (0);
00348 }
00349
00357 const char *Vect_get_map_name(struct Map_info *Map)
00358 {
00359 return (Map->head.map_name);
00360 }
00361
00370 int Vect_set_map_date(struct Map_info *Map, const char *str)
00371 {
00372 G_free(Map->head.source_date);
00373 Map->head.source_date = G_store(str);
00374 return (0);
00375 }
00376
00384 const char *Vect_get_map_date(struct Map_info *Map)
00385 {
00386 return (Map->head.source_date);
00387 }
00388
00397 int Vect_set_scale(struct Map_info *Map, int scale)
00398 {
00399 Map->head.orig_scale = scale;
00400 return (0);
00401 }
00402
00410 int Vect_get_scale(struct Map_info *Map)
00411 {
00412 return ((int)Map->head.orig_scale);
00413 }
00414
00423 int Vect_set_comment(struct Map_info *Map, const char *str)
00424 {
00425 G_free(Map->head.line_3);
00426 Map->head.line_3 = G_store(str);
00427 return (0);
00428 }
00429
00437 const char *Vect_get_comment(struct Map_info *Map)
00438 {
00439 return (Map->head.line_3);
00440 }
00441
00450 int Vect_set_zone(struct Map_info *Map, int zone)
00451 {
00452 Map->head.plani_zone = zone;
00453 return (0);
00454 }
00455
00456
00464 int Vect_get_zone(struct Map_info *Map)
00465 {
00466 return (Map->head.plani_zone);
00467 }
00468
00479 int Vect_get_proj(struct Map_info *Map)
00480 {
00481 return (Map->proj);
00482 }
00483
00484
00497 const char *Vect_get_proj_name(struct Map_info *Map)
00498 {
00499 char name[256];
00500 int n;
00501
00502 switch (n = Vect_get_proj(Map)) {
00503 case PROJECTION_XY:
00504 case PROJECTION_UTM:
00505 case PROJECTION_LL:
00506 case PROJECTION_SP:
00507 return G__projection_name(n);
00508 }
00509 if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
00510 strcpy(name, _("Unknown projection"));
00511 return G_store(name);
00512 }
00513
00522 int Vect_set_thresh(struct Map_info *Map, double thresh)
00523 {
00524 G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
00525 Map->head.digit_thresh = thresh;
00526 return (0);
00527 }
00528
00536 double Vect_get_thresh(struct Map_info *Map)
00537 {
00538 G_debug(1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh);
00539 return (Map->head.digit_thresh);
00540 }
00541
00542
00543
00544 static int lookup(const char *file, const char *key, char *value, size_t len)
00545 {
00546 char path[GPATH_MAX];
00547
00548 G__file_name(path, "", file, "PERMANENT");
00549 return G_lookup_key_value_from_file(path, key, value, (int)len) == 1;
00550 }