[GRASS-SVN] r65548 - grass-addons/grass7/vector/v.nnstat

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 8 11:39:16 PDT 2015


Author: evas
Date: 2015-07-08 11:39:16 -0700 (Wed, 08 Jul 2015)
New Revision: 65548

Modified:
   grass-addons/grass7/vector/v.nnstat/mbr.c
   grass-addons/grass7/vector/v.nnstat/v.nnstat.html
Log:
v.nnstat: fixed warnings and corrected wrong modification of originally right code (in mbr.c)

Modified: grass-addons/grass7/vector/v.nnstat/mbr.c
===================================================================
--- grass-addons/grass7/vector/v.nnstat/mbr.c	2015-07-08 12:28:22 UTC (rev 65547)
+++ grass-addons/grass7/vector/v.nnstat/mbr.c	2015-07-08 18:39:16 UTC (rev 65548)
@@ -55,96 +55,97 @@
 /* Function to obtain vertices of convex hull */
 int convexHull(struct points *pnts, struct convex *hull)
 {
-    int pointIdx, upPoints, loPoints;
-    int i, *upHull, *loHull;
-    int n = pnts->n;
+  int pointIdx, upPoints, loPoints;
+  int i, *upHull, *loHull;
+  int n = pnts->n;
 
-    double *ro, (*ri)[3], (*r)[3]; /* r = [xyz] */
-    r = (double (*)[3]) malloc(n * 3 * sizeof(double));
-    ri = &r[0];
-    ro = &pnts->r[0];
+  double *ro, (*ri)[3], (*r)[3]; /* r = [xyz] */
+  r = (double (*)[3]) G_malloc(n * 3 * sizeof(double));
+  ri = &r[0];
+  ro = &pnts->r[0];
 
-    for (i=0; i<n; i++) {      
-      (*ri)[0] = *ro;
-      (*ri)[1] = *(ro+1);
-      (*ri)[2] = *(ro+2);
-      ri++;
-      ro += 3;
-    }
+  for (i=0; i<n; i++) {      
+    (*ri)[0] = *ro;
+    (*ri)[1] = *(ro+1);
+    (*ri)[2] = *(ro+2);
+    ri++;
+    ro += 3;
+  }
     
-    /* sort points in ascending x order
-     * modified according to http://www.physicsforums.com/showthread.php?t=546209 */
-    qsort(&r[0][0], n, 3 * sizeof(double), cmpPoints);
+  /* sort points in ascending x order
+   * modified according to http://www.physicsforums.com/showthread.php?t=546209 */
+  qsort(&r[0][0], n, 3 * sizeof(double), cmpPoints);
 
-    hull->hull = (int *) G_malloc(n * 3 * sizeof(int));
+  hull->hull = (int *) G_malloc(n * 3 * sizeof(int));
     
-    /* compute upper hull */
-    upHull = hull->hull;
-    upHull[0] = 0;
-    upHull[1] = 1;
-    upPoints = 1; /* number of points in upper hull */
-    for (pointIdx = 2; pointIdx < n; pointIdx++) {
-	upPoints++;
-	upHull[upPoints] = pointIdx;
-	while (upPoints > 1 &&
-	       !rightTurn(r, upHull[upPoints], upHull[upPoints - 1],
-			  upHull[upPoints - 2])
-	    ) {
-	    upHull[upPoints - 1] = upHull[upPoints];
-	    upPoints--;
-	}
+  /* compute upper hull */
+  upHull = hull->hull;
+  upHull[0] = 0;
+  upHull[1] = 1;
+  upPoints = 1; /* number of points in upper hull */
+  for (pointIdx = 2; pointIdx < n; pointIdx++) {
+    upPoints++;
+    upHull[upPoints] = pointIdx;
+    while (upPoints > 1 &&
+	   !rightTurn(r, upHull[upPoints], upHull[upPoints - 1],
+		      upHull[upPoints - 2])
+	   ) {
+      upHull[upPoints - 1] = upHull[upPoints];
+      upPoints--;
     }
+  }
 
-    /* compute lower hull, overwrite last point of upper hull */
-    loHull = &(upHull[upPoints]);
-    loHull[0] = n - 1;
-    loHull[1] = n - 2;
-    loPoints = 1; /* number of points in lower hull */
-    for (pointIdx = n - 3; pointIdx >= 0; pointIdx--) {
-	loPoints++;
-	loHull[loPoints] = pointIdx;
-	while (loPoints > 1 &&
-	       !rightTurn(r, loHull[loPoints], loHull[loPoints - 1],
-			  loHull[loPoints - 2])
-	    ) {
-	    loHull[loPoints - 1] = loHull[loPoints];
-	    loPoints--;
-	}
+  /* compute lower hull, overwrite last point of upper hull */
+  loHull = &(upHull[upPoints]);
+  loHull[0] = n - 1;
+  loHull[1] = n - 2;
+  loPoints = 1; /* number of points in lower hull */
+  for (pointIdx = n - 3; pointIdx >= 0; pointIdx--) {
+    loPoints++;
+    loHull[loPoints] = pointIdx;
+    while (loPoints > 1 &&
+	   !rightTurn(r, loHull[loPoints], loHull[loPoints - 1],
+		      loHull[loPoints - 2])
+	   ) {
+      loHull[loPoints - 1] = loHull[loPoints];
+      loPoints--;
     }
-    hull->n = loPoints + upPoints;
+  }
+  hull->n = loPoints + upPoints;
+  
+  G_debug(3, "numPoints:%d loPoints:%d upPoints:%d",
+	  n, loPoints, upPoints);
 
-    G_debug(3, "numPoints:%d loPoints:%d upPoints:%d",
-	    n, loPoints, upPoints);
+  /* reclaim uneeded memory */
+  hull->hull = (int *) G_realloc(hull->hull, (hull->n + 1) * sizeof(int));
 
-    /* reclaim uneeded memory */
-    hull->hull = (int *) G_realloc(hull->hull, (hull->n + 1) * sizeof(int));
+  /* Obtain coordinates of hull vertices */
+  hull->coord = (double *) G_malloc((hull->n + 1) * 3 * sizeof(double)); /* 1st = last pnt */
 
-    /* Obtain coordinates of hull vertices */
-    hull->coord = (double *) G_malloc((hull->n + 1) * 3 * sizeof(double)); /* 1st = last pnt */
+  int *hh, *hh0;
+  double *hc, (*r0)[3];
+  hc = &hull->coord[0];
+  hh = &hull->hull[0];
+  ri = &r[0];
 
-    int *hh, *hh0;
-    double *hc, (*r0)[3];
-    hc = &hull->coord[0];
-    hh = &hull->hull[0];
+  for (i=0; i <= hull->n; i++) {
+    if (i < hull->n) {
+      *hc = (*(ri+*hh))[0];
+      *(hc+1) = (*(ri+*hh))[1];
+      *(hc+2) = (*(ri+*hh))[2];
+      hc += 3;
+      hh++;
+    }
+    else { // coords of 1st equal to coords of last hull vertex
+      r0 = &r[0];
+      hh0 = &hull->hull[0];
+      *hc = (*(r0+*hh0))[0];
+      *(hc+1) = (*(r0+*hh0))[1];
+      *(hc+2) = (*(r0+*hh0))[2];
+    }      
+  }
 
-    for (i=0; i<=hull->n; i++) {
-      if (i < hull->n) {
-	*hc = (*(r+*hh))[0];
-	*(hc+1) = (*(r+*hh))[1];
-	*(hc+2) = (*(r+*hh))[2];
-	r++;
-	hc += 3;
-	hh++;
-      }
-      else { // coords of 1st equal to coords of last hull vertex
-	r0 = &r[0];
-	hh0 = &hull->hull[0];
-	*hc = (*(r0+*hh0))[0];
-	*(hc+1) = (*(r0+*hh0))[1];
-	*(hc+2) = (*(r0+*hh0))[2];
-      }      
-    }
-    return hull->n;
+  return hull->n;
 }
 
 /* ---------------------------- 
@@ -172,8 +173,9 @@
   for (i=0; i < hull.n; i++) {
     /* Bearings of hull edges */
     us = bearing(*hc, *(hc+3), *(hc+1), *(hc+4)); // x0, x1, y0, y1
-    if (us == -9999) // Identical points
+    if (us == -9999) { // Identical points
       continue;
+    }
     cosus = cos(us);
     sinus = sin(us);
 
@@ -187,10 +189,12 @@
       /* Transformed extent */
       switch (k) {
       case 0:
-	r_min = r_max = triple(*ht, *(ht+1), *(ht+2)); break;
+	r_min = r_max = triple(*ht, *(ht+1), *(ht+2)); 
+	break;
       default:
 	r_min = triple(MIN(*ht, *r_min), MIN(*(ht+1), *(r_min+1)), MIN(*(ht+2), *(r_min+2)));
 	r_max = triple(MAX(*ht, *r_max), MAX(*(ht+1), *(r_max+1)), MAX(*(ht+2), *(r_max+2)));
+	break;
       }
       hc_k += 3;
     } // end k
@@ -201,5 +205,8 @@
     S_min = MIN(S, S_min);
   } // end i
 
