GRASS Programmer's Manual
6.4.2(2012)
|
00001 00014 #include <grass/vedit.h> 00015 00028 int Vedit_snap_point(struct Map_info *Map, 00029 int line, double *x, double *y, double *z, double thresh, 00030 int vertex) 00031 { 00032 struct line_pnts *Points; 00033 00034 int i, snapped; 00035 int line2snap, mindist_idx; 00036 double dist, mindist; 00037 00038 snapped = 0; 00039 mindist_idx = -1; 00040 mindist = thresh; 00041 00042 Points = Vect_new_line_struct(); 00043 00044 line2snap = Vect_find_line(Map, *x, *y, *z, -1, thresh, WITHOUT_Z, line); 00045 00046 if (line2snap > 0) { 00047 Vect_read_line(Map, Points, NULL, line2snap); 00048 00049 if (!Vect_line_alive(Map, line2snap)) { 00050 Vect_destroy_line_struct(Points); 00051 return snapped; 00052 } 00053 00054 for (i = 0; i < Points->n_points; i++) { 00055 if (i > 0 && i < Points->n_points - 1) 00056 if (!vertex) 00057 continue; 00058 dist = Vect_points_distance(*x, *y, *z, 00059 Points->x[i], Points->y[i], 00060 Points->z[i], WITHOUT_Z); 00061 00062 if (mindist >= dist) { 00063 mindist = dist; 00064 mindist_idx = i; 00065 } 00066 } 00067 00068 if (mindist_idx > -1) { 00069 *x = Points->x[mindist_idx]; 00070 *y = Points->y[mindist_idx]; 00071 *z = Points->z[mindist_idx]; 00072 snapped = 1; 00073 } 00074 } 00075 00076 G_debug(3, "Vedit_snap_point(): map=%s, line2snap=%d, snapped=%d", 00077 Map->name, line2snap, snapped); 00078 00079 Vect_destroy_line_struct(Points); 00080 00081 return snapped; 00082 } 00083 00099 int Vedit_snap_line(struct Map_info *Map, struct Map_info **BgMap, 00100 int nbgmaps, int line, struct line_pnts *Points, 00101 double thresh, int to_vertex) 00102 { 00103 int i, npoints, node, rewrite; 00104 double *x, *y, *z; 00105 00106 struct line_cats *Cats; 00107 00108 Cats = Vect_new_cats_struct(); 00109 00110 G_debug(3, "Vedit_snap_line(): thresh=%g, to_vertex=%d", thresh, 00111 to_vertex); 00112 00113 if (line > 0 && !Vect_line_alive(Map, line)) 00114 return -1; 00115 00116 npoints = Points->n_points; 00117 x = Points->x; 00118 y = Points->y; 00119 z = Points->z; 00120 00121 rewrite = 0; 00122 for (node = 0; node < npoints; node++) { 00123 if ((node > 0 && node < npoints - 1) && !to_vertex) 00124 continue; 00125 00126 if (Vedit_snap_point(Map, line, &x[node], &y[node], &z[node], thresh, 00127 to_vertex)) { 00128 rewrite = 1; 00129 } 00130 else { 00131 /* check also background maps */ 00132 for (i = 0; i < nbgmaps; i++) { 00133 if (Vedit_snap_point 00134 (BgMap[i], -1, &x[node], &y[node], &z[node], thresh, 00135 to_vertex)) { 00136 rewrite = 1; 00137 break; /* snapped, don't continue */ 00138 } 00139 } 00140 } 00141 } /* for each line vertex */ 00142 00143 /* close boundaries or lines */ 00144 if (!rewrite && 00145 Vect_points_distance(x[0], y[0], z[0], 00146 x[npoints - 1], y[npoints - 1], z[npoints - 1], 00147 WITHOUT_Z) <= thresh) { 00148 x[npoints - 1] = x[0]; 00149 y[npoints - 1] = y[0]; 00150 z[npoints - 1] = z[0]; 00151 00152 rewrite = 1; 00153 } 00154 00155 G_debug(3, "Vedit_snap_line(): line=%d, snapped=%d", line, rewrite); 00156 00157 Vect_destroy_cats_struct(Cats); 00158 00159 return rewrite; 00160 } 00161 00175 int Vedit_snap_lines(struct Map_info *Map, struct Map_info **BgMap, 00176 int nbgmaps, struct ilist *List, double thresh, 00177 int to_vertex) 00178 { 00179 int i, line, type; 00180 int nlines_modified = 0; 00181 00182 struct line_pnts *Points; 00183 struct line_cats *Cats; 00184 00185 Points = Vect_new_line_struct(); 00186 Cats = Vect_new_cats_struct(); 00187 00188 for (i = 0; i < List->n_values; i++) { 00189 line = List->value[i]; 00190 type = Vect_read_line(Map, Points, Cats, line); 00191 00192 if (!(type & (GV_POINT | GV_LINES))) { 00193 continue; 00194 } 00195 00196 if (Vedit_snap_line(Map, BgMap, nbgmaps, 00197 line, Points, thresh, to_vertex) == 1) { 00198 if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) { 00199 return -1; 00200 } 00201 00202 nlines_modified++; 00203 } 00204 } 00205 00206 Vect_destroy_line_struct(Points); 00207 Vect_destroy_cats_struct(Cats); 00208 00209 return nlines_modified; 00210 }