[GRASS-SVN] r54301 - grass/trunk/vector/v.to.rast

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Dec 15 15:17:13 PST 2012


Author: mmetz
Date: 2012-12-15 15:17:13 -0800 (Sat, 15 Dec 2012)
New Revision: 54301

Modified:
   grass/trunk/vector/v.to.rast/do_areas.c
   grass/trunk/vector/v.to.rast/do_lines.c
   grass/trunk/vector/v.to.rast/local.h
   grass/trunk/vector/v.to.rast/main.c
   grass/trunk/vector/v.to.rast/vect2rast.c
Log:
v.to.rast: add cats and where option (fix #1798)

Modified: grass/trunk/vector/v.to.rast/do_areas.c
===================================================================
--- grass/trunk/vector/v.to.rast/do_areas.c	2012-12-15 23:15:02 UTC (rev 54300)
+++ grass/trunk/vector/v.to.rast/do_areas.c	2012-12-15 23:17:13 UTC (rev 54301)
@@ -88,9 +88,10 @@
 }
 
 
-int sort_areas(struct Map_info *Map, struct line_pnts *Points, int field)
+int sort_areas(struct Map_info *Map, struct line_pnts *Points,
+               int field, struct cat_list *cat_list)
 {
-    int i, centroid;
+    int i, centroid, nareas_selected;
     struct line_cats *Cats;
     CELL cat;
 
@@ -107,33 +108,45 @@
 	(struct list *)G_calloc(nareas * sizeof(char), sizeof(struct list));
 
     /* store area size,cat,index in list */
+    nareas_selected = 0;
     for (i = 0; i < nareas; i++) {
-	list[i].index = i + 1;
-	Vect_get_area_points(Map, i + 1, Points);
-	list[i].size =
-	    G_area_of_polygon(Points->x, Points->y, Points->n_points);
 
 	centroid = Vect_get_area_centroid(Map, i + 1);
+	SETNULL(&cat);
 	if (centroid <= 0) {
-	    SETNULL(&cat);
 	    G_debug(2,_("Area without centroid (OK for island)"));
 	}
 	else {
 	    Vect_read_line(Map, NULL, Cats, centroid);
-	    Vect_cat_get(Cats, field, &cat);
-	    if (cat < 0) {
-		SETNULL(&cat);
-		G_warning(_("Area centroid without category"));
+	    if (field > 0) {
+		if (Vect_cats_in_constraint(Cats, field, cat_list)) {
+		    Vect_cat_get(Cats, field, &cat);
+		    nareas_selected++;
+		}
+		else {
+		    G_debug(2, _("Area centroid without category"));
+		}
 	    }
+	    else {
+		/* field < 1, process all areas with centroid */
+		cat = 0;
+		nareas_selected++;
+	    }
 	}
 
+	list[i].index = i + 1;
+	Vect_get_area_points(Map, i + 1, Points);
+	list[i].size =
+	    G_area_of_polygon(Points->x, Points->y, Points->n_points);
+
 	list[i].cat = cat;
     }
+    if (nareas_selected > 0) {
+	/* sort the list by size */
+	qsort(list, nareas * sizeof(char), sizeof(struct list), compare);
+    }
 
-    /* sort the list by size */
-    qsort(list, nareas * sizeof(char), sizeof(struct list), compare);
-
-    return nareas;
+    return nareas_selected;
 }
 
 

Modified: grass/trunk/vector/v.to.rast/do_lines.c
===================================================================
--- grass/trunk/vector/v.to.rast/do_lines.c	2012-12-15 23:15:02 UTC (rev 54300)
+++ grass/trunk/vector/v.to.rast/do_lines.c	2012-12-15 23:17:13 UTC (rev 54301)
@@ -14,8 +14,9 @@
 
 
 int do_lines(struct Map_info *Map, struct line_pnts *Points,
-	     dbCatValArray * Cvarr, int ctype, int field, int use,
-	     double value, int value_type, int feature_type, int *count_all)
+	     dbCatValArray * Cvarr, int ctype, int field,
+	     struct cat_list *cat_list, int use, double value,
+	     int value_type, int feature_type, int *count_all)
 {
     double min = 0, max, u;
     int nlines, type, cat, no_contour = 0;
@@ -36,7 +37,14 @@
     for (index = 1; index <= nlines; index++) {
 	G_percent(index, nlines, 2);
 	type = Vect_read_line(Map, Points, Cats, index);
-	Vect_cat_get(Cats, field, &cat);
+	cat = -1;
+	if (field > 0) {
+	    if (Vect_cats_in_constraint(Cats, field, cat_list)) {
+		Vect_cat_get(Cats, field, &cat);
+	    }
+	}
+	else
+	    cat = 0; /* categories do not matter */
 
 	if ((type & GV_POINT) || (type & GV_LINE))
 	    (*count_all)++;

Modified: grass/trunk/vector/v.to.rast/local.h
===================================================================
--- grass/trunk/vector/v.to.rast/local.h	2012-12-15 23:15:02 UTC (rev 54300)
+++ grass/trunk/vector/v.to.rast/local.h	2012-12-15 23:17:13 UTC (rev 54301)
@@ -29,11 +29,11 @@
 /* do_areas.c */
 int do_areas(struct Map_info *, struct line_pnts *, dbCatValArray *, int,
 	     int, double, int);
-int sort_areas(struct Map_info *, struct line_pnts *, int);
+int sort_areas(struct Map_info *, struct line_pnts *, int, struct cat_list *);
 
 /* do_lines.c */
 int do_lines(struct Map_info *, struct line_pnts *, dbCatValArray *, int, int,
-	     int, double, int, int, int *);
+	     struct cat_list *, int, double, int, int, int *);
 
 /* raster.c */
 int begin_rasterization(int, int);
@@ -51,6 +51,6 @@
 
 /* vect2rast.c */
 int vect_to_rast(const char *, const char *, const char *, const char *, int, int,
-		 double, int, const char *, const char *, int);
+		 double, int, const char *, const char *, int, char *, char *);
 
 #endif

