-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_reader.hpp
87 lines (68 loc) · 1.73 KB
/
point_reader.hpp
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
#ifndef POINT_READER_HPP
#define POINT_READER_HPP
#include "point.hpp"
#include "gml_check.hpp"
#include <tinyxml2/tinyxml2.h>
#include <boost/assert.hpp>
#include <tuple>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
class point_reader
{
public:
class point_reader_exception : public std::exception
{
public:
point_reader_exception(const std::string& msg)
: msg(msg)
{
}
virtual const char* what() const noexcept
{
return msg.c_str();
}
std::string msg;
};
point_reader(std::istream& input)
: input(input)
{
}
std::vector<point> read()
{
std::vector<point> points;
std::string current_line;
while (std::getline(input, current_line))
{
points.emplace_back(parse_point(current_line));
}
return points;
}
private:
point parse_point(const std::string& input_line)
{
point p;
p.line_id = point::NO_LINE_ID;
auto pos = input_line.find(':');
std::string input_id = input_line.substr(0, pos);
p.id = std::stoi(input_id);
tinyxml2::XMLDocument doc;
doc.Parse(input_line.c_str() + pos + 1);
std::stringstream coordinates_stream;
coordinates_stream << doc.RootElement()->FirstChild()->FirstChild()->ToText()->Value();
gml_check(doc.RootElement(), "gml:Point");
double x;
double y;
char delimiter;
if (coordinates_stream >> x >> delimiter >> y)
{
BOOST_ASSERT(delimiter == ',');
p.location = coordinate {x, y};
}
return p;
}
private:
std::istream& input;
};
#endif