[GRASS-dev] adding units and vertical datum metadata to raster maps

Hamish hamish_nospam at yahoo.com
Wed May 9 07:31:28 EDT 2007


(wish #392)

Hi,

I have hacked together the attached code to add units and vertical datum
metadata to raster maps. It still needs some cleanup before going into
CVS, but right now I am interested in comments on the approach taken.
[it works]

TODO: buffer overflow checking


TODO: create a fn in r.info to take care of G_asprint() work, right now
the "fancy output" section of the module is an ugly mess.
 [non-functional code, due to me being lost WRT passing pointers]

/*
        if (G_asprintf(&line, "Location: %s", G_location()) > 0)
            printline(line);
        else
            G_fatal_error(_("Cannot allocate memory for string"));
*/
        compose_line( &line, "Location: %s", G_location() );
        printline(line);
....

void compose_line(char *line, const char *fmt, ...)
{
    va_list ap;

    line = NULL;
    va_start(ap, fmt);

    if( G_asprintf(line, fmt, ap) <= 0 )
        G_fatal_error(_("Cannot allocate memory for string"));

    va_end(ap);
}


Hamish
-------------- next part --------------
Index: include/gisdefs.h
===================================================================
RCS file: /home/grass/grassrepository/grass6/include/gisdefs.h,v
retrieving revision 1.99
diff -u -r1.99 gisdefs.h
--- include/gisdefs.h	2 May 2007 06:00:33 -0000	1.99
+++ include/gisdefs.h	9 May 2007 11:14:37 -0000
@@ -23,11 +23,11 @@
 
 /*=========================== Constants/Defines ============================*/
 
-/* none */
+/* none (look in gis.h) */
 
 /*=========================== Typedefs/Structures ==========================*/
 
-/* none */
+/* none (look in gis.h) */
 
 /*============================== Prototypes ================================*/
 
@@ -1010,6 +1010,14 @@
 CELL G_get_raster_value_c(const void *, RASTER_MAP_TYPE);
 FCELL G_get_raster_value_f(const void *, RASTER_MAP_TYPE);
 DCELL G_get_raster_value_d(const void *, RASTER_MAP_TYPE);
+
+/* raster_metadata.c */
+int G_read_raster_units(const char *, const char *, char *);
+int G_read_raster_vdatum(const char *, const char *, char *);
+int G_write_raster_units(const char *, const char *);
+int G_write_raster_vdatum(const char *, const char *);
+int G__raster_misc_read_line(const char *, const char *, const char *, char *);
+int G__raster_misc_write_line(const char *, const char *, const char *);
 
 /* rd_cellhd.c */
 char *G__read_Cell_head(FILE *, struct Cell_head *, int);
Index: raster/r.info/main.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/raster/r.info/main.c,v
retrieving revision 2.18
diff -u -r2.18 main.c
--- raster/r.info/main.c	8 May 2007 08:02:09 -0000	2.18
+++ raster/r.info/main.c	9 May 2007 11:14:37 -0000
@@ -17,8 +17,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <grass/gis.h>
-#include "local_proto.h"
 #include <grass/glocale.h>
+#include "local_proto.h"
 
 #define printline(x) fprintf (out," | %-74.74s |\n",x)
 #define divider(x) \
@@ -38,6 +38,7 @@
     char *line = NULL;
     char tmp1[100], tmp2[100], tmp3[100];
     char timebuff[256];
+    char units[GNAME_MAX], vdatum[GNAME_MAX];
     int i, ret;
     CELL mincat = 0, maxcat = 0, cat;
     double zmin, zmax;		/* min and max data values */
@@ -62,7 +63,7 @@
     struct Flag *tflag;
     struct Flag *timestampflag;
     struct Flag *gflag;
-    struct Flag *hflag;
+    struct Flag *hflag, *uflag, *dflag;
 
     G_gisinit(argv[0]);
 
@@ -93,6 +94,14 @@
     hflag->key = 'h';
     hflag->description = _("Print raster history instead of info");
 
+    uflag = G_define_flag();
+    uflag->key = 'u';
+    uflag->description = _("Print raster map data units only");
+
+    dflag = G_define_flag();
+    dflag->key = 'd';
+    dflag->description = _("Print raster map vertical datum only");
+
     timestampflag = G_define_flag();
     timestampflag->key = 'p';
     timestampflag->description =
@@ -112,6 +121,11 @@
     is_reclass = G_get_reclass(name, mapset, &reclass);
     data_type = G_raster_map_type(name, mapset);
 
+    if( G_read_raster_units(name, mapset, units) != 0)
+	units[0] = '\0';
+    if( G_read_raster_vdatum(name, mapset, vdatum) != 0)
+	vdatum[0] = '\0';
+
     /*Check the Timestamp */
     time_ok = G_read_raster_timestamp(name, mapset, &ts) > 0;
     /*Check for valid entries, show none if no timestamp available */
@@ -129,7 +143,8 @@
     out = stdout;
 
     if (!rflag->answer && !sflag->answer && !tflag->answer && !gflag->answer &&
-	!hflag->answer && !timestampflag->answer) {
+	!hflag->answer && !timestampflag->answer && !uflag->answer && !dflag->answer)
+    {
 	divider('+');
 
 	if (G_asprintf
@@ -204,6 +219,15 @@
 	else
 	    G_fatal_error(_("Cannot allocate memory for string"));
 
+	if(units[0] || vdatum[0]) {
+	if (G_asprintf
+	    (&line, "  Data Units:   %-20.20s Vertical datum: %-9s",
+		units, vdatum ) > 0)
+	    printline(line);
+	else
+	    G_fatal_error(_("Cannot allocate memory for string"));
+	}
+
 	if (head_ok) {
 	    if (G_asprintf(&line, "  Rows:         %d", cellhd.rows) > 0)
 		printline(line);
@@ -430,6 +454,10 @@
 		fprintf(out, "timestamp=\"none\"\n");
 	    }
 	}
+	if (uflag->answer)
+	    fprintf(out, "units=%s\n", units);
+	if (dflag->answer)
+	    fprintf(out, "vertical_datum=%s\n", vdatum);
 
 	if (hflag->answer) {
 	    if (hist_ok) {
@@ -445,10 +473,11 @@
 		}
 	    }
 	}
-    }				/* else rflag or sflag or tflag or gflag or hflag */
+    }	/* else rflag or sflag or tflag or gflag or hflag */
 
     return EXIT_SUCCESS;
 }
