header.c

Go to the documentation of this file.
00001 /*
00002 ****************************************************************************
00003 *
00004 * MODULE:       Vector library 
00005 *               
00006 * AUTHOR(S):    Original author CERL, probably Dave Gerdes or Mike Higgins.
00007 *               Update to GRASS 5.7 Radim Blazek and David D. Gray.
00008 *
00009 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00010 *
00011 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012 *
00013 *               This program is free software under the GNU General Public
00014 *               License (>=v2). Read the file COPYING that comes with GRASS
00015 *               for details.
00016 *
00017 *****************************************************************************/
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #include <grass/gis.h>
00021 #include <grass/Vect.h>
00022 #include <grass/glocale.h>
00023 
00024 
00025 static int lookup(char *file, char *key, char *value, size_t len);
00026 
00027 
00034 int 
00035 Vect_print_header (struct Map_info *Map)
00036 {
00037   fprintf (stdout, "\nSelected information from dig header\n");
00038   fprintf (stdout, " Organization:  %s\n", Vect_get_organization(Map) );
00039   fprintf (stdout, " Map Name:      %s\n", Vect_get_map_name(Map) );
00040   fprintf (stdout, " Source Date:   %s\n", Vect_get_map_date(Map) );
00041   fprintf (stdout, " Orig. Scale:   %d\n", Vect_get_scale(Map)  );
00042 
00043   return 0;
00044 }
00045 
00046 /* Vect__write_head () writes head information to text file.
00047  * returns: GRASS_OK - success
00048  *          GRASS_ERR - error
00049  */
00050 int
00051 Vect__write_head (struct Map_info *Map)
00052 {
00053     char buf[200];      
00054     FILE *head_fp;
00055 
00056     sprintf (buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00057 
00058     head_fp = G_fopen_new (buf, GRASS_VECT_HEAD_ELEMENT);
00059     if ( head_fp == NULL)
00060       {
00061         G_warning ("Cannot Open Vector %s@%s Head File\n", Map->name, Map->mapset);
00062         return (GRASS_ERR);
00063       }
00064         
00065     fprintf (head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map) );
00066     fprintf (head_fp, "DIGIT DATE:   %s\n", Vect_get_date(Map) );
00067     fprintf (head_fp, "DIGIT NAME:   %s\n", Vect_get_person(Map) );
00068     fprintf (head_fp, "MAP NAME:     %s\n", Vect_get_map_name(Map) );
00069     fprintf (head_fp, "MAP DATE:     %s\n", Vect_get_map_date(Map) );
00070     fprintf (head_fp, "MAP SCALE:    %d\n", Vect_get_scale(Map) );
00071     fprintf (head_fp, "OTHER INFO:   %s\n", Vect_get_comment(Map) );
00072     fprintf (head_fp, "ZONE:         %d\n", Vect_get_zone(Map) );
00073     fprintf (head_fp, "MAP THRESH:   %f\n", Vect_get_thresh(Map) );
00074     
00075     fclose (head_fp);
00076     return (GRASS_OK);
00077 }
00078 
00079 /* Vect__read_head () reads head information from text file (GRASS_VECT_HEAD_ELEMENT). 
00080  * returns: GRASS_OK - success
00081  *          GRASS_ERR - error
00082  */
00083 int
00084 Vect__read_head (struct Map_info *Map)
00085 {
00086     FILE *head_fp;
00087     char buff[2001];
00088     char *ptr;
00089 
00090     /* Reset / init */
00091     Vect_set_organization ( Map, "" );  
00092     Vect_set_date ( Map, "" );  
00093     Vect_set_person ( Map, "" );  
00094     Vect_set_map_name ( Map, "" );  
00095     Vect_set_map_date ( Map, "" );  
00096     Vect_set_scale ( Map, 1 );  
00097     Vect_set_comment ( Map, "" );  
00098     Vect_set_zone ( Map, 0 );  
00099     Vect_set_thresh ( Map, 0. );  
00100 
00101     G_debug (1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
00102     sprintf (buff, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00103     head_fp = G_fopen_old (buff, GRASS_VECT_HEAD_ELEMENT, Map->mapset); 
00104     if ( head_fp == NULL)
00105       {
00106         G_warning ("Cannot Open Vector %s Head File\n", Map->name);
00107         return (GRASS_ERR);
00108       }
00109    
00110     while ( G_getl2 (buff, 2000, head_fp) ) {
00111 
00112         if (!(ptr = G_index (buff, ':'))) {
00113           G_warning ( "Corrupted row in head: %s\n", buff );
00114           continue;
00115         }
00116 
00117         ptr++;                  /* Search for the start of text */
00118         while (*ptr == ' ')
00119         ptr++;
00120 
00121         if (strncmp (buff, "ORGANIZATION:", sizeof(char)*12) == 0)
00122           Vect_set_organization ( Map, ptr );  
00123         else if (strncmp (buff, "DIGIT DATE:", sizeof(char)*11) == 0)
00124           Vect_set_date ( Map, ptr );  
00125         else if (strncmp (buff, "DIGIT NAME:", sizeof(char)*11) == 0)
00126           Vect_set_person ( Map, ptr );  
00127         else if (strncmp (buff, "MAP NAME:", sizeof(char)*9) == 0)
00128           Vect_set_map_name ( Map, ptr );  
00129         else if (strncmp (buff, "MAP DATE:", sizeof(char)*9) == 0)
00130           Vect_set_map_date ( Map, ptr );  
00131         else if (strncmp (buff, "MAP SCALE:", sizeof(char)*10) == 0)
00132           Vect_set_scale ( Map, atoi (ptr) );  
00133         else if (strncmp (buff, "OTHER INFO:", sizeof(char)*11) == 0)
00134           Vect_set_comment ( Map, ptr );  
00135         else if (strncmp (buff, "ZONE:", sizeof(char)*5) == 0 || strncmp (buff, "UTM ZONE:", sizeof(char)*9) == 0)
00136           Vect_set_zone ( Map, atoi (ptr) );  
00137         else if (strncmp (buff, "WEST EDGE:", sizeof(char)*10) == 0) {}
00138         else if (strncmp (buff, "EAST EDGE:", sizeof(char)*10) == 0) {}
00139         else if (strncmp (buff, "SOUTH EDGE:", sizeof(char)*11) == 0) {}
00140         else if (strncmp (buff, "NORTH EDGE:", sizeof(char)*11) == 0) {}
00141         else if (strncmp (buff, "MAP THRESH:", sizeof(char)*11) == 0)
00142           Vect_set_thresh ( Map, atof (ptr) );  
00143         else 
00144           G_warning("Unknown keyword %s in vector head\n", buff);
00145     }
00146     
00147     fclose (head_fp);
00148     return (GRASS_OK);
00149 }
00150 
00151 /* set and get header informations */
00152 /* name, mapset, full name */
00159 char *
00160 Vect_get_name (struct Map_info *Map)
00161 {
00162     return (Map->name);
00163 }
00164 
00171 char *
00172 Vect_get_mapset (struct Map_info *Map)
00173 {
00174     return (Map->mapset);
00175 }
00176 
00183 char *
00184 Vect_get_full_name (struct Map_info *Map)
00185 {
00186     char *ptr;
00187 
00188     ptr = G_malloc ( strlen(Map->name) +  strlen(Map->mapset) + 2 );
00189     sprintf (ptr, "%s@%s", Map->name, Map->mapset);
00190     return (ptr);
00191 }
00192 
00199 int
00200 Vect_is_3d (struct Map_info *Map )
00201 {
00202     return ( Map->head.with_z );
00203 }
00204 
00211 int
00212 Vect_set_organization (struct Map_info *Map, char *str )
00213 {
00214     G_free ( Map->head.organization );
00215     Map->head.organization = G_store ( str );
00216     return (0);
00217 }
00218 
00225 char *
00226 Vect_get_organization (struct Map_info *Map)
00227 {
00228     return (Map->head.organization);
00229 }
00230 
00237 /* SUGGESTION: this should be coupled to DateTime functions to support time series*/
00238 int
00239 Vect_set_date (struct Map_info *Map, char *str )
00240 {
00241     G_free ( Map->head.date );
00242     Map->head.date = G_store ( str );
00243     return (0);
00244 }
00245 
00252 /* SUGGESTION: this should be coupled to DateTime functions to support time series*/
00253 char *
00254 Vect_get_date (struct Map_info *Map)
00255 {
00256     return (Map->head.date);
00257 }
00258 
00265 int
00266 Vect_set_person (struct Map_info *Map, char *str )
00267 {
00268     G_free ( Map->head.your_name );
00269     Map->head.your_name = G_store ( str );
00270     return (0);
00271 }
00272 
00279 char *
00280 Vect_get_person (struct Map_info *Map)
00281 {
00282     return (Map->head.your_name);
00283 }
00284 
00291 int
00292 Vect_set_map_name (struct Map_info *Map, char *str )
00293 {
00294     G_free ( Map->head.map_name );
00295     Map->head.map_name = G_store ( str );
00296     return (0);
00297 }
00298 
00305 char *
00306 Vect_get_map_name (struct Map_info *Map)
00307 {
00308     return (Map->head.map_name);
00309 }
00310 
00317 int
00318 Vect_set_map_date (struct Map_info *Map, char *str )
00319 {
00320     G_free ( Map->head.source_date );
00321     Map->head.source_date = G_store ( str );
00322     return (0);
00323 }
00324 
00331 char *
00332 Vect_get_map_date (struct Map_info *Map)
00333 {
00334     return (Map->head.source_date);
00335 }
00336 
00343 int
00344 Vect_set_scale (struct Map_info *Map, int scale )
00345 {
00346     Map->head.orig_scale = scale;
00347     return (0);
00348 }
00349 
00356 int
00357 Vect_get_scale (struct Map_info *Map)
00358 {
00359     return ((int) Map->head.orig_scale);
00360 }
00361 
00368 int
00369 Vect_set_comment (struct Map_info *Map, char *str )
00370 {
00371     G_free ( Map->head.line_3 );
00372     Map->head.line_3 = G_store ( str );
00373     return (0);
00374 }
00375 
00382 char *
00383 Vect_get_comment (struct Map_info *Map)
00384 {
00385     return (Map->head.line_3);
00386 }
00387 
00394 int
00395 Vect_set_zone (struct Map_info *Map, int zone )
00396 {
00397     Map->head.plani_zone = zone;
00398     return (0);
00399 }
00400 
00401 
00408 int
00409 Vect_get_zone (struct Map_info *Map)
00410 {
00411     return (Map->head.plani_zone);
00412 }
00413 
00424 int
00425 Vect_get_proj (struct Map_info *Map)
00426 {
00427     return (Map->proj);
00428 }
00429 
00430 
00443 char *Vect_get_proj_name (struct Map_info *Map)
00444 {
00445     int n;
00446     static char name[256];
00447     char *G__projection_name();
00448 
00449     switch(n=Vect_get_proj(Map))
00450     {
00451     case PROJECTION_XY:
00452     case PROJECTION_UTM:
00453     case PROJECTION_LL:
00454     case PROJECTION_SP:
00455         return G__projection_name(n);
00456     }
00457     if(!lookup (PROJECTION_FILE, "name", name, sizeof(name)))
00458         strcpy (name, _("Unknown projection"));
00459     return name;
00460 }
00461 
00462 
00469 int
00470 Vect_set_thresh (struct Map_info *Map, double thresh )
00471 {
00472     G_debug ( 1, "Vect_set_thresh(): thresh = %f", thresh );
00473     Map->head.digit_thresh = thresh;
00474     return (0);
00475 }
00476 
00483 double
00484 Vect_get_thresh (struct Map_info *Map)
00485 {
00486     G_debug ( 1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh );
00487     return (Map->head.digit_thresh);
00488 }
00489 
00490 
00491 /* from lib/gis/proj3.c */
00492 static int lookup(char *file, char *key, char *value, size_t len)
00493 {
00494     char path[1024];
00495 
00496     G__file_name (path, "", file, "PERMANENT");
00497     return G_lookup_key_value_from_file(path, key, value, (int)len) == 1;
00498 }

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5