+  G_free(r_min);
+  G_free(r_max);
+
   return S_min;
 }

Modified: grass-addons/grass7/vector/v.nnstat/v.nnstat.html
===================================================================
--- grass-addons/grass7/vector/v.nnstat/v.nnstat.html	2015-07-08 12:28:22 UTC (rev 65547)
+++ grass-addons/grass7/vector/v.nnstat/v.nnstat.html	2015-07-08 18:39:16 UTC (rev 65548)
@@ -6,48 +6,154 @@
 <h3>Comparison of 2D and 3D NNA</h3>
 On the example of dataset that contains 2000 randomly distributed points, basic settings of analysis dimension (2D or 3D) will be examined:
 <ul>
-<li> <b>2D NNA</b> may be performed using <b>2D vector layer</b>. If 2D NNA is required to be performed using <b>3D vector layer</b>, <u>flag <i>-2</i></u> should be marked. In following figures, the results of both cases can be seen.
+<li> <b>2D NNA</b> may be performed using <b>2D vector layer</b>. If 2D NNA is required to be 
+performed using <b>3D vector layer</b>, <u>flag <i>-2</i></u> should be marked. The results of 
+both cases can be seen below.
 
 <div class="code"><pre>
-v.nnstat in=name_of_2D_vector_layer
+v.nnstat input=rand_2000_2d
 </pre></div>
 
