[GRASS-SVN] r72866 - grass/trunk/vector/v.to.db

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 21 12:49:23 PDT 2018


Author: mmetz
Date: 2018-06-21 12:49:23 -0700 (Thu, 21 Jun 2018)
New Revision: 72866

Modified:
   grass/trunk/vector/v.to.db/areas.c
   grass/trunk/vector/v.to.db/report.c
   grass/trunk/vector/v.to.db/units.c
   grass/trunk/vector/v.to.db/update.c
Log:
v.to.db: fix compactness and fractal dimension (fixes #3102)

Modified: grass/trunk/vector/v.to.db/areas.c
===================================================================
--- grass/trunk/vector/v.to.db/areas.c	2018-06-21 16:50:09 UTC (rev 72865)
+++ grass/trunk/vector/v.to.db/areas.c	2018-06-21 19:49:23 UTC (rev 72866)
@@ -59,11 +59,9 @@
 			Values[idx].d1 += perimeter;
 			break;
 		    case O_COMPACT:
-			Values[idx].d1 =
-			    perimeter / (2.0 * sqrt(M_PI * area));
-			break;
 		    case O_FD:
-			Values[idx].d1 = 2.0 * log(perimeter) / log(area);
+			Values[idx].d1 += area;
+			Values[idx].d2 += perimeter;
 			break;
 		    case O_BBOX:
 			if (Values[idx].d1 < Bbox.N) 
@@ -90,11 +88,9 @@
 		    Values[idx].d1 += perimeter;
 		    break;
 		case O_COMPACT:
-		    Values[idx].d1 =
-			perimeter / (2.0 * sqrt(M_PI * area));
-		    break;
 		case O_FD:
-		    Values[idx].d1 = 2.0 * log(perimeter) / log(area);
+		    Values[idx].d1 += area;
+		    Values[idx].d2 += perimeter;
 		    break;
 	        case O_BBOX:
 		    if (Values[idx].d1 < Bbox.N) 

Modified: grass/trunk/vector/v.to.db/report.c
===================================================================
--- grass/trunk/vector/v.to.db/report.c	2018-06-21 16:50:09 UTC (rev 72865)
+++ grass/trunk/vector/v.to.db/report.c	2018-06-21 19:49:23 UTC (rev 72866)
@@ -1,3 +1,4 @@
+#include <math.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 #include "global.h"
@@ -57,17 +58,29 @@
 	break;
 
     case O_COMPACT:
+	/* perimeter / (2.0 * sqrt(M_PI * area)) */
 	if (G_verbose() > G_verbose_min())
 	    fprintf(stdout, "cat%scompact\n", options.fs);
-	for (i = 0; i < vstat.rcat; i++)
+	for (i = 0; i < vstat.rcat; i++) {
+	    Values[i].d1 = Values[i].d2 / (2.0 * sqrt(M_PI * Values[i].d1));
 	    fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
+	}
 	break;
 
     case O_FD:
+	/* 2.0 * log(perimeter) / log(area) 
+	 * avoid division by zero: 
+	 * 2.0 * log(1 + perimeter) / log(1 + area)
+	 * more in line with compactness:
+	 * 2.0 * log(perimeter / (2.0 * sqrt(M_PI)) / log(area) */
 	if (G_verbose() > G_verbose_min())
 	    fprintf(stdout, "cat%sfd\n", options.fs);
-	for (i = 0; i < vstat.rcat; i++)
+	for (i = 0; i < vstat.rcat; i++) {
+	    if (Values[i].d1 == 1) /* log(1) == 0 */
+		Values[i].d1 += 0.000001;
+	    Values[i].d1 = 2.0 * log(Values[i].d2 / (2.0 * sqrt(M_PI))) / log(Values[i].d1);
 	    fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs, Values[i].d1);
+	}
 	break;
 
     case O_PERIMETER:

Modified: grass/trunk/vector/v.to.db/units.c
===================================================================
--- grass/trunk/vector/v.to.db/units.c	2018-06-21 16:50:09 UTC (rev 72865)
+++ grass/trunk/vector/v.to.db/units.c	2018-06-21 19:49:23 UTC (rev 72866)
@@ -23,6 +23,12 @@
 	for (i = 0; i < vstat.rcat; i++)
 	    Values[i].d1 *= sq_f;
 	break;
+    case O_FD:
+	for (i = 0; i < vstat.rcat; i++) {
+	    Values[i].d1 *= sq_f;
+	    Values[i].d2 *= f;
+	}
+	break;
     case O_AZIMUTH:
 	    if (rad == 0) {
 		    for (i = 0; i < vstat.rcat; i++) {

Modified: grass/trunk/vector/v.to.db/update.c
===================================================================
--- grass/trunk/vector/v.to.db/update.c	2018-06-21 16:50:09 UTC (rev 72865)
+++ grass/trunk/vector/v.to.db/update.c	2018-06-21 19:49:23 UTC (rev 72866)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 #include "global.h"
@@ -101,8 +102,6 @@
 
 	case O_LENGTH:
 	case O_AREA:
-	case O_COMPACT:
-	case O_FD:
 	case O_PERIMETER:
 	case O_SLOPE:
 	case O_SINUOUS:
@@ -119,6 +118,26 @@
 	       	Values[i].d4, Fi->key, Values[i].cat);
 	    break;
 
+	case O_COMPACT:
+	    /* perimeter / (2.0 * sqrt(M_PI * area)) */
+	    Values[i].d1 = Values[i].d2 / (2.0 * sqrt(M_PI * Values[i].d1));
+	    sprintf(buf2, "%s %f where %s = %d", buf1, Values[i].d1, Fi->key,
+		    Values[i].cat);
+	    break;
+
+	case O_FD:
+	    /* 2.0 * log(perimeter) / log(area) 
+	     * avoid division by zero: 
+	     * 2.0 * log(1 + perimeter) / log(1 + area)
+	     * more in line with compactness:
+	     * 2.0 * log(perimeter / (2.0 * sqrt(M_PI)) / log(area) */
+	    if (Values[i].d1 == 1) /* log(1) == 0 */
+		Values[i].d1 += 0.000001;
+	    Values[i].d1 = 2.0 * log(Values[i].d2 / (2.0 * sqrt(M_PI))) / log(Values[i].d1);
+	    sprintf(buf2, "%s %f where %s = %d", buf1, Values[i].d1, Fi->key,
+		    Values[i].cat);
+	    break;
+
 	case O_COOR:
 	case O_START:
 	case O_END:



More information about the grass-commit mailing list