forked from abellgithub/delaunator-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.hpp
74 lines (65 loc) · 2.14 KB
/
utils.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
#pragma once
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "rapidjson/document.h"
namespace utils {
inline std::string read_file(const char* filename) {
std::ifstream input_file(filename);
if(input_file.good()) {
std::string json_str(
(std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>()
);
return json_str;
} else {
printf("Error reading file %s", filename);
throw std::runtime_error("Error reading file");
}
}
inline std::vector< double> get_geo_json_points(std::string const& json) {
rapidjson::Document document;
if(document.Parse(json.c_str()).HasParseError()) {
throw std::runtime_error("Cannot parse JSON");
}
const rapidjson::Value& features = document["features"];
std::vector<double> coords;
// vector<double> y_vector;
for(rapidjson::SizeType i = 0; i < features.Size(); i++) {
const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"];
const double x = coordinates[0].GetDouble();
const double y = coordinates[1].GetDouble();
coords.push_back(x);
coords.push_back(y);
}
return coords;
}
template <typename T>
inline T get_json_value(const rapidjson::Value &v);
template <>
inline double get_json_value(const rapidjson::Value &v) {
return v.GetDouble();
}
template <>
inline size_t get_json_value(const rapidjson::Value &v) {
return static_cast<size_t>(v.GetUint64());
}
template <typename T>
inline std::vector<T> get_array_points(std::string const& json) {
std::vector<T> points;
rapidjson::Document document;
if(document.Parse(json.c_str()).HasParseError()) {
throw std::runtime_error("Cannot parse JSON");
}
if(!document.IsArray()) {
throw std::runtime_error("It's not JSON Array");
}
points.reserve(static_cast<std::size_t>(document.Size()));
for(rapidjson::SizeType i = 0; i < document.Size(); i++) {
// points.push_back(document[i].GetDouble());
points.push_back(get_json_value<T>(document[i]));
}
return points;
}
} // end ns utils