/** \brief Select features by coordinates \param[in] Map vector map \param[in] type feature type \param[in] coords coordinates GRASS parameters \param[in] thresh threshold value for searching \param[in,out] List list of selected features \return number of selected lines */ int sel_by_coordinates(struct Map_info *Map, int type, struct line_pnts *coords, double thresh, struct ilist *List) { int i; double east, north, maxdist; struct ilist *List_tmp, *List_in_box; struct line_pnts *box; if (first_selection) { List_tmp = List; first_selection = 0; } else { List_tmp = Vect_new_list(); } box = Vect_new_line_struct(); List_in_box = Vect_new_list(); if (thresh < 0) maxdist = max_distance(thresh); else maxdist = thresh; for (i = 0; i < coords->n_points; i++) { east = coords->x[i]; north = coords->y[i]; coord2bbox(east, north, maxdist, box); Vect_select_lines_by_polygon(Map, box, 0, NULL, type, List_in_box); if (List_in_box->n_values > 0) Vect_list_append_list(List_tmp, List_in_box); } G_debug(1, " %d lines selected (by coordinates)", List_tmp->n_values); /* merge lists (only duplicate items) */ if (List_tmp != List) { merge_lists(List, List_tmp); Vect_destroy_list(List_tmp); } Vect_destroy_line_struct(box); Vect_destroy_list(List_in_box); return List->n_values; }
/** \brief Reverse list selection \param[in] Map vector map \param[in] type feature type \param[in,out] reversed list \return 1 */ int reverse_selection(struct Map_info *Map, int type, struct ilist **List) { struct ilist *list_reverse; int line, nlines, ltype; list_reverse = Vect_new_list(); nlines = Vect_get_num_lines(Map); for (line = 1; line <= nlines; line++) { ltype = Vect_read_line(Map, NULL, NULL, line); if (!(ltype & type)) continue; if (!Vect_val_in_list(*List, line)) Vect_list_append(list_reverse, line); } Vect_destroy_list(*List); *List = list_reverse; return 1; }
/** \brief Select features by polygon \param[in] Map vector map \param[in] type feature type \param[in] poly polygon coordinates \param[in,out] List list of selected features \return number of selected lines */ int sel_by_polygon(struct Map_info *Map, int type, struct line_pnts *Polygon, struct ilist *List) { struct ilist *List_tmp; if (first_selection) { List_tmp = List; first_selection = 0; } else { List_tmp = Vect_new_list(); } /* no isles */ Vect_select_lines_by_polygon(Map, Polygon, 0, NULL, type, List_tmp); G_debug(1, " %d lines selected (by polygon)", List_tmp->n_values); /* merge lists (only duplicate items) */ if (List_tmp != List) { merge_lists(List, List_tmp); Vect_destroy_list(List_tmp); } return List->n_values; }
/** \brief Select features by category \param[in] Map vector map \param[in] cl_orig original list of categories (previously selected) \param[in] layer layer number \param[in] type feature type \param[in] cat category string \param[in,out] List list of selected features \return number of selected lines */ int sel_by_cat(struct Map_info *Map, struct cat_list *cl_orig, int layer, int type, char *cats, struct ilist *List) { struct ilist *List_tmp, *List_tmp1; struct cat_list *cl; int i, cat; if (first_selection || cl_orig) { List_tmp = List; first_selection = 0; } else { List_tmp = Vect_new_list(); } List_tmp1 = Vect_new_list(); if (cl_orig == NULL) { cl = Vect_new_cat_list(); Vect_str_to_cat_list(cats, cl); } else { cl = cl_orig; } for (i = 0; i < cl->n_ranges; i++) { for (cat = cl->min[i]; cat <= cl->max[i]; cat++) { Vect_cidx_find_all(Map, layer, type, cat, List_tmp1); Vect_list_append_list(List_tmp, List_tmp1); } } G_debug(1, " %d lines selected (by category)", List_tmp->n_values); /* merge lists (only duplicate items) */ if (List_tmp != List) { merge_lists(List, List_tmp); Vect_destroy_list(List_tmp); } Vect_destroy_list(List_tmp1); return List->n_values; }
bool QgsGrassFeatureIterator::close() { if ( mClosed ) return false; // finalization Vect_destroy_line_struct( mPoints ); Vect_destroy_cats_struct( mCats ); Vect_destroy_list( mList ); free( mSelection ); // tell provider that this iterator is not active anymore P->mActiveIterator = 0; mClosed = true; return true; }
/** \brief Select features by id \param[in] Map vector map \param[in] type feature type \param[in] ids ids list \param[in,out] List list of selected features \return number of selected lines */ int sel_by_id(struct Map_info *Map, int type, char *ids, struct ilist *List) { int i; int num, id; struct cat_list *il; /* NOTE: this is not cat list, but list of id's */ struct ilist *List_tmp; if (first_selection) { List_tmp = List; first_selection = 0; } else { List_tmp = Vect_new_list(); } il = Vect_new_cat_list(); Vect_str_to_cat_list(ids, il); num = Vect_get_num_lines(Map); for (i = 0; i < il->n_ranges; i++) { for (id = 1; id <= num; id++) { if (!(Vect_read_line(Map, NULL, NULL, id) & type)) { continue; } if (id >= il->min[i] && id <= il->max[i]) { Vect_list_append(List_tmp, id); } } } G_debug(1, " %d lines selected (by id)", List_tmp->n_values); /* merge lists (only duplicate items) */ if (List_tmp != List) { merge_lists(List, List_tmp); Vect_destroy_list(List_tmp); } Vect_destroy_cat_list(il); return List->n_values; }
/** \brief merge two list, i.e. store only duplicate items \param[in] alist,blist list to be merged \return result number of items */ static int merge_lists(struct ilist *alist, struct ilist *blist) { int i; struct ilist *list_del; list_del = Vect_new_list(); for (i = 0; i < alist->n_values; i++) { if (!Vect_val_in_list(blist, alist->value[i])) Vect_list_append(list_del, alist->value[i]); } Vect_list_delete_list(alist, list_del); Vect_destroy_list(list_del); return alist->n_values; }
bool QgsGrassFeatureIterator::close() { if ( mClosed ) return false; iteratorClosed(); // finalization Vect_destroy_line_struct( mPoints ); Vect_destroy_cats_struct( mCats ); Vect_destroy_list( mList ); free( mSelection ); sMutex.unlock(); mClosed = true; return true; }
/** \brief Select features by bbox \param[in] Map vector map \param[in] type feature type \param[in] bbox_opt bounding boxes \param[in,out] List list of selected features \return number of selected lines */ int sel_by_bbox(struct Map_info *Map, int type, double x1, double y1, double x2, double y2, struct ilist *List) { BOUND_BOX bbox; struct ilist *List_tmp; if (first_selection) { List_tmp = List; first_selection = 0; } else { List_tmp = Vect_new_list(); } /* bounding box */ bbox.N = y1 < y2 ? y2 : y1; bbox.S = y1 < y2 ? y1 : y2; bbox.W = x1 < x2 ? x1 : x2; bbox.E = x1 < x2 ? x2 : x1; bbox.T = PORT_DOUBLE_MAX; bbox.B = -PORT_DOUBLE_MAX; Vect_select_lines_by_box(Map, &bbox, type, List_tmp); G_debug(1, " %d lines selected (by bbox)", List_tmp->n_values); /* merge lists (only duplicate items) */ if (List_tmp != List) { merge_lists(List, List_tmp); Vect_destroy_list(List_tmp); } return List->n_values; }
int main(int argc, char *argv[]) { struct Map_info In, Out; static struct line_pnts *Points; struct line_cats *Cats; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *map_in, *map_out; struct Option *afield_opt, *nfield_opt, *abcol, *afcol, *ncol, *method_opt; int with_z; int afield, nfield, mask_type; dglGraph_s *graph; int i, bridges, articulations; struct ilist *bridge_list, *articulation_list; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("network")); G_add_keyword(_("articulation points")); module->description = _("Computes bridges and articulation points in the network."); /* Define the different options as defined in gis.h */ map_in = G_define_standard_option(G_OPT_V_INPUT); map_out = G_define_standard_option(G_OPT_V_OUTPUT); afield_opt = G_define_standard_option(G_OPT_V_FIELD); afield_opt->key = "alayer"; afield_opt->answer = "1"; afield_opt->description = _("Arc layer"); afield_opt->guisection = _("Cost"); nfield_opt = G_define_standard_option(G_OPT_V_FIELD); nfield_opt->key = "nlayer"; nfield_opt->answer = "2"; nfield_opt->description = _("Node layer"); nfield_opt->guisection = _("Cost"); afcol = G_define_standard_option(G_OPT_DB_COLUMN); afcol->key = "afcolumn"; afcol->required = NO; afcol->description = _("Arc forward/both direction(s) cost column (number)"); afcol->guisection = _("Cost"); abcol = G_define_standard_option(G_OPT_DB_COLUMN); abcol->key = "abcolumn"; abcol->required = NO; abcol->description = _("Arc backward direction cost column (number)"); abcol->guisection = _("Cost"); ncol = G_define_option(); ncol->key = "ncolumn"; ncol->type = TYPE_STRING; ncol->required = NO; ncol->description = _("Node cost column (number)"); ncol->guisection = _("Cost"); method_opt = G_define_option(); method_opt->key = "method"; method_opt->type = TYPE_STRING; method_opt->required = YES; method_opt->multiple = NO; method_opt->options = "bridge,articulation"; method_opt->descriptions = _("bridge;Finds bridges;" "articulation;Finds articulation points;"); method_opt->description = _("Feature type"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* TODO: make an option for this */ mask_type = GV_LINE | GV_BOUNDARY; Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); Vect_check_input_output_name(map_in->answer, map_out->answer, GV_FATAL_EXIT); Vect_set_open_level(2); if (1 > Vect_open_old(&In, map_in->answer, "")) G_fatal_error(_("Unable to open vector map <%s>"), map_in->answer); with_z = Vect_is_3d(&In); if (0 > Vect_open_new(&Out, map_out->answer, with_z)) { Vect_close(&In); G_fatal_error(_("Unable to create vector map <%s>"), map_out->answer); } /* parse filter option and select appropriate lines */ afield = Vect_get_field_number(&In, afield_opt->answer); nfield = Vect_get_field_number(&In, nfield_opt->answer); Vect_net_build_graph(&In, mask_type, afield, nfield, afcol->answer, abcol->answer, ncol->answer, 0, 0); graph = &(In.graph); Vect_copy_head_data(&In, &Out); Vect_hist_copy(&In, &Out); Vect_hist_command(&Out); if (method_opt->answer[0] == 'b') { bridge_list = Vect_new_list(); bridges = NetA_compute_bridges(graph, bridge_list); G_debug(3, "Bridges: %d", bridges); for (i = 0; i < bridges; i++) { int type = Vect_read_line(&In, Points, Cats, abs(bridge_list->value[i])); Vect_write_line(&Out, type, Points, Cats); } Vect_destroy_list(bridge_list); } else { articulation_list = Vect_new_list(); articulations = NetA_articulation_points(graph, articulation_list); G_debug(3, "Articulation points: %d", articulations); for (i = 0; i < articulations; i++) { double x, y, z; Vect_get_node_coor(&In, articulation_list->value[i], &x, &y, &z); Vect_reset_line(Points); Vect_append_point(Points, x, y, z); Vect_write_line(&Out, GV_POINT, Points, Cats); } Vect_destroy_list(articulation_list); } Vect_build(&Out); Vect_close(&In); Vect_close(&Out); exit(EXIT_SUCCESS); }
int area_area(struct Map_info *In, int *field, struct Map_info *Tmp, struct Map_info *Out, struct field_info *Fi, dbDriver * driver, int operator, int *ofield, ATTRIBUTES * attr, struct ilist *BList, double snap) { int ret, input, line, nlines, area, nareas; int in_area, in_centr, out_cat; struct line_pnts *Points; struct line_cats *Cats; CENTR *Centr; char buf[1000]; dbString stmt; int nmodif; int verbose; verbose = G_verbose(); Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); /* optional snap */ if (snap > 0) { int i, j, snapped_lines = 0; struct bound_box box; struct boxlist *boxlist = Vect_new_boxlist(0); struct ilist *reflist = Vect_new_list(); G_message(_("Snapping boundaries with %g ..."), snap); /* snap boundaries in B to boundaries in A, * not modifying boundaries in A */ if (BList->n_values > 1) qsort(BList->value, BList->n_values, sizeof(int), cmp_int); snapped_lines = 0; nlines = BList->n_values; for (i = 0; i < nlines; i++) { line = BList->value[i]; Vect_read_line(Tmp, Points, Cats, line); /* select lines by box */ Vect_get_line_box(Tmp, line, &box); box.E += snap; box.W -= snap; box.N += snap; box.S -= snap; box.T = 0.0; box.B = 0.0; Vect_select_lines_by_box(Tmp, &box, GV_BOUNDARY, boxlist); if (boxlist->n_values > 0) { Vect_reset_list(reflist); for (j = 0; j < boxlist->n_values; j++) { int aline = boxlist->id[j]; if (!bsearch(&aline, BList->value, BList->n_values, sizeof(int), cmp_int)) { G_ilist_add(reflist, aline); } } /* snap bline to alines */ if (Vect_snap_line(Tmp, reflist, Points, snap, 0, NULL, NULL)) { /* rewrite bline*/ Vect_delete_line(Tmp, line); ret = Vect_write_line(Tmp, GV_BOUNDARY, Points, Cats); G_ilist_add(BList, ret); snapped_lines++; G_debug(3, "line %d snapped", line); } } } Vect_destroy_boxlist(boxlist); Vect_destroy_list(reflist); G_verbose_message(n_("%d boundary snapped", "%d boundaries snapped", snapped_lines), snapped_lines); } /* same procedure like for v.in.ogr: * Vect_clean_small_angles_at_nodes() can change the geometry so that new intersections * are created. We must call Vect_break_lines(), Vect_remove_duplicates() * and Vect_clean_small_angles_at_nodes() until no more small dangles are found */ do { G_message(_("Breaking lines...")); Vect_break_lines_list(Tmp, NULL, BList, GV_BOUNDARY, NULL); /* Probably not necessary for LINE x AREA */ G_message(_("Removing duplicates...")); Vect_remove_duplicates(Tmp, GV_BOUNDARY, NULL); G_message(_("Cleaning boundaries at nodes...")); nmodif = Vect_clean_small_angles_at_nodes(Tmp, GV_BOUNDARY, NULL); } while (nmodif > 0); /* ?: May be result of Vect_break_lines() + Vect_remove_duplicates() any dangle or bridge? * In that case, calls to Vect_remove_dangles() and Vect_remove_bridges() would be also necessary */ G_set_verbose(0); /* should be fast, be silent */ Vect_build_partial(Tmp, GV_BUILD_AREAS); G_set_verbose(verbose); nlines = Vect_get_num_lines(Tmp); ret = 0; for (line = 1; line <= nlines; line++) { if (!Vect_line_alive(Tmp, line)) continue; if (Vect_get_line_type(Tmp, line) == GV_BOUNDARY) { int left, rite; Vect_get_line_areas(Tmp, line, &left, &rite); if (left == 0 || rite == 0) { /* invalid boundary */ ret = 1; break; } } } if (ret) { Vect_remove_dangles(Tmp, GV_BOUNDARY, -1, NULL); Vect_remove_bridges(Tmp, NULL, NULL, NULL); } G_set_verbose(0); Vect_build_partial(Tmp, GV_BUILD_NONE); Vect_build_partial(Tmp, GV_BUILD_BASE); G_set_verbose(verbose); G_message(_("Merging lines...")); Vect_merge_lines(Tmp, GV_BOUNDARY, NULL, NULL); /* Attach islands */ G_message(_("Attaching islands...")); /* can take some time, show messages */ Vect_build_partial(Tmp, GV_BUILD_ATTACH_ISLES); /* Calculate new centroids for all areas */ nareas = Vect_get_num_areas(Tmp); Centr = (CENTR *) G_malloc((nareas + 1) * sizeof(CENTR)); /* index from 1 ! */ for (area = 1; area <= nareas; area++) { ret = Vect_get_point_in_area(Tmp, area, &(Centr[area].x), &(Centr[area].y)); if (ret < 0) { G_warning(_("Cannot calculate area centroid")); Centr[area].valid = 0; } else { Centr[area].valid = 1; } } /* Query input maps */ for (input = 0; input < 2; input++) { G_message(_("Querying vector map <%s>..."), Vect_get_full_name(&(In[input]))); for (area = 1; area <= nareas; area++) { Centr[area].cat[input] = Vect_new_cats_struct(); G_percent(area, nareas, 1); in_area = Vect_find_area(&(In[input]), Centr[area].x, Centr[area].y); if (in_area > 0) { in_centr = Vect_get_area_centroid(&(In[input]), in_area); if (in_centr > 0) { int i; Vect_read_line(&(In[input]), NULL, Cats, in_centr); /* Add all cats with original field number */ for (i = 0; i < Cats->n_cats; i++) { if (Cats->field[i] == field[input]) { ATTR *at; Vect_cat_set(Centr[area].cat[input], ofield[input + 1], Cats->cat[i]); /* Mark as used */ at = find_attr(&(attr[input]), Cats->cat[i]); if (!at) G_fatal_error(_("Attribute not found")); at->used = 1; } } } } } } G_message(_("Writing centroids...")); db_init_string(&stmt); out_cat = 1; for (area = 1; area <= nareas; area++) { int i; G_percent(area, nareas, 1); /* check the condition */ switch (operator) { case OP_AND: if (! (Centr[area].cat[0]->n_cats > 0 && Centr[area].cat[1]->n_cats > 0)) continue; break; case OP_OR: if (! (Centr[area].cat[0]->n_cats > 0 || Centr[area].cat[1]->n_cats > 0)) continue; break; case OP_NOT: if (! (Centr[area].cat[0]->n_cats > 0 && !(Centr[area].cat[1]->n_cats > 0))) continue; break; case OP_XOR: if ((Centr[area].cat[0]->n_cats > 0 && Centr[area].cat[1]->n_cats > 0) || (!(Centr[area].cat[0]->n_cats > 0) && !(Centr[area].cat[1]->n_cats > 0))) continue; break; } Vect_reset_line(Points); Vect_reset_cats(Cats); Vect_append_point(Points, Centr[area].x, Centr[area].y, 0.0); if (ofield[0] > 0) { /* Add new cats for all combinations of input cats (-1 in cycle for null) */ for (i = -1; i < Centr[area].cat[0]->n_cats; i++) { int j; if (i == -1 && Centr[area].cat[0]->n_cats > 0) continue; /* no need to make null */ for (j = -1; j < Centr[area].cat[1]->n_cats; j++) { if (j == -1 && Centr[area].cat[1]->n_cats > 0) continue; /* no need to make null */ if (ofield[0] > 0) Vect_cat_set(Cats, ofield[0], out_cat); /* attributes */ if (driver) { ATTR *at; sprintf(buf, "insert into %s values ( %d", Fi->table, out_cat); db_set_string(&stmt, buf); /* cata */ if (i >= 0) { if (attr[0].columns) { at = find_attr(&(attr[0]), Centr[area].cat[0]->cat[i]); if (!at) G_fatal_error(_("Attribute not found")); if (at->values) db_append_string(&stmt, at->values); else db_append_string(&stmt, attr[0].null_values); } else { sprintf(buf, ", %d", Centr[area].cat[0]->cat[i]); db_append_string(&stmt, buf); } } else { if (attr[0].columns) { db_append_string(&stmt, attr[0].null_values); } else { sprintf(buf, ", null"); db_append_string(&stmt, buf); } } /* catb */ if (j >= 0) { if (attr[1].columns) { at = find_attr(&(attr[1]), Centr[area].cat[1]->cat[j]); if (!at) G_fatal_error(_("Attribute not found")); if (at->values) db_append_string(&stmt, at->values); else db_append_string(&stmt, attr[1].null_values); } else { sprintf(buf, ", %d", Centr[area].cat[1]->cat[j]); db_append_string(&stmt, buf); } } else { if (attr[1].columns) { db_append_string(&stmt, attr[1].null_values); } else { sprintf(buf, ", null"); db_append_string(&stmt, buf); } } db_append_string(&stmt, " )"); G_debug(3, "%s", db_get_string(&stmt)); if (db_execute_immediate(driver, &stmt) != DB_OK) G_warning(_("Unable to insert new record: '%s'"), db_get_string(&stmt)); } out_cat++; } } } /* Add all cats from input vectors */ if (ofield[1] > 0 && field[0] > 0) { for (i = 0; i < Centr[area].cat[0]->n_cats; i++) { if (Centr[area].cat[0]->field[i] == field[0]) Vect_cat_set(Cats, ofield[1], Centr[area].cat[0]->cat[i]); } } if (ofield[2] > 0 && field[1] > 0 && ofield[1] != ofield[2]) { for (i = 0; i < Centr[area].cat[1]->n_cats; i++) { if (Centr[area].cat[1]->field[i] == field[1]) Vect_cat_set(Cats, ofield[2], Centr[area].cat[1]->cat[i]); } } Vect_write_line(Tmp, GV_CENTROID, Points, Cats); Vect_write_line(Out, GV_CENTROID, Points, Cats); } G_set_verbose(0); /* should be fast, be silent */ Vect_build_partial(Tmp, GV_BUILD_CENTROIDS); G_set_verbose(verbose); /* Copy valid boundaries to final output */ nlines = Vect_get_num_lines(Tmp); for (line = 1; line <= nlines; line++) { int i, ltype, side[2], centr[2]; G_percent(line, nlines, 1); /* must be before any continue */ if (!Vect_line_alive(Tmp, line)) continue; ltype = Vect_read_line(Tmp, Points, Cats, line); if (!(ltype & GV_BOUNDARY)) continue; Vect_get_line_areas(Tmp, line, &side[0], &side[1]); for (i = 0; i < 2; i++) { if (side[i] == 0) { /* This should not happen ! */ centr[i] = 0; continue; } if (side[i] > 0) { area = side[i]; } else { /* island */ area = Vect_get_isle_area(Tmp, abs(side[i])); } if (area > 0) centr[i] = Vect_get_area_centroid(Tmp, area); else centr[i] = 0; } if (centr[0] || centr[1]) Vect_write_line(Out, GV_BOUNDARY, Points, Cats); } return 0; }
int export_areas_multi(struct Map_info *In, int field, int donocat, OGRFeatureDefnH Ogr_featuredefn,OGRLayerH Ogr_layer, struct field_info *Fi, dbDriver *driver, int ncol, int *colctype, const char **colname, int doatt, int nocat, int *n_noatt, int *n_nocat) { int i, n_exported, area; int cat, ncats_field, line, type, findex, ipart; struct line_pnts *Points; struct line_cats *Cats; struct ilist *cat_list, *line_list, *lcats; OGRGeometryH Ogr_geometry, Ogr_geometry_part; OGRFeatureH Ogr_feature; OGRwkbGeometryType wkbtype, wkbtype_part; Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); cat_list = Vect_new_list(); line_list = Vect_new_list(); lcats = Vect_new_list(); n_exported = 0; /* check if category index is available for given field */ findex = Vect_cidx_get_field_index(In, field); if (findex == -1) G_fatal_error(_("Unable to export multi-features. No category index for layer %d."), field); /* determine type */ wkbtype_part = wkbPolygon; wkbtype = get_multi_wkbtype(wkbtype_part); ncats_field = Vect_cidx_get_unique_cats_by_index(In, findex, cat_list); G_debug(1, "n_cats = %d for layer %d", ncats_field, field); if (donocat) G_message(_("Exporting features with category...")); for (i = 0; i < cat_list->n_values; i++) { G_percent(i, cat_list->n_values - 1, 5); cat = cat_list->value[i]; /* find all centroids with given category */ Vect_cidx_find_all(In, field, GV_CENTROID, cat, line_list); /* create multi-feature */ Ogr_geometry = OGR_G_CreateGeometry(wkbtype); /* build simple features geometry, go through all parts */ for (ipart = 0; ipart < line_list->n_values; ipart++) { line = line_list->value[ipart]; G_debug(3, "cat=%d, line=%d -> part=%d", cat, line, ipart); /* get centroid's category */ Vect_read_line(In, NULL, Cats, line); /* check for category consistency */ Vect_field_cat_get(Cats, field, lcats); if (!Vect_val_in_list(lcats, cat)) G_fatal_error(_("Unable to create multi-feature. " "Category %d not found in line %d, field %d"), cat, line, field); /* find correspoding area */ area = Vect_get_centroid_area(In, line); if (area == 0) continue; /* create polygon from area */ Ogr_geometry_part = create_polygon(In, area, Points); /* add part */ OGR_G_AddGeometryDirectly(Ogr_geometry, Ogr_geometry_part); } if (!OGR_G_IsEmpty(Ogr_geometry)) { /* write multi-feature */ Ogr_feature = OGR_F_Create(Ogr_featuredefn); OGR_F_SetGeometry(Ogr_feature, Ogr_geometry); mk_att(cat, Fi, driver, ncol, colctype, colname, doatt, nocat, Ogr_feature, n_noatt); OGR_L_CreateFeature(Ogr_layer, Ogr_feature); OGR_F_Destroy(Ogr_feature); n_exported++; } else { /* skip empty features */ G_debug(3, "multi-feature is empty -> skipped"); } OGR_G_DestroyGeometry(Ogr_geometry); } if (donocat) G_message(_("Exporting features without category...")); /* check lines without category, if -c flag is given write them as * one multi-feature */ Ogr_geometry = OGR_G_CreateGeometry(wkbtype); Vect_rewind(In); Vect_set_constraint_type(In, GV_CENTROID); while(TRUE) { type = Vect_read_next_line(In, NULL, Cats); if (type < 0) break; /* get centroid's category */ Vect_cat_get(Cats, field, &cat); if (cat > 0) continue; /* skip features with category */ if (cat < 0 && !donocat) { (*n_nocat)++; continue; /* skip lines without category, do not export * not labeled */ } /* find correspoding area */ line = Vect_get_next_line_id(In); area = Vect_get_centroid_area(In, line); if (area == 0) continue; /* create polygon from area */ Ogr_geometry_part = create_polygon(In, area, Points); /* add part */ OGR_G_AddGeometryDirectly(Ogr_geometry, Ogr_geometry_part); (*n_nocat)++; } if (!OGR_G_IsEmpty(Ogr_geometry)) { /* write multi-feature */ Ogr_feature = OGR_F_Create(Ogr_featuredefn); OGR_F_SetGeometry(Ogr_feature, Ogr_geometry); mk_att(cat, Fi, driver, ncol, colctype, colname, doatt, nocat, Ogr_feature, n_noatt); OGR_L_CreateFeature(Ogr_layer, Ogr_feature); OGR_F_Destroy(Ogr_feature); n_exported++; } else { /* skip empty features */ G_debug(3, "multi-feature is empty -> skipped"); } OGR_G_DestroyGeometry(Ogr_geometry); Vect_destroy_line_struct(Points); Vect_destroy_cats_struct(Cats); Vect_destroy_list(cat_list); Vect_destroy_list(line_list); Vect_destroy_list(lcats); return n_exported; }
int main(int argc, char **argv) { struct GModule *module; struct Option *map_opt, *field_opt, *fs_opt, *vs_opt, *nv_opt, *col_opt, *where_opt, *file_opt; struct Flag *c_flag, *v_flag, *r_flag; dbDriver *driver; dbString sql, value_string; dbCursor cursor; dbTable *table; dbColumn *column; dbValue *value; struct field_info *Fi; int ncols, col, more; struct Map_info Map; char query[1024]; struct ilist *list_lines; struct bound_box *min_box, *line_box; int i, line, area, init_box, cat; module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("database")); G_add_keyword(_("attribute table")); module->description = _("Prints vector map attributes."); map_opt = G_define_standard_option(G_OPT_V_MAP); field_opt = G_define_standard_option(G_OPT_V_FIELD); col_opt = G_define_standard_option(G_OPT_DB_COLUMNS); where_opt = G_define_standard_option(G_OPT_DB_WHERE); fs_opt = G_define_standard_option(G_OPT_F_SEP); fs_opt->description = _("Output field separator"); fs_opt->guisection = _("Format"); vs_opt = G_define_standard_option(G_OPT_F_SEP); vs_opt->key = "vs"; vs_opt->description = _("Output vertical record separator"); vs_opt->answer = NULL; vs_opt->guisection = _("Format"); nv_opt = G_define_option(); nv_opt->key = "nv"; nv_opt->type = TYPE_STRING; nv_opt->required = NO; nv_opt->description = _("Null value indicator"); nv_opt->guisection = _("Format"); file_opt = G_define_standard_option(G_OPT_F_OUTPUT); file_opt->key = "file"; file_opt->required = NO; file_opt->description = _("Name for output file (if omitted or \"-\" output to stdout)"); r_flag = G_define_flag(); r_flag->key = 'r'; r_flag->description = _("Print minimal region extent of selected vector features instead of attributes"); c_flag = G_define_flag(); c_flag->key = 'c'; c_flag->description = _("Do not include column names in output"); c_flag->guisection = _("Format"); v_flag = G_define_flag(); v_flag->key = 'v'; v_flag->description = _("Vertical output (instead of horizontal)"); v_flag->guisection = _("Format"); G_gisinit(argv[0]); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* set input vector map name and mapset */ if (file_opt->answer && strcmp(file_opt->answer, "-") != 0) { if (NULL == freopen(file_opt->answer, "w", stdout)) { G_fatal_error(_("Unable to open file <%s> for writing"), file_opt->answer); } } if (r_flag->answer) { min_box = (struct bound_box *) G_malloc(sizeof(struct bound_box)); G_zero((void *)min_box, sizeof(struct bound_box)); line_box = (struct bound_box *) G_malloc(sizeof(struct bound_box)); list_lines = Vect_new_list(); } else { min_box = line_box = NULL; list_lines = NULL; } db_init_string(&sql); db_init_string(&value_string); /* open input vector */ if (!r_flag->answer) Vect_open_old_head2(&Map, map_opt->answer, "", field_opt->answer); else { if (2 > Vect_open_old2(&Map, map_opt->answer, "", field_opt->answer)) { Vect_close(&Map); G_fatal_error(_("Unable to open vector map <%s> at topology level. " "Flag '%c' requires topology level."), map_opt->answer, r_flag->key); } } if ((Fi = Vect_get_field2(&Map, field_opt->answer)) == NULL) G_fatal_error(_("Database connection not defined for layer <%s>"), field_opt->answer); driver = db_start_driver_open_database(Fi->driver, Fi->database); if (!driver) G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver); if (col_opt->answer) sprintf(query, "SELECT %s FROM ", col_opt->answer); else sprintf(query, "SELECT * FROM "); db_set_string(&sql, query); db_append_string(&sql, Fi->table); if (where_opt->answer) { char *buf = NULL; buf = G_malloc((strlen(where_opt->answer) + 8)); sprintf(buf, " WHERE %s", where_opt->answer); db_append_string(&sql, buf); G_free(buf); } if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) G_fatal_error(_("Unable to open select cursor")); table = db_get_cursor_table(&cursor); ncols = db_get_table_number_of_columns(table); /* column names if horizontal output (ignore for -r) */ if (!v_flag->answer && !c_flag->answer && !r_flag->answer) { for (col = 0; col < ncols; col++) { column = db_get_table_column(table, col); if (col) fprintf(stdout, "%s", fs_opt->answer); fprintf(stdout, "%s", db_get_column_name(column)); } fprintf(stdout, "\n"); } init_box = 1; /* fetch the data */ while (1) { if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) G_fatal_error(_("Unable to fetch data from table <%s>"), Fi->table); if (!more) break; cat = -1; for (col = 0; col < ncols; col++) { column = db_get_table_column(table, col); value = db_get_column_value(column); if (cat < 0 && strcmp(Fi->key, db_get_column_name(column)) == 0) { cat = db_get_value_int(value); if (r_flag->answer) break; } if (r_flag->answer) continue; db_convert_column_value_to_string(column, &value_string); if (!c_flag->answer && v_flag->answer) fprintf(stdout, "%s%s", db_get_column_name(column), fs_opt->answer); if (col && !v_flag->answer) fprintf(stdout, "%s", fs_opt->answer); if (nv_opt->answer && db_test_value_isnull(value)) fprintf(stdout, "%s", nv_opt->answer); else fprintf(stdout, "%s", db_get_string(&value_string)); if (v_flag->answer) fprintf(stdout, "\n"); } if (r_flag->answer) { /* get minimal region extent */ Vect_cidx_find_all(&Map, Vect_get_field_number(&Map, field_opt->answer), -1, cat, list_lines); for (i = 0; i < list_lines->n_values; i++) { line = list_lines->value[i]; area = Vect_get_centroid_area(&Map, line); if (area > 0) { if (!Vect_get_area_box(&Map, area, line_box)) G_fatal_error(_("Unable to get bounding box of area %d"), area); } else { if (!Vect_get_line_box(&Map, line, line_box)) G_fatal_error(_("Unable to get bounding box of line %d"), line); } if (init_box) { Vect_box_copy(min_box, line_box); init_box = 0; } else { Vect_box_extend(min_box, line_box); } } } else { if (!v_flag->answer) fprintf(stdout, "\n"); else if (vs_opt->answer) fprintf(stdout, "%s\n", vs_opt->answer); } } if (r_flag->answer) { fprintf(stdout, "n=%f\n", min_box->N); fprintf(stdout, "s=%f\n", min_box->S); fprintf(stdout, "w=%f\n", min_box->W); fprintf(stdout, "e=%f\n", min_box->E); if (Vect_is_3d(&Map)) { fprintf(stdout, "t=%f\n", min_box->T); fprintf(stdout, "b=%f\n", min_box->B); } fflush(stdout); G_free((void *)min_box); G_free((void *)line_box); Vect_destroy_list(list_lines); } db_close_cursor(&cursor); db_close_database_shutdown_driver(driver); Vect_close(&Map); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct Map_info In, Out, cut_map; static struct line_pnts *Points; struct line_cats *Cats; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *map_in, *map_out, *cut_out; struct Option *afield_opt, *nfield_opt, *abcol, *afcol, *ncol; struct Option *catsource_opt, *wheresource_opt; struct Option *catsink_opt, *wheresink_opt; int with_z; int afield, nfield, mask_type; struct varray *varray_source, *varray_sink; dglGraph_s *graph; int i, nlines, *flow, total_flow; struct ilist *source_list, *sink_list, *cut; int find_cut; char buf[2000]; /* Attribute table */ dbString sql; dbDriver *driver; struct field_info *Fi; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("network")); G_add_keyword(_("flow")); module->description = _("Computes the maximum flow between two sets of nodes in the network."); /* Define the different options as defined in gis.h */ map_in = G_define_standard_option(G_OPT_V_INPUT); afield_opt = G_define_standard_option(G_OPT_V_FIELD); afield_opt->key = "arc_layer"; afield_opt->answer = "1"; afield_opt->label = _("Arc layer"); afield_opt->guisection = _("Cost"); nfield_opt = G_define_standard_option(G_OPT_V_FIELD); nfield_opt->key = "node_layer"; nfield_opt->answer = "2"; nfield_opt->label = _("Node layer"); nfield_opt->guisection = _("Cost"); map_out = G_define_standard_option(G_OPT_V_OUTPUT); cut_out = G_define_standard_option(G_OPT_V_OUTPUT); cut_out->key = "cut"; cut_out->description = _("Name for output vector map containing a minimum cut"); afcol = G_define_standard_option(G_OPT_DB_COLUMN); afcol->key = "arc_column"; afcol->required = NO; afcol->description = _("Arc forward/both direction(s) cost column (number)"); afcol->guisection = _("Cost"); abcol = G_define_standard_option(G_OPT_DB_COLUMN); abcol->key = "arc_backward_column"; abcol->required = NO; abcol->description = _("Arc backward direction cost column (number)"); abcol->guisection = _("Cost"); ncol = G_define_standard_option(G_OPT_DB_COLUMN); ncol->key = "node_column"; ncol->required = NO; ncol->description = _("Node cost column (number)"); ncol->guisection = _("Cost"); catsource_opt = G_define_standard_option(G_OPT_V_CATS); catsource_opt->key = "source_cats"; catsource_opt->label = _("Source category values"); catsource_opt->guisection = _("Source"); wheresource_opt = G_define_standard_option(G_OPT_DB_WHERE); wheresource_opt->key = "source_where"; wheresource_opt->label = _("Source WHERE conditions of SQL statement without 'where' keyword"); wheresource_opt->guisection = _("Source"); catsink_opt = G_define_standard_option(G_OPT_V_CATS); catsink_opt->key = "sink_cats"; catsink_opt->label = _("Sink category values"); catsink_opt->guisection = _("Sink"); wheresink_opt = G_define_standard_option(G_OPT_DB_WHERE); wheresink_opt->key = "sink_where"; wheresink_opt->label = _("Sink WHERE conditions of SQL statement without 'where' keyword"); wheresink_opt->guisection = _("Sink"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); find_cut = (cut_out->answer[0]); /* TODO: make an option for this */ mask_type = GV_LINE | GV_BOUNDARY; Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); Vect_check_input_output_name(map_in->answer, map_out->answer, G_FATAL_EXIT); Vect_set_open_level(2); if (1 > Vect_open_old(&In, map_in->answer, "")) G_fatal_error(_("Unable to open vector map <%s>"), map_in->answer); with_z = Vect_is_3d(&In); if (0 > Vect_open_new(&Out, map_out->answer, with_z)) { Vect_close(&In); G_fatal_error(_("Unable to create vector map <%s>"), map_out->answer); } if (find_cut && 0 > Vect_open_new(&cut_map, cut_out->answer, with_z)) { Vect_close(&In); Vect_close(&Out); G_fatal_error(_("Unable to create vector map <%s>"), cut_out->answer); } /* parse filter option and select appropriate lines */ afield = Vect_get_field_number(&In, afield_opt->answer); nfield = Vect_get_field_number(&In, nfield_opt->answer); /* Create table */ Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE); Vect_map_add_dblink(&Out, 1, NULL, Fi->table, GV_KEY_COLUMN, Fi->database, Fi->driver); db_init_string(&sql); driver = db_start_driver_open_database(Fi->driver, Fi->database); if (driver == NULL) G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver); db_set_error_handler_driver(driver); sprintf(buf, "create table %s (cat integer, flow double precision)", Fi->table); db_set_string(&sql, buf); G_debug(2, "%s", db_get_string(&sql)); if (db_execute_immediate(driver, &sql) != DB_OK) { db_close_database_shutdown_driver(driver); G_fatal_error(_("Unable to create table: '%s'"), db_get_string(&sql)); } if (db_create_index2(driver, Fi->table, GV_KEY_COLUMN) != DB_OK) G_warning(_("Cannot create index")); if (db_grant_on_table (driver, Fi->table, DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK) G_fatal_error(_("Cannot grant privileges on table <%s>"), Fi->table); db_begin_transaction(driver); source_list = Vect_new_list(); sink_list = Vect_new_list(); if (NetA_initialise_varray (&In, nfield, GV_POINT, wheresource_opt->answer, catsource_opt->answer, &varray_source) <= 0) { G_fatal_error(_("No source features selected. " "Please check options '%s', '%s'."), catsource_opt->key, wheresource_opt->key); } if (NetA_initialise_varray (&In, nfield, GV_POINT, wheresink_opt->answer, catsink_opt->answer, &varray_sink) <= 0) { G_fatal_error(_("No sink features selected. " "Please check options '%s', '%s'."), catsink_opt->key, wheresink_opt->key); } NetA_varray_to_nodes(&In, varray_source, source_list, NULL); NetA_varray_to_nodes(&In, varray_sink, sink_list, NULL); if (source_list->n_values == 0) G_fatal_error(_("No sources")); if (sink_list->n_values == 0) G_fatal_error(_("No sinks")); Vect_copy_head_data(&In, &Out); Vect_hist_copy(&In, &Out); Vect_hist_command(&Out); if (0 != Vect_net_build_graph(&In, mask_type, afield, nfield, afcol->answer, abcol->answer, ncol->answer, 0, 0)) G_fatal_error(_("Unable to build graph for vector map <%s>"), Vect_get_full_name(&In)); graph = Vect_net_get_graph(&In); nlines = Vect_get_num_lines(&In); flow = (int *)G_calloc(nlines + 1, sizeof(int)); if (!flow) G_fatal_error(_("Out of memory")); total_flow = NetA_flow(graph, source_list, sink_list, flow); G_debug(3, "Max flow: %d", total_flow); if (find_cut) { cut = Vect_new_list(); total_flow = NetA_min_cut(graph, source_list, sink_list, flow, cut); G_debug(3, "Min cut: %d", total_flow); } G_message(_("Writing the output...")); G_percent_reset(); for (i = 1; i <= nlines; i++) { G_percent(i, nlines, 1); int type = Vect_read_line(&In, Points, Cats, i); Vect_write_line(&Out, type, Points, Cats); if (type == GV_LINE) { int cat; Vect_cat_get(Cats, afield, &cat); if (cat == -1) continue; /*TODO: warning? */ sprintf(buf, "insert into %s values (%d, %f)", Fi->table, cat, flow[i] / (double)In.dgraph.cost_multip); db_set_string(&sql, buf); G_debug(3, "%s", db_get_string(&sql)); if (db_execute_immediate(driver, &sql) != DB_OK) { db_close_database_shutdown_driver(driver); G_fatal_error(_("Cannot insert new record: %s"), db_get_string(&sql)); }; } } if (find_cut) { for (i = 0; i < cut->n_values; i++) { int type = Vect_read_line(&In, Points, Cats, cut->value[i]); Vect_write_line(&cut_map, type, Points, Cats); } Vect_destroy_list(cut); Vect_build(&cut_map); Vect_close(&cut_map); } db_commit_transaction(driver); db_close_database_shutdown_driver(driver); G_free(flow); Vect_destroy_list(source_list); Vect_destroy_list(sink_list); Vect_build(&Out); Vect_close(&In); Vect_close(&Out); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct GModule *module; struct GParams params; struct Map_info Map; struct Map_info **BgMap; /* backgroud vector maps */ int nbgmaps; /* number of registrated background maps */ enum mode action_mode; FILE *ascii; int i; int move_first, snap; int ret, layer; double move_x, move_y, move_z, thresh[3]; struct line_pnts *coord; struct ilist *List; struct cat_list *Clist; ascii = NULL; List = NULL; BgMap = NULL; nbgmaps = 0; coord = NULL; Clist = NULL; G_gisinit(argv[0]); module = G_define_module(); module->overwrite = TRUE; G_add_keyword(_("vector")); G_add_keyword(_("editing")); G_add_keyword(_("geometry")); module->description = _("Edits a vector map, allows adding, deleting " "and modifying selected vector features."); if (!parser(argc, argv, ¶ms, &action_mode)) exit(EXIT_FAILURE); /* get list of categories */ Clist = Vect_new_cat_list(); if (params.cat->answer && Vect_str_to_cat_list(params.cat->answer, Clist)) { G_fatal_error(_("Unable to get category list <%s>"), params.cat->answer); } /* open input file */ if (params.in->answer) { if (strcmp(params.in->answer, "-") != 0) { ascii = fopen(params.in->answer, "r"); if (ascii == NULL) G_fatal_error(_("Unable to open file <%s>"), params.in->answer); } else { ascii = stdin; } } if (!ascii && action_mode == MODE_ADD) G_fatal_error(_("Required parameter <%s> not set"), params.in->key); if (action_mode == MODE_CREATE) { int overwrite; overwrite = G_check_overwrite(argc, argv); if (G_find_vector2(params.map->answer, G_mapset())) { if (!overwrite) G_fatal_error(_("Vector map <%s> already exists"), params.map->answer); } /* 3D vector maps? */ ret = Vect_open_new(&Map, params.map->answer, WITHOUT_Z); if (Vect_maptype(&Map) == GV_FORMAT_OGR_DIRECT) { int type; type = Vect_option_to_types(params.type); if (type != GV_POINT && type != GV_LINE && type != GV_BOUNDARY) G_fatal_error(_("Supported feature type for OGR layer: " "%s, %s or %s"), "point", "line", "boundary"); V2_open_new_ogr(&Map, type); } if (ret == -1) { G_fatal_error(_("Unable to create vector map <%s>"), params.map->answer); } G_debug(1, "Map created"); if (ascii) { /* also add new vector features */ action_mode = MODE_ADD; } } else { /* open selected vector file */ if (action_mode == MODE_ADD) /* write */ ret = Vect_open_update2(&Map, params.map->answer, G_mapset(), params.fld->answer); else /* read-only -- select features */ ret = Vect_open_old2(&Map, params.map->answer, G_mapset(), params.fld->answer); if (ret < 2) G_fatal_error(_("Unable to open vector map <%s> at topological level %d"), params.map->answer, 2); } G_debug(1, "Map opened"); /* open backgroud maps */ if (params.bmaps->answer) { i = 0; while (params.bmaps->answers[i]) { const char *bmap = params.bmaps->answers[i]; const char *mapset = G_find_vector2(bmap, ""); if (!mapset) G_fatal_error(_("Vector map <%s> not found"), bmap); if (strcmp( G_fully_qualified_name(params.map->answer, G_mapset()), G_fully_qualified_name(bmap, mapset)) == 0) { G_fatal_error(_("Unable to open vector map <%s> as the background map. " "It is given as vector map to be edited."), bmap); } nbgmaps++; BgMap = (struct Map_info **)G_realloc( BgMap, nbgmaps * sizeof(struct Map_info *)); BgMap[nbgmaps - 1] = (struct Map_info *)G_malloc(sizeof(struct Map_info)); if (Vect_open_old(BgMap[nbgmaps - 1], bmap, "") == -1) G_fatal_error(_("Unable to open vector map <%s>"), bmap); G_verbose_message(_("Background vector map <%s> registered"), bmap); i++; } } layer = Vect_get_field_number(&Map, params.fld->answer); i = 0; while (params.maxdist->answers[i]) { switch (i) { case THRESH_COORDS: thresh[THRESH_COORDS] = max_distance(atof(params.maxdist->answers[THRESH_COORDS])); thresh[THRESH_SNAP] = thresh[THRESH_QUERY] = thresh[THRESH_COORDS]; break; case THRESH_SNAP: thresh[THRESH_SNAP] = max_distance(atof(params.maxdist->answers[THRESH_SNAP])); break; case THRESH_QUERY: thresh[THRESH_QUERY] = atof(params.maxdist->answers[THRESH_QUERY]); break; default: break; } i++; } move_first = params.move_first->answer ? 1 : 0; snap = NO_SNAP; if (strcmp(params.snap->answer, "node") == 0) snap = SNAP; else if (strcmp(params.snap->answer, "vertex") == 0) snap = SNAPVERTEX; if (snap != NO_SNAP && thresh[THRESH_SNAP] <= 0) { G_warning(_("Threshold for snapping must be > 0. No snapping applied.")); snap = NO_SNAP; } if (action_mode != MODE_CREATE && action_mode != MODE_ADD) { /* select lines */ List = Vect_new_list(); G_message(_("Selecting features...")); if (action_mode == MODE_COPY && BgMap && BgMap[0]) { List = select_lines(BgMap[0], action_mode, ¶ms, thresh, List); } else { List = select_lines(&Map, action_mode, ¶ms, thresh, List); } } if ((action_mode != MODE_CREATE && action_mode != MODE_ADD && action_mode != MODE_SELECT)) { if (List->n_values < 1) { G_warning(_("No features selected, nothing to edit")); action_mode = MODE_NONE; ret = 0; } else { /* reopen the map for updating */ if (action_mode == MODE_ZBULK && !Vect_is_3d(&Map)) { Vect_close(&Map); G_fatal_error(_("Vector map <%s> is not 3D. Tool '%s' requires 3D vector map. " "Please convert the vector map " "to 3D using e.g. %s."), params.map->answer, params.tool->answer, "v.extrude"); } Vect_close(&Map); Vect_open_update2(&Map, params.map->answer, G_mapset(), params.fld->answer); } } /* coords option -> array */ if (params.coord->answers) { coord = Vect_new_line_struct(); int i = 0; double east, north; while (params.coord->answers[i]) { east = atof(params.coord->answers[i]); north = atof(params.coord->answers[i + 1]); Vect_append_point(coord, east, north, 0.0); i += 2; } } /* perform requested editation */ switch (action_mode) { case MODE_CREATE: break; case MODE_ADD: if (!params.header->answer) Vect_read_ascii_head(ascii, &Map); int num_lines; num_lines = Vect_get_num_lines(&Map); ret = Vect_read_ascii(ascii, &Map); G_message(_("%d features added"), ret); if (ret > 0) { int iline; struct ilist *List_added; List_added = Vect_new_list(); for (iline = num_lines + 1; iline <= Vect_get_num_lines(&Map); iline++) Vect_list_append(List_added, iline); G_verbose_message(_("Threshold value for snapping is %.2f"), thresh[THRESH_SNAP]); if (snap != NO_SNAP) { /* apply snapping */ /* snap to vertex ? */ Vedit_snap_lines(&Map, BgMap, nbgmaps, List_added, thresh[THRESH_SNAP], snap == SNAP ? FALSE : TRUE); } if (params.close->answer) { /* close boundaries */ int nclosed; nclosed = close_lines(&Map, GV_BOUNDARY, thresh[THRESH_SNAP]); G_message(_("%d boundaries closed"), nclosed); } Vect_destroy_list(List_added); } break; case MODE_DEL: ret = Vedit_delete_lines(&Map, List); G_message(_("%d features deleted"), ret); break; case MODE_MOVE: move_x = atof(params.move->answers[0]); move_y = atof(params.move->answers[1]); move_z = atof(params.move->answers[2]); G_verbose_message(_("Threshold value for snapping is %.2f"), thresh[THRESH_SNAP]); ret = Vedit_move_lines(&Map, BgMap, nbgmaps, List, move_x, move_y, move_z, snap, thresh[THRESH_SNAP]); G_message(_("%d features moved"), ret); break; case MODE_VERTEX_MOVE: move_x = atof(params.move->answers[0]); move_y = atof(params.move->answers[1]); move_z = atof(params.move->answers[2]); G_verbose_message(_("Threshold value for snapping is %.2f"), thresh[THRESH_SNAP]); ret = Vedit_move_vertex(&Map, BgMap, nbgmaps, List, coord, thresh[THRESH_COORDS], thresh[THRESH_SNAP], move_x, move_y, move_z, move_first, snap); G_message(_("%d vertices moved"), ret); break; case MODE_VERTEX_ADD: ret = Vedit_add_vertex(&Map, List, coord, thresh[THRESH_COORDS]); G_message(_("%d vertices added"), ret); break; case MODE_VERTEX_DELETE: ret = Vedit_remove_vertex(&Map, List, coord, thresh[THRESH_COORDS]); G_message(_("%d vertices removed"), ret); break; case MODE_BREAK: if (params.coord->answer) { ret = Vedit_split_lines(&Map, List, coord, thresh[THRESH_COORDS], NULL); } else { ret = Vect_break_lines_list(&Map, List, NULL, GV_LINES, NULL); } G_message(_("%d lines broken"), ret); break; case MODE_CONNECT: G_verbose_message(_("Threshold value for snapping is %.2f"), thresh[THRESH_SNAP]); ret = Vedit_connect_lines(&Map, List, thresh[THRESH_SNAP]); G_message(_("%d lines connected"), ret); break; case MODE_MERGE: ret = Vedit_merge_lines(&Map, List); G_message(_("%d lines merged"), ret); break; case MODE_SELECT: ret = print_selected(List); break; case MODE_CATADD: ret = Vedit_modify_cats(&Map, List, layer, 0, Clist); G_message(_("%d features modified"), ret); break; case MODE_CATDEL: ret = Vedit_modify_cats(&Map, List, layer, 1, Clist); G_message(_("%d features modified"), ret); break; case MODE_COPY: if (BgMap && BgMap[0]) { if (nbgmaps > 1) G_warning(_("Multiple background maps were given. " "Selected features will be copied only from " "vector map <%s>."), Vect_get_full_name(BgMap[0])); ret = Vedit_copy_lines(&Map, BgMap[0], List); } else { ret = Vedit_copy_lines(&Map, NULL, List); } G_message(_("%d features copied"), ret); break; case MODE_SNAP: G_verbose_message(_("Threshold value for snapping is %.2f"), thresh[THRESH_SNAP]); ret = snap_lines(&Map, List, thresh[THRESH_SNAP]); break; case MODE_FLIP: ret = Vedit_flip_lines(&Map, List); G_message(_("%d lines flipped"), ret); break; case MODE_NONE: break; case MODE_ZBULK:{ double start, step; double x1, y1, x2, y2; start = atof(params.zbulk->answers[0]); step = atof(params.zbulk->answers[1]); x1 = atof(params.bbox->answers[0]); y1 = atof(params.bbox->answers[1]); x2 = atof(params.bbox->answers[2]); y2 = atof(params.bbox->answers[3]); ret = Vedit_bulk_labeling(&Map, List, x1, y1, x2, y2, start, step); G_message(_("%d lines labeled"), ret); break; } case MODE_CHTYPE:{ ret = Vedit_chtype_lines(&Map, List); if (ret > 0) { G_message(_("%d features converted"), ret); } else { G_message(_("No feature modified")); } break; } default: G_warning(_("Operation not implemented")); ret = -1; break; } Vect_hist_command(&Map); /* build topology only if requested or if tool!=select */ if (!(action_mode == MODE_SELECT || params.topo->answer == 1 || !MODE_NONE)) { Vect_build_partial(&Map, GV_BUILD_NONE); Vect_build(&Map); } if (List) Vect_destroy_list(List); Vect_close(&Map); G_debug(1, "Map closed"); /* close background maps */ for (i = 0; i < nbgmaps; i++) { Vect_close(BgMap[i]); G_free((void *)BgMap[i]); } G_free((void *)BgMap); if (coord) Vect_destroy_line_struct(coord); if (Clist) Vect_destroy_cat_list(Clist); G_done_msg(" "); if (ret > -1) { exit(EXIT_SUCCESS); } else { exit(EXIT_FAILURE); } }
/** \brief Select features according to SQL where statement \param[in] Map vector map \param[in] layer layer number \param[in] type feature type \param[in] where 'where' statement \param[in,out] List list of selected features \return number of selected lines */ int sel_by_where(struct Map_info *Map, int layer, int type, char *where, struct ilist *List) { struct cat_list *cat_list; struct ilist *List_tmp; struct field_info *Fi; dbDriver *driver; dbHandle handle; int *cats, ncats; if (first_selection) { List_tmp = List; first_selection = 0; } else { List_tmp = Vect_new_list(); } cat_list = Vect_new_cat_list(); if (layer < 1) { G_fatal_error(_("Layer must be > 0 for 'where'")); } Fi = Vect_get_field(Map, layer); if (!Fi) { G_fatal_error(_("Database connection not defined for layer %d"), layer); } driver = db_start_driver(Fi->driver); if (!driver) G_fatal_error(_("Unable to start driver <%s>"), Fi->driver); db_init_handle(&handle); db_set_handle(&handle, Fi->database, NULL); if (db_open_database(driver, &handle) != DB_OK) G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver); ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats); db_close_database(driver); db_shutdown_driver(driver); Vect_array_to_cat_list(cats, ncats, cat_list); /* free array of cats */ if (ncats >= 0) G_free(cats); sel_by_cat(Map, cat_list, layer, type, NULL, List_tmp); G_debug(1, " %d lines selected (by where)", List_tmp->n_values); /* merge lists (only duplicate items) */ if (List_tmp != List) { merge_lists(List, List_tmp); Vect_destroy_list(List_tmp); } Vect_destroy_cat_list(cat_list); return List->n_values; }
/* Calculate costs for MST on given set of terminals. * If AList / NList is not NULL, list of arcs / nodes in MST is created * Note: qsort() from more (say >30) terminals, takes long time (most of mst()). * To improve the speed, there are used two sorted queues of costs: * 1. for all combinations of in trms * 2. from 'sp' to all other terminals * Because 1. is sorted only if new sp as added to list of terminals, * and 2. is much shorter than 1., a lot of time is saved. */ int mst(struct Map_info *Map, int *trms, int ntrms, /* array of terminal, number of terminals */ double *cst, double max_cst, /* cost, maximum cost */ struct ilist *AList, struct ilist *NList, /* list of arcs/nodes in ST */ int sp, /* Steiner point (node) to be tested with terminals, (0 = ignore) */ int rebuild) { /* rebuild the sorted list of costs for terminals */ int i, j, node1, node2, com1, com2, t1, t2, line; static int k; int tcpos, scpos; /* current position in the term_costs / sp_costs */ double tcst; struct ilist *List; int nsteps, quse; int nall; /* number of terminals + sp ( if used ) */ if (AList != NULL) { Vect_reset_list(AList); } List = Vect_new_list(); /* Create sorted array for all combinations of terms */ if (rebuild) { k = 0; for (i = 0; i < ntrms; i++) { for (j = i + 1; j < ntrms; j++) { term_costs[k].term1 = i; term_costs[k].term2 = j; get_node_costs(trms[i], trms[j], &tcst); term_costs[k].cost = tcst; k++; } } qsort((void *)term_costs, k, sizeof(COST), cmp); /* this takes most of a time in mst() */ for (i = 0; i < k; i++) { G_debug(3, " %d - %d cost = %f\n", term_costs[i].term1, term_costs[i].term2, term_costs[i].cost); } } /* Create sorted array for all combinations of sp -> terms */ if (sp > 0) { for (i = 0; i < ntrms; i++) { sp_costs[i].term1 = -1; /* not needed */ sp_costs[i].term2 = i; get_node_costs(sp, trms[i], &tcst); sp_costs[i].cost = tcst; } qsort((void *)sp_costs, ntrms, sizeof(COST), cmp); for (i = 0; i < ntrms; i++) { G_debug(3, " %d - %d cost = %f\n", sp_costs[i].term1, sp_costs[i].term2, sp_costs[i].cost); } } tcst = 0; /* MST has number_of_terminals-1 arcs */ if (sp > 0) { nall = ntrms + 1; nsteps = ntrms; /* i.e. + one StP */ } else { nall = ntrms; nsteps = ntrms - 1; } G_debug(1, "nall = %d\n", nall); for (i = 0; i < nall; i++) comps[i] = 0; tcpos = 0; scpos = 0; G_debug(2, "nsteps = %d\n", nsteps); for (i = 0; i < nsteps; i++) { G_debug(2, "step = %d\n", i); /* Take the best (lowest costs, no cycle) from both queues */ /* For both queues go to next lowest costs without cycle */ /* treminal costs */ for (j = tcpos; j < k; j++) { t1 = term_costs[j].term1; t2 = term_costs[j].term2; com1 = comps[t1]; com2 = comps[t2]; if (com1 != com2 || com1 == 0) { /* will not create cycle -> candidate */ tcpos = j; break; } } if (j == k) { /* arc without cycle not found */ tcpos = -1; } /* StP costs */ if (sp > 0) { for (j = scpos; j < ntrms; j++) { t1 = ntrms; /* StP is on first fre position */ t2 = sp_costs[j].term2; com1 = comps[t1]; com2 = comps[t2]; G_debug(3, "scpos: j = %d comps(%d) = %d coms(%d) = %d\n", j, t1, com1, t2, com2); if (com1 != com2 || com1 == 0) { /* will not create cycle -> candidate */ scpos = j; G_debug(3, " ok -> scpos = %d\n", scpos); break; } } if (j == ntrms) { /* arc without cycle not found */ scpos = -1; } } else { scpos = -1; } /* Do not access invalid items even for debugging */ if (tcpos != -1 && scpos != -1) G_debug(3, "tcost = %f, scost = %f\n", term_costs[tcpos].cost, sp_costs[scpos].cost); /* Now we have positions set on lowest costs in each queue or -1 if no more/not used */ if (tcpos >= 0 && scpos >= 0) { if (term_costs[tcpos].cost < sp_costs[scpos].cost) quse = 1; /* use terms queue */ else quse = 2; /* use sp queue */ } else if (tcpos >= 0) { quse = 1; /* use terms queue */ } else { quse = 2; /* use sp queue */ } /* Now we know from which queue take next arc -> add arc to components */ if (quse == 1) { t1 = term_costs[tcpos].term1; t2 = term_costs[tcpos].term2; tcst += term_costs[tcpos].cost; tcpos++; } else { t1 = ntrms; t2 = sp_costs[scpos].term2; tcst += sp_costs[scpos].cost; scpos++; } G_debug(3, "quse = %d t1 = %d t2 = %d\n", quse, t1, t2); G_debug(3, "tcst = %f (max = %f)\n", tcst, max_cst); com1 = comps[t1]; com2 = comps[t2]; comps[t1] = i + 1; comps[t2] = i + 1; G_debug(3, "comps(%d) = %d coms(%d) = %d\n", t1, i + 1, t2, i + 1); /* reset connected branches */ for (j = 0; j < nall; j++) { if (comps[j] == com1 && com1 != 0) comps[j] = i + 1; if (comps[j] == com2 && com2 != 0) comps[j] = i + 1; } if (tcst > max_cst) { G_debug(3, "cost > max -> return\n"); *cst = PORT_DOUBLE_MAX; return 1; } /* add to list of arcs */ if (AList != NULL) { node1 = trms[t1]; node2 = trms[t2]; Vect_net_shortest_path(Map, node1, node2, List, NULL); for (j = 0; j < List->n_values; j++) { Vect_list_append(AList, abs(List->value[j])); } } } /* create list of nodes */ if (NList != NULL) { Vect_reset_list(NList); for (i = 0; i < AList->n_values; i++) { line = AList->value[i]; Vect_get_line_nodes(Map, line, &node1, &node2); Vect_list_append(NList, node1); Vect_list_append(NList, node2); } } *cst = tcst; Vect_destroy_list(List); return 1; }
/*! \brief Render vector features into list \param Map pointer to Map_info structure \param box bounding box of region to be rendered \param draw_flag types of objects to be rendered (see vedit.h) \param center_easing, center_northing, map_width, map_height, map_res values used for conversion en->xy \return pointer to robject_list structure */ struct robject_list *Vedit_render_map(struct Map_info *Map, struct bound_box *box, int draw_flag, double center_easting, double center_northing, int map_width, int map_height, double map_res) { int i, nfeat, fid; struct ilist *list; struct robject_list *list_obj; struct robject *robj; /* define region */ region.center_easting = center_easting; region.center_northing = center_northing; region.map_width = map_width; region.map_height = map_height; region.map_res = map_res; region.map_west = center_easting - (map_width / 2.) * map_res; region.map_north = center_northing + (map_height / 2.) * map_res; list = Vect_new_list(); list_obj = NULL; state.nitems_alloc = 1000; list_obj = (struct robject_list *)G_malloc(sizeof(struct robject_list)); list_obj->nitems = 0; list_obj->item = (struct robject **)G_malloc(state.nitems_alloc * sizeof(struct robject *)); /* area */ if (draw_flag & DRAW_AREA) { nfeat = Vect_select_areas_by_box(Map, box, list); for (i = 0; i < nfeat; i++) { fid = list->value[i]; draw_area(Map, fid, list_obj); } } /* draw lines inside of current display region */ nfeat = Vect_select_lines_by_box(Map, box, GV_POINTS | GV_LINES, /* fixme */ list); G_debug(1, "Vedit_render_map(): region: w=%f, e=%f, s=%f, n=%f nlines=%d", box->W, box->E, box->S, box->N, nfeat); /* features */ for (i = 0; i < list->n_values; i++) { fid = list->value[i]; robj = draw_line(Map, fid, draw_flag); if (!robj) continue; list_append(list_obj, robj); if (state.type & GV_LINES) { /* vertices */ if (draw_flag & DRAW_VERTEX) { robj = draw_line_vertices(); robj->fid = fid; if (robj) list_append(list_obj, robj); } /* nodes */ if (draw_flag & (DRAW_NODEONE | DRAW_NODETWO)) { draw_line_nodes(Map, fid, draw_flag, list_obj); } /* direction */ if (draw_flag & DRAW_DIRECTION) { draw_line_dir(list_obj, fid); } } } list_obj->item = (struct robject **)G_realloc(list_obj->item, list_obj->nitems * sizeof(struct robject *)); Vect_destroy_list(list); return list_obj; }
int main(int argc, char **argv) { int i, j, k, ret; int nlines, type, ltype, afield, tfield, geo, cat; int sp, nsp, nspused, node, line; struct Option *map, *output, *afield_opt, *tfield_opt, *afcol, *type_opt, *term_opt, *nsp_opt; struct Flag *geo_f; struct GModule *module; struct Map_info Map, Out; int *testnode; /* array all nodes: 1 - should be tested as Steiner, * 0 - no need to test (unreachable or terminal) */ struct ilist *TList; /* list of terminal nodes */ struct ilist *StArcs; /* list of arcs on Steiner tree */ struct ilist *StNodes; /* list of nodes on Steiner tree */ struct boxlist *pointlist; double cost, tmpcost; struct cat_list *Clist; struct line_cats *Cats; struct line_pnts *Points; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("network")); G_add_keyword(_("steiner tree")); module->label = _("Creates Steiner tree for the network and given terminals."); module->description = _("Note that 'Minimum Steiner Tree' problem is NP-hard " "and heuristic algorithm is used in this module so " "the result may be sub optimal."); map = G_define_standard_option(G_OPT_V_INPUT); output = G_define_standard_option(G_OPT_V_OUTPUT); type_opt = G_define_standard_option(G_OPT_V_TYPE); type_opt->key = "arc_type"; type_opt->options = "line,boundary"; type_opt->answer = "line,boundary"; type_opt->label = _("Arc type"); afield_opt = G_define_standard_option(G_OPT_V_FIELD); afield_opt->key = "arc_layer"; afield_opt->answer = "1"; afield_opt->label = _("Arc layer"); tfield_opt = G_define_standard_option(G_OPT_V_FIELD); tfield_opt->key = "node_layer"; tfield_opt->answer = "2"; tfield_opt->label = _("Node layer (used for terminals)"); afcol = G_define_option(); afcol->key = "acolumn"; afcol->type = TYPE_STRING; afcol->required = NO; afcol->description = _("Arcs' cost column (for both directions)"); term_opt = G_define_standard_option(G_OPT_V_CATS); term_opt->key = "terminal_cats"; term_opt->required = YES; term_opt->description = _("Categories of points on terminals (layer is specified by nlayer)"); nsp_opt = G_define_option(); nsp_opt->key = "npoints"; nsp_opt->type = TYPE_INTEGER; nsp_opt->required = NO; nsp_opt->multiple = NO; nsp_opt->answer = "-1"; nsp_opt->description = _("Number of Steiner points (-1 for all possible)"); geo_f = G_define_flag(); geo_f->key = 'g'; geo_f->description = _("Use geodesic calculation for longitude-latitude locations"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); Cats = Vect_new_cats_struct(); Points = Vect_new_line_struct(); type = Vect_option_to_types(type_opt); afield = atoi(afield_opt->answer); TList = Vect_new_list(); StArcs = Vect_new_list(); StNodes = Vect_new_list(); Clist = Vect_new_cat_list(); tfield = atoi(tfield_opt->answer); Vect_str_to_cat_list(term_opt->answer, Clist); G_debug(1, "Imput categories:\n"); for (i = 0; i < Clist->n_ranges; i++) { G_debug(1, "%d - %d\n", Clist->min[i], Clist->max[i]); } if (geo_f->answer) geo = 1; else geo = 0; Vect_check_input_output_name(map->answer, output->answer, G_FATAL_EXIT); Vect_set_open_level(2); if (Vect_open_old(&Map, map->answer, "") < 0) G_fatal_error(_("Unable to open vector map <%s>"), map->answer); nnodes = Vect_get_num_nodes(&Map); nlines = Vect_get_num_lines(&Map); /* Create list of terminals based on list of categories */ for (i = 1; i <= nlines; i++) { ltype = Vect_get_line_type(&Map, i); if (!(ltype & GV_POINT)) continue; Vect_read_line(&Map, Points, Cats, i); if (!(Vect_cat_get(Cats, tfield, &cat))) continue; node = Vect_find_node(&Map, Points->x[0], Points->y[0], Points->z[0], 0, 0); if (!node) { G_warning(_("Point is not connected to the network (cat=%d)"), cat); continue; } if (Vect_cat_in_cat_list(cat, Clist)) { Vect_list_append(TList, node); } } nterms = TList->n_values; /* GTC Terminal refers to an Steiner tree endpoint */ G_message(_("Number of terminals: %d\n"), nterms); if (nterms < 2) { /* GTC Terminal refers to an Steiner tree endpoint */ G_fatal_error(_("Not enough terminals (< 2)")); } /* Number of steiner points */ nsp = atoi(nsp_opt->answer); if (nsp > nterms - 2) { nsp = nterms - 2; G_warning(_("Requested number of Steiner points > than possible")); } else if (nsp == -1) { nsp = nterms - 2; } G_message(_("Number of Steiner points set to %d\n"), nsp); testnode = (int *)G_malloc((nnodes + 1) * sizeof(int)); for (i = 1; i <= nnodes; i++) testnode[i] = 1; /* Alloc arrays of costs for nodes, first node at 1 (0 not used) */ nodes_costs = (double **)G_malloc((nnodes) * sizeof(double *)); for (i = 0; i < nnodes; i++) { nodes_costs[i] = (double *)G_malloc((nnodes - i) * sizeof(double)); for (j = 0; j < nnodes - i; j++) nodes_costs[i][j] = -1; /* init, i.e. cost was not calculated yet */ } /* alloc memory from each to each other (not directed) terminal */ i = nterms + nterms - 2; /* max number of terms + Steiner points */ comps = (int *)G_malloc(i * sizeof(int)); i = i * (i - 1) / 2; /* number of combinations */ term_costs = (COST *) G_malloc(i * sizeof(COST)); /* alloc memory for costs from Stp to each other terminal */ i = nterms + nterms - 2 - 1; /* max number of terms + Steiner points - 1 */ sp_costs = (COST *) G_malloc(i * sizeof(COST)); terms = (int *)G_malloc((nterms + nterms - 2) * sizeof(int)); /* i.e. +(nterms - 2) St Points */ /* Create initial parts from list of terminals */ G_debug(1, "List of terminal nodes (%d):\n", nterms); for (i = 0; i < nterms; i++) { G_debug(1, "%d\n", TList->value[i]); terms[i] = TList->value[i]; testnode[terms[i]] = 0; /* do not test as Steiner */ } /* Build graph */ Vect_net_build_graph(&Map, type, afield, 0, afcol->answer, NULL, NULL, geo, 0); /* Init costs for all terminals */ for (i = 0; i < nterms; i++) init_node_costs(&Map, terms[i]); /* Test if all terminal may be connected */ for (i = 1; i < nterms; i++) { ret = get_node_costs(terms[0], terms[i], &cost); if (ret == 0) { /* GTC Terminal refers to an Steiner tree endpoint */ G_fatal_error(_("Terminal at node [%d] cannot be connected " "to terminal at node [%d]"), terms[0], terms[i]); } } /* Remove not reachable from list of SP candidates */ j = 0; for (i = 1; i <= nnodes; i++) { ret = get_node_costs(terms[0], i, &cost); if (ret == 0) { testnode[i] = 0; G_debug(2, "node %d removed from list of Steiner point candidates\n", i ); j++; } } G_message(_("[%d] (not reachable) nodes removed from list " "of Steiner point candidates"), j); /* calc costs for terminals MST */ ret = mst(&Map, terms, nterms, &cost, PORT_DOUBLE_MAX, NULL, NULL, 0, 1); /* no StP, rebuild */ G_message(_("MST costs = %f"), cost); /* Go through all nodes and try to use as steiner points -> find that which saves most costs */ nspused = 0; for (j = 0; j < nsp; j++) { sp = 0; G_verbose_message(_("Search for [%d]. Steiner point"), j + 1); for (i = 1; i <= nnodes; i++) { G_percent(i, nnodes, 1); if (testnode[i] == 0) { G_debug(3, "skip test for %d\n", i); continue; } ret = mst(&Map, terms, nterms + j, &tmpcost, cost, NULL, NULL, i, 0); G_debug(2, "cost = %f x %f\n", tmpcost, cost); if (tmpcost < cost) { /* sp candidate */ G_debug(3, " steiner candidate node = %d mst = %f (x last = %f)\n", i, tmpcost, cost); sp = i; cost = tmpcost; } } if (sp > 0) { G_message(_("Steiner point at node [%d] was added " "to terminals (MST costs = %f)"), sp, cost); terms[nterms + j] = sp; init_node_costs(&Map, sp); testnode[sp] = 0; nspused++; /* rebuild for nex cycle */ ret = mst(&Map, terms, nterms + nspused, &tmpcost, PORT_DOUBLE_MAX, NULL, NULL, 0, 1); } else { /* no steiner found */ G_message(_("No Steiner point found -> leaving cycle")); break; } } G_message(_("Number of added Steiner points: %d " "(theoretic max is %d).\n"), nspused, nterms - 2); /* Build lists of arcs and nodes for final version */ ret = mst(&Map, terms, nterms + nspused, &cost, PORT_DOUBLE_MAX, StArcs, StNodes, 0, 0); /* Calculate true costs, which may be lower than MST if steiner points were not used */ if (nsp < nterms - 2) { G_message(_("Spanning tree costs on complet graph = %f\n" "(may be higher than resulting Steiner tree costs!!!)"), cost); } else G_message(_("Steiner tree costs = %f"), cost); /* Write arcs to new map */ if (Vect_open_new(&Out, output->answer, Vect_is_3d(&Map)) < 0) G_fatal_error(_("Unable to create vector map <%s>"), output->answer); Vect_hist_command(&Out); G_debug(1, "Steiner tree:"); G_debug(1, "Arcs' categories (layer %d, %d arcs):", afield, StArcs->n_values); for (i = 0; i < StArcs->n_values; i++) { line = StArcs->value[i]; ltype = Vect_read_line(&Map, Points, Cats, line); Vect_write_line(&Out, ltype, Points, Cats); Vect_cat_get(Cats, afield, &cat); G_debug(1, "arc cat = %d", cat); } G_debug(1, "Nodes' categories (layer %d, %d nodes):", tfield, StNodes->n_values); k = 0; pointlist = Vect_new_boxlist(0); for (i = 0; i < StNodes->n_values; i++) { double x, y, z; struct bound_box box; node = StNodes->value[i]; Vect_get_node_coor(&Map, node, &x, &y, &z); box.E = box.W = x; box.N = box.S = y; box.T = box.B = z; Vect_select_lines_by_box(&Map, &box, GV_POINT, pointlist); nlines = Vect_get_node_n_lines(&Map, node); for (j = 0; j < pointlist->n_values; j++) { line = pointlist->id[j]; ltype = Vect_read_line(&Map, Points, Cats, line); if (!(ltype & GV_POINT)) continue; if (!(Vect_cat_get(Cats, tfield, &cat))) continue; Vect_write_line(&Out, ltype, Points, Cats); G_debug(1, "node cat = %d", cat); k++; } } Vect_build(&Out); G_message(n_("A Steiner tree with %d arc has been built", "A Steiner tree with %d arcs has been built", StArcs->n_values), StArcs->n_values); /* Free, ... */ Vect_destroy_list(StArcs); Vect_destroy_list(StNodes); Vect_close(&Map); Vect_close(&Out); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int i, j, k, ret, city, city1; int nlines, type, ltype, afield, tfield, geo, cat; int node, node1, node2, line; struct Option *map, *output, *afield_opt, *tfield_opt, *afcol, *type_opt, *term_opt; struct Flag *geo_f; struct GModule *module; char *mapset; struct Map_info Map, Out; struct ilist *TList; /* list of terminal nodes */ struct ilist *List; struct ilist *StArcs; /* list of arcs on Steiner tree */ struct ilist *StNodes; /* list of nodes on Steiner tree */ double cost, tmpcost, tcost; struct cat_list *Clist; struct line_cats *Cats; struct line_pnts *Points; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("vector, network, salesman"); module->label = _("Creates a cycle connecting given nodes (Traveling salesman problem)."); module->description = _("Note that TSP is NP-hard, heuristic algorithm is used by " "this module and created cycle may be sub optimal"); map = G_define_standard_option(G_OPT_V_INPUT); output = G_define_standard_option(G_OPT_V_OUTPUT); type_opt = G_define_standard_option(G_OPT_V_TYPE); type_opt->options = "line,boundary"; type_opt->answer = "line,boundary"; type_opt->description = _("Arc type"); afield_opt = G_define_standard_option(G_OPT_V_FIELD); afield_opt->key = "alayer"; afield_opt->description = _("Arc layer"); tfield_opt = G_define_standard_option(G_OPT_V_FIELD); tfield_opt->key = "nlayer"; tfield_opt->answer = "2"; tfield_opt->description = _("Node layer (used for cities)"); afcol = G_define_option(); afcol->key = "acolumn"; afcol->type = TYPE_STRING; afcol->required = NO; afcol->description = _("Arcs' cost column (for both directions)"); term_opt = G_define_standard_option(G_OPT_V_CATS); term_opt->key = "ccats"; term_opt->required = YES; term_opt->description = _("Categories of points ('cities') on nodes " "(layer is specified by nlayer)"); geo_f = G_define_flag(); geo_f->key = 'g'; geo_f->description = _("Use geodesic calculation for longitude-latitude locations"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); Cats = Vect_new_cats_struct(); Points = Vect_new_line_struct(); type = Vect_option_to_types(type_opt); afield = atoi(afield_opt->answer); TList = Vect_new_list(); List = Vect_new_list(); StArcs = Vect_new_list(); StNodes = Vect_new_list(); Clist = Vect_new_cat_list(); tfield = atoi(tfield_opt->answer); Vect_str_to_cat_list(term_opt->answer, Clist); G_debug(1, "Imput categories:\n"); for (i = 0; i < Clist->n_ranges; i++) { G_debug(1, "%d - %d\n", Clist->min[i], Clist->max[i]); } if (geo_f->answer) geo = 1; else geo = 0; Vect_check_input_output_name(map->answer, output->answer, GV_FATAL_EXIT); mapset = G_find_vector2(map->answer, NULL); if (mapset == NULL) G_fatal_error(_("Vector map <%s> not found"), map->answer); Vect_set_open_level(2); Vect_open_old(&Map, map->answer, mapset); nnodes = Vect_get_num_nodes(&Map); /* Create list of terminals based on list of categories */ for (i = 1; i <= nnodes; i++) { nlines = Vect_get_node_n_lines(&Map, i); for (j = 0; j < nlines; j++) { line = abs(Vect_get_node_line(&Map, i, j)); ltype = Vect_read_line(&Map, NULL, Cats, line); if (!(ltype & GV_POINT)) continue; if (!(Vect_cat_get(Cats, tfield, &cat))) continue; if (Vect_cat_in_cat_list(cat, Clist)) { Vect_list_append(TList, i); } } } ncities = TList->n_values; G_message(_("Number of cities: [%d]"), ncities); if (ncities < 2) G_fatal_error(_("Not enough cities (< 2)")); /* Alloc memory */ cities = (int *)G_malloc(ncities * sizeof(int)); cused = (int *)G_malloc(ncities * sizeof(int)); for (i = 0; i < ncities; i++) { G_debug(1, "%d\n", TList->value[i]); cities[i] = TList->value[i]; cused[i] = 0; /* not in cycle */ } costs = (COST **) G_malloc(ncities * sizeof(COST *)); for (i = 0; i < ncities; i++) { costs[i] = (COST *) G_malloc(ncities * sizeof(COST)); } cycle = (int *)G_malloc((ncities + 1) * sizeof(int)); /* + 1 is for output cycle */ /* Build graph */ Vect_net_build_graph(&Map, type, afield, 0, afcol->answer, NULL, NULL, geo, 0); /* Create sorted lists of costs */ for (i = 0; i < ncities; i++) { k = 0; for (j = 0; j < ncities; j++) { if (i == j) continue; ret = Vect_net_shortest_path(&Map, cities[i], cities[j], NULL, &cost); if (ret == -1) G_fatal_error(_("Destination node [%d] is unreachable " "from node [%d]"), cities[i], cities[j]); costs[i][k].city = j; costs[i][k].cost = cost; k++; } qsort((void *)costs[i], k, sizeof(COST), cmp); } /* debug: print sorted */ for (i = 0; i < ncities; i++) { for (j = 0; j < ncities - 1; j++) { city = costs[i][j].city; G_debug(2, "%d -> %d = %f\n", cities[i], cities[city], costs[i][j].cost); } } /* find 2 cities with largest distance */ cost = -1; for (i = 0; i < ncities; i++) { tmpcost = costs[i][ncities - 2].cost; if (tmpcost > cost) { cost = tmpcost; city = i; } } G_debug(2, "biggest costs %d - %d\n", city, costs[city][ncities - 2].city); /* add this 2 cities to array */ add_city(city, -1); add_city(costs[city][ncities - 2].city, 0); /* In each step, find not used city, with biggest cost to any used city, and insert * into cycle between 2 nearest nodes */ for (i = 0; i < ncities - 2; i++) { cost = -1; G_debug(2, "---- %d ----\n", i); for (j = 0; j < ncities; j++) { if (cused[j] == 1) continue; tmpcost = 0; for (k = 0; k < ncities - 1; k++) { G_debug(2, "? %d (%d) - %d (%d) \n", j, cnode(j), costs[j][k].city, cnode(costs[j][k].city)); if (!cused[costs[j][k].city]) continue; /* only used */ tmpcost += costs[j][k].cost; break; /* first nearest */ } G_debug(2, " cost = %f x %f\n", tmpcost, cost); if (tmpcost > cost) { cost = tmpcost; city = j; } } G_debug(2, "add %d\n", city); /* add to cycle on lovest costs */ cycle[ncyc] = cycle[0]; /* tmp for cycle */ cost = PORT_DOUBLE_MAX; for (j = 0; j < ncyc; j++) { node1 = cities[cycle[j]]; node2 = cities[cycle[j + 1]]; ret = Vect_net_shortest_path(&Map, node1, node2, NULL, &tcost); tmpcost = -tcost; node1 = cities[cycle[j]]; node2 = cities[city]; ret = Vect_net_shortest_path(&Map, node1, node2, NULL, &tcost); tmpcost += tcost; node1 = cities[cycle[j + 1]]; node2 = cities[city]; ret = Vect_net_shortest_path(&Map, node1, node2, NULL, &tcost); tmpcost += tcost; G_debug(2, "? %d - %d cost = %f x %f\n", node1, node2, tmpcost, cost); if (tmpcost < cost) { city1 = j; cost = tmpcost; } } add_city(city, city1); } /* Print */ G_debug(2, "Cycle:\n"); for (i = 0; i < ncities; i++) { G_debug(2, "%d: %d: %d\n", i, cycle[i], cities[cycle[i]]); } /* Create list of arcs */ cycle[ncities] = cycle[0]; for (i = 0; i < ncities; i++) { node1 = cities[cycle[i]]; node2 = cities[cycle[i + 1]]; G_debug(2, " %d -> %d\n", node1, node2); ret = Vect_net_shortest_path(&Map, node1, node2, List, NULL); for (j = 0; j < List->n_values; j++) { line = abs(List->value[j]); Vect_list_append(StArcs, line); Vect_get_line_nodes(&Map, line, &node1, &node2); Vect_list_append(StNodes, node1); Vect_list_append(StNodes, node2); } } /* Write arcs to new map */ Vect_open_new(&Out, output->answer, Vect_is_3d(&Map)); Vect_hist_command(&Out); fprintf(stdout, "\nCycle:\n"); fprintf(stdout, "Arcs' categories (layer %d, %d arcs):\n", afield, StArcs->n_values); for (i = 0; i < StArcs->n_values; i++) { line = StArcs->value[i]; ltype = Vect_read_line(&Map, Points, Cats, line); Vect_write_line(&Out, ltype, Points, Cats); Vect_cat_get(Cats, afield, &cat); if (i > 0) printf(","); fprintf(stdout, "%d", cat); } fprintf(stdout, "\n\n"); fprintf(stdout, "Nodes' categories (layer %d, %d nodes):\n", tfield, StNodes->n_values); k = 0; for (i = 0; i < StNodes->n_values; i++) { node = StNodes->value[i]; nlines = Vect_get_node_n_lines(&Map, node); for (j = 0; j < nlines; j++) { line = abs(Vect_get_node_line(&Map, node, j)); ltype = Vect_read_line(&Map, Points, Cats, line); if (!(ltype & GV_POINT)) continue; if (!(Vect_cat_get(Cats, tfield, &cat))) continue; Vect_write_line(&Out, ltype, Points, Cats); if (k > 0) fprintf(stdout, ","); fprintf(stdout, "%d", cat); k++; } } fprintf(stdout, "\n\n"); Vect_build(&Out); /* Free, ... */ Vect_destroy_list(StArcs); Vect_destroy_list(StNodes); Vect_close(&Map); Vect_close(&Out); exit(EXIT_SUCCESS); }
/*! \brief Extensive tests for correct topology - lines or boundaries of zero length - intersecting boundaries, ie. overlapping areas - areas without centroids that are not isles \param Map vector map \param[out] Err vector map where errors will be written or NULL \return 1 on success \return 0 on error */ int Vect_topo_check(struct Map_info *Map, struct Map_info *Err) { int line, nlines; int nerrors, n_zero_lines, n_zero_boundaries; struct line_pnts *Points; struct line_cats *Cats; /* rebuild topology if needed */ if (Vect_get_built(Map) != GV_BUILD_ALL) { Vect_build_partial(Map, GV_BUILD_NONE); Vect_build(Map); } G_message(_("Checking for topological errors...")); Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); /* lines or boundaries of zero length */ n_zero_lines = n_zero_boundaries = 0; nlines = Vect_get_num_lines(Map); for (line = 1; line <= nlines; line++) { int type; if (!Vect_line_alive(Map, line)) continue; type = Vect_get_line_type(Map, line); if (type & GV_LINES) { double len; Vect_read_line(Map, Points, Cats, line); len = Vect_line_length(Points); if (len == 0) { if (type & GV_LINE) n_zero_lines++; else if (type & GV_BOUNDARY) n_zero_boundaries++; if (Err) Vect_write_line(Err, type, Points, Cats); } } } if (n_zero_lines) G_warning(_("Number of lines of length zero: %d"), n_zero_lines); if (n_zero_boundaries) G_warning(_("Number of boundaries of length zero: %d"), n_zero_boundaries); /* remaining checks are for areas only */ if (Vect_get_num_primitives(Map, GV_BOUNDARY) == 0) return 1; /* intersecting boundaries -> overlapping areas */ nerrors = Vect_check_line_breaks(Map, GV_BOUNDARY, Err); if (nerrors) G_warning(_("Number of boundary intersections: %d"), nerrors); /* areas without centroids that are not isles * only makes sense if all boundaries are correct */ nerrors = 0; for (line = 1; line <= nlines; line++) { int type; if (!Vect_line_alive(Map, line)) continue; type = Vect_get_line_type(Map, line); if (type == GV_BOUNDARY) { struct P_topo_b *topo = (struct P_topo_b *)Map->plus.Line[line]->topo; if (topo->left == 0 || topo->right == 0) { G_debug(3, "line = %d left = %d right = %d", line, topo->left, topo->right); nerrors++; } } } if (nerrors) G_warning(_("Skipping further checks because of incorrect boundaries")); else { int i, area, left, right, neighbour; int nareas = Vect_get_num_areas(Map); struct ilist *List = Vect_new_list(); nerrors = 0; for (area = 1; area <= nareas; area++) { if (!Vect_area_alive(Map, area)) continue; line = Vect_get_area_centroid(Map, area); if (line != 0) continue; /* has centroid */ Vect_get_area_boundaries(Map, area, List); for (i = 0; i < List->n_values; i++) { line = List->value[i]; Vect_get_line_areas(Map, abs(line), &left, &right); if (line > 0) neighbour = left; else neighbour = right; if (neighbour < 0) { neighbour = Vect_get_isle_area(Map, abs(neighbour)); if (!neighbour) { /* borders outer void */ nerrors++; if (Err) { Vect_read_line(Map, Points, Cats, abs(line)); Vect_write_line(Err, GV_BOUNDARY, Points, Cats); } } /* else neighbour is > 0, check below */ } if (neighbour > 0) { if (Vect_get_area_centroid(Map, neighbour) == 0) { /* neighbouring area does not have a centroid either */ nerrors++; if (Err) { Vect_read_line(Map, Points, Cats, abs(line)); Vect_write_line(Err, GV_BOUNDARY, Points, Cats); } } } } } Vect_destroy_list(List); if (nerrors) G_warning(_("Number of redundant holes: %d"), nerrors); } /* what else ? */ Vect_destroy_line_struct(Points); Vect_destroy_cats_struct(Cats); return 1; }
/*! \brief Connect lines in given threshold \code \ \ id1 \ -> \ \ id2 --------- -----+--- \endcode If two lines are selected and <i>thresh</i> is -1, no limit is applied. \param Map pointer to Map_info \param List list of selected lines \param thresh threshold value \return number of modified lines \return -1 on error */ int Vedit_connect_lines(struct Map_info *Map, struct ilist *List, double thresh) { int nlines_modified, connected; int i, j, node[2], n_nodes; int line, found; double x, y, z; struct ilist *List_exclude, *List_found; nlines_modified = 0; List_exclude = Vect_new_list(); List_found = Vect_new_list(); n_nodes = 2; /* collect lines to be modified */ for (i = 0; i < List->n_values; i++) { line = List->value[i]; if (!Vect_line_alive(Map, line)) continue; if (Vect_get_line_type(Map, line) & GV_POINTS) continue; node[0] = node[1] = -1; Vect_get_line_nodes(Map, line, &(node[0]), &(node[1])); if (node[0] < 0 || node[1] < 0) continue; connected = 0; Vect_reset_list(List_exclude); Vect_list_append(List_exclude, line); for (j = 0; j < n_nodes && !connected; j++) { /* for each line node find lines in threshold */ Vect_get_node_coor(Map, node[j], &x, &y, &z); do { /* find first nearest line */ found = Vect_find_line_list(Map, x, y, z, GV_LINES, thresh, WITHOUT_Z, List_exclude, List_found); if (found > 0 && Vect_line_alive(Map, found)) { /* try to connect lines (given node) */ G_debug(3, "Vedit_connect_lines(): lines=%d,%d", line, found); if (connect_lines(Map, !j, line, found, thresh, List)) { G_debug(3, "Vedit_connect_lines(): lines=%d,%d -> connected", line, found); nlines_modified += 2; connected = 1; } } Vect_list_append(List_exclude, found); } while(List_found->n_values > 0 && !connected); } } Vect_destroy_list(List_exclude); Vect_destroy_list(List_found); return nlines_modified; }
/*! \brief Split selected lines on given position \param Map pointer to Map_info \param List list of selected lines \param coord points location \param thresh threshold \param[out] List_updated list of rewritten features (or NULL) \return number of modified lines \return -1 on error */ int Vedit_split_lines(struct Map_info *Map, struct ilist *List, struct line_pnts *coord, double thresh, struct ilist *List_updated) { int i, j, l; int type, line, seg, newline; int nlines_modified; double px, py, spdist, lpdist, dist; double *x, *y, *z; struct line_pnts *Points, *Points2; struct line_cats *Cats; struct ilist *List_in_box; nlines_modified = 0; Points = Vect_new_line_struct(); Points2 = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); List_in_box = Vect_new_list(); for (i = 0; i < List->n_values; i++) { line = List->value[i]; if (!Vect_line_alive(Map, line)) continue; type = Vect_read_line(Map, Points, Cats, line); if (!(type & GV_LINES)) continue; x = Points->x; y = Points->y; z = Points->z; for (j = 0; j < coord->n_points; j++) { seg = Vect_line_distance(Points, coord->x[j], coord->y[j], coord->z[j], WITHOUT_Z, &px, &py, NULL, &dist, &spdist, &lpdist); if (dist > thresh) { continue; } G_debug(3, "Vedit_split_lines(): line=%d, x=%f, y=%f, px=%f, py=%f, seg=%d, " "dist=%f, spdist=%f, lpdist=%f", line, coord->x[j], coord->y[j], px, py, seg, dist, spdist, lpdist); if (spdist <= 0.0 || spdist >= Vect_line_length(Points)) continue; G_debug(3, "Vedit_split_lines(): line=%d", line); /* copy first line part */ for (l = 0; l < seg; l++) { Vect_append_point(Points2, x[l], y[l], z[l]); } /* add last vertex */ Vect_append_point(Points2, px, py, 0.0); /* rewrite the line */ newline = Vect_rewrite_line(Map, line, type, Points2, Cats); if (newline < 0) { return -1; } if (List_updated) Vect_list_append(List_updated, newline); Vect_reset_line(Points2); /* add given vertex */ Vect_append_point(Points2, px, py, 0.0); /* copy second line part */ for (l = seg; l < Points->n_points; l++) { Vect_append_point(Points2, x[l], y[l], z[l]); } /* rewrite the line */ newline = Vect_write_line(Map, type, Points2, Cats); if (newline < 0) { return -1; } if (List_updated) Vect_list_append(List_updated, newline); nlines_modified++; } /* for each bounding box */ } /* for each selected line */ Vect_destroy_line_struct(Points); Vect_destroy_line_struct(Points2); Vect_destroy_cats_struct(Cats); Vect_destroy_list(List_in_box); return nlines_modified; }
int main(int argc, char *argv[]) { struct Map_info In, Out; static struct line_pnts *Points; struct line_cats *Cats; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *map_in, *map_out; struct Option *afield_opt, *nfield_opt, *afcol, *ncol; struct Flag *geo_f; int with_z; int afield, nfield, mask_type; dglGraph_s *graph; int i, edges, geo; struct ilist *tree_list; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("network")); G_add_keyword(_("spanning tree")); module->description = _("Computes minimum spanning tree for the network."); /* Define the different options as defined in gis.h */ map_in = G_define_standard_option(G_OPT_V_INPUT); map_out = G_define_standard_option(G_OPT_V_OUTPUT); afield_opt = G_define_standard_option(G_OPT_V_FIELD); afield_opt->key = "alayer"; afield_opt->answer = "1"; afield_opt->label = _("Arc layer"); afield_opt->guisection = _("Cost"); nfield_opt = G_define_standard_option(G_OPT_V_FIELD); nfield_opt->key = "nlayer"; nfield_opt->answer = "2"; nfield_opt->label = _("Node layer"); nfield_opt->guisection = _("Cost"); afcol = G_define_standard_option(G_OPT_DB_COLUMN); afcol->key = "afcolumn"; afcol->required = NO; afcol->description = _("Arc forward/both direction(s) cost column (number)"); afcol->guisection = _("Cost"); ncol = G_define_standard_option(G_OPT_DB_COLUMN); ncol->key = "ncolumn"; ncol->required = NO; ncol->description = _("Node cost column (number)"); ncol->guisection = _("Cost"); geo_f = G_define_flag(); geo_f->key = 'g'; geo_f->description = _("Use geodesic calculation for longitude-latitude locations"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* TODO: make an option for this */ mask_type = GV_LINE | GV_BOUNDARY; Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); Vect_check_input_output_name(map_in->answer, map_out->answer, G_FATAL_EXIT); Vect_set_open_level(2); if (1 > Vect_open_old(&In, map_in->answer, "")) G_fatal_error(_("Unable to open vector map <%s>"), map_in->answer); with_z = Vect_is_3d(&In); if (0 > Vect_open_new(&Out, map_out->answer, with_z)) { Vect_close(&In); G_fatal_error(_("Unable to create vector map <%s>"), map_out->answer); } if (geo_f->answer) { geo = 1; if (G_projection() != PROJECTION_LL) G_warning(_("The current projection is not longitude-latitude")); } else geo = 0; /* parse filter option and select appropriate lines */ afield = Vect_get_field_number(&In, afield_opt->answer); nfield = Vect_get_field_number(&In, nfield_opt->answer); if (0 != Vect_net_build_graph(&In, mask_type, afield, nfield, afcol->answer, NULL, ncol->answer, geo, 0)) G_fatal_error(_("Unable to build graph for vector map <%s>"), Vect_get_full_name(&In)); graph = Vect_net_get_graph(&In); Vect_copy_head_data(&In, &Out); Vect_hist_copy(&In, &Out); Vect_hist_command(&Out); tree_list = Vect_new_list(); edges = NetA_spanning_tree(graph, tree_list); G_debug(3, "Edges: %d", edges); for (i = 0; i < edges; i++) { int type = Vect_read_line(&In, Points, Cats, abs(tree_list->value[i])); Vect_write_line(&Out, type, Points, Cats); } Vect_destroy_list(tree_list); Vect_build(&Out); Vect_close(&In); Vect_close(&Out); exit(EXIT_SUCCESS); }
void QgsGrassFeatureIterator::setSelectionRect( const QgsRectangle& rect, bool useIntersect ) { QgsDebugMsg( QString( "useIntersect = %1 rect = %2" ).arg( useIntersect ).arg( rect.toString() ) ); // TODO: selection of edited lines // Lock because functions using static/global variables are used // (e.g. static LocList in Vect_select_lines_by_box, global BranchBuf in RTreeGetBranches) QgsGrass::lock(); mSelection.fill( false ); BOUND_BOX box; box.N = rect.yMaximum(); box.S = rect.yMinimum(); box.E = rect.xMaximum(); box.W = rect.xMinimum(); box.T = PORT_DOUBLE_MAX; box.B = -PORT_DOUBLE_MAX; // Init structures struct ilist * list = Vect_new_list(); if ( !useIntersect ) { // select by bounding boxes only if ( mSource->mLayerType == QgsGrassProvider::POINT || mSource->mLayerType == QgsGrassProvider::CENTROID || mSource->mLayerType == QgsGrassProvider::LINE || mSource->mLayerType == QgsGrassProvider::FACE || mSource->mLayerType == QgsGrassProvider::BOUNDARY || mSource->mLayerType == QgsGrassProvider::TOPO_POINT || mSource->mLayerType == QgsGrassProvider::TOPO_LINE || mSource->mEditing ) { QgsDebugMsg( "Vect_select_lines_by_box" ); int type = mSource->mGrassType; if ( mSource->mEditing ) { type = GV_POINTS | GV_LINES; } QgsDebugMsg( QString( "type = %1" ).arg( type ) ); Vect_select_lines_by_box( mSource->map(), &box, type, list ); } else if ( mSource->mLayerType == QgsGrassProvider::POLYGON ) { Vect_select_areas_by_box( mSource->map(), &box, list ); } else if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE ) { Vect_select_nodes_by_box( mSource->map(), &box, list ); } } else { // check intersection struct line_pnts *polygon = Vect_new_line_struct(); // Using z coor -PORT_DOUBLE_MAX/PORT_DOUBLE_MAX we cover 3D, Vect_select_lines_by_polygon is // using dig_line_box to get the box, it is not perfect, Vect_select_lines_by_polygon // should clarify better how 2D/3D is treated Vect_append_point( polygon, rect.xMinimum(), rect.yMinimum(), -PORT_DOUBLE_MAX ); Vect_append_point( polygon, rect.xMaximum(), rect.yMinimum(), PORT_DOUBLE_MAX ); Vect_append_point( polygon, rect.xMaximum(), rect.yMaximum(), 0 ); Vect_append_point( polygon, rect.xMinimum(), rect.yMaximum(), 0 ); Vect_append_point( polygon, rect.xMinimum(), rect.yMinimum(), 0 ); if ( mSource->mLayerType == QgsGrassProvider::POINT || mSource->mLayerType == QgsGrassProvider::CENTROID || mSource->mLayerType == QgsGrassProvider::LINE || mSource->mLayerType == QgsGrassProvider::FACE || mSource->mLayerType == QgsGrassProvider::BOUNDARY || mSource->mLayerType == QgsGrassProvider::TOPO_POINT || mSource->mLayerType == QgsGrassProvider::TOPO_LINE || mSource->mEditing ) { QgsDebugMsg( "Vect_select_lines_by_polygon" ); int type = mSource->mGrassType; if ( mSource->mEditing ) { type = GV_POINTS | GV_LINES; } QgsDebugMsg( QString( "type = %1" ).arg( type ) ); Vect_select_lines_by_polygon( mSource->map(), polygon, 0, NULL, type, list ); } else if ( mSource->mLayerType == QgsGrassProvider::POLYGON ) { Vect_select_areas_by_polygon( mSource->map(), polygon, 0, NULL, list ); } else if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE ) { // There is no Vect_select_nodes_by_polygon but for nodes it is the same as by box Vect_select_nodes_by_box( mSource->map(), &box, list ); } Vect_destroy_line_struct( polygon ); } for ( int i = 0; i < list->n_values; i++ ) { int lid = list->value[i]; if ( lid < 1 || lid >= mSelection.size() ) // should not happen { QgsDebugMsg( QString( "lid %1 out of range <1,%2>" ).arg( lid ).arg( mSelection.size() ) ); continue; } mSelection.setBit( lid ); } Vect_destroy_list( list ); QgsDebugMsg( QString( " %1 features selected" ).arg( list->n_values ) ); QgsGrass::unlock(); }
/** \brief Select vector features \param[in] Map vector map \param[in] action_mode tool \param[in] params GRASS parameters \param[in] List list of selected features \return list of newly selected features */ struct ilist *select_lines(struct Map_info *Map, enum mode action_mode, struct GParams *params, double *thresh, struct ilist *List) { int layer, type; layer = atoi(params->fld->answer); type = Vect_option_to_types(params->type); /* select by id's */ if (params->id->answer != NULL) { sel_by_id(Map, type, params->id->answer, List); } /* select by category (ignore tools catdel and catadd) */ if ((action_mode != MODE_CATADD && action_mode != MODE_CATDEL) && params->cat->answer != NULL) { sel_by_cat(Map, NULL, layer, type, params->cat->answer, List); } /* select by coordinates (+threshold) */ if (params->coord->answer != NULL) { int i; double east, north; struct line_pnts *coords; coords = Vect_new_line_struct(); i = 0; while (params->coord->answers[i]) { east = atof(params->coord->answers[i]); north = atof(params->coord->answers[i + 1]); Vect_append_point(coords, east, north, 0.0); i += 2; } G_verbose_message(_("Threshold value for coordinates is %.2f"), thresh[THRESH_COORDS]); sel_by_coordinates(Map, type, coords, thresh[THRESH_COORDS], List); Vect_destroy_line_struct(coords); } /* select by bbox */ if (params->bbox->answer != NULL) { struct line_pnts *bbox; double x1, y1, x2, y2; bbox = Vect_new_line_struct(); x1 = atof(params->bbox->answers[0]); y1 = atof(params->bbox->answers[1]); x2 = atof(params->bbox->answers[2]); y2 = atof(params->bbox->answers[3]); Vect_append_point(bbox, x1, y1, -PORT_DOUBLE_MAX); Vect_append_point(bbox, x2, y1, PORT_DOUBLE_MAX); Vect_append_point(bbox, x2, y2, -PORT_DOUBLE_MAX); Vect_append_point(bbox, x1, y2, PORT_DOUBLE_MAX); Vect_append_point(bbox, x1, y1, -PORT_DOUBLE_MAX); /* sel_by_bbox not used */ /* sel_by_bbox(Map, type, x1, y1, x2, y2, List); */ sel_by_polygon(Map, type, bbox, List); Vect_destroy_line_struct(bbox); } /* select by polygon */ if (params->poly->answer != NULL) { int i; struct line_pnts *Polygon; Polygon = Vect_new_line_struct(); for (i = 0; params->poly->answers[i]; i += 2) { Vect_append_point(Polygon, atof(params->poly->answers[i]), atof(params->poly->answers[i + 1]), 0.0); } /* if first and last point of polygon does not match */ if (atof(params->poly->answers[i - 1]) != atof(params->poly->answers[0])) { Vect_append_point(Polygon, atof(params->poly->answers[0]), atof(params->poly->answers[1]), 0.0); } sel_by_polygon(Map, type, Polygon, List); Vect_destroy_line_struct(Polygon); } /* select by where statement */ if (params->where->answer != NULL) { sel_by_where(Map, layer, type, params->where->answer, List); } /* selecy by query */ if (params->query->answer != NULL) { int query_type; struct ilist *List_tmp; if (first_selection) { List_tmp = List; first_selection = 0; } else { List_tmp = Vect_new_list(); } query_type = QUERY_UNKNOWN; if (strcmp(params->query->answer, "length") == 0) { query_type = QUERY_LENGTH; } else if (strcmp(params->query->answer, "dangle") == 0) { query_type = QUERY_DANGLE; } G_verbose_message(_("Threshold value for querying is %.2f"), thresh[THRESH_QUERY]); Vedit_select_by_query(Map, type, layer, thresh[THRESH_QUERY], query_type, List_tmp); /* merge lists (only duplicate items) */ if (List_tmp != List) { merge_lists(List, List_tmp); Vect_destroy_list(List_tmp); } } if (params->reverse->answer) { reverse_selection(Map, type, &List); } G_message(_("%d of %d features selected from vector map <%s>"), List->n_values, Vect_get_num_lines(Map), Vect_get_full_name(Map)); return List; }