Skip to content

Commit 6cfe88e

Browse files
committed
save out to file
1 parent 3acd5b4 commit 6cfe88e

File tree

4 files changed

+384
-49
lines changed

4 files changed

+384
-49
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ CTestTestfile.cmake
1111
build
1212
includes
1313
.DS_Store
14-
test-files/out.json
14+
out

src/main.cpp

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,62 +47,85 @@ namespace {
4747
// Points points = {.x_vector = x_vector, .y_vector = y_vector};
4848
return coords;
4949
}
50+
51+
const char* serialize_to_json(Delaunator &delaunator) {
52+
rapidjson::StringBuffer sb;
53+
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
54+
writer.StartObject();
55+
writer.String("type"); writer.String("FeatureCollection");
56+
writer.String("crs");
57+
writer.StartObject();
58+
writer.String("type"); writer.String("name");
59+
writer.String("properties");
60+
writer.StartObject();
61+
writer.String("name"); writer.String("urn:ogc:def:crs:EPSG::900913");
62+
writer.EndObject();
63+
writer.EndObject();
64+
writer.String("features");
65+
writer.StartArray();
66+
for(long int i = 0; i < delaunator.triangles.size(); i+=3) {
67+
writer.StartObject();
68+
writer.String("type"); writer.String("Feature");
69+
writer.String("properties"); writer.StartObject(); writer.EndObject();
70+
writer.String("geometry");
71+
writer.StartObject();
72+
writer.String("type"); writer.String("Polygon");
73+
writer.String("coordinates");
74+
writer.StartArray();
75+
writer.StartArray();
76+
writer.StartArray();
77+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i]]);
78+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i] + 1]);
79+
writer.EndArray();
80+
writer.StartArray();
81+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 1]]);
82+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 1] + 1]);
83+
writer.EndArray();
84+
writer.StartArray();
85+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 2]]);
86+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 2] + 1]);
87+
writer.EndArray();
88+
writer.StartArray();
89+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i]]);
90+
writer.Uint(delaunator.coords[2 * delaunator.triangles[i] + 1]);
91+
writer.EndArray();
92+
writer.EndArray();
93+
writer.EndArray();
94+
writer.EndObject();
95+
writer.EndObject();
96+
}
97+
writer.EndArray();
98+
writer.EndObject();
99+
return sb.GetString();
100+
}
50101
}
51102

52103

53104
int main(int, char* argv[]) {
54105
const char* filename = argv[1];
106+
const char* output = argv[2];
55107
string json = read_file(filename);
56108
const vector<double> coords = get_geo_json_points(json);
57109
Delaunator delaunator(coords);
58-
rapidjson::StringBuffer sb;
59-
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
60-
writer.StartObject();
61-
writer.String("type"); writer.String("FeatureCollection");
62-
writer.String("crs");
63-
writer.StartObject();
64-
writer.String("type"); writer.String("name");
65-
writer.String("properties");
66-
writer.StartObject();
67-
writer.String("name"); writer.String("urn:ogc:def:crs:EPSG::900913");
68-
writer.EndObject();
69-
writer.EndObject();
70-
writer.String("features");
71-
writer.StartArray();
72-
for(long int i = 0; i < delaunator.triangles.size(); i+=3) {
73-
writer.StartObject();
74-
writer.String("type"); writer.String("Feature");
75-
writer.String("properties"); writer.StartObject(); writer.EndObject();
76-
writer.String("geometry");
77-
writer.StartObject();
78-
writer.String("type"); writer.String("Polygon");
79-
writer.String("coordinates");
80-
writer.StartArray();
81-
writer.StartArray();
82-
writer.StartArray();
83-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i]]);
84-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i] + 1]);
85-
writer.EndArray();
86-
writer.StartArray();
87-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 1]]);
88-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 1] + 1]);
89-
writer.EndArray();
90-
writer.StartArray();
91-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 2]]);
92-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i + 2] + 1]);
93-
writer.EndArray();
94-
writer.StartArray();
95-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i]]);
96-
writer.Uint(delaunator.coords[2 * delaunator.triangles[i] + 1]);
97-
writer.EndArray();
98-
writer.EndArray();
99-
writer.EndArray();
100-
writer.EndObject();
101-
writer.EndObject();
102-
}
103-
writer.EndArray();
104-
writer.EndObject();
105-
puts(sb.GetString());
110+
const char* out_json = serialize_to_json(delaunator);
111+
112+
if (output) {
113+
printf("Writing to file %s", output);
114+
ofstream stream;
115+
stream.open(output);
116+
stream << out_json;
117+
stream.close();
118+
// cout << output << endl;
119+
} else {
120+
puts(out_json);
121+
}
122+
123+
// cout << output << endl;
124+
// if (sizeof(argv) > 2) {
125+
// puts("ouput to file");
126+
// } else {
127+
// puts(out_json);
128+
// }
106129
return 0;
107130
// cout << delaunator.triangles << endl;
108131
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"type": "FeatureCollection",
3+
"name": "map-25p",
4+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
5+
"features": [
6+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1464686.336000547511503, 6917398.185751880519092 ] } },
7+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1479667.993544442113489, 6923360.273958121426404 ] } },
8+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1487617.444486100459471, 6904556.76499997638166 ] } },
9+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1524612.966176125919446, 6893702.70698347967118 ] } },
10+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1522472.729384140809998, 6916328.067355887033045 ] } },
11+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1506573.827500824118033, 6909448.734810220077634 ] } },
12+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1486088.703920396743342, 6888963.61122979503125 ] } },
13+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1500917.487407721113414, 6881931.404627557843924 ] } },
14+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1469731.179867369355634, 6898900.424906868487597 ] } },
15+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1502751.976086565293372, 6896913.062171457335353 ] } },
16+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1484101.34118498233147, 6877498.056987018324435 ] } },
17+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1463310.469491414260119, 6878568.175383010879159 ] } },
18+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1511771.545424216194078, 6883613.019249833188951 ] } },
19+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1489910.555334655800834, 6869395.731988795101643 ] } },
20+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1495261.147314618108794, 6909601.608866788446903 ] } },
21+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1465756.454396540066227, 6868937.109819080680609 ] } },
22+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1505656.583161402028054, 6871077.346611065790057 ] } },
23+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1501223.235520861810073, 6924277.518297546543181 ] } },
24+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1516204.893064756412059, 6908072.868301087059081 ] } },
25+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1460252.988360007293522, 6894161.329153183847666 ] } },
26+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1476916.260526175843552, 6867255.495196804404259 ] } },
27+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1497095.635993462288752, 6865115.25840481929481 ] } },
28+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1512841.663820208515972, 6919691.296600436791778 ] } },
29+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1496484.139767180895433, 6907767.120187944732606 ] } },
30+
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1476610.512413035146892, 6920914.289053001441061 ] } }
31+
]
32+
}

0 commit comments

Comments
 (0)