Skip to content

Commit

Permalink
better integrate into existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
metzm committed Nov 22, 2024
1 parent 8c86bba commit a6fd68d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 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
49 changes: 5 additions & 44 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 Down Expand Up @@ -131,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 Expand Up @@ -358,48 +361,6 @@ int main(int argc, char *argv[])
db_close_database_shutdown_driver(driver);
}

if (centroid_flg->answer && !no_topol->answer && feature == GV_AREA) {
int line, nlines;
int area, ret;
struct line_pnts *Points;
double x, y;

Vect_build_partial(&Map, GV_BUILD_ATTACH_ISLES);
nlines = Vect_get_num_lines(&Map);

Points = Vect_new_line_struct();

for (line = 1; line <= nlines; line++) {
if (!Vect_line_alive(&Map, line))
continue;
if (Vect_get_line_type(&Map, line) != GV_CENTROID)
continue;
if (Vect_read_line(&Map, Points, Cats, line) < 1) {
G_fatal_error(
_("Unable to read feature %d from vector map <%s>"), line,
Vect_get_full_name(&Map));
}
area = Vect_find_area(&Map, Points->x[0], Points->y[0]);
if (area == 0) {
G_warning(_("No area for line %d"), line);
}
else {
ret = Vect_get_point_in_area(&Map, area, &x, &y);
if (ret < 0) {
G_warning(_("Unable to calculate area centroid"));
}
else {
Points->x[0] = x;
Points->y[0] = y;
if (Vect_rewrite_line(&Map, line, GV_CENTROID, Points,
Cats) < 1) {
G_fatal_error(_("Unable to rewrite centroid!"));
}
}
}
}
}

if (!no_topol->answer)
Vect_build(&Map);

Expand Down

0 comments on commit a6fd68d

Please sign in to comment.