-
Notifications
You must be signed in to change notification settings - Fork 8
/
gausskruger_cli.cpp
142 lines (127 loc) · 5.51 KB
/
gausskruger_cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Erik Lundin 2016.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <boost/program_options.hpp>
#include "gausskruger.h"
using namespace gausskruger;
namespace po = boost::program_options;
#define EXE_NAME "gausskruger"
class ParameterProjection : public Projection
{
public:
ParameterProjection(double flattening, double equatorialRadius, double centralMeridian,
double scale, double falseNorthing, double falseEasting) :
mFlattening(flattening),
mEquatorialRadius(equatorialRadius),
mCentralMeridian(centralMeridian),
mScale(scale),
mFalseNorthing(falseNorthing),
mFalseEasting(falseEasting) {}
double flattening() { return mFlattening; }
double equatorialRadius() { return mEquatorialRadius; }
double centralMeridian() { return mCentralMeridian; }
double scale() { return mScale; }
double falseNorthing() { return mFalseNorthing; }
double falseEasting() { return mFalseEasting; }
private:
double mFlattening;
double mEquatorialRadius;
double mCentralMeridian;
double mScale;
double mFalseNorthing;
double mFalseEasting;
};
int main(int argc, char *argv[])
{
double inverseFlattening;
double equatorialRadius;
double centralMeridian;
double scale;
double falseNorthing;
double falseEasting;
std::vector<double> coords;
int nDecimals;
try {
// Projection parameters
po::options_description projection_parameters("Projection parameters (mandatory)");
projection_parameters.add_options()
("invflattening,i", po::value<double>(&inverseFlattening),
"inverse flattening of the ellipsoid")
("radius,a", po::value<double>(&equatorialRadius),
"equatorial radius, a.k.a. semi-major axis of the ellipsoid")
("meridian,m", po::value<double>(¢ralMeridian),
"longitude of the central meridian")
("scale,s", po::value<double>(&scale),
"scale factor along the central meridian")
("falsenorthing,n", po::value<double>(&falseNorthing),
"false northing")
("falseeasting,e", po::value<double>(&falseEasting),
"false easting")
;
// Options
po::options_description options("Options");
options.add_options()
("help,h", "print this help")
("decimals,d", po::value<int>(&nDecimals)->default_value(3), "number of decimals")
("reverse,r",
"reverse transformation (grid to geodetic), default is geodetic to grid")
;
// Positional parameters (input coordinates)
po::options_description hidden_parameters;
hidden_parameters.add_options()
("coords", po::value<std::vector<double> >(&coords))
;
po::positional_options_description positional_parameters;
positional_parameters.add("coords", 2);
// Store the parameters
po::options_description all_parameters;
all_parameters.add(options).add(projection_parameters).add(hidden_parameters);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(all_parameters).positional(positional_parameters).run(), vm);
po::notify(vm);
// Print help (if requested or no other options is given)
po::options_description visible_parameters;
visible_parameters.add(projection_parameters).add(options);
if (argc == 1 || vm.count("help")) {
std::cout << "Usage: " << EXE_NAME << " <projection> [options] latitude longitude\n"
<< " " << EXE_NAME << " <projection> [options] -r northing easting\n"
<< visible_parameters << std::endl;
return 1;
}
// Check for all mandatory parameters
if (!(vm.count("invflattening") && vm.count("radius") && vm.count("meridian") && vm.count("scale")
&& vm.count("falsenorthing") && vm.count("falseeasting"))) {
std::cout << "Missing mandatory projection parameter(s)" << std::endl;
return 2;
}
if (coords.size() != 2) {
std::cerr << "Exactly two coordinate values have to be entered" << std::endl;
return 3;
}
// Set number of decimals in output
std::cout.setf(std::ios::fixed);
std::cout.precision(nDecimals);
// Do the actual transformation
ParameterProjection projection(1 / inverseFlattening, equatorialRadius,
centralMeridian, scale, falseNorthing, falseEasting);
if (vm.count("reverse")) {
double lat, lon;
projection.gridToGeodetic(coords.at(0), coords.at(1), lat, lon);
std::cout << std::fixed << "Latitude: " << lat << "\nLongitude: " << lon << std::endl;
} else {
double northing, easting;
projection.geodeticToGrid(coords.at(0), coords.at(1), northing, easting);
std::cout << std::fixed << "Northing: " << northing << "\nEasting: " << easting << std::endl;
}
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 4;
}
return 0;
}