-<div align="center" style="margin: 10px">
-<img src="images/rand_2000_2D.png" border=0><br>
-<i>2D NNA using 2D vector layer</i>
-</div>
+Output in the command line:
+<table>
+	<tr>
+		<td>
+			<div class="code"><pre>
+				Input coordinates have been read...
+				Computing average distance between nearest neighbors...
+ 				 100%
 
+
+				*** Nearest Neighbour Analysis results ***
+				Input settings .. 3D layer: 0 3D NNA: 0
+				Number of points .......... 2000
+				Area ...................... 398645718.651701 [units^2]
+				Density of points ......... 0.000005
+				Average distance between the nearest neighbours ........... 225.859 [units]
+				Average expected distance between the nearest neighbours .. 223.228 [units]
+				Ratio rA/rE ............... 1.011785
+
+				*** Results of two-tailed test of the mean ***
+				Null hypothesis: Point set is randomly distributed within the region.
+				Standard variate of the normal curve> c = 1.008239
+				Null hypothesis IS NOT REJECTED at the significance level alpha = 0.05
+			</pre></div>
+		</td>
+	</tr>
+</table>
+
 <div class="code"><pre>
-v.nnstat in=name_of_3D_vector_layer -2
+v.nnstat input=rand_2000_3d -2
 </pre></div>
 
-<div align="center" style="margin: 10px">
-<img src="images/rand_2000_3D_2D.png" border=0><br>
-<i>2D NNA using 3D vector layer (the 3rd dimension is not considered in the analysis)</i>
-</div>
+Output in the command line:
+<table>
+	<tr>
+		<td>
+			<div class="code"><pre>
+				Input coordinates have been read...
+				Computing average distance between nearest neighbors...
+ 				 100%
 
-<p><b><u>NOTE:</u></b> Comparing the results of 2D NNA with results summarized in (<a href="http://geoinformatics.fsv.cvut.cz/pdf/geoinformatics-fce-ctu-2013-11.pdf">Stopkova, 2013</a>), there can be seen small difference between the values of area. It is assumed to be caused by differences in transformed coordinates of the convex hull that have been computed using two versions of the module. This bug will be fixed soon.</p>
 
