[GRASS-SVN] r58830 - grass-addons/grass7/vector/v.centerpoint
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Feb 1 13:20:32 PST 2014
Author: mmetz
Date: 2014-02-01 13:20:32 -0800 (Sat, 01 Feb 2014)
New Revision: 58830
Modified:
grass-addons/grass7/vector/v.centerpoint/areas.c
grass-addons/grass7/vector/v.centerpoint/lines.c
grass-addons/grass7/vector/v.centerpoint/main.c
grass-addons/grass7/vector/v.centerpoint/points.c
grass-addons/grass7/vector/v.centerpoint/v.centerpoint.html
Log:
v.centerpoint: fix areas and category transfer
Modified: grass-addons/grass7/vector/v.centerpoint/areas.c
===================================================================
--- grass-addons/grass7/vector/v.centerpoint/areas.c 2014-02-01 21:17:30 UTC (rev 58829)
+++ grass-addons/grass7/vector/v.centerpoint/areas.c 2014-02-01 21:20:32 UTC (rev 58830)
@@ -11,7 +11,7 @@
{
int area, nareas, isle, nisles, nisles_alloc;
struct line_pnts *Points, **IPoints, *OPoints;
- struct line_cats *Cats;
+ struct line_cats *Cats, *ICats;
struct bound_box box;
double x, y, z, meanx, meany, meanz;
double *xp, *yp;
@@ -21,6 +21,7 @@
Points = Vect_new_line_struct();
OPoints = Vect_new_line_struct();
Cats = Vect_new_cats_struct();
+ ICats = Vect_new_cats_struct();
nisles_alloc = 10;
IPoints = G_malloc(nisles_alloc * sizeof (struct line_pnts *));
@@ -42,10 +43,14 @@
for (area = 1; area <= nareas; area++) {
Vect_reset_cats(Cats);
- if (layer > 1) {
- Vect_get_area_cats(In, area, Cats);
- if (!Vect_cats_in_constraint(Cats, layer, cat_list))
- continue;
+ if (Vect_get_area_cats(In, area, ICats) != 0)
+ continue;
+ if (!Vect_cats_in_constraint(ICats, layer, cat_list))
+ continue;
+
+ for (i = 0; i < ICats->n_cats; i++) {
+ if (ICats->field[i] == layer)
+ Vect_cat_set(Cats, 1, ICats->cat[i]);
}
Vect_get_area_points(In, area, Points);
@@ -59,9 +64,10 @@
}
nisles_alloc = nisles;
}
- for (isle = 1; isle < nisles; isle++) {
- Vect_get_isle_points(In, isle, IPoints[isle - 1]);
- Vect_line_prune(IPoints[isle - 1]);
+ for (isle = 0; isle < nisles; isle++) {
+ Vect_get_isle_points(In, Vect_get_area_isle(In, area, isle),
+ IPoints[isle]);
+ Vect_line_prune(IPoints[isle]);
}
/* surveyor's / shoelace formula */
@@ -104,6 +110,7 @@
if (Out) {
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, z);
+ Vect_cat_set(Cats, 2, 7);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
@@ -136,10 +143,14 @@
for (area = 1; area <= nareas; area++) {
Vect_reset_cats(Cats);
- if (layer > 1) {
- Vect_get_area_cats(In, area, Cats);
- if (!Vect_cats_in_constraint(Cats, layer, cat_list))
- continue;
+ if (Vect_get_area_cats(In, area, ICats) != 0)
+ continue;
+ if (!Vect_cats_in_constraint(ICats, layer, cat_list))
+ continue;
+
+ for (i = 0; i < ICats->n_cats; i++) {
+ if (ICats->field[i] == layer)
+ Vect_cat_set(Cats, 1, ICats->cat[i]);
}
Vect_get_area_points(In, area, Points);
@@ -154,10 +165,11 @@
}
nisles_alloc = nisles;
}
- for (isle = 1; isle < nisles; isle++) {
- Vect_get_isle_points(In, isle, IPoints[isle - 1]);
- Vect_line_prune(IPoints[isle - 1]);
- nwc += IPoints[isle - 1]->n_points - 1;
+ for (isle = 0; isle < nisles; isle++) {
+ Vect_get_isle_points(In, Vect_get_area_isle(In, area, isle),
+ IPoints[isle]);
+ Vect_line_prune(IPoints[isle]);
+ nwc += IPoints[isle]->n_points - 1;
}
if (nwc_alloc < nwc) {
@@ -378,6 +390,7 @@
if (Out) {
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, 0);
+ Vect_cat_set(Cats, 2, 8);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
@@ -410,10 +423,14 @@
for (area = 1; area <= nareas; area++) {
Vect_reset_cats(Cats);
- if (layer > 1) {
- Vect_get_area_cats(In, area, Cats);
- if (!Vect_cats_in_constraint(Cats, layer, cat_list))
- continue;
+ if (Vect_get_area_cats(In, area, ICats) != 0)
+ continue;
+ if (!Vect_cats_in_constraint(ICats, layer, cat_list))
+ continue;
+
+ for (i = 0; i < ICats->n_cats; i++) {
+ if (ICats->field[i] == layer)
+ Vect_cat_set(Cats, 1, ICats->cat[i]);
}
Vect_get_area_points(In, area, Points);
@@ -428,10 +445,11 @@
}
nisles_alloc = nisles;
}
- for (isle = 1; isle < nisles; isle++) {
- Vect_get_isle_points(In, isle, IPoints[isle - 1]);
- Vect_line_prune(IPoints[isle - 1]);
- nw += IPoints[isle - 1]->n_points - 1;
+ for (isle = 0; isle < nisles; isle++) {
+ Vect_get_isle_points(In, Vect_get_area_isle(In, area, isle),
+ IPoints[isle]);
+ Vect_line_prune(IPoints[isle]);
+ nw += IPoints[isle]->n_points - 1;
}
if (nw_alloc < nw) {
@@ -547,6 +565,7 @@
if (Out) {
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, 0);
+ Vect_cat_set(Cats, 2, 9);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
Modified: grass-addons/grass7/vector/v.centerpoint/lines.c
===================================================================
--- grass-addons/grass7/vector/v.centerpoint/lines.c 2014-02-01 21:17:30 UTC (rev 58829)
+++ grass-addons/grass7/vector/v.centerpoint/lines.c 2014-02-01 21:20:32 UTC (rev 58830)
@@ -10,14 +10,15 @@
struct cat_list *cat_list, int n_primitives, int mode)
{
struct line_pnts *Points, *OPoints;
- struct line_cats *Cats;
+ struct line_cats *Cats, *ICats;
double x, y, z;
int type;
- int counter;
+ int i, counter;
Points = Vect_new_line_struct();
OPoints = Vect_new_line_struct();
Cats = Vect_new_cats_struct();
+ ICats = Vect_new_cats_struct();
/* mid point */
if (mode & L_MID) {
@@ -33,10 +34,14 @@
if (!(type & GV_LINE))
continue;
- if (layer > 0 &&
- !Vect_cats_in_constraint(Cats, layer, cat_list))
+ if (!Vect_cats_in_constraint(ICats, layer, cat_list))
continue;
+ for (i = 0; i < ICats->n_cats; i++) {
+ if (ICats->field[i] == layer)
+ Vect_cat_set(Cats, 1, ICats->cat[i]);
+ }
+
Vect_line_prune(Points);
len = Vect_line_length(Points) / 2.;
@@ -46,6 +51,7 @@
if (Out) {
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, z);
+ Vect_cat_set(Cats, 2, 4);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
@@ -58,7 +64,6 @@
if (mode & L_MEAN) {
double len, slen;
double dx, dy, dz;
- int i;
G_message(_("Calculating centers of gravity for lines..."));
counter = 0;
@@ -69,10 +74,14 @@
counter++;
if (!(type & GV_LINE))
continue;
- if (layer > 0 &&
- !Vect_cats_in_constraint(Cats, layer, cat_list))
+ if (!Vect_cats_in_constraint(ICats, layer, cat_list))
continue;
+ for (i = 0; i < ICats->n_cats; i++) {
+ if (ICats->field[i] == layer)
+ Vect_cat_set(Cats, 1, ICats->cat[i]);
+ }
+
Vect_line_prune(Points);
len = 0.;
@@ -100,6 +109,7 @@
if (Out) {
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, z);
+ Vect_cat_set(Cats, 2, 5);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
@@ -117,7 +127,7 @@
double xmean, ymean, zmean;
double medx, medy, medz;
double dist, distsum, dist2all, lastdist2all;
- int i, j, iter, maxiter = 100;
+ int j, iter, maxiter = 100;
SPoints = Vect_new_line_struct();
@@ -130,10 +140,14 @@
counter++;
if (!(type & GV_LINE))
continue;
- if (layer > 0 &&
- !Vect_cats_in_constraint(Cats, layer, cat_list))
+ if (!Vect_cats_in_constraint(ICats, layer, cat_list))
continue;
+ for (i = 0; i < ICats->n_cats; i++) {
+ if (ICats->field[i] == layer)
+ Vect_cat_set(Cats, 1, ICats->cat[i]);
+ }
+
Vect_line_prune(Points);
len = G_malloc((Points->n_points - 1) * sizeof(double));
@@ -235,6 +249,7 @@
if (Out) {
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, z);
+ Vect_cat_set(Cats, 2, 6);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
Modified: grass-addons/grass7/vector/v.centerpoint/main.c
===================================================================
--- grass-addons/grass7/vector/v.centerpoint/main.c 2014-02-01 21:17:30 UTC (rev 58829)
+++ grass-addons/grass7/vector/v.centerpoint/main.c 2014-02-01 21:20:32 UTC (rev 58830)
@@ -62,7 +62,7 @@
type_opt->answer = "point,line,area";
type_opt->guisection = _("Selection");
- lyr_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
+ lyr_opt = G_define_standard_option(G_OPT_V_FIELD);
lyr_opt->guisection = _("Selection");
cats_opt = G_define_standard_option(G_OPT_V_CATS);
@@ -174,14 +174,14 @@
if (1 > Vect_open_old(&In, old->answer, mapset))
G_fatal_error(_("Unable to open vector map <%s>"), old->answer);
- layer = -1;
- if (lyr_opt->answer)
- layer = Vect_get_field_number(&In, lyr_opt->answer);
+ layer = Vect_get_field_number(&In, lyr_opt->answer);
- if (layer > 0)
- cat_list = Vect_cats_set_constraint(&In, layer, where_opt->answer,
- cats_opt->answer);
+ if (layer < 1)
+ G_fatal_error(_("Invalid %s answer: %s"), lyr_opt->key, lyr_opt->answer);
+ cat_list = Vect_cats_set_constraint(&In, layer, where_opt->answer,
+ cats_opt->answer);
+
out3d = Vect_is_3d(&In);
Outp = NULL;
@@ -228,7 +228,7 @@
}
if (nlines) {
lines_center(&In, Outp, layer, cat_list, nprimitives, mode);
- Vect_copy_tables(&In, &Out, layer);
+ Vect_copy_table(&In, &Out, layer, 1, NULL, GV_1TABLE);
}
Vect_close(&In);
@@ -240,10 +240,12 @@
if (1 > Vect_open_old(&In, old->answer, mapset))
G_warning(_("Unable to open vector map <%s> with topology"), old->answer);
else {
- int nareas = Vect_get_num_areas(&In);
-
- if (nareas)
+ if (Vect_get_num_areas(&In) > 0) {
areas_center(&In, Outp, layer, cat_list, mode);
+ Vect_copy_table(&In, &Out, layer, 1, NULL, GV_1TABLE);
+ }
+ else
+ G_warning(_("No areas in input vector <%s>"), old->answer);
Vect_close(&In);
}
Modified: grass-addons/grass7/vector/v.centerpoint/points.c
===================================================================
--- grass-addons/grass7/vector/v.centerpoint/points.c 2014-02-01 21:17:30 UTC (rev 58829)
+++ grass-addons/grass7/vector/v.centerpoint/points.c 2014-02-01 21:20:32 UTC (rev 58830)
@@ -48,7 +48,8 @@
if (!(type & GV_POINT))
continue;
- if (layer > 0 && !Vect_cats_in_constraint(Cats, layer, cat_list))
+ if (layer > 0 &&
+ !Vect_cats_in_constraint(Cats, layer, cat_list))
continue;
xsum += Points->x[0];
@@ -71,7 +72,7 @@
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, z);
Vect_reset_cats(OCats);
- Vect_cat_set(OCats, 1, 1);
+ Vect_cat_set(OCats, 2, 1);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
@@ -161,7 +162,7 @@
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, z);
Vect_reset_cats(OCats);
- Vect_cat_set(OCats, 1, 2);
+ Vect_cat_set(OCats, 2, 2);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
@@ -216,7 +217,7 @@
Vect_reset_line(OPoints);
Vect_append_point(OPoints, x, y, z);
Vect_reset_cats(OCats);
- Vect_cat_set(OCats, 1, 3);
+ Vect_cat_set(OCats, 2, 3);
Vect_write_line(Out, GV_POINT, OPoints, Cats);
}
else
Modified: grass-addons/grass7/vector/v.centerpoint/v.centerpoint.html
===================================================================
--- grass-addons/grass7/vector/v.centerpoint/v.centerpoint.html 2014-02-01 21:17:30 UTC (rev 58829)
+++ grass-addons/grass7/vector/v.centerpoint/v.centerpoint.html 2014-02-01 21:20:32 UTC (rev 58830)
@@ -25,8 +25,9 @@
<li><b>8</b> - median of each area using area triangulation</li>
<li><b>9</b> - median of each area using boundaries</li>
</ul>
-If an output vector is given, center points inherit the categories of
-the respective lines and areas. For points, the category is set to 1
+If an output vector is given, categories of the respective lines and
+areas are transferred from the selected layer to layer 1. Layer 2 holds
+the same category values as for output to stdout.
<h4>Point centers</h4>
<ul>
More information about the grass-commit
mailing list