[GRASS-SVN] r32627 - grass/trunk/raster/r.support

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 7 17:27:37 EDT 2008


Author: glynn
Date: 2008-08-07 17:27:37 -0400 (Thu, 07 Aug 2008)
New Revision: 32627

Added:
   grass/trunk/raster/r.support/Makefile
   grass/trunk/raster/r.support/check.c
   grass/trunk/raster/r.support/description.html
   grass/trunk/raster/r.support/front.c
   grass/trunk/raster/r.support/histo.c
   grass/trunk/raster/r.support/local_proto.h
Removed:
   grass/trunk/raster/r.support/Makefile
   grass/trunk/raster/r.support/front/
   grass/trunk/raster/r.support/modcats/
   grass/trunk/raster/r.support/modcolr/
   grass/trunk/raster/r.support/modhead/
   grass/trunk/raster/r.support/modhist/
Log:
Remove terminal-interactive sub-modules
Replace yes/no questions with flags


Deleted: grass/trunk/raster/r.support/Makefile
===================================================================
--- grass/trunk/raster/r.support/Makefile	2008-08-07 21:26:18 UTC (rev 32626)
+++ grass/trunk/raster/r.support/Makefile	2008-08-07 21:27:37 UTC (rev 32627)
@@ -1,14 +0,0 @@
-MODULE_TOPDIR = ../..
-
-SUBDIRS = \
-	modcats \
-	modcolr \
-	modhist	\
-	modhead	\
-	front
-
-include $(MODULE_TOPDIR)/include/Make/Dir.make
-
-default: parsubdirs
-
-clean: cleansubdirs

Copied: grass/trunk/raster/r.support/Makefile (from rev 32621, grass/trunk/raster/r.support/front/Makefile)
===================================================================
--- grass/trunk/raster/r.support/Makefile	                        (rev 0)
+++ grass/trunk/raster/r.support/Makefile	2008-08-07 21:27:37 UTC (rev 32627)
@@ -0,0 +1,12 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.support
+
+LIBES = $(GISLIB)
+DEPENDENCIES = $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd
+
+

Copied: grass/trunk/raster/r.support/check.c (from rev 32621, grass/trunk/raster/r.support/front/check.c)
===================================================================
--- grass/trunk/raster/r.support/check.c	                        (rev 0)
+++ grass/trunk/raster/r.support/check.c	2008-08-07 21:27:37 UTC (rev 32627)
@@ -0,0 +1,76 @@
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+
+/*
+ * check_stats() - Check and update statistics 
+ *
+ * RETURN: EXIT_SUCCESS / EXIT_FAILURE
+ */
+int check_stats(const char *name, const char *mapset)
+{
+    RASTER_MAP_TYPE data_type;
+    struct Histogram histogram;
+    struct Categories cats;
+    struct Range range;
+    struct FPRange fprange;
+    int i;
+    int cats_ok;
+    int max;
+
+    data_type = G_raster_map_type(name, mapset);
+
+    G_message(_("\n  Updating statistics for [%s]"), name);
+    if (!do_histogram(name, mapset))
+	return EXIT_SUCCESS;
+    if (G_read_histogram(name, mapset, &histogram) <= 0)
+	return EXIT_SUCCESS;
+
+    /* Init histogram range */
+    if (data_type == CELL_TYPE)
+	G_init_range(&range);
+    else
+	G_init_fp_range(&fprange);
+
+    /* Update histogram range */
+    i = G_get_histogram_num(&histogram);
+    while (i >= 0) {
+	if (data_type == CELL_TYPE)
+	    G_update_range(G_get_histogram_cat(i--, &histogram), &range);
+	else
+	    G_update_fp_range((DCELL) G_get_histogram_cat(i--, &histogram),
+			      &fprange);
+    }
+
+    /* Write histogram range */
+    if (data_type == CELL_TYPE)
+	G_write_range(name, &range);
+    else
+	G_write_fp_range(name, &fprange);
+
+    /* Get category status and max */
+    cats_ok = (G_read_cats(name, mapset, &cats) >= 0);
+    max = (data_type == CELL_TYPE ? range.max : fprange.max);
+
+    /* Further category checks */
+    if (!cats_ok)
+	G_init_cats(max, "", &cats);
+    else if (cats.num != max) {
+	cats.num = max;
+	cats_ok = 0;
+    }
+
+    /* Update categories if needed */
+    if (!cats_ok) {
+	G_message(_("   Updating the number of categories for "
+		    "[%s]\n\n"), name);
+	G_write_cats(name, &cats);
+    }
+
+    G_free_histogram(&histogram);
+    G_free_cats(&cats);
+
+    return EXIT_SUCCESS;
+}

