Ver código fonte

arraystatslib: add AS_option_to_algorithm()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@63911 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 10 anos atrás
pai
commit
92f799b252

+ 3 - 3
display/d.thematic.area/main.c

@@ -325,9 +325,9 @@ int main(int argc, char **argv)
 
 	    /* Get classbreaks for given algorithm and number of classbreaks.
 	     * class_info takes any info coming from the classification algorithms */
-	    class_info =
-		AS_class_apply_algorithm(algo_opt->answer, data, nrec, &nbreaks,
-                                         breakpoints);
+	    class_info = AS_class_apply_algorithm(AS_option_to_algorithm(algo_opt),
+                                                  data, nrec, &nbreaks,
+                                                  breakpoints);
 
 	}
 	else {

+ 6 - 0
include/arraystats.h

@@ -22,6 +22,12 @@ struct GASTATS
     double stdev;
 };
 
+#define CLASS_INTERVAL 1
+#define CLASS_STDEV    2
+#define CLASS_QUANT    3
+#define CLASS_EQUIPROB 4
+#define CLASS_DISCONT  5
+      
 #include <grass/defs/arraystats.h>
 
 #endif

+ 2 - 1
include/defs/arraystats.h

@@ -6,7 +6,8 @@ void AS_eqdrt(double[], double[], int, int, double *);
 void AS_basic_stats(double *, int, struct GASTATS *);
 
 /* class.c */
-double AS_class_apply_algorithm(char *, double *, int, int *, double *);
+int AS_option_to_algorithm(const struct Option *);
+double AS_class_apply_algorithm(int, double *, int, int *, double *);
 int AS_class_interval(double *, int, int, double *);
 int AS_class_quant(double *, int, int, double *);
 double AS_class_discont(double *, int, int, double *);

+ 36 - 13
lib/arraystats/class.c

@@ -3,27 +3,50 @@
 #include <grass/glocale.h>
 #include <grass/arraystats.h>
 
-double AS_class_apply_algorithm(char *algo, double *data, int nrec, int *nbreaks,
+int AS_option_to_algorithm(const struct Option * option)
+{
+    if (G_strcasecmp(option->answer, "int") == 0)
+        return CLASS_INTERVAL;
+    if (G_strcasecmp(option->answer, "std") == 0)
+        return CLASS_STDEV;
+    if (G_strcasecmp(option->answer, "qua") == 0)
+        return CLASS_QUANT;
+    if (G_strcasecmp(option->answer, "equ") == 0)
+        return CLASS_EQUIPROB;
+    if (G_strcasecmp(option->answer, "dis") == 0)
+        return CLASS_DISCONT;
+
+    G_fatal_error(_("Unknown algorithm '%s'"), option->answer);
+}
+    
+double AS_class_apply_algorithm(int algo, double *data, int nrec, int *nbreaks,
                                 double *classbreaks)
 {
     double finfo = 0.0;
 
-    if (G_strcasecmp(algo, "int") == 0)
-	finfo = AS_class_interval(data, nrec, *nbreaks, classbreaks);
-    else if (G_strcasecmp(algo, "std") == 0)
-	finfo = AS_class_stdev(data, nrec, *nbreaks, classbreaks);
-    else if (G_strcasecmp(algo, "qua") == 0)
+    switch (algo) {
+    case CLASS_INTERVAL:
+        finfo = AS_class_interval(data, nrec, *nbreaks, classbreaks);
+        break;
+    case CLASS_STDEV:
+        finfo = AS_class_stdev(data, nrec, *nbreaks, classbreaks);
+        break;
+    case CLASS_QUANT:
         finfo = AS_class_quant(data, nrec, *nbreaks, classbreaks);
-    else if (G_strcasecmp(algo, "equ") == 0)
-	finfo = AS_class_equiprob(data, nrec, nbreaks, classbreaks);
-    else if (G_strcasecmp(algo, "dis") == 0)
-	    /*	finfo = class_discont(data, nrec, *nbreaks, classbreaks); disabled because of bugs */
+        break;
+    case CLASS_EQUIPROB:
+        finfo = AS_class_equiprob(data, nrec, nbreaks, classbreaks);
+        break;
+    case CLASS_DISCONT:
+        /*	finfo = class_discont(data, nrec, *nbreaks, classbreaks); disabled because of bugs */
         G_fatal_error(_("Discont algorithm currently not available because of bugs"));
-    else
-	G_fatal_error(_("%s: Unknown algorithm"), algo);
+        break;
+    default:
+        break;
+    }
 
     if (finfo == 0)
-	G_fatal_error(_("%s: Error in classification algorithm"), algo);
+	G_fatal_error(_("Classification algorithm failed"));
 
     return finfo;
 }

+ 3 - 2
vector/v.class/main.c

@@ -164,9 +164,10 @@ int main(int argc, char *argv[])
      * finfo takes any info coming from the classification algorithms
      * equ algorithm can alter number of class breaks */
     finfo =
-	AS_class_apply_algorithm(algo_opt->answer, data, nrec, &nbreaks,
+        AS_class_apply_algorithm(AS_option_to_algorithm(algo_opt),
+                                 data, nrec, &nbreaks,
                                  classbreaks);
-
+    
 
     if (G_strcasecmp(algo_opt->answer, "dis") == 0 && finfo < 3.84148)
 	G_warning(_("The discontinuities algorithm indicates that some "