[GRASS-SVN] r44508 - grass/branches/develbranch_6/imagery/i.ortho.photo/menu

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Dec 2 04:40:25 EST 2010


Author: mmetz
Date: 2010-12-02 01:40:25 -0800 (Thu, 02 Dec 2010)
New Revision: 44508

Added:
   grass/branches/develbranch_6/imagery/i.ortho.photo/menu/i.photo.rectify.c
   grass/branches/develbranch_6/imagery/i.ortho.photo/menu/target.c
Modified:
   grass/branches/develbranch_6/imagery/i.ortho.photo/menu/local_proto.h
   grass/branches/develbranch_6/imagery/i.ortho.photo/menu/menu.c
   grass/branches/develbranch_6/imagery/i.ortho.photo/menu/run.c
Log:
add interactive command construction for i.photo.rectify

Copied: grass/branches/develbranch_6/imagery/i.ortho.photo/menu/i.photo.rectify.c (from rev 44501, grass/branches/develbranch_6/imagery/i.ortho.photo/i.photo.rectify/ask_method.c)
===================================================================
--- grass/branches/develbranch_6/imagery/i.ortho.photo/menu/i.photo.rectify.c	                        (rev 0)
+++ grass/branches/develbranch_6/imagery/i.ortho.photo/menu/i.photo.rectify.c	2010-12-02 09:40:25 UTC (rev 44508)
@@ -0,0 +1,274 @@
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <grass/gis.h>
+#include <grass/vask.h>
+#include <grass/imagery.h>
+#include <grass/ortholib.h>
+#include <grass/glocale.h>
+#include "orthophoto.h"
+#include "local_proto.h"
+
+#define L2BDIM 6
+#define BDIM (1<<(L2BDIM))
+#define L2BSIZE (2*(L2BDIM))
+#define BSIZE (1<<(L2BSIZE))
+#define HI(i) ((i)>>(L2BDIM))
+#define LO(i) ((i)&((BDIM)-1))
+
+typedef DCELL block[BDIM][BDIM];   /* FCELL sufficient ? */
+
+/* interactive command line construction for i.photo.rectify */
+/* return 1 on success, 0 on failure
+ */
+int rectify(char *groupname)
+{
+    int i, max_rows, max_cols;
+    int nx, ny;
+    struct Cell_head win, target_window;
+    double max_mb_img, max_mb_elev, max_mb;
+    struct Ortho_Image_Group group;
+    int do_all, ok, repeat;
+    char extension[GNAME_MAX];
+    char result[GNAME_MAX];
+
+    char pgm[1024];
+    int stat;
+
+    sprintf(pgm, "i.photo.rectify  group=%s", groupname);
+    strcpy(group.name, groupname);
+
+    /* get group ref */
+    if (!I_get_group_ref(group.name, (struct Ref *)&group.group_ref)) {
+	fprintf(stderr, _("Could not read REF file for group [%s]"), group.name);
+	fprintf(stderr, _("Orthorectification cancelled"));
+	return 0;
+    }
+    if (group.group_ref.nfiles <= 0) {
+	fprintf(stderr, _("No files in this group!"));
+	fprintf(stderr, _("Orthorectification cancelled"));
+	return 0;
+    }
+    
+    /* get target location/mapset */
+    get_target(group.name, &target_window);
+
+    /* rectify all images in group ? */
+    do_all = 1;
+    if (group.group_ref.nfiles > 1) {
+	G_clear_screen();
+	do_all = G_yes(_("\nRectify all images in the group? "), do_all);
+    }
+    
+    if (!do_all) {
+	int got_one = 0;
+	/* create list of files to be rectified */
+	for (i = 0; i < group.group_ref.nfiles; i++) {
+	    ok = 1;
+	    char buf[100];
+
+	    sprintf(buf, _("\nRectify image <%s>? "), group.group_ref.file[i].name);
+	    ok = G_yes(buf, ok);
+	    if (ok) {
+		if (!got_one) {
+		    sprintf(pgm, "%s input=%s@%s", pgm, group.group_ref.file[i].name, group.group_ref.file[i].mapset);
+		}
+		else {
+		    sprintf(pgm, "%s,%s@%s", pgm, group.group_ref.file[i].name, group.group_ref.file[i].mapset);
+		}
+		got_one = 1;
+	    }
+	}
+	if (!got_one) {
+	    fprintf(stderr,_("\nNo images selected, orthorectification cancelled."));
+	    G_sleep(3);
+	    return 0;
+	}
+    }
+    else
+	sprintf(pgm, "%s -a", pgm);
+    
+    /* name extension for rectified maps */
+    sprintf(extension, ".ortho");
+
+    repeat = 1;
+    while (repeat) {
+	repeat = 0;
+	V_clear();
+	V_line(1, _("Enter an extension to be appended to rectified maps:"));
+	V_ques(extension, 's', 3, 0, 20);
+	V_intrpt_ok();
+	if (!V_call())
+	    return 0;
+
+	/* test for legal file name */
+	sprintf(result, "%s%s", group.group_ref.file[0].name, extension);
+	if (G_legal_filename(result) < 0) {
+	    G_clear_screen();
+	    fprintf(stderr, _("Extension <%s> is illegal"), extension);
+	    repeat = G_yes(_("\nChoose another extension? "), 1);
+	    if (!repeat) {
+		fprintf(stderr,_("Orthorectification cancelled."));
+		G_sleep(3);
+		return 0;
+	    }
+	}
+    }
+    sprintf(pgm, "%s extension=%s", pgm, extension);
+    
+    /* overwrite maps in target location/mapset */
+    G_clear_screen();
+    ok = G_yes(_("\nOverwrite maps in target location/mapset? "), 0);
+    if (ok) {
+	sprintf(pgm, "%s --o", pgm);
+    }
+
+    /* use current region settings in target location (def.=calculate smallest area) ? */
+    ok = 0;
+    while (1) {
+	char buf[100];
+
+	fprintf(stderr, "Please select one of the following options\n");
+	fprintf(stderr,
+		" 1. Use the current window in the target location\n");
+	fprintf(stderr,
+		" 2. Determine the smallest window which covers the image\n");
+	fprintf(stderr, "> ");
+	if (!G_gets(buf))
+	    continue;
+	G_strip(buf);
+
+	if (strcmp(buf, "1") == 0) {
+	    ok = 1;
+	    break;
+	}
+	if (strcmp(buf, "2") == 0)
+	    break;
+    }
+
+    if (ok) {
+	sprintf(pgm, "%s -c", pgm);
+    }
+    else {
+	/* ask for target resolution */
+	double res = -1;
+	
+	while (1) {
+	    char buf[100];
+
+	    fprintf(stderr, "Desired target resolution\n");
+	    fprintf(stderr,
+		    " RETURN   determine automatically\n");
+	    fprintf(stderr, "> ");
+	    if (!G_gets(buf))
+		continue;
+
+	    if (*buf == 0) {  /* determine automatically */
+		break;
+	    }
+
+	    G_strip(buf);
+
+	    if ((res = atof(buf)) <= 0) {
+		fprintf(stderr, "Resolution must be larger than zero!");
+		G_clear_screen();
+	    }
+	    else
+		break;
+	}
+	if (res > 0)
+	    sprintf(pgm, "%s res=%f", pgm, res);
+    }
+
+
+    /* interpolation method */
+    while (1) {
+	char buf[100];
+
+	G_clear_screen();
+	fprintf(stderr, "\n");
+	fprintf(stderr, _("Please select one of the following interpolation methods\n"));
+	fprintf(stderr, _(" 1. nearest neighbor\n"));
+	fprintf(stderr, _(" 2. bilinear\n"));
+	fprintf(stderr, _(" 3. bicubic\n"));
+	fprintf(stderr, _(" 4. bilinear with fallback\n"));
+	fprintf(stderr, _(" 5. bicubic with fallback\n"));
+	fprintf(stderr, "> ");
+	if (!G_gets(buf))
+	    continue;
+	G_strip(buf);
+
+	if (strcmp(buf, "1") == 0) {
+	    sprintf(pgm, "%s method=nearest", pgm);
+	    break;
+	}
+	if (strcmp(buf, "2") == 0) {
+	    sprintf(pgm, "%s method=bilinear", pgm);
+	    break;
+	}
+	if (strcmp(buf, "3") == 0) {
+	    sprintf(pgm, "%s method=cubic", pgm);
+	    break;
+	}
+	if (strcmp(buf, "4") == 0) {
+	    sprintf(pgm, "%s method=bilinear_f", pgm);
+	    break;
+	}
+	if (strcmp(buf, "5") == 0) {
+	    sprintf(pgm, "%s method=cubic_f", pgm);
+	    break;
+	}
+    }
+
+    /* amount of memory to use */
+    max_rows = max_cols = 0;
+    for (i = 0; i < group.group_ref.nfiles; i++) {
+	G_get_cellhd(group.group_ref.file[i].name,
+		     group.group_ref.file[i].mapset, &win);
+	if (max_rows < win.rows)
+	    max_rows = win.rows;
+	if (max_cols < win.cols)
+	    max_cols = win.cols;
+    }
+
+    ny = (max_rows + BDIM - 1) / BDIM;
+    nx = (max_cols + BDIM - 1) / BDIM;
+
+    max_mb_img = ((double)nx * ny * sizeof(block)) / (1<<20);
+
+    ny = (target_window.rows + BDIM - 1) / BDIM;
+    nx = (target_window.cols + BDIM - 1) / BDIM;
+
+    max_mb_elev = ((double)nx * ny * sizeof(block)) / (1<<20);
+    max_mb = max_mb_img + max_mb_elev + 0.5; /* + 0.5 for rounding */
+    if (max_mb < 1)
+	max_mb = 1;
+
+    fprintf(stderr, "\n\n");
+    while (1) {
+	char buf[100];
+	int seg_mb = max_mb;;
+
+	fprintf(stderr, _("Amount of memory to use in MB\n"));
+	fprintf(stderr, _("RETURN   use %d MB to keep all data in RAM\n"), (int)(max_mb));
+	fprintf(stderr, "> ");
+	if (!G_gets(buf))
+	    continue;
+
+	if (*buf == 0) {		/* all in memory */
+	    sprintf(pgm, "%s memory=%d", pgm, seg_mb);
+	    break;
+	}
+
+	G_strip(buf);
+	if ((seg_mb = atoi(buf)) > 0) {
+	    sprintf(pgm, "%s memory=%d", pgm, seg_mb);
+	    break;
+	}
+    }
+
+    if ((stat = G_system(pgm)))
+	G_sleep(3);
+
+    return stat;
+}

