Skip to content

Commit

Permalink
r.to.vect: new flag to re-center centroids (#4690)
Browse files Browse the repository at this point in the history
* r.to.vect: new flag to re-center centroids
  • Loading branch information
metzm authored Nov 22, 2024
1 parent 9f09469 commit c2fb118
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
19 changes: 19 additions & 0 deletions raster/r.to.vect/areas_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ int write_area(

catNum = 1;

if (centroid_flag) {
Vect_build_partial(&Map, GV_BUILD_ATTACH_ISLES);
}

G_important_message(_("Writing areas..."));
for (i = 0, p = a_list; i < n_areas; i++, p++) {
G_percent(i, n_areas, 3);
Expand Down Expand Up @@ -327,6 +331,21 @@ int write_area(
break;
}

if (centroid_flag) {
int area, ret;

area = Vect_find_area(&Map, x, y);
if (area == 0) {
G_warning(_("No area for centroid %d"), i);
}
else {
ret = Vect_get_point_in_area(&Map, area, &x, &y);
if (ret < 0) {
G_warning(_("Unable to calculate area centroid"));
}
}
}

Vect_reset_line(points);
Vect_append_point(points, x, y, 0.0);

Expand Down
3 changes: 2 additions & 1 deletion raster/r.to.vect/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ extern int n_alloced_ptrs;

extern int
smooth_flag; /* this is 0 for no smoothing, 1 for smoothing of lines */
extern int value_flag; /* use raster values as categories */
extern int value_flag; /* use raster values as categories */
extern int centroid_flag; /* re-center centroids */

extern struct Categories RastCats;
extern int has_cats; /* Category labels available */
Expand Down
15 changes: 13 additions & 2 deletions raster/r.to.vect/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ int row_length, row_count, n_rows;
int total_areas;
int n_alloced_ptrs;

int smooth_flag; /* this is 0 for no smoothing, 1 for smoothing of lines */
int value_flag; /* use raster values as categories */
int smooth_flag; /* this is 0 for no smoothing, 1 for smoothing of lines */
int value_flag; /* use raster values as categories */
int centroid_flag; /* re-center centroids */

struct Categories RastCats;
int has_cats; /* Category labels available */
struct field_info *Fi;
Expand All @@ -66,6 +68,7 @@ int main(int argc, char *argv[])
struct GModule *module;
struct Option *in_opt, *out_opt, *feature_opt, *column_name;
struct Flag *smooth_flg, *value_flg, *z_flg, *no_topol, *notab_flg;
struct Flag *centroid_flg;
int feature, notab_flag;

G_gisinit(argv[0]);
Expand Down Expand Up @@ -115,6 +118,13 @@ int main(int argc, char *argv[])
no_topol->label = _("Do not build vector topology");
no_topol->description = _("Recommended for massive point conversion");

centroid_flg = G_define_flag();
centroid_flg->key = 'c';
centroid_flg->label =
_("Move centroids to more central locations within areas");
centroid_flg->description = _("Default: centroids are located anywhere in "
"areas, often close to boundaries");

notab_flg = G_define_standard_flag(G_FLG_V_TABLE);

if (G_parser(argc, argv))
Expand All @@ -123,6 +133,7 @@ int main(int argc, char *argv[])
feature = Vect_option_to_types(feature_opt);
smooth_flag = (smooth_flg->answer) ? SMOOTH : NO_SMOOTH;
value_flag = value_flg->answer;
centroid_flag = centroid_flg->answer;
notab_flag = notab_flg->answer;

if (z_flg->answer && (feature != GV_POINT))
Expand Down
5 changes: 5 additions & 0 deletions raster/r.to.vect/r.to.vect.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ <h3>Area conversion</h3>
input file. If the raster map contains other data (i.e., line edges,
or point data) the output may be wrong.

<p>
By default, area centroids are often located close to boundaries and not
in the middle of an area. Centroids can be more centrally located with
the <em>-c</em> flag.

<h2>EXAMPLES</h2>

The examples are based on the North Carolina sample dataset:
Expand Down

0 comments on commit c2fb118

Please sign in to comment.