-<li> <b>3D NNA</b> can be performed just using <b>3D vector layer</b>. If 3D NNA is required to be performed using <b>2D vector layer</b>, <u>name of the column in attribute table that contains elevation values</u> must be set. The results are summarized in following tables.
+				*** Nearest Neighbour Analysis results ***
+				Input settings .. 3D layer: 1 3D NNA: 0
+				Number of points .......... 2000
+				Area ...................... 398645718.651701 [units^2]
+				Density of points ......... 0.000005
+				Average distance between the nearest neighbours ........... 225.859 [units]
+				Average expected distance between the nearest neighbours .. 223.228 [units]
+				Ratio rA/rE ............... 1.011785
+				
+				*** Results of two-tailed test of the mean ***
+				Null hypothesis: Point set is randomly distributed within the region.
+				Standard variate of the normal curve> c = 1.008239
+				Null hypothesis IS NOT REJECTED at the significance level alpha = 0.05
+			</pre></div>
+		</td>
+	</tr>
+</table>
 
+<p><b>NOTE:</b> Comparing the results of 2D NNA with results summarized in 
+(<a href="http://geoinformatics.fsv.cvut.cz/pdf/geoinformatics-fce-ctu-2013-11.pdf">Stopkova, 2013</a>), there can be 
+seen small difference between the values of area. It is assumed to be caused by differences in transformed coordinates of 
+the convex hull that have been computed using two versions of the module.
+
+<li> <b>3D NNA</b> can be performed just using <b>3D vector layer</b>. If 3D NNA is required to be performed using 
+<b>2D vector layer</b>, <u>name of the column in attribute table that contains elevation values</u> must be set. 
+The results of both cases can be seen below.
+
 <div class="code"><pre>
-v.nnstat in=name_of_3D_vector_layer
+v.nnstat input=rand_2000_3d
 </pre></div>
 
-<div align="center" style="margin: 10px">
-<img src="images/rand_2000_3D_3D.png" border=0><br>
-<i>3D NNA using 3D vector layer</i>
-</div>
+Output in the command line:
+<table>
+	<tr>
+		<td>
+			<div class="code"><pre>
+				Input coordinates have been read...
+				Computing average distance between nearest neighbors...
+				 100%
+				Reading 3D vertices...
+ 				 100%
+				Constructing 3D hull...
+  				  99%
 
+				*** Nearest Neighbour Analysis results ***
+				Input settings .. 3D layer: 1 3D NNA: 1
+				Number of points .......... 2000
+				Volume .................... 398423031180.489197 [units^3]
+				Density of points ......... 0.000000
+				Average distance between the nearest neighbours ........... 346.072 [units]
+				Average expected distance between the nearest neighbours .. 323.531 [units]
+				Ratio rA/rE ............... 1.069670
+				
+				*** Results of two-tailed test of the mean ***
+				Null hypothesis: Point set is randomly distributed within the region.
+				Standard variate of the normal curve> c = 0.191691
+				Null hypothesis IS NOT REJECTED at the significance level alpha = 0.05
+			</pre></div>
+		</td>
+	</tr>
+</table>
+
 <div class="code"><pre>
-v.nnstat in=name_of_2D_vector_layer zcol=name_of_attribute_column_with_elevations
+v.nnstat input=rand_2000_2d zcolumn=z
 </pre></div>
 
-<div align="center" style="margin: 10px">
-<img src="images/rand_2000_2_5D.png" border=0><br>
-<i>3D NNA using 2D vector layer and elevation values obtained from attribute table of the layer</i>
-</div>
+Output in the command line:
+<table>
+	<tr>
+		<td>
+			<div class="code"><pre>
+				Reading elevations from attribute table: 2000 records selected
+				Input coordinates have been read...
+				Computing average distance between nearest neighbors...
+				 100%
+				Reading 3D vertices...
+ 				 100%
+				Constructing 3D hull...
+  				  99%
 
+				*** Nearest Neighbour Analysis results ***
+				Input settings .. 3D layer: 0 .. 3D NNA: 1 .. zcolumn: z
+				Number of points .......... 2000
+				Volume .................... 398423031180.489197 [units^3]
+				Density of points ......... 0.000000
+				Average distance between the nearest neighbours ........... 346.072 [units]
+				Average expected distance between the nearest neighbours .. 323.531 [units]
+				Ratio rA/rE ............... 1.069670
+				
+				*** Results of two-tailed test of the mean ***
+				Null hypothesis: Point set is randomly distributed within the region.
+				Standard variate of the normal curve> c = 0.191691
+				Null hypothesis IS NOT REJECTED at the significance level alpha = 0.05
+			</pre></div>
+		</td>
+	</tr>
+</table>
+
 <li> <b>Warning</b>: If flag <i>-2</i> is set up together with <i>zcolumn</i>, the flag will have higher priority and 2D NNA will be performed.
 </ul>
 



More information about the grass-commit mailing list