[GRASS-SVN] r69357 - grass/trunk/vector/v.to.rast
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Sep 3 07:36:02 PDT 2016
Author: mmetz
Date: 2016-09-03 07:36:02 -0700 (Sat, 03 Sep 2016)
New Revision: 69357
Modified:
grass/trunk/vector/v.to.rast/support.c
Log:
v.to.rast: fix category labels for use=attr
Modified: grass/trunk/vector/v.to.rast/support.c
===================================================================
--- grass/trunk/vector/v.to.rast/support.c 2016-09-03 14:16:26 UTC (rev 69356)
+++ grass/trunk/vector/v.to.rast/support.c 2016-09-03 14:36:02 UTC (rev 69357)
@@ -5,6 +5,7 @@
* -1 - error
*/
+#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <grass/gis.h>
@@ -15,7 +16,14 @@
#include <grass/glocale.h>
#include "local.h"
+struct My_labels_rule
+{
+ dbString label;
+ double d;
+ int i;
+};
+
int update_hist(const char *raster_name, const char *vector_name, long scale)
{
struct History hist;
@@ -202,13 +210,31 @@
return 1;
}
+static int cmp_labels_d(const void *a, const void *b)
+{
+ struct My_labels_rule *al = (struct My_labels_rule *) a;
+ struct My_labels_rule *bl = (struct My_labels_rule *) b;
+ if (al->d < bl->d)
+ return -1;
+
+ return (al->d > bl->d);
+}
+
+static int cmp_labels_i(const void *a, const void *b)
+{
+ struct My_labels_rule *al = (struct My_labels_rule *) a;
+ struct My_labels_rule *bl = (struct My_labels_rule *) b;
+
+ return (al->i - bl->i);
+}
+
/* add labels to raster cells */
int update_labels(const char *rast_name, const char *vector_map, int field,
const char *label_column, int use, int val,
const char *attr_column)
{
- int i;
+ int i, j;
/* Map */
struct Map_info Map;
@@ -223,12 +249,7 @@
/* labels */
struct Categories rast_cats;
int labels_n_values = 0;
- struct My_labels_rule
- {
- dbString label;
- double d;
- int i;
- } *my_labels_rules;
+ struct My_labels_rule *my_labels_rules;
/* init raster categories */
Rast_init_cats("Categories", &rast_cats);
@@ -333,21 +354,107 @@
/* close the database driver */
db_close_database_shutdown_driver(Driver);
- /* set the color rules: for each rule */
- if (is_fp) {
- /* add label */
- for (i = 0; i < labels_n_values - 1; i++)
- Rast_set_cat(&my_labels_rules[i].d,
- &my_labels_rules[i + 1].d,
- db_get_string(&my_labels_rules[i].label),
- &rast_cats, DCELL_TYPE);
+ /* set the labels */
+ if (labels_n_values > 0) {
+ char *lblstr;
+
+ /* remove values without labels */
+ j = 0;
+ for (i = 0; i < labels_n_values; i++) {
+ lblstr = db_get_string(&my_labels_rules[i].label);
+ if (lblstr && *lblstr) {
+ my_labels_rules[j] = my_labels_rules[i];
+ j++;
+ }
+ }
+ labels_n_values = j;
}
- else {
- for (i = 0; i < labels_n_values; i++)
- Rast_set_c_cat(&(my_labels_rules[i].i),
- &(my_labels_rules[i].i),
- db_get_string(&my_labels_rules[i].label),
- &rast_cats);
+
+ if (labels_n_values > 0) {
+ char *lblstr;
+
+ if (is_fp) {
+ DCELL val1, val2;
+
+ qsort(my_labels_rules, labels_n_values,
+ sizeof(struct My_labels_rule), cmp_labels_d);
+
+ /* remove duplicate values */
+ j = 1;
+ for (i = 1; i < labels_n_values; i++) {
+ if (my_labels_rules[i - 1].d != my_labels_rules[i].d) {
+ my_labels_rules[j] = my_labels_rules[i];
+ j++;
+ }
+ else {
+ if (strcmp(db_get_string(&my_labels_rules[i].label),
+ db_get_string(&my_labels_rules[i - 1].label)) != 0) {
+ G_warning(_("Different labels for the same value are not supported"));
+ }
+ }
+ }
+ labels_n_values = j;
+
+ /* add labels */
+ i = 0;
+ val1 = val2 = my_labels_rules[i].d;
+ lblstr = db_get_string(&my_labels_rules[i].label);
+
+ for (i = 1; i < labels_n_values; i++) {
+ if (strcmp(lblstr, db_get_string(&my_labels_rules[i].label)) == 0) {
+ val2 = my_labels_rules[i].d;
+ }
+ else {
+ Rast_set_d_cat(&val1, &val2, lblstr,
+ &rast_cats);
+
+ val1 = val2 = my_labels_rules[i].d;
+ lblstr = db_get_string(&my_labels_rules[i].label);
+ }
+ }
+ Rast_set_d_cat(&val1, &val2, lblstr, &rast_cats);
+ }
+ else {
+ CELL val1, val2;
+
+ qsort(my_labels_rules, labels_n_values,
+ sizeof(struct My_labels_rule), cmp_labels_i);
+
+ /* remove duplicate values */
+ j = 1;
+ for (i = 1; i < labels_n_values; i++) {
+ if (my_labels_rules[i - 1].i != my_labels_rules[i].i) {
+ my_labels_rules[j] = my_labels_rules[i];
+ j++;
+ }
+ else {
+ if (strcmp(db_get_string(&my_labels_rules[i].label),
+ db_get_string(&my_labels_rules[i - 1].label)) != 0) {
+ G_warning(_("Different labels for the same value are not supported"));
+ }
+ }
+ }
+ labels_n_values = j;
+
+ /* add labels */
+ i = 0;
+ val1 = val2 = my_labels_rules[i].i;
+ lblstr = db_get_string(&my_labels_rules[i].label);
+
+ for (i = 1; i < labels_n_values; i++) {
+ if (strcmp(lblstr, db_get_string(&my_labels_rules[i].label)) == 0) {
+ val2 = my_labels_rules[i].i;
+ }
+ else {
+ Rast_set_c_cat(&val1, &val2, lblstr,
+ &rast_cats);
+
+ val1 = val2 = my_labels_rules[i].i;
+ lblstr = db_get_string(&my_labels_rules[i].label);
+ }
+ }
+ Rast_set_c_cat(&val1, &val2, lblstr, &rast_cats);
+ }
}
}
break;
More information about the grass-commit
mailing list