-
Notifications
You must be signed in to change notification settings - Fork 8
/
ElevationVectors.cpp
87 lines (67 loc) · 2.37 KB
/
ElevationVectors.cpp
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
#include <ElevationVectors.h>
#include <biome.h>
#include <sstream>
#include <fstream>
// this file is basically the same as biome vectors. They should be merged.
ElevationVectors::ElevationVectors(MinecraftWorld &world)
: y_resolution(world.y_resolution)
{
/* empty */
}
void ElevationVectors::add_chunk(int chunkx, int chunkz, Grid16 elevation)
{
for(int x=0; x<16; x++) {
for(int z=0; z<16; z++) {
auto e = elevation[x][z];
int xl = (chunkx*16+x);
int zl = (chunkz*16+z);
int xh = xl + 1;
int zh = zl + 1;
add_rectangle_to_polygon_set(polysets[e/y_resolution], xl, zl, xh, zh);
}
}
}
void ElevationVectors::write(std::string filename)
{
nlohmann::json top;
top["type"] = "FeatureCollection";
PolygonHolesSet prev;
for(auto iter = polysets.rbegin(); iter!=polysets.rend(); iter++) {
//for(auto ps : polysets) {
int elevation = (*iter).first;
// we're going from upper elevations on down. each layer, merge in the polys from above.
PolygonHolesSet &ps = (*iter).second;
PolygonHolesSet merged;
#if 0
prev.insert(prev.end(), ps.begin(), ps.end());
std::cout << "for elevation: " << elevation << " we have " << prev.size() << " going in and ";
gtl::assign(merged, prev);
// can't I just assign above?
prev = merged;
std::cout << merged.size() << " coming out\n" << std::flush;
#else
std::cout << "for elevation: " << elevation << " we have " << ps.size() << " going in and ";
gtl::assign(merged, ps);
std::cout << merged.size() << " coming out\n" << std::flush;
#endif
for (auto poly : merged) {
// if a poly simplifies to nothing.
#if 0
std::optional<nlohmann::json> coords = polygon_to_json_simplified(poly);
#else
std::optional<nlohmann::json> coords = polygon_to_json(poly);
#endif
if (!coords) { continue; }
nlohmann::json feature;
feature["type"] = "Feature";
feature["properties"]["elevation"] = elevation;
feature["geometry"]["type"] = "Polygon";
feature["geometry"]["coordinates"] = *coords;
top["features"].push_back(feature);
}
}
std::ofstream file;
file.open(filename);
file << top.dump(2);
file.close();
}