1/*
2 * Copyright (c) 2014-2023 National Technology and Engineering
3 * Solutions of Sandia, LLC. Under the terms of Contract DE-NA0003525
4 * with National Technology and Engineering Solutions of Sandia, LLC,
5 * the U.S. Government retains certain rights in this software.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <tracktable/CommandLineFactories/AssemblerFromCommandLine.h>
32#include <tracktable/CommandLineFactories/PointReaderFromCommandLine.h>
33#include <tracktable/Domain/Terrestrial.h>
34#include <tracktable/RW/TrajectoryWriter.h>
35
36#include <fstream>
37
38using TrajectoryT = tracktable::domain::terrestrial::trajectory_type;
39using PointT = typename TrajectoryT::point_type;
40using PointReaderT = tracktable::PointReader<PointT>;
41using PointReaderIteratorT = typename PointReaderT::iterator;
42using AssemblerT = tracktable::AssembleTrajectories<TrajectoryT, PointReaderIteratorT>;
43
44static constexpr auto helpmsg = R"(
45--------------------------------------------------------------------------------
46The assemble example demonstrates:
47 - Using command line factories to read points and assemble trajectories
48 - Writing trajectories to file for later use
49
50Typical use:
51 ./assemble --input=/data/flights.tsv --output=/data/flights.trj
52
53Defaults assume a tab separated file formatted as :
54
55OBJECTID TIMESTAMP LON LAT
56
57Default output is standard out
58--------------------------------------------------------------------------------)";
59
60int main(int _argc, char* _argv[]) {
61 //Set log level to reduce unecessary output
62 tracktable::set_log_level(tracktable::log::info);
63 // Create a basic command line option with boost
64 boost::program_options::options_description commandLineOptions;
65 commandLineOptions.add_options()("help", "Print help");
66
67 // Create command line factories
68 tracktable::PointReaderFromCommandLine<PointT> readerFactory;
69 tracktable::AssemblerFromCommandLine<TrajectoryT> assemblerFactory;
70 // Add options from the factories
71 readerFactory.addOptions(commandLineOptions);
72 assemblerFactory.addOptions(commandLineOptions);
73
74 // And a command line option for output
75 commandLineOptions.add_options()("output", bpo::value<std::string>()->default_value("-"),
76 "file to write to (use '-' for stdout), overridden by 'separate-kmls'");
77
78 /** Boost program options using a variable map to tie everything together.
79 * one parse will have a single variable map. We need to let the factories know
80 * about this variable map so they can pull information out of it */
81 auto vm = std::make_shared<boost::program_options::variables_map>();
82 readerFactory.setVariables(vm);
83 assemblerFactory.setVariables(vm);
84
85 // Parse the command lines, don't forget the 'notify' after
86 try {
87 // We use this try/catch to automatically display help when an unknown option is used
88 boost::program_options::store(
89 boost::program_options::command_line_parser(_argc, _argv).options(commandLineOptions).run(), *vm);
90 boost::program_options::notify(*vm);
91 } catch (boost::program_options::error e) {
92 std::cerr << e.what();
93 std::cerr << helpmsg << "\n\n";
94 std::cerr << commandLineOptions << std::endl;
95 return 1;
96 }
97 /** Parsing will give an error of an incorrect option is used, but it won't
98 * display the help unless we tell it too */
99 if (vm->count("help") != 0) {
100 std::cerr << helpmsg << "\n\n";
101 std::cerr << commandLineOptions << std::endl;
102 return 1;
103 }
104
105 // Create Point Reader and assembler
106 auto pointReader = readerFactory.createPointReader();
107 auto assembler = assemblerFactory.createAssembler(pointReader);
108
109 // We default to standard out
110 std::ostream* o = &std::cout;
111 auto filename = (*vm)["output"].as<std::string>();
112 std::cerr << "Writing to ";
113 if ("-" != filename) {
114 // we swap in a file if a filename is specified
115 std::cerr << filename << std::endl;
116 o = new std::ofstream(filename);
117 } else {
118 std::cerr << "standard out" << std::endl;
119 }
120
121 // trajectory writer with default options
122 tracktable::TrajectoryWriter writer(*o);
123
124 auto count = 0u;
125 std::cerr << std::right;
126 // We don't need to bother storing trajectories, we can just write them
127 for (auto t = assembler->begin(); t != assembler->end(); ++t) {
128 writer.write(*t);
129 std::cerr << "\b\b\b\b\b\b\b\b\b\b" << std::setw(10) // Using backspaces for in place counter
130 << count++;
131 }
132 std::cerr << std::endl;
133 return 0;
134}