-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_reader.hpp
69 lines (53 loc) · 1.4 KB
/
line_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
#ifndef LINE_READER_HPP
#define LINE_READER_HPP
#include "poly_line.hpp"
#include "gml_check.hpp"
#include <tinyxml2/tinyxml2.h>
#include <boost/assert.hpp>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
class line_reader
{
public:
line_reader(std::istream& input)
: input(input)
{
}
std::vector<poly_line> read()
{
std::vector<poly_line> lines;
std::string current_line;
while (std::getline(input, current_line))
{
lines.emplace_back(parse_line(current_line));
}
return lines;
}
private:
poly_line parse_line(const std::string& input_line)
{
poly_line l;
auto pos = input_line.find(':');
std::string input_id = input_line.substr(0, pos);
l.id = std::stoi(input_id);
tinyxml2::XMLDocument doc;
doc.Parse(input_line.c_str() + pos + 1);
gml_check(doc.RootElement(), "gml:LineString");
std::stringstream coordinates_stream;
coordinates_stream << doc.RootElement()->FirstChild()->FirstChild()->ToText()->Value();
double x;
double y;
char delimiter;
while (coordinates_stream >> x >> delimiter >> y)
{
BOOST_ASSERT(delimiter == ',');
l.coordinates.emplace_back(coordinate {x, y});
}
return l;
}
private:
std::istream& input;
};
#endif