Copied: grass/trunk/raster/r.support/description.html (from rev 32621, grass/trunk/raster/r.support/front/description.html)
===================================================================
--- grass/trunk/raster/r.support/description.html	                        (rev 0)
+++ grass/trunk/raster/r.support/description.html	2008-08-07 21:27:37 UTC (rev 32627)
@@ -0,0 +1,42 @@
+<h2>DESCRIPTION</h2>
+
+<b>r.support</b> allows the user to create and/or edit raster map support 
+information. Editing of raster map color tables, category labels, header, 
+history, and title is supported. Category labels can also be copied from
+another raster map.
+
+<h2>NOTES</h2>
+
+If metadata options such as <b>title</b> or <b>history</b> are given the
+module will run  non-interactively. If only the map name is given
+<em>r.support</em> will run interactively within a terminal shell and the
+user with be prompted for input.
+<p>
+Freeform metadata information is stored in a "<tt>hist</tt>" file which may be
+appended to by using the <b>history</b> option. Currently this is limited to
+50 lines of text with a maximum line length of 78 characters. Any input
+larger than this will be wrapped to the next line.
+All other metadata strings available as standard options are limited to
+79 characters.
+
+<h2>SEE ALSO</h2>
+<em>
+<a href="r.category.html">r.category</a>,
+<a href="r.describe.html">r.describe</a>,
+<a href="r.info.html">r.info</a>,
+<a href="r.null.html">r.null</a>,
+<a href="r.region.html">r.region</a>,
+<a href="r.report.html">r.report</a>,
+<a href="r.timestamp.html">r.timestamp</a>
+</em>
+
+<h2>AUTHORS</h2>
+
+Micharl Shapiro, CERL: Original author<br>
+<a href="MAILTO:rez at touchofmadness.com">Brad Douglas</a>: GRASS 6 Port<br>
+M. Hamish Bowman: command line enhancements<br>
+Markus Neteler: category copy from other map
+
+<p>
+<i>Last changed: $Date$</i>
+</p>

