forked from systemed/tilemaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_object.cpp
236 lines (202 loc) · 6.8 KB
/
output_object.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
OutputObject - any object (node, linestring, polygon) to be outputted to tiles
*/
#include "output_object.h"
#include "helpers.h"
#include <iostream>
using namespace std;
namespace geom = boost::geometry;
// **********************************************************
std::ostream& operator<<(std::ostream& os, OutputGeometryType geomType)
{
switch(geomType) {
case POINT_:
os << "POINT";
break;
case LINESTRING_:
os << "LINESTRING";
break;
case MULTILINESTRING_:
os << "MULTILINESTRING";
break;
case POLYGON_:
os << "POLYGON";
break;
}
return os;
}
// Write attribute key/value pairs (dictionary-encoded)
void OutputObject::writeAttributes(
vector<string> *keyList,
vector<vector_tile::Tile_Value> *valueList,
vector_tile::Tile_Feature *featurePtr,
char zoom) const {
for(auto const &it: attributes->values) {
if (it.minzoom > zoom) continue;
// Look for key
std::string const &key = it.key;
auto kt = find(keyList->begin(), keyList->end(), key);
if (kt != keyList->end()) {
uint32_t subscript = kt - keyList->begin();
featurePtr->add_tags(subscript);
} else {
uint32_t subscript = keyList->size();
keyList->push_back(key);
featurePtr->add_tags(subscript);
}
// Look for value
vector_tile::Tile_Value const &value = it.value;
int subscript = findValue(valueList, value);
if (subscript>-1) {
featurePtr->add_tags(subscript);
} else {
uint32_t subscript = valueList->size();
valueList->push_back(value);
featurePtr->add_tags(subscript);
}
//if(value.has_string_value())
// std::cout << "Write attr: " << key << " " << value.string_value() << std::endl;
}
}
Geometry buildWayGeometry(OSMStore &osmStore, OutputObject const &oo, const TileBbox &bbox)
{
switch(oo.geomType) {
case POINT_:
{
auto p = osmStore.retrieve_point((oo.objectID >> OSMID_TYPE_OFFSET) > 0 ? osmStore.osm() : osmStore.shp(), oo.objectID);
if (geom::within(p, bbox.clippingBox)) {
return p;
}
return MultiLinestring();
}
case LINESTRING_:
{
auto const &ls = osmStore.retrieve_linestring((oo.objectID >> OSMID_TYPE_OFFSET) > 0 ? osmStore.osm() : osmStore.shp(), oo.objectID);
MultiLinestring out;
if(ls.empty())
return out;
Linestring current_ls;
geom::append(current_ls, ls[0]);
for(size_t i = 1; i < ls.size(); ++i) {
if(!geom::intersects(Linestring({ ls[i-1], ls[i] }), bbox.clippingBox)) {
if(current_ls.size() > 1)
out.push_back(std::move(current_ls));
current_ls.clear();
}
geom::append(current_ls, ls[i]);
}
if(current_ls.size() > 1)
out.push_back(std::move(current_ls));
MultiLinestring result;
geom::intersection(out, bbox.getExtendBox(), result);
return result;
}
case MULTILINESTRING_:
{
auto const &mls = osmStore.retrieve_multi_linestring((oo.objectID >> OSMID_TYPE_OFFSET) > 0 ? osmStore.osm() : osmStore.shp(), oo.objectID);
// investigate whether filtering the constituent linestrings improves performance
MultiLinestring result;
geom::intersection(mls, bbox.getExtendBox(), result);
return result;
}
case POLYGON_:
{
auto const &input = osmStore.retrieve_multi_polygon((oo.objectID >> OSMID_TYPE_OFFSET) > 0 ? osmStore.osm() : osmStore.shp(), oo.objectID);
Box box = bbox.clippingBox;
if (bbox.endZoom) {
for(auto const &p: input) {
for(auto const &inner: p.inners()) {
for(std::size_t i = 0; i < inner.size() - 1; ++i)
{
Point p1 = inner[i];
Point p2 = inner[i + 1];
if(geom::within(p1, bbox.clippingBox) != geom::within(p2, bbox.clippingBox)) {
box.min_corner() = Point(
std::min(box.min_corner().x(), std::min(p1.x(), p2.x())),
std::min(box.min_corner().y(), std::min(p1.y(), p2.y())));
box.max_corner() = Point(
std::max(box.max_corner().x(), std::max(p1.x(), p2.x())),
std::max(box.max_corner().y(), std::max(p1.y(), p2.y())));
}
}
}
for(std::size_t i = 0; i < p.outer().size() - 1; ++i) {
Point p1 = p.outer()[i];
Point p2 = p.outer()[i + 1];
if(geom::within(p1, bbox.clippingBox) != geom::within(p2, bbox.clippingBox)) {
box.min_corner() = Point(
std::min(box.min_corner().x(), std::min(p1.x(), p2.x())),
std::min(box.min_corner().y(), std::min(p1.y(), p2.y())));
box.max_corner() = Point(
std::max(box.max_corner().x(), std::max(p1.x(), p2.x())),
std::max(box.max_corner().y(), std::max(p1.y(), p2.y())));
}
}
}
Box extBox = bbox.getExtendBox();
box.min_corner() = Point(
std::max(box.min_corner().x(), extBox.min_corner().x()),
std::max(box.min_corner().y(), extBox.min_corner().y()));
box.max_corner() = Point(
std::min(box.max_corner().x(), extBox.max_corner().x()),
std::min(box.max_corner().y(), extBox.max_corner().y()));
}
MultiPolygon mp;
geom::assign(mp, input);
fast_clip(mp, box);
geom::correct(mp);
return mp;
}
default:
throw std::runtime_error("Invalid output geometry");
}
}
LatpLon buildNodeGeometry(OSMStore &osmStore, OutputObject const &oo, const TileBbox &bbox)
{
switch(oo.geomType) {
case POINT_:
{
auto p = osmStore.retrieve_point((oo.objectID >> OSMID_TYPE_OFFSET) > 0 ? osmStore.osm() : osmStore.shp(), oo.objectID);
LatpLon out;
out.latp = p.y();
out.lon = p.x();
return out;
}
default:
break;
}
throw std::runtime_error("Geometry type is not point");
}
// Find a value in the value dictionary
// (we can't easily use find() because of the different value-type encoding -
// should be possible to improve this though)
int OutputObject::findValue(vector<vector_tile::Tile_Value> *valueList, vector_tile::Tile_Value const &value) const {
for (size_t i=0; i<valueList->size(); i++) {
vector_tile::Tile_Value v = valueList->at(i);
if (v.has_string_value() && value.has_string_value() && v.string_value()==value.string_value()) { return i; }
if (v.has_float_value() && value.has_float_value() && v.float_value() ==value.float_value() ) { return i; }
if (v.has_bool_value() && value.has_bool_value() && v.bool_value() ==value.bool_value() ) { return i; }
}
return -1;
}
// Comparision functions
bool operator==(const OutputObjectRef x, const OutputObjectRef y) {
return
x->layer == y->layer &&
x->z_order == y->z_order &&
x->geomType == y->geomType &&
x->attributes == y->attributes &&
x->objectID == y->objectID;
}
namespace vector_tile {
bool operator==(const vector_tile::Tile_Value &x, const vector_tile::Tile_Value &y) {
std::string strx = x.SerializeAsString();
std::string stry = y.SerializeAsString();
return strx == stry;
}
bool operator<(const vector_tile::Tile_Value &x, const vector_tile::Tile_Value &y) {
std::string strx = x.SerializeAsString();
std::string stry = y.SerializeAsString();
return strx < stry;
}
}