Modified: grass/branches/develbranch_6/imagery/i.ortho.photo/menu/local_proto.h
===================================================================
--- grass/branches/develbranch_6/imagery/i.ortho.photo/menu/local_proto.h	2010-12-02 09:38:49 UTC (rev 44507)
+++ grass/branches/develbranch_6/imagery/i.ortho.photo/menu/local_proto.h	2010-12-02 09:40:25 UTC (rev 44508)
@@ -1,3 +1,13 @@
+/* env.c */
+int select_current_env(void);
+int select_target_env(void);
+
+/* i.photo.rectify.c */
+int rectify(char *);
+
 /* run.c */
 int run_etc_imagery(char *, char *);
 int run_system(char *);
+
+/* target.c */
+int get_target(char *, struct Cell_head *);

Modified: grass/branches/develbranch_6/imagery/i.ortho.photo/menu/menu.c
===================================================================
--- grass/branches/develbranch_6/imagery/i.ortho.photo/menu/menu.c	2010-12-02 09:38:49 UTC (rev 44507)
+++ grass/branches/develbranch_6/imagery/i.ortho.photo/menu/menu.c	2010-12-02 09:40:25 UTC (rev 44508)
@@ -128,7 +128,12 @@
 	    run_etc_imagery("i.photo.init", group.name);
 	if (strcmp(buf, "7") == 0)
 	    run_etc_imagery("i.photo.2target", group.name);