Copied: grass/trunk/raster/r.support/front.c (from rev 32621, grass/trunk/raster/r.support/front/front.c)
===================================================================
--- grass/trunk/raster/r.support/front.c	                        (rev 0)
+++ grass/trunk/raster/r.support/front.c	2008-08-07 21:27:37 UTC (rev 32627)
@@ -0,0 +1,314 @@
+/*
+ **********************************************************************
+ *
+ * MODULE:       r.support (GRASS core)
+ *
+ * AUTHOR(S):    Original by Michael Shapiro - CERL
+ *               Preliminary parser support by Markus Neteler, rast parameter
+ *               Port to 6.x by Brad Douglas
+ *
+ * PURPOSE:      Build support files for raster map
+ *               - Edit header
+ *               - Update status (histogram, range)
+ *
+ * COPYRIGHT:    (C) 2000-2007 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General 
+ *               Public License (>=v2). Read the file COPYING that comes
+ *               with GRASS for details.
+ *
+ **********************************************************************/
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+/* two less than lib/gis/put_title.c  G_put_cell_title()
+   if only one less a newline gets appended in the cats file. bug? */
+#define MAX_TITLE_LEN 1022
+
+int main(int argc, char *argv[])
+{
+    char rname[GNAME_MAX];	/* Reclassed map name */
+    char rmapset[GMAPSET_MAX];	/* Reclassed mapset   */
+    char *mapset;		/* Raster mapset      */
+    struct Cell_head cellhd;
+    struct GModule *module;
+    struct Option *raster, *title_opt, *history_opt;
+    struct Option *datasrc1_opt, *datasrc2_opt, *datadesc_opt;
+    struct Option *map_opt, *units_opt, *vdatum_opt;
+    struct Flag *stats_flag, *null_flag, *del_flag;
+    int cellhd_ok;		/* Is cell header OK? */
+    int is_reclass;		/* Is raster reclass? */
+    char *infile, *cmapset;
+    char title[MAX_TITLE_LEN + 1], datasrc[RECORD_LEN + 1];
+    struct History hist;
+
+    /* Initialize GIS engine */
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->keywords = _("raster, metadata");
+    module->description = _("Allows creation and/or modification of "
+			    "raster map layer support files.");
+
+    raster = G_define_standard_option(G_OPT_R_MAP);
+
+    title_opt = G_define_option();
+    title_opt->key = "title";
+    title_opt->key_desc = "\"phrase\"";
+    title_opt->type = TYPE_STRING;
+    title_opt->required = NO;
+    title_opt->description = _("Text to use for map title");
+
+    history_opt = G_define_option();
+    history_opt->key = "history";
+    history_opt->key_desc = "\"phrase\"";
+    history_opt->type = TYPE_STRING;
+    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");
+
+    datasrc1_opt = G_define_option();
+    datasrc1_opt->key = "source1";
+    datasrc1_opt->key_desc = "\"phrase\"";
+    datasrc1_opt->type = TYPE_STRING;
+    datasrc1_opt->required = NO;
+    datasrc1_opt->description = _("Text to use for data source, line 1");
+
+    datasrc2_opt = G_define_option();
+    datasrc2_opt->key = "source2";
+    datasrc2_opt->key_desc = "\"phrase\"";
+    datasrc2_opt->type = TYPE_STRING;
+    datasrc2_opt->required = NO;
+    datasrc2_opt->description = _("Text to use for data source, line 2");
+
+    datadesc_opt = G_define_option();
+    datadesc_opt->key = "description";
+    datadesc_opt->key_desc = "\"phrase\"";
+    datadesc_opt->type = TYPE_STRING;
+    datadesc_opt->required = NO;
+    datadesc_opt->description =
+	_("Text to use for data description or keyword(s)");
+
+    map_opt = G_define_option();
+    map_opt->key = "raster";
+    map_opt->type = TYPE_STRING;
+    map_opt->required = NO;
+    map_opt->gisprompt = "old,cell,raster";
+    map_opt->description = _("Raster map from which to copy category table");
+
+    stats_flag = G_define_flag();
+    stats_flag->key = 's';
+    stats_flag->description = _("Update statistics (histogram, range)");
+
+    null_flag = G_define_flag();
+    null_flag->key = 'n';
+    null_flag->description = _("Create/reset the null file");
+
+    del_flag = G_define_flag();
+    del_flag->key = 'd';
+    del_flag->description = _("Delete the null file");
+
+    /* Parse command-line options */
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* Make sure raster exists and set mapset */
+    infile = raster->answer;
+    mapset = G_find_cell2(infile, G_mapset());	/* current mapset only for editing */
+    if (mapset == NULL)
+	G_fatal_error(_("Raster map <%s> not found in current mapset"),
+		      infile);
+
+    cellhd_ok = (G_get_cellhd(raster->answer, mapset, &cellhd) >= 0);
+    is_reclass = (G_is_reclass(raster->answer, mapset, rname, rmapset) > 0);
+
+    if (title_opt->answer) {
+	strncpy(title, title_opt->answer, MAX_TITLE_LEN);
+	title[MAX_TITLE_LEN] = '\0';	/* strncpy doesn't null terminate oversized input */
+	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) {
+	G_read_history(raster->answer, mapset, &hist);
+
+	if (hist.edlinecnt >= MAXEDLINES)
+	    G_fatal_error(_("Not enough room in history file"));
+
+	/* two less than defined as if only one less a newline gets appended in the hist file. bug? */
+	/* Should be RECORD_LEN, but r.info truncates at > 71 chars */
+	if (strlen(history_opt->answer) > 71) {
+	    int i;
+
+	    for (i = 0; i < strlen(history_opt->answer); i += 71) {
+		char *tmp = &history_opt->answer[i];
+
+		strncpy(hist.edhist[hist.edlinecnt], tmp, 71);
+
+		/* strncpy doesn't null terminate oversized input */
+		hist.edhist[hist.edlinecnt][RECORD_LEN - 2] = '\0';
+		hist.edlinecnt++;
+
+		G_debug(3, "new history line= [%s] (%d chars)",
+			hist.edhist[hist.edlinecnt],
+			strlen(hist.edhist[hist.edlinecnt]));
+	    }
+	}
+	else {
+	    strncpy(hist.edhist[hist.edlinecnt], history_opt->answer,
+		    RECORD_LEN - 2);
+
+	    /* strncpy doesn't null terminate oversized input */
+	    hist.edhist[hist.edlinecnt][RECORD_LEN - 2] = '\0';
+	    hist.edlinecnt++;
+
+	    G_debug(3, "new history line= [%s] (%d chars)",
+		    hist.edhist[hist.edlinecnt],
+		    strlen(hist.edhist[hist.edlinecnt]));
+	}
+
+	G_write_history(raster->answer, &hist);
+    }
+
+    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 (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) {
+	G_read_history(raster->answer, mapset, &hist);
+
+	if (datasrc1_opt->answer) {
+	    strncpy(datasrc, datasrc1_opt->answer, RECORD_LEN);
+	    datasrc[RECORD_LEN] = '\0';	/* strncpy doesn't null terminate oversized input */
+	    G_strip(datasrc);
+	    G_debug(3, "map datasrc1= [%s]  (%d chars)", datasrc,
+		    strlen(datasrc));
+	    strncpy(hist.datsrc_1, datasrc, RECORD_LEN);
+	}
+	if (datasrc2_opt->answer) {
+	    strncpy(datasrc, datasrc2_opt->answer, RECORD_LEN);
+	    datasrc[RECORD_LEN] = '\0';	/* strncpy doesn't null terminate oversized input */
+	    G_strip(datasrc);
+	    G_debug(3, "map datasrc2= [%s]  (%d chars)", datasrc,
+		    strlen(datasrc));
+	    strncpy(hist.datsrc_2, datasrc, RECORD_LEN);
+	}
+
+	if (datadesc_opt->answer) {
+	    strncpy(datasrc, datadesc_opt->answer, RECORD_LEN);
+	    datasrc[RECORD_LEN] = '\0';	/* strncpy doesn't null terminate oversized input */
+	    G_strip(datasrc);
+	    G_debug(3, "map datadesc= [%s]  (%d chars)", datasrc,
+		    strlen(datasrc));
+	    strncpy(hist.keywrd, datasrc, RECORD_LEN);
+	}
+
+	G_write_history(raster->answer, &hist);
+    }
+
+    if (map_opt->answer) {	/* use cats from another map */
+	int fd;
+	struct Categories cats;
+
+	cmapset = G_find_cell2(map_opt->answer, "");
+	if (cmapset == NULL)
+	    G_fatal_error(_("Raster map <%s> not found"), map_opt->answer);
+
+	if ((fd = G_open_cell_old(infile, mapset)) < 0)
+	    G_fatal_error(_("Unable to open raster map <%s>"), infile);
+	G_init_cats((CELL) 0, "", &cats);
+	if (0 > G_read_cats(map_opt->answer, cmapset, &cats))
+	    G_fatal_error(_("Unable to read category file of raster map <%s@%s>"),
+			  map_opt->answer, cmapset);
+
+	if (G_write_cats(infile, &cats) >= 0)
+	    G_message(_("cats table for [%s] set to %s"), infile,
+		      map_opt->answer);
+	G_close_cell(fd);
+	G_free_cats(&cats);
+    }
+
+
+    if (title_opt->answer || history_opt->answer || units_opt->answer
+	|| vdatum_opt->answer || datasrc1_opt->answer || datasrc2_opt->answer
+	|| datadesc_opt->answer || map_opt->answer)
+	exit(EXIT_SUCCESS);
+
+
+    /* Check the histogram and range */
+    if (stats_flag->answer)
+	check_stats(raster->answer, mapset);
+
+    /* null file */
+    if (null_flag->answer) {
+	unsigned char *null_bits;
+	int row, col;
+	int null_fd;
+
+	if (is_reclass)
+	    G_fatal_error(_("[%s] is a reclass of another map. Exiting."),
+			  raster->answer);
+
+	/* Create a file of no-nulls */
+	null_bits = G__allocate_null_bits(cellhd.cols);
+	for (col = 0; col < G__null_bitstream_size(cellhd.cols); col++)
+	    null_bits[col] = 0;
+
+	/* Open null file for writing */
+	null_fd = G_open_new_misc("cell_misc", "null", raster->answer);
+
+	G_message(_("Writing new null file for [%s]... "), raster->answer);
+	for (row = 0; row < cellhd.rows; row++) {
+	    G_percent(row, cellhd.rows, 1);
+	    if (G__write_null_bits(null_fd, null_bits, row, cellhd.cols, 0) <
+		0)
+		G_fatal_error(_("Error writing null row [%d]."), row);
+	}
+	G_percent(row, cellhd.rows, 1);
+
+	/* Cleanup */
+	close(null_fd);
+	G_free(null_bits);
+    }
+
+    if (del_flag->answer) {
+	int null_fd;
+	char path[GPATH_MAX];
+
+	if (is_reclass)
+	    G_fatal_error(_("[%s] is a reclass of another map. Exiting."),
+			  raster->answer);
+
+	/* Write a file of no-nulls */
+	G_message(_("Removing null file for [%s]...\n"), raster->answer);
+
+	null_fd = G_open_new_misc("cell_misc", "null", raster->answer);
+	G__file_name_misc(path, "cell_misc", "null", raster->answer, mapset);
+	unlink(path);
+	close(null_fd);
+
+	G_done_msg(_("Done."));
+    }
+
+    return EXIT_SUCCESS;
+}

