|
1 | 1 | #include "rapidjson/document.h" |
2 | 2 | #include "rapidjson/prettywriter.h" |
3 | 3 | #include "delaunator.h" |
| 4 | +#include "json-helpers.h" |
4 | 5 | #include <cstdio> |
5 | 6 | #include <fstream> |
6 | | -#include <string> |
7 | | -#include <exception> |
8 | 7 | #include <vector> |
9 | 8 | #include <initializer_list> |
10 | 9 | // #include "prettyprint.hpp" |
11 | 10 | #include <iostream> |
12 | 11 | using namespace std; |
13 | 12 |
|
14 | 13 | namespace { |
15 | | - string read_file(const char* filename) { |
16 | | - ifstream input_file(filename); |
17 | | - if(input_file.good()) { |
18 | | - string json_str( |
19 | | - (istreambuf_iterator<char>(input_file)), |
20 | | - istreambuf_iterator<char>() |
21 | | - ); |
22 | | - return json_str; |
23 | | - } else { |
24 | | - printf("Error reading file %s", filename); |
25 | | - throw exception(); |
26 | | - } |
27 | | - } |
28 | | - |
29 | | - vector<double> get_geo_json_points(const string& json) { |
30 | | - rapidjson::Document document; |
31 | | - if(document.Parse(json.c_str()).HasParseError()) { |
32 | | - fprintf(stderr, "Cannot parse JSON"); |
33 | | - throw exception(); |
34 | | - } |
35 | | - const rapidjson::Value& features = document["features"]; |
36 | | - vector<double> coords; |
37 | | - // vector<double> y_vector; |
38 | | - for(rapidjson::SizeType i = 0; i < features.Size(); i++) { |
39 | | - const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"]; |
40 | | - const double x = coordinates[0].GetDouble(); |
41 | | - const double y = coordinates[1].GetDouble(); |
42 | | - coords.push_back(x); |
43 | | - coords.push_back(y); |
44 | | - } |
45 | | - return coords; |
46 | | - } |
47 | 14 |
|
48 | 15 | const string serialize_to_json(Delaunator &delaunator) { |
49 | 16 | rapidjson::StringBuffer sb; |
@@ -101,8 +68,8 @@ namespace { |
101 | 68 | int main(int, char* argv[]) { |
102 | 69 | const char* filename = argv[1]; |
103 | 70 | const char* output = argv[2]; |
104 | | - string json = read_file(filename); |
105 | | - const vector<double> coords = get_geo_json_points(json); |
| 71 | + string json = json_helpers::read_file(filename); |
| 72 | + const vector<double> coords = json_helpers::get_geo_json_points(json); |
106 | 73 | Delaunator delaunator(move(coords)); |
107 | 74 | const char* out_json = serialize_to_json(delaunator).c_str(); |
108 | 75 |
|
|
0 commit comments