[GRASS-SVN] r53904 - grass/trunk/vector/v.overlay

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Nov 18 23:43:42 PST 2012


Author: mmetz
Date: 2012-11-18 23:43:42 -0800 (Sun, 18 Nov 2012)
New Revision: 53904

Modified:
   grass/trunk/vector/v.overlay/area_area.c
   grass/trunk/vector/v.overlay/line_area.c
   grass/trunk/vector/v.overlay/main.c
Log:
v.overlay: copy attributes

Modified: grass/trunk/vector/v.overlay/area_area.c
===================================================================
--- grass/trunk/vector/v.overlay/area_area.c	2012-11-18 20:30:36 UTC (rev 53903)
+++ grass/trunk/vector/v.overlay/area_area.c	2012-11-19 07:43:42 UTC (rev 53904)
@@ -112,11 +112,13 @@
 	for (area = 1; area <= nareas; area++) {
 	    Centr[area].cat[input] = Vect_new_cats_struct();
 
+	    G_percent(area, nareas, 1);
+
 	    in_area =
 		Vect_find_area(&(In[input]), Centr[area].x, Centr[area].y);
 	    if (in_area > 0) {
 		in_centr = Vect_get_area_centroid(&(In[input]), in_area);
-		if (in_centr > 0) {
+		if (in_centr > 0 && ofield[input + 1] > 0) {
 		    int i;
 
 		    Vect_read_line(&(In[input]), NULL, Cats, in_centr);
@@ -125,7 +127,7 @@
 			if (Cats->field[i] == field[input]) {
 			    ATTR *at;
 
-			    Vect_cat_set(Centr[area].cat[input], field[input],
+			    Vect_cat_set(Centr[area].cat[input], ofield[input + 1],
 					 Cats->cat[i]);
 
 			    /* Mark as used */
@@ -138,7 +140,6 @@
 		    }
 		}
 	    }
-	    G_percent(area, nareas, 1);
 	}
     }
 
@@ -149,6 +150,8 @@
     for (area = 1; area <= nareas; area++) {
 	int i;
 
+	G_percent(area, nareas, 1);
+
 	/* check the condition */
 	switch (operator) {
 	case OP_AND:
@@ -183,114 +186,114 @@
 
 	Vect_append_point(Points, Centr[area].x, Centr[area].y, 0.0);
 
-	/* Add new cats for all combinations of input cats (-1 in cycle for null) */
-	/* TODO: put cats of input maps into different layers, i.e.
-	 * preserve cat values, change layer number if needed */
-	for (i = -1; i < Centr[area].cat[0]->n_cats; i++) {
-	    int j;
+	if (ofield[0]  > 0) {
+	    /* Add new cats for all combinations of input cats (-1 in cycle for null) */
+	    for (i = -1; i < Centr[area].cat[0]->n_cats; i++) {
+		int j;
 
-	    if (i == -1 && Centr[area].cat[0]->n_cats > 0)
-		continue;	/* no need to make null */
-
-	    for (j = -1; j < Centr[area].cat[1]->n_cats; j++) {
-		if (j == -1 && Centr[area].cat[1]->n_cats > 0)
+		if (i == -1 && Centr[area].cat[0]->n_cats > 0)
 		    continue;	/* no need to make null */
 
-		if (ofield[0] > 0)
-		    Vect_cat_set(Cats, ofield[0], out_cat);
+		for (j = -1; j < Centr[area].cat[1]->n_cats; j++) {
+		    if (j == -1 && Centr[area].cat[1]->n_cats > 0)
+			continue;	/* no need to make null */
 
-		/* attributes */
-		if (driver) {
-		    ATTR *at;
+		    if (ofield[0] > 0)
+			Vect_cat_set(Cats, ofield[0], out_cat);
 
-		    sprintf(buf, "insert into %s values ( %d", Fi->table,
-			    out_cat);
-		    db_set_string(&stmt, buf);
+		    /* attributes */
+		    if (driver) {
+			ATTR *at;
 
-		    /* cata */
-		    if (i >= 0) {
-			if (attr[0].columns) {
-			    at = find_attr(&(attr[0]),
-					   Centr[area].cat[0]->cat[i]);
-			    if (!at)
-				G_fatal_error(_("Attribute not found"));
+			sprintf(buf, "insert into %s values ( %d", Fi->table,
+				out_cat);
+			db_set_string(&stmt, buf);
 
-			    if (at->values)
-				db_append_string(&stmt, at->values);
-			    else
-				db_append_string(&stmt, attr[0].null_values);
+			/* cata */
+			if (i >= 0) {
+			    if (attr[0].columns) {
+				at = find_attr(&(attr[0]),
+					       Centr[area].cat[0]->cat[i]);
+				if (!at)
+				    G_fatal_error(_("Attribute not found"));
+
+				if (at->values)
+				    db_append_string(&stmt, at->values);
+				else
+				    db_append_string(&stmt, attr[0].null_values);
+			    }
+			    else {
+				sprintf(buf, ", %d", Centr[area].cat[0]->cat[i]);
+				db_append_string(&stmt, buf);
+			    }
 			}
 			else {
-			    sprintf(buf, ", %d", Centr[area].cat[0]->cat[i]);
-			    db_append_string(&stmt, buf);
+			    if (attr[0].columns) {
+				db_append_string(&stmt, attr[0].null_values);
+			    }
+			    else {
+				sprintf(buf, ", null");
+				db_append_string(&stmt, buf);
+			    }
 			}
-		    }
-		    else {
-			if (attr[0].columns) {
-			    db_append_string(&stmt, attr[0].null_values);
-			}
-			else {
-			    sprintf(buf, ", null");
-			    db_append_string(&stmt, buf);
-			}
-		    }
 
-		    /* catb */
-		    if (j >= 0) {
-			if (attr[1].columns) {
-			    at = find_attr(&(attr[1]),
-					   Centr[area].cat[1]->cat[j]);
-			    if (!at)
-				G_fatal_error(_("Attribute not found"));
+			/* catb */
+			if (j >= 0) {
+			    if (attr[1].columns) {
+				at = find_attr(&(attr[1]),
+					       Centr[area].cat[1]->cat[j]);
+				if (!at)
+				    G_fatal_error(_("Attribute not found"));
 
-			    if (at->values)
-				db_append_string(&stmt, at->values);
-			    else
-				db_append_string(&stmt, attr[1].null_values);
+				if (at->values)
+				    db_append_string(&stmt, at->values);
+				else
+				    db_append_string(&stmt, attr[1].null_values);
+			    }
+			    else {
+				sprintf(buf, ", %d", Centr[area].cat[1]->cat[j]);
+				db_append_string(&stmt, buf);
+			    }
 			}
 			else {
-			    sprintf(buf, ", %d", Centr[area].cat[1]->cat[j]);
-			    db_append_string(&stmt, buf);
+			    if (attr[1].columns) {
+				db_append_string(&stmt, attr[1].null_values);
+			    }
+			    else {
+				sprintf(buf, ", null");
+				db_append_string(&stmt, buf);
+			    }
 			}
-		    }
-		    else {
-			if (attr[1].columns) {
-			    db_append_string(&stmt, attr[1].null_values);
-			}
-			else {
-			    sprintf(buf, ", null");
-			    db_append_string(&stmt, buf);
-			}
-		    }
 
-		    db_append_string(&stmt, " )");
+			db_append_string(&stmt, " )");
 
-		    G_debug(3, db_get_string(&stmt));
+			G_debug(3, db_get_string(&stmt));
 
-		    if (db_execute_immediate(driver, &stmt) != DB_OK)
-			G_warning(_("Unable to insert new record: '%s'"),
-				  db_get_string(&stmt));
+			if (db_execute_immediate(driver, &stmt) != DB_OK)
+			    G_warning(_("Unable to insert new record: '%s'"),
+				      db_get_string(&stmt));
+		    }
+		    out_cat++;
 		}
-		out_cat++;
 	    }
 	}
 
 	/* Add all cats from imput vectors */
-	if (ofield[1] > 0) {
+	if (ofield[1] > 0 && field[0] > 0) {
 	    for (i = 0; i < Centr[area].cat[0]->n_cats; i++) {
-		Vect_cat_set(Cats, ofield[1], Centr[area].cat[0]->cat[i]);
+		if (Centr[area].cat[0]->field[i] == field[0])
+		    Vect_cat_set(Cats, ofield[1], Centr[area].cat[0]->cat[i]);
 	    }
 	}
 
-	if (ofield[2] > 0) {
+	if (ofield[2] > 0 && field[1] > 0 && ofield[1] != ofield[2]) {
 	    for (i = 0; i < Centr[area].cat[1]->n_cats; i++) {
-		Vect_cat_set(Cats, ofield[2], Centr[area].cat[1]->cat[i]);
+		if (Centr[area].cat[1]->field[i] == field[1])
+		    Vect_cat_set(Cats, ofield[2], Centr[area].cat[1]->cat[i]);
 	    }
 	}
 
 	Vect_write_line(Out, GV_CENTROID, Points, Cats);
-
-	G_percent(area, nareas, 1);
     }
 
     /* Build topology and remove boundaries with area without centroid on both sides */

Modified: grass/trunk/vector/v.overlay/line_area.c
===================================================================
--- grass/trunk/vector/v.overlay/line_area.c	2012-11-18 20:30:36 UTC (rev 53903)
+++ grass/trunk/vector/v.overlay/line_area.c	2012-11-19 07:43:42 UTC (rev 53904)
@@ -71,11 +71,6 @@
     G_message(_("Merging lines..."));
     Vect_merge_lines(Out, GV_LINE, NULL, NULL);
 
-#if 0
-    /* Basic topology needed only */
-    Vect_build_partial(Out, GV_BUILD_BASE);
-#endif
-
     nlines = Vect_get_num_lines(Out);
 
     /* Warning!: cleaning process (break) creates new vertices which are usually slightly 
@@ -85,6 +80,7 @@
      */
 
     /* Check if the line is inside or outside binput area */
+    G_message(_("Selecting lines..."));
     ncat = 1;
     for (line = 1; line <= nlines; line++) {
 	int ltype;
@@ -135,109 +131,111 @@
 
 	    Vect_reset_cats(OCats);
 
-	    /* rewrite with all combinations of acat - bcat (-1 in cycle for null) */
-	    /* TODO: put cats of input maps into different layers, i.e.
-	     * preserve cat values, change layer number if needed */
-	    for (i = -1; i < Cats->n_cats; i++) {	/* line cats */
-		int j;
+	    if (ofield[0]  > 0) {
+		/* rewrite with all combinations of acat - bcat (-1 in cycle for null) */
+		for (i = -1; i < Cats->n_cats; i++) {	/* line cats */
+		    int j;
 
-		if (i == -1 && Cats->n_cats > 0)
-		    continue;	/* no need to make null */
-
-		for (j = -1; j < ACats->n_cats; j++) {
-		    if (j == -1 && ACats->n_cats > 0)
+		    if (i == -1 && Cats->n_cats > 0)
 			continue;	/* no need to make null */
 
-		    if (ofield[0] > 0)
-			Vect_cat_set(OCats, ofield[0], ncat);
+		    for (j = -1; j < ACats->n_cats; j++) {
+			if (j == -1 && ACats->n_cats > 0)
+			    continue;	/* no need to make null */
 
-		    /* Attributes */
-		    if (driver) {
-			ATTR *at;
+			if (ofield[0] > 0)
+			    Vect_cat_set(OCats, ofield[0], ncat);
 
-			sprintf(buf, "insert into %s values ( %d", Fi->table,
-				ncat);
-			db_set_string(&stmt, buf);
+			/* Attributes */
+			if (driver) {
+			    ATTR *at;
 
-			/* cata */
-			if (i >= 0) {
-			    if (attr[0].columns) {
-				at = find_attr(&(attr[0]), Cats->cat[i]);
-				if (!at)
-				    G_fatal_error(_("Attribute not found"));
+			    sprintf(buf, "insert into %s values ( %d", Fi->table,
+				    ncat);
+			    db_set_string(&stmt, buf);
 
-				if (at->values)
-				    db_append_string(&stmt, at->values);
-				else
-				    db_append_string(&stmt,
-						     attr[0].null_values);
+			    /* cata */
+			    if (i >= 0) {
+				if (attr[0].columns) {
+				    at = find_attr(&(attr[0]), Cats->cat[i]);
+				    if (!at)
+					G_fatal_error(_("Attribute not found"));
+
+				    if (at->values)
+					db_append_string(&stmt, at->values);
+				    else
+					db_append_string(&stmt,
+							 attr[0].null_values);
+				}
+				else {
+				    sprintf(buf, ", %d", Cats->cat[i]);
+				    db_append_string(&stmt, buf);
+				}
 			    }
 			    else {
-				sprintf(buf, ", %d", Cats->cat[i]);
-				db_append_string(&stmt, buf);
+				if (attr[0].columns) {
+				    db_append_string(&stmt, attr[0].null_values);
+				}
+				else {
+				    sprintf(buf, ", null");
+				    db_append_string(&stmt, buf);
+				}
 			    }
-			}
-			else {
-			    if (attr[0].columns) {
-				db_append_string(&stmt, attr[0].null_values);
-			    }
-			    else {
-				sprintf(buf, ", null");
-				db_append_string(&stmt, buf);
-			    }
-			}
 
-			/* catb */
-			if (j >= 0) {
-			    if (attr[1].columns) {
-				at = find_attr(&(attr[1]), ACats->cat[j]);
-				if (!at)
-				    G_fatal_error(_("Attribute not found"));
+			    /* catb */
+			    if (j >= 0) {
+				if (attr[1].columns) {
+				    at = find_attr(&(attr[1]), ACats->cat[j]);
+				    if (!at)
+					G_fatal_error(_("Attribute not found"));
 
-				if (at->values)
-				    db_append_string(&stmt, at->values);
-				else
-				    db_append_string(&stmt,
-						     attr[1].null_values);
+				    if (at->values)
+					db_append_string(&stmt, at->values);
+				    else
+					db_append_string(&stmt,
+							 attr[1].null_values);
+				}
+				else {
+				    sprintf(buf, ", %d", ACats->cat[j]);
+				    db_append_string(&stmt, buf);
+				}
 			    }
 			    else {
-				sprintf(buf, ", %d", ACats->cat[j]);
-				db_append_string(&stmt, buf);
+				if (attr[1].columns) {
+				    db_append_string(&stmt, attr[1].null_values);
+				}
+				else {
+				    sprintf(buf, ", null");
+				    db_append_string(&stmt, buf);
+				}
 			    }
-			}
-			else {
-			    if (attr[1].columns) {
-				db_append_string(&stmt, attr[1].null_values);
-			    }
-			    else {
-				sprintf(buf, ", null");
-				db_append_string(&stmt, buf);
-			    }
-			}
 
-			db_append_string(&stmt, " )");
+			    db_append_string(&stmt, " )");
 
-			G_debug(3, db_get_string(&stmt));
+			    G_debug(3, db_get_string(&stmt));
 
-			if (db_execute_immediate(driver, &stmt) != DB_OK)
-			    G_warning(_("Unable to insert new record: '%s'"),
-				      db_get_string(&stmt));
-		    }
+			    if (db_execute_immediate(driver, &stmt) != DB_OK)
+				G_warning(_("Unable to insert new record: '%s'"),
+					  db_get_string(&stmt));
+			}
 
-		    ncat++;
+			ncat++;
+		    }
 		}
 	    }
 
-	    /* Add all cats from imput vectors */
-	    if (ofield[1] > 0) {
+	    /* Add cats from input vectors */
+	    if (ofield[1] > 0 && field[0] > 0) {
 		for (i = 0; i < Cats->n_cats; i++) {
-		    Vect_cat_set(OCats, ofield[1], Cats->cat[i]);
+		    if (Cats->field[i] == field[0])
+			Vect_cat_set(OCats, ofield[1], Cats->cat[i]);
 		}
 	    }
 
-	    if (ofield[2] > 0) {
+	    if (ofield[2] > 0 && field[1] > 0 && ofield[1] != ofield[2]) {
 		for (i = 0; i < ACats->n_cats; i++) {
-		    Vect_cat_set(OCats, ofield[2], ACats->cat[i]);
+		    if (Cats->field[i] == field[1])
+			Vect_cat_set(OCats, ofield[2], ACats->cat[i]);
 		}
 	    }
 

Modified: grass/trunk/vector/v.overlay/main.c
===================================================================
--- grass/trunk/vector/v.overlay/main.c	2012-11-18 20:30:36 UTC (rev 53903)
+++ grass/trunk/vector/v.overlay/main.c	2012-11-19 07:43:42 UTC (rev 53904)
@@ -138,15 +138,14 @@
 
     if (operator_opt->answer[0] == 'a')
 	operator = OP_AND;
-
     else if (operator_opt->answer[0] == 'o')
 	operator = OP_OR;
-
     else if (operator_opt->answer[0] == 'n')
 	operator = OP_NOT;
-
     else if (operator_opt->answer[0] == 'x')
 	operator = OP_XOR;
+    else
+	G_fatal_error(_("Unknown operator '%s'"), operator_opt->answer);
 
     /* OP_OR, OP_XOR is not supported for lines,
        mostly because I'am not sure if they make enouhg sense */
@@ -226,6 +225,11 @@
 		if (!(ltype & type[input]))
 		    continue;
 	    }
+	    
+	    /* lines and boundaries must have at least 2 distinct vertices */
+	    Vect_line_prune(Points);
+	    if (Points->n_points < 2)
+		continue;
 
 	    /* TODO: figure out a reasonable threshold */
 	    if (Points->n_points > 100) {
@@ -297,8 +301,6 @@
 
 	G_debug(3, "%d cats read from index", attr[input].n);
 
-	G_verbose_message(_("Collecting input attributes..."));
-
 	attr[input].null_values = NULL;
 	attr[input].columns = NULL;
 
@@ -313,6 +315,8 @@
 	    dbValue *Value;
 	    int sqltype, ctype;
 
+	    G_verbose_message(_("Collecting input attributes..."));
+
 	    inFi = Vect_get_field(&(In[input]), field[input]);
 	    if (!inFi) {
 		G_warning(_("Database connection not defined for layer %d"),
@@ -392,7 +396,7 @@
 	    attr[input].columns = G_store(db_get_string(&col_defs));
 
 	    while (1) {
-		int cat;
+		int cat = -1;
 		ATTR *at;
 
 		if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
@@ -531,6 +535,23 @@
 	db_commit_transaction(driver);
 	db_close_database_shutdown_driver(driver);
     }
+    if (ofield[0] < 1 && !table_flag->answer) {
+	int otype;
+	
+	if (type[0] == GV_AREA)
+	    otype = GV_CENTROID;
+	else
+	    otype = GV_LINE;
+	
+	/* copy attributes from ainput */
+	if (ofield[1] > 0 && field[0] > 0) {
+	    Vect_copy_table(&In[0], &Out, field[0], ofield[1], NULL, otype);
+	}
+	/* copy attributes from binput */
+	if (ofield[2] > 0 && field[1] > 0 && ofield[1] != ofield[2]) {
+	    Vect_copy_table(&In[1], &Out, field[1], ofield[2], NULL, otype);
+	}
+    }
 
     Vect_close(&(In[0]));
     Vect_close(&(In[1]));



More information about the grass-commit mailing list