int main(int argc, char *argv[]) { struct Map_info in, out, vis; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *input, *output; /* The input map */ struct Option *coor, *ovis; char *mapset; struct Point *points; struct Line *lines; int num_points, num_lines; int n = 0; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); module->keywords = _("vector, path, visibility"); module->description = _("Visibility graph construction."); /* define the arguments needed */ input = G_define_standard_option(G_OPT_V_INPUT); output = G_define_standard_option(G_OPT_V_OUTPUT); coor = G_define_option(); coor->key = "coordinate"; coor->key_desc = "x,y"; coor->type = TYPE_STRING; coor->required = NO; coor->multiple = YES; coor->description = _("One or more coordinates"); ovis = G_define_option(); ovis->key = "vis"; ovis->type = TYPE_STRING; ovis->required = NO; ovis->description = _("Add points after computing the vis graph"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); Vect_check_input_output_name(input->answer, output->answer, GV_FATAL_EXIT); Vect_set_open_level(2); mapset = G_find_vector2(input->answer, NULL); /* finds the map */ if (mapset == NULL) G_fatal_error("Vector map <%s> not found", input->answer); if (Vect_open_old(&in, input->answer, mapset) < 1) /* opens the map */ G_fatal_error(_("Unable to open vector map <%s>"), G_fully_qualified_name(input->answer, mapset)); if (Vect_open_new(&out, output->answer, WITHOUT_Z) < 0) { Vect_close(&in); G_fatal_error(_("Unable to create vector map <%s>"), output->answer); } if (ovis->answer != NULL) { mapset = G_find_vector2(ovis->answer, NULL); if (Vect_open_old(&vis, ovis->answer, mapset) < 1) G_fatal_error(_("Unable to open vector map <%s>"), G_fully_qualified_name(ovis->answer, mapset)); if (Vect_copy_map_lines(&vis, &out) > 0) G_fatal_error(_("Unable to copy elements from vector map <%s>"), G_fully_qualified_name(ovis->answer, mapset)); } if (G_projection() == PROJECTION_LL) G_warning(_("Lat-long projection")); /* counting how many points and lines we have to allocate */ count(&in, &num_points, &num_lines); /* modify the number if we have new points to add */ if (coor->answers != NULL) num_points += count_new(coor->answers); /* and allocate */ points = G_malloc(num_points * sizeof(struct Point)); lines = G_malloc(num_lines * sizeof(struct Line)); /* and finally set the lines */ load_lines(&in, &points, &num_points, &lines, &num_lines); if (coor->answers != NULL) add_points(coor->answers, &points, &num_points); if (ovis->answer == NULL) construct_visibility(points, num_points, lines, num_lines, &out); else visibility_points(points, num_points, lines, num_lines, &out, n); G_free(points); G_free(lines); Vect_build(&out); Vect_close(&out); Vect_close(&in); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct Map_info in, out, vis; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *input, *output; /* The input map */ struct Option *coor, *ovis; struct Point *points; struct Line *lines; int num_points, num_lines; int n = 0; /* 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(_("shortest path")); G_add_keyword(_("visibility")); module->description = _("Performs visibility graph construction."); /* define the arguments needed */ input = G_define_standard_option(G_OPT_V_INPUT); output = G_define_standard_option(G_OPT_V_OUTPUT); coor = G_define_standard_option(G_OPT_M_COORDS); ovis = G_define_standard_option(G_OPT_V_MAP); ovis->key = "visibility"; ovis->required = NO; ovis->label = _("Name of input vector map containing visible points"); ovis->description = _("Add points after computing the visibility graph"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); Vect_check_input_output_name(input->answer, output->answer, G_FATAL_EXIT); Vect_set_open_level(2); if (Vect_open_old(&in, input->answer, "") < 1) /* opens the map */ G_fatal_error(_("Unable to open vector map <%s>"), input->answer); if (Vect_open_new(&out, output->answer, WITHOUT_Z) < 0) { Vect_close(&in); G_fatal_error(_("Unable to create vector map <%s>"), output->answer); } if (ovis->answer != NULL) { if (Vect_open_old(&vis, ovis->answer, "") < 1) G_fatal_error(_("Unable to open vector map <%s>"), ovis->answer); if (Vect_copy_map_lines(&vis, &out) > 0) G_fatal_error(_("Unable to copy elements from vector map <%s>"), ovis->answer); } if (G_projection() == PROJECTION_LL) G_warning(_("Lat-long projection")); /* counting how many points and lines we have to allocate */ count(&in, &num_points, &num_lines); /* modify the number if we have new points to add */ if (coor->answers != NULL) num_points += count_new(coor->answers); /* and allocate */ points = G_malloc(num_points * sizeof(struct Point)); lines = G_malloc(num_lines * sizeof(struct Line)); /* and finally set the lines */ load_lines(&in, &points, &num_points, &lines, &num_lines); if (coor->answers != NULL) add_points(coor->answers, &points, &num_points); if (ovis->answer == NULL) construct_visibility(points, num_points, lines, num_lines, &out); else visibility_points(points, num_points, lines, num_lines, &out, n); G_free(points); G_free(lines); Vect_copy_head_data(&in, &out); Vect_hist_copy(&in, &out); Vect_hist_command(&out); Vect_build(&out); Vect_close(&out); Vect_close(&in); exit(EXIT_SUCCESS); }