[GRASS-SVN] r53943 - in grass-addons/grass7/imagery: . i.rotate

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 20 20:11:16 PST 2012


Author: ychemin
Date: 2012-11-20 20:11:16 -0800 (Tue, 20 Nov 2012)
New Revision: 53943

Added:
   grass-addons/grass7/imagery/i.rotate/
   grass-addons/grass7/imagery/i.rotate/Makefile
   grass-addons/grass7/imagery/i.rotate/i.rotate.html
   grass-addons/grass7/imagery/i.rotate/main.c
Log:
Added i.rotate, may not be perfect yet, please try

Added: grass-addons/grass7/imagery/i.rotate/Makefile
===================================================================
--- grass-addons/grass7/imagery/i.rotate/Makefile	                        (rev 0)
+++ grass-addons/grass7/imagery/i.rotate/Makefile	2012-11-21 04:11:16 UTC (rev 53943)
@@ -0,0 +1,11 @@
+MODULE_TOPDIR = ../..
+
+PGM = i.rotate
+
+LIBES = $(RASTERLIB) $(GISLIB)
+DEPENDENCIES = $(RASTERDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+
+default: cmd

Added: grass-addons/grass7/imagery/i.rotate/i.rotate.html
===================================================================
--- grass-addons/grass7/imagery/i.rotate/i.rotate.html	                        (rev 0)
+++ grass-addons/grass7/imagery/i.rotate/i.rotate.html	2012-11-21 04:11:16 UTC (rev 53943)
@@ -0,0 +1,24 @@
+<H2>DESCRIPTION</H2>
+
+<EM>i.rotate</EM> Rotates a input raster by a clockwise angle. 
+
+<H2>NOTES</H2>
+
+<p>
+
+<H2>TODO</H2>
+Checking if all pixels in output raster are populated, if not apply a NN algorithm.
+
+<H2>SEE ALSO</H2>
+
+<em>
+<A HREF="i.flip.html">i.flip</A><br>
+</em>
+
+<H2>AUTHORS</H2>
+
+Yann Chemin, CGIAR, Sri Lanka<BR>
+
+
+<p>
+<i>Last changed: $Date: 2012-11-20 07:16:20 +1000 $</i>

Added: grass-addons/grass7/imagery/i.rotate/main.c
===================================================================
--- grass-addons/grass7/imagery/i.rotate/main.c	                        (rev 0)
+++ grass-addons/grass7/imagery/i.rotate/main.c	2012-11-21 04:11:16 UTC (rev 53943)
@@ -0,0 +1,126 @@
+
+/****************************************************************************
+ *
+ * MODULE:       i.rotate
+ * AUTHOR(S):    Yann Chemin - yann.chemin at gmail.com
+ * PURPOSE:      Calculates an arbitrary rotation of the image from the 
+ * 		 center of the computing window
+ *
+ * COPYRIGHT:    (C) 2002-2012 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+#include <math.h>
+#include <grass/la.h>
+
+int main(int argc, char *argv[]) 
+{
+    int nrows, ncols;
+    int row, col;
+    struct GModule *module;
+    struct Option *input1, *output1;
+    struct History history;	/*metadata */
+    char *result1;		/*output raster name */
+    int infd;
+    int outfd1;
+    char *rnetday;
+    void *inrast;
+
+    DCELL * outrast1;
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    G_add_keyword(_("imagery"));
+    G_add_keyword(_("rotation"));
+    G_add_keyword(_("computation window centre"));
+    module->description =
+	_("Rotates the image around the centre of the computational window");
+    
+    /* Define the different options */ 
+    input1 = G_define_standard_option(G_OPT_R_INPUT);
+    output1 = G_define_standard_option(G_OPT_R_OUTPUT);
+
+    if (G_parser(argc, argv))
+        exit(EXIT_FAILURE);
+
+    rnetday = input1->answer;
+    result1 = output1->answer;
+    
+    infd = Rast_open_old(rnetday, "");
+    inrast = Rast_allocate_d_buf();
+    
+    nrows = Rast_window_rows();
+    ncols = Rast_window_cols();
+    outrast1 = Rast_allocate_d_buf();
+    outfd1 = Rast_open_new(result1, DCELL_TYPE);
+
+    /*mat_struct *G_matrix_init(int rows, int cols, int ldim)*/
+    /*Increase dimension of image to > of sqrt(2) to fit rotated angles*/
+    int matnrows=nrows*1;
+    int matncols=ncols*1;
+    int matN=matnrows*matncols;
+    mat_struct *matin = G_matrix_init (matnrows, matncols, matN);
+    G_matrix_zero(matin);
+    mat_struct *matout = G_matrix_init (matnrows, matncols, matN);
+    G_matrix_zero(matout);
+    int deltarow=0.5*(matnrows-nrows);
+    int deltacol=0.5*(matncols-ncols);
+    DCELL d;
+    /* Load input matrix with row&col shift to keep center of image*/ 
+    for (row = 0; row < nrows; row++)
+	Rast_get_d_row(infd,inrast,row);
+    for (col = 0; col < ncols; col++)
+    {
+            d = ((DCELL *) inrast)[col];
+	    if (Rast_is_d_null_value(&d))
+		matin[row+deltarow][col+deltacol]=-999.99;
+	    else {
+		matin[row+deltarow][col+deltacol] = d;
+	    }
+    }
+    double theta=20.0;
+    double thetarad=theta*3.1415927/180;
+    double costheta=cos(thetarad);
+    double sintheta=sin(thetarad);
+    int newrow,newcol;
+    /*Rotate the matrix*/
+    for (row = 0; row < nrows; row++)
+    for (col = 0; col < ncols; col++)
+    {
+	newcol=(col-0.5*ncols)*costheta+(row-0.5*nrows)*sintheta+0.5*ncols;
+	newrow=(row-0.5*nrows)*costheta-(col-0.5*ncols)*sintheta+0.5*nrows;
+        matout[newrow][newcol] = matin[row][col];
+    }
+
+    /*Output to raster file*/
+    for (row = 0; row < nrows; row++)
+    {
+    for (col = 0; col < ncols; col++)
+    {
+        outrast1[col]=matout[row][col];
+    }
+    Rast_put_d_row(outfd1,outrast1);
+    }
+
+    G_free(inrast);
+    Rast_close(infd);
+    G_free(outrast1);
+    Rast_close(outfd1);
+    Rast_short_history(result1, "raster", &history);
+    Rast_command_history(&history);
+    Rast_write_history(result1, &history);
+    exit(EXIT_SUCCESS);
+}
+
+



More information about the grass-commit mailing list