[GRASS-SVN] r64140 - grass/branches/releasebranch_7_0/general/g.rename
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Jan 13 09:44:40 PST 2015
Author: hcho
Date: 2015-01-13 09:44:40 -0800 (Tue, 13 Jan 2015)
New Revision: 64140
Modified:
grass/branches/releasebranch_7_0/general/g.rename/main.c
Log:
g.rename: Backport the #2546 fix (updating reclassed_to)
Modified: grass/branches/releasebranch_7_0/general/g.rename/main.c
===================================================================
--- grass/branches/releasebranch_7_0/general/g.rename/main.c 2015-01-13 17:40:35 UTC (rev 64139)
+++ grass/branches/releasebranch_7_0/general/g.rename/main.c 2015-01-13 17:44:40 UTC (rev 64140)
@@ -3,13 +3,13 @@
*
* MODULE: g.rename
* AUTHOR(S): CERL (original contributor)
- * Radim Blazek <radim.blazek gmail.com>,
- * Cedric Shock <cedricgrass shockfamily.net>,
- * Glynn Clements <glynn gclements.plus.com>,
- * Markus Neteler <neteler itc.it>,
+ * Radim Blazek <radim.blazek gmail.com>,
+ * Cedric Shock <cedricgrass shockfamily.net>,
+ * Glynn Clements <glynn gclements.plus.com>,
+ * Markus Neteler <neteler itc.it>,
* Martin Landa <landa.martin gmail.com>,
* Huidae Cho <grass4u gmail.com>
- * PURPOSE:
+ * PURPOSE: Rename map names
* COPYRIGHT: (C) 1994-2007, 2011-2014 by the GRASS Development Team
*
* This program is free software under the GNU General Public
@@ -25,15 +25,16 @@
#include <grass/glocale.h>
#include <grass/manage.h>
+void update_reclass_maps(const char *, const char *);
+void update_base_map(const char *, const char *, const char *);
+
int main(int argc, char *argv[])
{
- int i, n;
+ int n;
struct GModule *module;
struct Option **parm;
- char *old, *new;
- int nrmaps, nlist;
- const char *mapset, *location_path;
- char **rmaps;
+ int nlist;
+ const char *mapset;
int result = EXIT_SUCCESS;
G_gisinit(argv[0]);
@@ -57,10 +58,12 @@
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
- location_path = G_location_path();
mapset = G_mapset();
for (n = 0; n < nlist; n++) {
+ int i;
+ char *old, *new;
+
if (parm[n]->answers == NULL)
continue;
i = 0;
@@ -73,7 +76,7 @@
G_warning(_("%s <%s> not found"), M_get_list(n)->maindesc, old);
continue;
}
- if (M_find(n, new, "") && !(module->overwrite)) {
+ if (M_find(n, new, mapset) && !(module->overwrite)) {
G_warning(_("<%s> already exists in mapset <%s>"), new,
M_find(n, new, ""));
continue;
@@ -93,54 +96,116 @@
result = EXIT_FAILURE;
}
- if (!renamed && strcmp(parm[n]->key, "raster") == 0 &&
- Rast_is_reclassed_to(old, mapset, &nrmaps, &rmaps) > 0) {
- int ptr, l;
- char buf1[256], buf2[256], buf3[256], *str;
- FILE *fp;
+ if (!renamed && strcmp(parm[n]->key, "raster") == 0) {
+ update_reclass_maps(new, mapset);
+ update_base_map(old, new, mapset);
+ }
+ }
+ }
+ exit(result);
+}
- G_message(_("Renaming reclass maps"));
+void update_reclass_maps(const char *name, const char *mapset)
+{
+ int nrmaps;
+ char **rmaps;
- for (; *rmaps; rmaps++) {
- G_message(" %s", *rmaps);
- sprintf(buf3, "%s", *rmaps);
- if ((str = strchr(buf3, '@'))) {
- *str = 0;
- sprintf(buf2, "%s", str + 1);
- }
- else {
- sprintf(buf2, "%s", mapset);
- }
- sprintf(buf1, "%s/%s/cellhd/%s", location_path, buf2,
- buf3);
+ if (Rast_is_reclassed_to(name, mapset, &nrmaps, &rmaps) <= 0)
+ return;
- fp = fopen(buf1, "r");
- if (fp == NULL)
- continue;
+ G_message(_("Updating reclass maps"));
- fgets(buf2, 255, fp);
- fgets(buf2, 255, fp);
- fgets(buf2, 255, fp);
+ for (; *rmaps; rmaps++) {
+ char buf1[256], buf2[256], buf3[256], *str;
+ FILE *fp;
+ int ptr, l;
- ptr = G_ftell(fp);
- G_fseek(fp, 0L, SEEK_END);
- l = G_ftell(fp) - ptr;
+ G_message(" %s", *rmaps);
+ sprintf(buf3, "%s", *rmaps);
+ if ((str = strchr(buf3, '@'))) {
+ *str = 0;
+ sprintf(buf2, "%s", str + 1);
+ }
+ else {
+ sprintf(buf2, "%s", mapset);
+ }
+ G_file_name(buf1, "cellhd", buf3, buf2);
- str = (char *)G_malloc(l);
- G_fseek(fp, ptr, SEEK_SET);
- fread(str, l, 1, fp);
- fclose(fp);
+ fp = fopen(buf1, "r");
+ if (fp == NULL)
+ continue;
- fp = fopen(buf1, "w");
- fprintf(fp, "reclass\n");
- fprintf(fp, "name: %s\n", new);
- fprintf(fp, "mapset: %s\n", mapset);
- fwrite(str, l, 1, fp);
- G_free(str);
- fclose(fp);
- }
- }
+ fgets(buf2, 255, fp);
+ fgets(buf2, 255, fp);
+ fgets(buf2, 255, fp);
+
+ ptr = G_ftell(fp);
+ G_fseek(fp, 0L, SEEK_END);
+ l = G_ftell(fp) - ptr;
+
+ str = (char *)G_malloc(l);
+ G_fseek(fp, ptr, SEEK_SET);
+ fread(str, l, 1, fp);
+ fclose(fp);
+
+ fp = fopen(buf1, "w");
+ fprintf(fp, "reclass\n");
+ fprintf(fp, "name: %s\n", name);
+ fprintf(fp, "mapset: %s\n", mapset);
+ fwrite(str, l, 1, fp);
+ G_free(str);
+ fclose(fp);
+ }
+}
+
+void update_base_map(const char *old, const char *new, const char *mapset)
+{
+ int i, nrmaps, found;
+ char bname[GNAME_MAX], bmapset[GMAPSET_MAX], rpath[GPATH_MAX];
+ char *xold, *xnew, **rmaps;
+ FILE *fp;
+
+ if (Rast_is_reclass(new, mapset, bname, bmapset) <= 0)
+ return;
+
+ if (Rast_is_reclassed_to(bname, bmapset, &nrmaps, &rmaps) <= 0)
+ nrmaps = 0;
+
+ found = 0;
+ xold = G_fully_qualified_name(old, mapset);
+ for (i = 0; i < nrmaps; i++) {
+ if (strcmp(xold, rmaps[i]) == 0) {
+ found = 1;
+ break;
}
}
- exit(result);
+
+ if (!found) {
+ G_fatal_error(_("Unable to find reclass information for <%s> in "
+ "base map <%s@%s>"), xold, bname, bmapset);
+ }
+
+ G_message(_("Updating base map <%s@%s>"), bname, bmapset);
+
+ G_file_name_misc(rpath, "cell_misc", "reclassed_to", bname, bmapset);
+
+ fp = fopen(rpath, "w");
+ if (fp == NULL) {
+ G_fatal_error(_("Unable to update dependency file in <%s@%s>"),
+ bname, bmapset);
+ }
+
+ xnew = G_fully_qualified_name(new, mapset);
+ for (; *rmaps; rmaps++) {
+ if (strcmp(xold, *rmaps) == 0) {
+ fprintf(fp, "%s\n", xnew);
+ }
+ else {
+ fprintf(fp, "%s\n", *rmaps);
+ }
+ }
+
+ G_free(xold);
+ G_free(xnew);
+ fclose(fp);
}
More information about the grass-commit
mailing list