-	if (strcmp(buf, "8") == 0)
-	    run_etc_imagery("i.photo.rectify", group.name);
+	if (strcmp(buf, "8") == 0) {
+	    rectify(group.name);
+	    /*
+	    sprintf(buf, "i.photo.rectify group=%s", group.name);
+	    run_system(buf);
+	    */
+	}
     }
 }

Modified: grass/branches/develbranch_6/imagery/i.ortho.photo/menu/run.c
===================================================================
--- grass/branches/develbranch_6/imagery/i.ortho.photo/menu/run.c	2010-12-02 09:38:49 UTC (rev 44507)
+++ grass/branches/develbranch_6/imagery/i.ortho.photo/menu/run.c	2010-12-02 09:40:25 UTC (rev 44508)
@@ -12,9 +12,9 @@
     /* but for now they live in etc/  */
     sprintf(buf, "%s/etc/%s  %s", G_gisbase(), pgm, group);
 
-    if (stat = G_system(buf))
+    if ((stat = G_system(buf)))
 	G_sleep(3);
-    return 0;
+    return stat;
 }
 
 int run_system(char *pgm)
@@ -23,7 +23,7 @@
     int stat;
 
     sprintf(buf, "%s", pgm);
-    if (stat = G_system(buf))
+    if ((stat = G_system(buf)))
 	G_sleep(3);
-    return ((int)stat);
+    return stat;
 }

Copied: grass/branches/develbranch_6/imagery/i.ortho.photo/menu/target.c (from rev 44501, grass/branches/develbranch_6/imagery/i.ortho.photo/i.photo.rectify/target.c)
===================================================================
--- grass/branches/develbranch_6/imagery/i.ortho.photo/menu/target.c	                        (rev 0)
+++ grass/branches/develbranch_6/imagery/i.ortho.photo/menu/target.c	2010-12-02 09:40:25 UTC (rev 44508)
@@ -0,0 +1,38 @@
+#include <unistd.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/imagery.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+int get_target(char *name, struct Cell_head *target_window)
+{
+    char location[40];
+    char mapset[40];
+    char buf[1024];
+    int stat;
+
+    if (!I_get_target(name, location, mapset)) {
+	sprintf(buf, "Target information for group [%s] missing.\n", name);
+	goto error;
+    }
+
+    sprintf(buf, "%s/%s", G_gisdbase(), location);
+    if (access(buf, 0) != 0) {
+	sprintf(buf, "Target location [%s] not found\n", location);
+	goto error;
+    }
+    select_target_env();
+    G__setenv("LOCATION_NAME", location);
+    stat = G__mapset_permissions(mapset);
+    if (stat > 0) {
+	G__setenv("MAPSET", mapset);
+	G_get_window(target_window);
+	select_current_env();
+	return 1;
+    }
+    sprintf(buf, "Mapset [%s] in target location [%s] - ", mapset, location);
+    strcat(buf, stat == 0 ? "permission denied\n" : "not found\n");
+  error:
+    G_fatal_error(buf);
+}



More information about the grass-commit mailing list