1 #include "guidance/turn_instruction.hpp"
2 
3 #include "engine/plugins/plugin_base.hpp"
4 #include "engine/plugins/tile.hpp"
5 
6 #include "util/coordinate_calculation.hpp"
7 #include "util/string_view.hpp"
8 #include "util/vector_tile.hpp"
9 #include "util/web_mercator.hpp"
10 
11 #include <boost/geometry.hpp>
12 #include <boost/geometry/geometries/geometries.hpp>
13 #include <boost/geometry/geometries/point_xy.hpp>
14 #include <boost/geometry/multi/geometries/multi_linestring.hpp>
15 
16 #include <vtzero/builder.hpp>
17 #include <vtzero/geometry.hpp>
18 #include <vtzero/index.hpp>
19 
20 #include <algorithm>
21 #include <numeric>
22 #include <string>
23 #include <unordered_map>
24 #include <utility>
25 #include <vector>
26 
27 #include <cmath>
28 #include <cstdint>
29 
30 namespace osrm
31 {
32 namespace engine
33 {
34 namespace plugins
35 {
36 
37 constexpr const static int MIN_ZOOM_FOR_TURNS = 15;
38 
39 namespace
40 {
41 
42 using RTreeLeaf = datafacade::BaseDataFacade::RTreeLeaf;
43 // Simple container class for WGS84 coordinates
44 template <typename T> struct Point final
45 {
Pointosrm::engine::plugins::__anon68b6244a0111::Point46     Point(T _x, T _y) : x(_x), y(_y) {}
47 
48     const T x;
49     const T y;
50 };
51 
52 // Simple container to hold a bounding box
53 struct BBox final
54 {
BBoxosrm::engine::plugins::__anon68b6244a0111::BBox55     BBox(const double _minx, const double _miny, const double _maxx, const double _maxy)
56         : minx(_minx), miny(_miny), maxx(_maxx), maxy(_maxy)
57     {
58     }
59 
widthosrm::engine::plugins::__anon68b6244a0111::BBox60     double width() const { return maxx - minx; }
heightosrm::engine::plugins::__anon68b6244a0111::BBox61     double height() const { return maxy - miny; }
62 
63     const double minx;
64     const double miny;
65     const double maxx;
66     const double maxy;
67 };
68 
69 // Simple container for integer coordinates (i.e. pixel coords)
70 struct point_type_i final
71 {
point_type_iosrm::engine::plugins::__anon68b6244a0111::point_type_i72     point_type_i(std::int64_t _x, std::int64_t _y) : x(_x), y(_y) {}
73 
74     const std::int64_t x;
75     const std::int64_t y;
76 };
77 
78 using FixedPoint = Point<std::int32_t>;
79 using FloatPoint = Point<double>;
80 
81 using FixedLine = std::vector<FixedPoint>;
82 using FloatLine = std::vector<FloatPoint>;
83 
84 // We use boost::geometry to clip lines/points that are outside or cross the boundary
85 // of the tile we're rendering.  We need these types defined to use boosts clipping
86 // logic
87 typedef boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian> point_t;
88 typedef boost::geometry::model::linestring<point_t> linestring_t;
89 typedef boost::geometry::model::box<point_t> box_t;
90 typedef boost::geometry::model::multi_linestring<linestring_t> multi_linestring_t;
91 const static box_t clip_box(point_t(-util::vector_tile::BUFFER, -util::vector_tile::BUFFER),
92                             point_t(util::vector_tile::EXTENT + util::vector_tile::BUFFER,
93                                     util::vector_tile::EXTENT + util::vector_tile::BUFFER));
94 
95 /**
96  * Return the x1,y1,x2,y2 pixel coordinates of a line in a given
97  * tile.
98  *
99  * @param start the first coordinate of the line
100  * @param target the last coordinate of the line
101  * @param tile_bbox the boundaries of the tile, in mercator coordinates
102  * @return a FixedLine with coordinates relative to the tile_bbox.
103  */
floatLineToTileLine(const FloatLine & geo_line,const BBox & tile_bbox)104 linestring_t floatLineToTileLine(const FloatLine &geo_line, const BBox &tile_bbox)
105 {
106     linestring_t unclipped_line;
107 
108     for (auto const &pt : geo_line)
109     {
110         double px_merc = pt.x * util::web_mercator::DEGREE_TO_PX;
111         double py_merc = util::web_mercator::latToY(util::FloatLatitude{pt.y}) *
112                          util::web_mercator::DEGREE_TO_PX;
113         // convert lon/lat to tile coordinates
114         const auto px = std::round(
115             ((px_merc - tile_bbox.minx) * util::web_mercator::TILE_SIZE / tile_bbox.width()) *
116             util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE);
117         const auto py = std::round(
118             ((tile_bbox.maxy - py_merc) * util::web_mercator::TILE_SIZE / tile_bbox.height()) *
119             util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE);
120 
121         boost::geometry::append(unclipped_line, point_t(px, py));
122     }
123 
124     return unclipped_line;
125 }
126 
coordinatesToTileLine(const std::vector<util::Coordinate> & points,const BBox & tile_bbox)127 std::vector<FixedLine> coordinatesToTileLine(const std::vector<util::Coordinate> &points,
128                                              const BBox &tile_bbox)
129 {
130     FloatLine geo_line;
131     for (auto const &c : points)
132     {
133         geo_line.emplace_back(static_cast<double>(util::toFloating(c.lon)),
134                               static_cast<double>(util::toFloating(c.lat)));
135     }
136 
137     linestring_t unclipped_line = floatLineToTileLine(geo_line, tile_bbox);
138 
139     multi_linestring_t clipped_line;
140     boost::geometry::intersection(clip_box, unclipped_line, clipped_line);
141 
142     std::vector<FixedLine> result;
143 
144     // b::g::intersection might return a line with one point if the
145     // original line was very short and coords were dupes
146     for (auto const &cl : clipped_line)
147     {
148         if (cl.size() < 2)
149             continue;
150 
151         FixedLine tile_line;
152         for (const auto &p : cl)
153             tile_line.emplace_back(p.get<0>(), p.get<1>());
154 
155         result.emplace_back(std::move(tile_line));
156     }
157 
158     return result;
159 }
160 
161 /**
162  * Return the x1,y1,x2,y2 pixel coordinates of a line in a given
163  * tile.
164  *
165  * @param start the first coordinate of the line
166  * @param target the last coordinate of the line
167  * @param tile_bbox the boundaries of the tile, in mercator coordinates
168  * @return a FixedLine with coordinates relative to the tile_bbox.
169  */
coordinatesToTileLine(const util::Coordinate start,const util::Coordinate target,const BBox & tile_bbox)170 FixedLine coordinatesToTileLine(const util::Coordinate start,
171                                 const util::Coordinate target,
172                                 const BBox &tile_bbox)
173 {
174     FloatLine geo_line;
175     geo_line.emplace_back(static_cast<double>(util::toFloating(start.lon)),
176                           static_cast<double>(util::toFloating(start.lat)));
177     geo_line.emplace_back(static_cast<double>(util::toFloating(target.lon)),
178                           static_cast<double>(util::toFloating(target.lat)));
179 
180     linestring_t unclipped_line = floatLineToTileLine(geo_line, tile_bbox);
181 
182     multi_linestring_t clipped_line;
183     boost::geometry::intersection(clip_box, unclipped_line, clipped_line);
184 
185     FixedLine tile_line;
186 
187     // b::g::intersection might return a line with one point if the
188     // original line was very short and coords were dupes
189     if (!clipped_line.empty() && clipped_line[0].size() == 2)
190     {
191         for (const auto &p : clipped_line[0])
192         {
193             tile_line.emplace_back(p.get<0>(), p.get<1>());
194         }
195     }
196 
197     return tile_line;
198 }
199 
200 /**
201  * Converts lon/lat into coordinates inside a Mercator projection tile (x/y pixel values)
202  *
203  * @param point the lon/lat you want the tile coords for
204  * @param tile_bbox the mercator boundaries of the tile
205  * @return a point (x,y) on the tile defined by tile_bbox
206  */
coordinatesToTilePoint(const util::Coordinate point,const BBox & tile_bbox)207 FixedPoint coordinatesToTilePoint(const util::Coordinate point, const BBox &tile_bbox)
208 {
209     const FloatPoint geo_point{static_cast<double>(util::toFloating(point.lon)),
210                                static_cast<double>(util::toFloating(point.lat))};
211 
212     const double px_merc = geo_point.x * util::web_mercator::DEGREE_TO_PX;
213     const double py_merc = util::web_mercator::latToY(util::FloatLatitude{geo_point.y}) *
214                            util::web_mercator::DEGREE_TO_PX;
215 
216     const auto px = static_cast<std::int32_t>(std::round(
217         ((px_merc - tile_bbox.minx) * util::web_mercator::TILE_SIZE / tile_bbox.width()) *
218         util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE));
219     const auto py = static_cast<std::int32_t>(std::round(
220         ((tile_bbox.maxy - py_merc) * util::web_mercator::TILE_SIZE / tile_bbox.height()) *
221         util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE));
222 
223     return FixedPoint{px, py};
224 }
225 
getEdges(const DataFacadeBase & facade,unsigned x,unsigned y,unsigned z)226 std::vector<RTreeLeaf> getEdges(const DataFacadeBase &facade, unsigned x, unsigned y, unsigned z)
227 {
228     double min_lon, min_lat, max_lon, max_lat;
229 
230     // Convert the z,x,y mercator tile coordinates into WGS84 lon/lat values
231     //
232     util::web_mercator::xyzToWGS84(
233         x, y, z, min_lon, min_lat, max_lon, max_lat, util::web_mercator::TILE_SIZE * 0.10);
234 
235     util::Coordinate southwest{util::FloatLongitude{min_lon}, util::FloatLatitude{min_lat}};
236     util::Coordinate northeast{util::FloatLongitude{max_lon}, util::FloatLatitude{max_lat}};
237 
238     // Fetch all the segments that are in our bounding box.
239     // This hits the OSRM StaticRTree
240     return facade.GetEdgesInBox(southwest, northeast);
241 }
242 
getEdgeIndex(const std::vector<RTreeLeaf> & edges)243 std::vector<std::size_t> getEdgeIndex(const std::vector<RTreeLeaf> &edges)
244 {
245     // In order to ensure consistent tile encoding, we need to process
246     // all edges in the same order.  Differences in OSX/Linux/Windows
247     // sorting methods mean that GetEdgesInBox doesn't return the same
248     // ordered array on all platforms.
249     // GetEdgesInBox is marked `const`, so we can't sort the array itself,
250     // instead we create an array of indexes and sort that instead.
251     std::vector<std::size_t> sorted_edge_indexes(edges.size(), 0);
252     std::iota(
253         sorted_edge_indexes.begin(), sorted_edge_indexes.end(), 0); // fill with 0,1,2,3,...N-1
254 
255     // Now, sort that array based on the edges list, using the u/v node IDs
256     // as the sort condition
257     std::sort(sorted_edge_indexes.begin(),
258               sorted_edge_indexes.end(),
259               [&edges](const std::size_t &left, const std::size_t &right) -> bool {
260                   return (edges[left].u != edges[right].u) ? edges[left].u < edges[right].u
261                                                            : edges[left].v < edges[right].v;
262               });
263 
264     return sorted_edge_indexes;
265 }
266 
getSegregatedNodes(const DataFacadeBase & facade,const std::vector<RTreeLeaf> & edges)267 std::vector<NodeID> getSegregatedNodes(const DataFacadeBase &facade,
268                                        const std::vector<RTreeLeaf> &edges)
269 {
270     std::vector<NodeID> result;
271 
272     for (RTreeLeaf const &e : edges)
273     {
274         if (e.forward_segment_id.enabled && facade.IsSegregated(e.forward_segment_id.id))
275             result.push_back(e.forward_segment_id.id);
276     }
277 
278     return result;
279 }
280 
281 struct SpeedLayer : public vtzero::layer_builder
282 {
283 
284     vtzero::value_index_small_uint uint_index;
285     vtzero::value_index<vtzero::double_value_type, float, std::unordered_map> double_index;
286     vtzero::value_index_internal<std::unordered_map> string_index;
287     vtzero::value_index_bool bool_index;
288 
289     vtzero::index_value key_speed;
290     vtzero::index_value key_is_small;
291     vtzero::index_value key_datasource;
292     vtzero::index_value key_weight;
293     vtzero::index_value key_duration;
294     vtzero::index_value key_name;
295     vtzero::index_value key_rate;
296     vtzero::index_value key_is_startpoint;
297 
SpeedLayerosrm::engine::plugins::__anon68b6244a0111::SpeedLayer298     SpeedLayer(vtzero::tile_builder &tile)
299         : layer_builder(tile, "speeds"), uint_index(*this), double_index(*this),
300           string_index(*this), bool_index(*this), key_speed(add_key_without_dup_check("speed")),
301           key_is_small(add_key_without_dup_check("is_small")),
302           key_datasource(add_key_without_dup_check("datasource")),
303           key_weight(add_key_without_dup_check("weight")),
304           key_duration(add_key_without_dup_check("duration")),
305           key_name(add_key_without_dup_check("name")), key_rate(add_key_without_dup_check("rate")),
306           key_is_startpoint(add_key_without_dup_check("is_startpoint"))
307     {
308     }
309 
310 }; // struct SpeedLayer
311 
312 class SpeedLayerFeatureBuilder : public vtzero::linestring_feature_builder
313 {
314 
315     SpeedLayer &m_layer;
316 
317   public:
SpeedLayerFeatureBuilder(SpeedLayer & layer,uint64_t id)318     SpeedLayerFeatureBuilder(SpeedLayer &layer, uint64_t id)
319         : vtzero::linestring_feature_builder(layer), m_layer(layer)
320     {
321         set_id(id);
322     }
323 
set_speed(unsigned int value)324     void set_speed(unsigned int value)
325     {
326         add_property(m_layer.key_speed, m_layer.uint_index(std::min(value, 127u)));
327     }
328 
set_is_small(bool value)329     void set_is_small(bool value) { add_property(m_layer.key_is_small, m_layer.bool_index(value)); }
330 
set_datasource(const std::string & value)331     void set_datasource(const std::string &value)
332     {
333         add_property(m_layer.key_datasource,
334                      m_layer.string_index(vtzero::encoded_property_value{value}));
335     }
336 
set_weight(double value)337     void set_weight(double value) { add_property(m_layer.key_weight, m_layer.double_index(value)); }
338 
set_duration(double value)339     void set_duration(double value)
340     {
341         add_property(m_layer.key_duration, m_layer.double_index(value));
342     }
343 
set_name(const boost::string_ref & value)344     void set_name(const boost::string_ref &value)
345     {
346         add_property(
347             m_layer.key_name,
348             m_layer.string_index(vtzero::encoded_property_value{value.data(), value.size()}));
349     }
350 
set_rate(double value)351     void set_rate(double value) { add_property(m_layer.key_rate, m_layer.double_index(value)); }
352 
set_is_startpoint(bool value)353     void set_is_startpoint(bool value)
354     {
355         add_property(m_layer.key_is_startpoint, m_layer.bool_index(value));
356     }
357 
358 }; // class SpeedLayerFeatureBuilder
359 
360 struct TurnsLayer : public vtzero::layer_builder
361 {
362 
363     vtzero::value_index<vtzero::sint_value_type, int, std::unordered_map> int_index;
364     vtzero::value_index<vtzero::float_value_type, float, std::unordered_map> float_index;
365     vtzero::value_index_internal<std::unordered_map> string_index;
366 
367     vtzero::index_value key_bearing_in;
368     vtzero::index_value key_turn_angle;
369     vtzero::index_value key_cost;
370     vtzero::index_value key_weight;
371     vtzero::index_value key_turn_type;
372     vtzero::index_value key_turn_modifier;
373 
TurnsLayerosrm::engine::plugins::__anon68b6244a0111::TurnsLayer374     TurnsLayer(vtzero::tile_builder &tile)
375         : layer_builder(tile, "turns"), int_index(*this), float_index(*this), string_index(*this),
376           key_bearing_in(add_key_without_dup_check("bearing_in")),
377           key_turn_angle(add_key_without_dup_check("turn_angle")),
378           key_cost(add_key_without_dup_check("cost")),
379           key_weight(add_key_without_dup_check("weight")),
380           key_turn_type(add_key_without_dup_check("type")),
381           key_turn_modifier(add_key_without_dup_check("modifier"))
382     {
383     }
384 
385 }; // struct TurnsLayer
386 
387 class TurnsLayerFeatureBuilder : public vtzero::point_feature_builder
388 {
389 
390     TurnsLayer &m_layer;
391 
392   public:
TurnsLayerFeatureBuilder(TurnsLayer & layer,uint64_t id)393     TurnsLayerFeatureBuilder(TurnsLayer &layer, uint64_t id)
394         : vtzero::point_feature_builder(layer), m_layer(layer)
395     {
396         set_id(id);
397     }
398 
set_bearing_in(int value)399     void set_bearing_in(int value)
400     {
401         add_property(m_layer.key_bearing_in, m_layer.int_index(value));
402     }
403 
set_turn_angle(int value)404     void set_turn_angle(int value)
405     {
406         add_property(m_layer.key_turn_angle, m_layer.int_index(value));
407     }
408 
set_cost(float value)409     void set_cost(float value) { add_property(m_layer.key_cost, m_layer.float_index(value)); }
410 
set_weight(float value)411     void set_weight(float value) { add_property(m_layer.key_weight, m_layer.float_index(value)); }
412 
set_turn(osrm::guidance::TurnInstruction value)413     void set_turn(osrm::guidance::TurnInstruction value)
414     {
415         const auto type = osrm::guidance::internalInstructionTypeToString(value.type);
416         const auto modifier = osrm::guidance::instructionModifierToString(value.direction_modifier);
417         add_property(
418             m_layer.key_turn_type,
419             m_layer.string_index(vtzero::encoded_property_value{type.data(), type.size()}));
420         add_property(
421             m_layer.key_turn_modifier,
422             m_layer.string_index(vtzero::encoded_property_value{modifier.data(), modifier.size()}));
423     }
424 }; // class TurnsLayerFeatureBuilder
425 
encodeVectorTile(const DataFacadeBase & facade,unsigned x,unsigned y,unsigned z,const std::vector<RTreeLeaf> & edges,const std::vector<std::size_t> & sorted_edge_indexes,const std::vector<routing_algorithms::TurnData> & all_turn_data,const std::vector<NodeID> & segregated_nodes,std::string & pbf_buffer)426 void encodeVectorTile(const DataFacadeBase &facade,
427                       unsigned x,
428                       unsigned y,
429                       unsigned z,
430                       const std::vector<RTreeLeaf> &edges,
431                       const std::vector<std::size_t> &sorted_edge_indexes,
432                       const std::vector<routing_algorithms::TurnData> &all_turn_data,
433                       const std::vector<NodeID> &segregated_nodes,
434                       std::string &pbf_buffer)
435 {
436     vtzero::tile_builder tile;
437 
438     const auto get_geometry_id = [&facade](auto edge) {
439         return facade.GetGeometryIndex(edge.forward_segment_id.id).id;
440     };
441 
442     // Convert tile coordinates into mercator coordinates
443     double min_mercator_lon, min_mercator_lat, max_mercator_lon, max_mercator_lat;
444     util::web_mercator::xyzToMercator(
445         x, y, z, min_mercator_lon, min_mercator_lat, max_mercator_lon, max_mercator_lat);
446     const BBox tile_bbox{min_mercator_lon, min_mercator_lat, max_mercator_lon, max_mercator_lat};
447 
448     // XXX leaving in some superfluous scopes to make diff easier to read.
449     {
450         {
451             // Begin the layer features block
452             {
453                 SpeedLayer speeds_layer{tile};
454 
455                 // Each feature gets a unique id, starting at 1
456                 unsigned id = 1;
457                 for (const auto &edge_index : sorted_edge_indexes)
458                 {
459                     const auto &edge = edges[edge_index];
460                     const auto geometry_id = get_geometry_id(edge);
461 
462                     // Get coordinates for start/end nodes of segment (NodeIDs u and v)
463                     const auto a = facade.GetCoordinateOfNode(edge.u);
464                     const auto b = facade.GetCoordinateOfNode(edge.v);
465                     // Calculate the length in meters
466                     const double length =
467                         osrm::util::coordinate_calculation::haversineDistance(a, b);
468 
469                     const auto forward_weight_range =
470                         facade.GetUncompressedForwardWeights(geometry_id);
471                     const auto reverse_weight_range =
472                         facade.GetUncompressedReverseWeights(geometry_id);
473                     const auto forward_duration_range =
474                         facade.GetUncompressedForwardDurations(geometry_id);
475                     const auto reverse_duration_range =
476                         facade.GetUncompressedReverseDurations(geometry_id);
477                     const auto forward_datasource_range =
478                         facade.GetUncompressedForwardDatasources(geometry_id);
479                     const auto reverse_datasource_range =
480                         facade.GetUncompressedReverseDatasources(geometry_id);
481                     const auto forward_weight = forward_weight_range[edge.fwd_segment_position];
482                     const auto reverse_weight = reverse_weight_range[reverse_weight_range.size() -
483                                                                      edge.fwd_segment_position - 1];
484 
485                     const auto forward_duration = forward_duration_range[edge.fwd_segment_position];
486                     const auto reverse_duration =
487                         reverse_duration_range[reverse_duration_range.size() -
488                                                edge.fwd_segment_position - 1];
489                     const auto forward_datasource_idx =
490                         forward_datasource_range(edge.fwd_segment_position);
491                     const auto reverse_datasource_idx = reverse_datasource_range(
492                         reverse_datasource_range.size() - edge.fwd_segment_position - 1);
493 
494                     const auto is_startpoint = edge.is_startpoint;
495 
496                     const auto component_id = facade.GetComponentID(edge.forward_segment_id.id);
497                     const auto name_id = facade.GetNameIndex(edge.forward_segment_id.id);
498                     auto name = facade.GetNameForID(name_id);
499 
500                     // If this is a valid forward edge, go ahead and add it to the tile
501                     if (forward_duration != 0 && edge.forward_segment_id.enabled)
502                     {
503                         // Calculate the speed for this line
504                         std::uint32_t speed_kmh_idx =
505                             static_cast<std::uint32_t>(round(length / forward_duration * 10 * 3.6));
506 
507                         // Rate values are in meters per weight-unit - and similar to speeds, we
508                         // present 1 decimal place of precision (these values are added as
509                         // double/10) lower down
510                         std::uint32_t forward_rate =
511                             static_cast<std::uint32_t>(round(length / forward_weight * 10.));
512 
513                         auto tile_line = coordinatesToTileLine(a, b, tile_bbox);
514                         if (!tile_line.empty())
515                         {
516                             SpeedLayerFeatureBuilder fbuilder{speeds_layer, id};
517                             fbuilder.add_linestring_from_container(tile_line);
518 
519                             fbuilder.set_speed(speed_kmh_idx);
520                             fbuilder.set_is_small(component_id.is_tiny);
521                             fbuilder.set_datasource(
522                                 facade.GetDatasourceName(forward_datasource_idx).to_string());
523                             fbuilder.set_weight(forward_weight / 10.0);
524                             fbuilder.set_duration(forward_duration / 10.0);
525                             fbuilder.set_name(name);
526                             fbuilder.set_rate(forward_rate / 10.0);
527                             fbuilder.set_is_startpoint(is_startpoint);
528 
529                             fbuilder.commit();
530                         }
531                     }
532 
533                     // Repeat the above for the coordinates reversed and using the `reverse`
534                     // properties
535                     if (reverse_duration != 0 && edge.reverse_segment_id.enabled)
536                     {
537                         // Calculate the speed for this line
538                         std::uint32_t speed_kmh_idx =
539                             static_cast<std::uint32_t>(round(length / reverse_duration * 10 * 3.6));
540 
541                         // Rate values are in meters per weight-unit - and similar to speeds, we
542                         // present 1 decimal place of precision (these values are added as
543                         // double/10) lower down
544                         std::uint32_t reverse_rate =
545                             static_cast<std::uint32_t>(round(length / reverse_weight * 10.));
546 
547                         auto tile_line = coordinatesToTileLine(b, a, tile_bbox);
548                         if (!tile_line.empty())
549                         {
550                             SpeedLayerFeatureBuilder fbuilder{speeds_layer, id};
551                             fbuilder.add_linestring_from_container(tile_line);
552 
553                             fbuilder.set_speed(speed_kmh_idx);
554                             fbuilder.set_is_small(component_id.is_tiny);
555                             fbuilder.set_datasource(
556                                 facade.GetDatasourceName(reverse_datasource_idx).to_string());
557                             fbuilder.set_weight(reverse_weight / 10.0);
558                             fbuilder.set_duration(reverse_duration / 10.0);
559                             fbuilder.set_name(name);
560                             fbuilder.set_rate(reverse_rate / 10.0);
561                             fbuilder.set_is_startpoint(is_startpoint);
562 
563                             fbuilder.commit();
564                         }
565                     }
566                 }
567             }
568         }
569 
570         // Only add the turn layer to the tile if it has some features (we sometimes won't
571         // for tiles of z<16, and tiles that don't show any intersections)
572         if (!all_turn_data.empty())
573         {
574             TurnsLayer turns_layer{tile};
575             uint64_t id = 0;
576             for (const auto &turn_data : all_turn_data)
577             {
578                 const auto tile_point = coordinatesToTilePoint(turn_data.coordinate, tile_bbox);
579                 if (boost::geometry::within(point_t(tile_point.x, tile_point.y), clip_box))
580                 {
581                     TurnsLayerFeatureBuilder fbuilder{turns_layer, ++id};
582                     fbuilder.add_point(tile_point);
583 
584                     fbuilder.set_bearing_in(turn_data.in_angle);
585                     fbuilder.set_turn_angle(turn_data.turn_angle);
586                     fbuilder.set_cost(turn_data.duration / 10.0);
587                     fbuilder.set_weight(turn_data.weight / 10.0);
588                     fbuilder.set_turn(turn_data.turn_instruction);
589 
590                     fbuilder.commit();
591                 }
592             }
593         }
594 
595         // OSM Node tile layer
596         {
597             std::vector<NodeID> internal_nodes;
598             internal_nodes.reserve(edges.size() * 2);
599             for (const auto &edge : edges)
600             {
601                 internal_nodes.push_back(edge.u);
602                 internal_nodes.push_back(edge.v);
603             }
604             std::sort(internal_nodes.begin(), internal_nodes.end());
605             auto new_end = std::unique(internal_nodes.begin(), internal_nodes.end());
606             internal_nodes.resize(new_end - internal_nodes.begin());
607 
608             vtzero::layer_builder osmnodes_layer{tile, "osmnodes"};
609 
610             for (const auto &internal_node : internal_nodes)
611             {
612                 const auto coord = facade.GetCoordinateOfNode(internal_node);
613                 const auto tile_point = coordinatesToTilePoint(coord, tile_bbox);
614                 if (!boost::geometry::within(point_t(tile_point.x, tile_point.y), clip_box))
615                 {
616                     continue;
617                 }
618 
619                 vtzero::point_feature_builder fbuilder{osmnodes_layer};
620                 fbuilder.set_id(
621                     static_cast<OSMNodeID::value_type>(facade.GetOSMNodeIDOfNode(internal_node)));
622                 fbuilder.add_point(tile_point);
623                 fbuilder.commit();
624             }
625         }
626 
627         // Internal nodes tile layer
628         {
629             vtzero::layer_builder internal_nodes_layer{tile, "internal-nodes"};
630 
631             for (auto edgeNodeID : segregated_nodes)
632             {
633                 auto const geomIndex = facade.GetGeometryIndex(edgeNodeID);
634 
635                 std::vector<util::Coordinate> points;
636                 if (geomIndex.forward)
637                 {
638                     for (auto const nodeID : facade.GetUncompressedForwardGeometry(geomIndex.id))
639                         points.push_back(facade.GetCoordinateOfNode(nodeID));
640                 }
641                 else
642                 {
643                     for (auto const nodeID : facade.GetUncompressedReverseGeometry(geomIndex.id))
644                         points.push_back(facade.GetCoordinateOfNode(nodeID));
645                 }
646 
647                 auto tile_lines = coordinatesToTileLine(points, tile_bbox);
648                 if (!tile_lines.empty())
649                 {
650                     vtzero::linestring_feature_builder fbuilder{internal_nodes_layer};
651                     for (auto const &tile_line : tile_lines)
652                     {
653                         fbuilder.add_linestring_from_container(tile_line);
654                     }
655                     fbuilder.commit();
656                 }
657             }
658         }
659     }
660 
661     tile.serialize(pbf_buffer);
662 }
663 } // namespace
664 
HandleRequest(const RoutingAlgorithmsInterface & algorithms,const api::TileParameters & parameters,osrm::engine::api::ResultT & result) const665 Status TilePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
666                                  const api::TileParameters &parameters,
667                                  osrm::engine::api::ResultT &result) const
668 {
669     BOOST_ASSERT(parameters.IsValid());
670 
671     auto &pbf_buffer = result.get<std::string>();
672     const auto &facade = algorithms.GetFacade();
673     auto edges = getEdges(facade, parameters.x, parameters.y, parameters.z);
674     auto segregated_nodes = getSegregatedNodes(facade, edges);
675 
676     auto edge_index = getEdgeIndex(edges);
677 
678     std::vector<routing_algorithms::TurnData> turns;
679 
680     // If we're zooming into 16 or higher, include turn data.  Why?  Because turns make the map
681     // really cramped, so we don't bother including the data for tiles that span a large area.
682     if (parameters.z >= MIN_ZOOM_FOR_TURNS && algorithms.HasGetTileTurns())
683     {
684         turns = algorithms.GetTileTurns(edges, edge_index);
685     }
686 
687     encodeVectorTile(facade,
688                      parameters.x,
689                      parameters.y,
690                      parameters.z,
691                      edges,
692                      edge_index,
693                      turns,
694                      segregated_nodes,
695                      pbf_buffer);
696 
697     return Status::Ok;
698 }
699 } // namespace plugins
700 } // namespace engine
701 } // namespace osrm
702