Modified: grass/trunk/vector/v.to.rast/main.c
===================================================================
--- grass/trunk/vector/v.to.rast/main.c	2012-12-15 23:15:02 UTC (rev 54300)
+++ grass/trunk/vector/v.to.rast/main.c	2012-12-15 23:17:13 UTC (rev 54301)
@@ -29,8 +29,9 @@
 int main(int argc, char *argv[])
 {
     struct GModule *module;
-    struct Option *input, *output, *rows, *col, *field_opt, *use_opt,
-	*val_opt, *rgbcol_opt, *label_opt, *type_opt;
+    struct Option *input, *output, *rows, *col, *use_opt, *val_opt,
+		  *field_opt, *type_opt, *where_opt, *cats_opt,
+	          *rgbcol_opt, *label_opt;
     int nrows, use, value_type, type;
     double value;
     char *desc;
@@ -53,6 +54,12 @@
     type_opt->answer = "point,line,area";
     type_opt->guisection = _("Selection");
     
+    cats_opt = G_define_standard_option(G_OPT_V_CATS);
+    cats_opt->guisection = _("Selection");
+    
+    where_opt = G_define_standard_option(G_OPT_DB_WHERE);
+    where_opt->guisection = _("Selection");
+
     output = G_define_standard_option(G_OPT_R_OUTPUT);
     
     use_opt = G_define_option();
@@ -146,7 +153,8 @@
 
     if (vect_to_rast(input->answer, output->answer, field_opt->answer,
 		     col->answer, nrows, use, value, value_type,
-		     rgbcol_opt->answer, label_opt->answer, type)) {
+		     rgbcol_opt->answer, label_opt->answer, type,
+		     where_opt->answer, cats_opt->answer)) {
 	exit(EXIT_FAILURE);
     }
 

Modified: grass/trunk/vector/v.to.rast/vect2rast.c
===================================================================
--- grass/trunk/vector/v.to.rast/vect2rast.c	2012-12-15 23:15:02 UTC (rev 54300)
+++ grass/trunk/vector/v.to.rast/vect2rast.c	2012-12-15 23:17:13 UTC (rev 54301)
@@ -10,11 +10,12 @@
 int vect_to_rast(const char *vector_map, const char *raster_map, const char *field_name,
 		 const char *column, int nrows, int use, double value,
 		 int value_type, const char *rgbcolumn, const char *labelcolumn,
-		 int ftype)
+		 int ftype, char *where, char *cats)
 {
     struct Map_info Map;
     struct line_pnts *Points;
     int i, field;
+    struct cat_list *cat_list = NULL;
     int fd;			/* for raster map */
     int nareas, nlines;		/* number of converted features */
     int nareas_all, nplines_all;	/* number of all areas, points/lines */
@@ -37,6 +38,10 @@
     Vect_open_old2(&Map, vector_map, "", field_name);
     field = Vect_get_field_number(&Map, field_name);
 
+    if (field > 0)
+	cat_list = Vect_cats_set_constraint(&Map, field, where, cats);
+
+
     if ((use == USE_Z) && !(Vect_is_3d(&Map)))
 	G_fatal_error(_("Vector map <%s> is not 3D"),
 		      Vect_get_full_name(&Map));
@@ -129,8 +134,8 @@
     Points = Vect_new_line_struct();
 
     if (use != USE_Z && use != USE_D && (ftype & GV_AREA)) {
-	if ((nareas = sort_areas(&Map, Points, field)) < 0)
-	    G_fatal_error(_("Unable to process areas from vector map <%s>"),
+	if ((nareas = sort_areas(&Map, Points, field, cat_list)) == 0)
+	    G_warning(_("No areas selected from vector map <%s>"),
 			  vector_map);
 
 	G_debug(1, "%d areas sorted", nareas);
@@ -163,8 +168,9 @@
 
 	if (nlines) {
 	    if ((nlines =
-		 do_lines(&Map, Points, &cvarr, ctype, field, use, value,
-			  value_type, ftype, &nplines_all)) < 0) {
+		 do_lines(&Map, Points, &cvarr, ctype, field, cat_list, 
+		          use, value, value_type, ftype,
+			  &nplines_all)) < 0) {
 		G_warning(_("Problem processing lines from vector map <%s>, continuing..."),
 			  vector_map);
 		stat = -1;



More information about the grass-commit mailing list