[GRASS-SVN] r72869 - grass/branches/releasebranch_7_4/vector/v.to.db
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jun 21 13:57:47 PDT 2018
Author: mmetz
Date: 2018-06-21 13:57:47 -0700 (Thu, 21 Jun 2018)
New Revision: 72869
Modified:
grass/branches/releasebranch_7_4/vector/v.to.db/areas.c
grass/branches/releasebranch_7_4/vector/v.to.db/report.c
grass/branches/releasebranch_7_4/vector/v.to.db/units.c
grass/branches/releasebranch_7_4/vector/v.to.db/update.c
Log:
v.to.db: fix compactness and fractal dimension (fixes #3102, backport trunk r72866,8)
Modified: grass/branches/releasebranch_7_4/vector/v.to.db/areas.c
===================================================================
--- grass/branches/releasebranch_7_4/vector/v.to.db/areas.c 2018-06-21 20:55:25 UTC (rev 72868)
+++ grass/branches/releasebranch_7_4/vector/v.to.db/areas.c 2018-06-21 20:57:47 UTC (rev 72869)
@@ -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/branches/releasebranch_7_4/vector/v.to.db/report.c
===================================================================
--- grass/branches/releasebranch_7_4/vector/v.to.db/report.c 2018-06-21 20:55:25 UTC (rev 72868)
+++ grass/branches/releasebranch_7_4/vector/v.to.db/report.c 2018-06-21 20:57:47 UTC (rev 72869)
@@ -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/branches/releasebranch_7_4/vector/v.to.db/units.c
===================================================================
--- grass/branches/releasebranch_7_4/vector/v.to.db/units.c 2018-06-21 20:55:25 UTC (rev 72868)
+++ grass/branches/releasebranch_7_4/vector/v.to.db/units.c 2018-06-21 20:57:47 UTC (rev 72869)
@@ -23,6 +23,13 @@
for (i = 0; i < vstat.rcat; i++)
Values[i].d1 *= sq_f;
break;
+ case O_COMPACT:
+ 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/branches/releasebranch_7_4/vector/v.to.db/update.c
===================================================================
--- grass/branches/releasebranch_7_4/vector/v.to.db/update.c 2018-06-21 20:55:25 UTC (rev 72868)
+++ grass/branches/releasebranch_7_4/vector/v.to.db/update.c 2018-06-21 20:57:47 UTC (rev 72869)
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <math.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
#include "global.h"
@@ -11,7 +12,7 @@
int i, *catexst, *cex, upd, fcat;
char buf1[2000], buf2[2000], left[20], right[20];
struct field_info *qFi, *Fi;
- dbString stmt;
+ dbString stmt, strval;
dbDriver *driver;
vstat.dupl = 0;
@@ -21,6 +22,7 @@
vstat.error = 0;
db_init_string(&stmt);
+ db_init_string(&strval);
/* layer to find table to read from */
qFi = Vect_get_field(Map, options.qfield);
@@ -100,8 +102,6 @@
case O_LENGTH:
case O_AREA:
- case O_COMPACT:
- case O_FD:
case O_PERIMETER:
case O_SLOPE:
case O_SINUOUS:
@@ -118,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:
@@ -196,8 +216,10 @@
Fi->key, Values[i].cat);
break;
case (DB_C_TYPE_STRING):
+ db_set_string(&strval, Values[i].str1);
+ db_double_quote_string(&strval);
sprintf(buf2, "%s '%s' where %s = %d", buf1,
- Values[i].str1, Fi->key, Values[i].cat);
+ db_get_string(&strval), Fi->key, Values[i].cat);
break;
case (DB_C_TYPE_DATETIME):
sprintf(buf2, "%s '%s' where %s = %d", buf1,
@@ -248,6 +270,8 @@
vstat.update++;
}
else {
+ G_warning(_("Cannot update table: %s"),
+ db_get_string(&stmt));
vstat.error++;
}
}
More information about the grass-commit
mailing list