Copied: grass/trunk/raster/r.support/histo.c (from rev 32621, grass/trunk/raster/r.support/front/histo.c)
===================================================================
--- grass/trunk/raster/r.support/histo.c	                        (rev 0)
+++ grass/trunk/raster/r.support/histo.c	2008-08-07 21:27:37 UTC (rev 32627)
@@ -0,0 +1,49 @@
+#include <stdlib.h>
+#include <grass/gis.h>
+
+
+/* 
+ * do_histogram() - Creates histogram for CELL
+ *
+ * RETURN: EXIT_SUCCESS / EXIT_FAILURE
+ */
+int do_histogram(const char *name, const char *mapset)
+{
+    CELL *cell;
+    struct Cell_head cellhd;
+    struct Cell_stats statf;
+    int nrows, ncols;
+    int row;
+    int fd;
+
+    if (G_get_cellhd(name, mapset, &cellhd) < 0)
+	return EXIT_FAILURE;
+
+    G_set_window(&cellhd);
+    if ((fd = G_open_cell_old(name, mapset)) < 0)
+	return EXIT_FAILURE;
+
+    nrows = G_window_rows();
+    ncols = G_window_cols();
+    cell = G_allocate_cell_buf();
+
+    G_init_cell_stats(&statf);
+    for (row = 0; row < nrows; row++) {
+	if (G_get_map_row_nomask(fd, cell, row) < 0)
+	    break;
+
+	G_update_cell_stats(cell, ncols, &statf);
+    }
+
+    if (row == nrows)
+	G_write_histogram_cs(name, &statf);
+
+    G_free_cell_stats(&statf);
+    G_close_cell(fd);
+    G_free(cell);
+
+    if (row == nrows)
+	return EXIT_SUCCESS;
+
+    return EXIT_FAILURE;
+}

Copied: grass/trunk/raster/r.support/local_proto.h (from rev 32621, grass/trunk/raster/r.support/front/local_proto.h)
===================================================================
--- grass/trunk/raster/r.support/local_proto.h	                        (rev 0)
+++ grass/trunk/raster/r.support/local_proto.h	2008-08-07 21:27:37 UTC (rev 32627)
@@ -0,0 +1,5 @@
+/* check.c */
+int check_stats(const char *, const char *);
+
+/* histo.c */
+int do_histogram(const char *, const char *);



More information about the grass-commit mailing list