+
 
 /**************************************************************************/
 int format_double(double value, char *buf)
Index: raster/r.support/front/front.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/raster/r.support/front/front.c,v
retrieving revision 1.15
diff -u -r1.15 front.c
--- raster/r.support/front/front.c	5 May 2007 14:24:53 -0000	1.15
+++ raster/r.support/front/front.c	9 May 2007 11:14:37 -0000
@@ -37,7 +37,7 @@
     char *mapset;		/* Raster mapset      */
     struct Cell_head cellhd;
     struct GModule *module;
-    struct Option *raster, *title_opt, *history_opt, *map_opt;
+    struct Option *raster, *title_opt, *history_opt, *map_opt, *units_opt, *vdatum_opt;
     char buf[512];
     int cellhd_ok;		/* Is cell header OK? */
     int is_reclass;		/* Is raster reclass? */
@@ -71,6 +71,18 @@
     history_opt->required    = NO;
     history_opt->description = _("Text to append to the next line of the map's metadata file");
 
+    units_opt = G_define_option();
+    units_opt->key = "units";
+    units_opt->type        = TYPE_STRING;
+    units_opt->required    = NO;
+    units_opt->description = _("Text to use for map data units");
+
+    vdatum_opt = G_define_option();
+    vdatum_opt->key = "vdatum";
+    vdatum_opt->type        = TYPE_STRING;
+    vdatum_opt->required    = NO;
+    vdatum_opt->description = _("Text to use for map vertical datum");
+
     map_opt = G_define_option();
     map_opt->key = "rast";
     map_opt->type = TYPE_STRING;
@@ -78,6 +90,7 @@
     map_opt->gisprompt = "old,cell,raster";
     map_opt->description = _("Raster map name from which to copy category table");
 
+
     /* Parse command-line options */
     if (G_parser(argc,argv))
 	exit(EXIT_FAILURE);
@@ -97,9 +110,6 @@
 	G_strip(title);
 	G_debug(3, "map title= [%s]  (%d chars)", title, strlen(title));
 	G_put_cell_title(raster->answer, title);
-
-	if(! history_opt->answer)
-	    exit(EXIT_SUCCESS);
     }
 
     if(history_opt->answer) {
@@ -141,8 +151,19 @@
         }
 
 	G_write_history(raster->answer, &hist);
-	exit(EXIT_SUCCESS);
     }
+
+    if(units_opt->answer)
+	G_write_raster_units(raster->answer, units_opt->answer);
+
+    if(vdatum_opt->answer)
+	G_write_raster_vdatum(raster->answer, vdatum_opt->answer);
+
+
+    if(title_opt->answer || history_opt->answer || units_opt->answer || vdatum_opt->answer)
+	exit(EXIT_SUCCESS);
+
+
 
     if (map_opt->answer) {	/* use cats from another map */
 	int fd;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: raster_metadata.c
Type: text/x-csrc
Size: 2419 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-dev/attachments/20070509/923f475d/raster_metadata.bin


More information about the grass-dev mailing list