Exemplo n.º 1
0
void Command::print_arguments(const std::string& command) {
    if (m_vout.verbose()) {
        m_vout << "Started osmium " << command << '\n'
               << "  " << get_osmium_long_version() << '\n'
               << "  " << get_libosmium_version() << '\n'
               << "Command line options and default settings:\n";
        show_arguments();
    }
}
Exemplo n.º 2
0
Options::Options(int argc, char* argv[]) {
    static struct option long_options[] = {
        {"bbox-overlap",    required_argument, nullptr, 'b'},
        {"close-distance",  required_argument, nullptr, 'c'},
        {"no-index",              no_argument, nullptr, 'i'},
        {"debug",                 no_argument, nullptr, 'd'},
        {"help",                  no_argument, nullptr, 'h'},
        {"output-lines",          no_argument, nullptr, 'l'},
        {"max-points",      required_argument, nullptr, 'm'},
        {"output-database", required_argument, nullptr, 'o'},
        {"output-polygons", required_argument, nullptr, 'p'},
        {"output-rings",          no_argument, nullptr, 'r'},
        {"overwrite",             no_argument, nullptr, 'f'},
        {"srs",             required_argument, nullptr, 's'},
        {"write-segments",  required_argument, nullptr, 'S'},
        {"verbose",               no_argument, nullptr, 'v'},
        {"version",               no_argument, nullptr, 'V'},
        {nullptr,                           0, nullptr, 0}
    };

    while (true) {
        const int c = getopt_long(argc, argv, "b:c:idhlm:o:p:rfs:S:vV", long_options, nullptr);
        if (c == -1) {
            break;
        }

        switch (c) {
            case 'b':
                bbox_overlap = std::atof(optarg); // NOLINT(cert-err34-c) atof is good enough for this use case
                break;
            case 'c':
                close_distance = std::atoi(optarg); // NOLINT(cert-err34-c) atoi is good enough for this use case
                if (close_distance == 0) {
                    close_rings = false;
                }
                break;
            case 'i':
                create_index = false;
                break;
            case 'd':
                debug = true;
                std::cerr << "Enabled debug option\n";
                break;
            case 'h':
                print_help();
                std::exit(return_code_ok);
            case 'l':
                output_lines = true;
                break;
            case 'm':
                max_points_in_polygon = std::atoi(optarg); // NOLINT(cert-err34-c) atoi is good enough for this use case
                if (max_points_in_polygon == 0) {
                    split_large_polygons = false;
                }
                break;
            case 'p':
                if (!std::strcmp(optarg, "none")) {
                    output_polygons = output_polygon_type::none;
                } else if (!std::strcmp(optarg, "land")) {
                    output_polygons = output_polygon_type::land;
                } else if (!std::strcmp(optarg, "water")) {
                    output_polygons = output_polygon_type::water;
                } else if (!std::strcmp(optarg, "both")) {
                    output_polygons = output_polygon_type::both;
                } else {
                    std::cerr << "Unknown argument '" << optarg << "' for -p/--output-polygon option\n";
                    std::exit(return_code_cmdline);
                }
                break;
            case 'o':
                output_database = optarg;
                break;
            case 'r':
                output_rings = true;
                break;
            case 'f':
                overwrite_output = true;
                break;
            case 's':
                epsg = get_epsg(optarg);
                break;
            case 'S':
                segmentfile = optarg;
                break;
            case 'v':
                verbose = true;
                break;
            case 'V':
                std::cout << "osmcoastline " << get_osmcoastline_long_version() << " / " << get_libosmium_version() << '\n'
                          << "Copyright (C) 2012-2019  Jochen Topf <*****@*****.**>\n"
                          << "License: GNU GENERAL PUBLIC LICENSE Version 3 <https://gnu.org/licenses/gpl.html>.\n"
                          << "This is free software: you are free to change and redistribute it.\n"
                          << "There is NO WARRANTY, to the extent permitted by law.\n";
                std::exit(return_code_ok);
            default:
                std::exit(return_code_cmdline);
        }
    }

    if (!split_large_polygons && (output_polygons == output_polygon_type::water || output_polygons == output_polygon_type::both)) {
        std::cerr << "Can not use -m/--max-points=0 when writing out water polygons\n";
        std::exit(return_code_cmdline);
    }

    if (optind != argc - 1) {
        std::cerr << "Usage: osmcoastline [OPTIONS] OSMFILE\n";
        std::exit(return_code_cmdline);
    }

    if (output_database.empty()) {
        std::cerr << "Missing --output-database/-o option.\n";
        std::exit(return_code_cmdline);
    }

    if (bbox_overlap == -1) {
        if (epsg == 4326) {
            bbox_overlap = 0.0001;
        } else {
            bbox_overlap = 10;
        }
    }

    inputfile = argv[optind];
}
Exemplo n.º 3
0
int main(int argc, char *argv[]) {
#ifdef _WIN32
    _setmode(1, _O_BINARY);
#endif

    std::string command = argv[0];

    // remove path from command
    // (backslash for windows, slash for everybody else)
    if (command.find_last_of("/\\") != std::string::npos) {
        command = command.substr(command.find_last_of("/\\") + 1);
    }

    std::vector<std::string> arguments;

    for (int i = 1; i < argc; ++i) {
        arguments.push_back(argv[i]);
    }

    if (command == "osmium" || command == "osmium.exe") {
        if (arguments.size() == 0) {
            command = "help";
        } else {
            if (arguments.front() == "--help" || arguments.front() == "-h") {
                command = "help";
            } else if (arguments.front() == "--version") {
                command = "version";
            } else {
                command = arguments.front();
            }
            arguments.erase(arguments.begin());
        }
    } else {
        if (command.substr(0, 7) == "osmium-") {
            command = command.substr(7);
        }
    }

    if (command == "version") {
        std::cout << get_osmium_long_version() << '\n'
                  << get_libosmium_version() << '\n'
                  << "Copyright (C) 2013-2016  Jochen Topf <*****@*****.**>\n"
                  << "License: GNU GENERAL PUBLIC LICENSE Version 3 <http://gnu.org/licenses/gpl.html>.\n"
                  << "This is free software: you are free to change and redistribute it.\n"
                  << "There is NO WARRANTY, to the extent permitted by law.\n";

        return return_code::okay;
    }

    std::unique_ptr<Command> cmd = CommandFactory::instance().create_command(command);

    if (!cmd) {
        std::cerr << "Unknown command or option '" << command << "'. Try 'osmium help'.\n";
        return return_code::fatal;
    }

    try {
        if (!cmd->setup(arguments)) {
            return return_code::okay;
        }
    } catch (boost::program_options::error& e) {
        std::cerr << "Error parsing command line: " << e.what() << std::endl;
        return return_code::fatal;
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
        return return_code::fatal;
    }

    cmd->print_arguments(command);

    try {
        if (cmd->run()) {
            return return_code::okay;
        }
    } catch (std::bad_alloc& e) {
        std::cerr << "Out of memory. Read the MEMORY USAGE section of the osmium(1) manpage.\n";
    } catch (std::exception& e) {
        std::cerr << e.what() << "\n";
    }

    return return_code::error;
}
Exemplo n.º 4
0
int main(int argc, char* argv[]) {
    std::string output_filename;
    bool verbose = false;

    static struct option long_options[] = {
        {"help",         no_argument, nullptr, 'h'},
        {"output", required_argument, nullptr, 'o'},
        {"verbose",      no_argument, nullptr, 'v'},
        {"version",      no_argument, nullptr, 'V'},
        {nullptr,                  0, nullptr, 0}
    };

    while (true) {
        const int c = getopt_long(argc, argv, "ho:vV", long_options, nullptr);
        if (c == -1) {
            break;
        }

        switch (c) {
            case 'h':
                print_help();
                std::exit(return_code_ok);
            case 'o':
                output_filename = optarg;
                break;
            case 'v':
                verbose = true;
                break;
            case 'V':
                std::cout << "osmcoastline_filter " << get_osmcoastline_long_version() << " / " << get_libosmium_version() << '\n'
                          << "Copyright (C) 2012-2019  Jochen Topf <*****@*****.**>\n"
                          << "License: GNU GENERAL PUBLIC LICENSE Version 3 <https://gnu.org/licenses/gpl.html>.\n"
                          << "This is free software: you are free to change and redistribute it.\n"
                          << "There is NO WARRANTY, to the extent permitted by law.\n";
                std::exit(return_code_ok);
            default:
                std::exit(return_code_fatal);
        }
    }

    if (output_filename.empty()) {
        std::cerr << "Missing -o/--output=OSMFILE option\n";
        std::exit(return_code_cmdline);
    }

    if (optind != argc - 1) {
        std::cerr << "Usage: osmcoastline_filter [OPTIONS] OSMFILE\n";
        std::exit(return_code_cmdline);
    }

    try {
        // The vout object is an output stream we can write to instead of
        // std::cerr. Nothing is written if we are not in verbose mode.
        // The running time will be prepended to output lines.
        osmium::util::VerboseOutput vout{verbose};

        osmium::io::Header header;
        header.set("generator", std::string{"osmcoastline_filter/"} + get_osmcoastline_version());
        header.add_box(osmium::Box{-180.0, -90.0, 180.0, 90.0});

        osmium::io::File infile{argv[optind]};

        vout << "Started osmcoastline_filter " << get_osmcoastline_long_version() << " / " << get_libosmium_version() << '\n';

        osmium::io::Writer writer{output_filename, header};
        auto output_it = osmium::io::make_output_iterator(writer);

        osmium::index::IdSetSmall<osmium::object_id_type> ids;

        vout << "Reading ways (1st pass through input file)...\n";
        {
            osmium::io::Reader reader{infile, osmium::osm_entity_bits::way};
            const auto ways = osmium::io::make_input_iterator_range<const osmium::Way>(reader);
            for (const osmium::Way& way : ways) {
                if (way.tags().has_tag("natural", "coastline")) {
                    *output_it++ = way;
                    for (const auto& nr : way.nodes()) {
                        ids.set(nr.ref());
                    }
                }
            }
            reader.close();
        }

        vout << "Preparing node ID list...\n";
        ids.sort_unique();

        vout << "Reading nodes (2nd pass through input file)...\n";
        {
            osmium::io::Reader reader{infile, osmium::osm_entity_bits::node};
            const auto nodes = osmium::io::make_input_iterator_range<const osmium::Node>(reader);

            auto first = ids.cbegin();
            const auto last = ids.cend();
            std::copy_if(nodes.cbegin(), nodes.cend(), output_it, [&first, &last](const osmium::Node& node){
                while (*first < node.id() && first != last) {
                    ++first;
                }

                if (node.id() == *first) {
                    if (first != last) {
                        ++first;
                    }
                    return true;
                }

                return node.tags().has_tag("natural", "coastline");
            });

            reader.close();
        }

        writer.close();

        vout << "All done.\n";
        osmium::MemoryUsage mem;
        if (mem.current() > 0) {
            vout << "Memory used: current: " << mem.current() << " MBytes\n"
                << "             peak:    " << mem.peak() << " MBytes\n";
        }
    } catch (const std::exception& e) {
        std::cerr << e.what() << '\n';
        std::exit(return_code_fatal);
    }
}