From 14e746046524694b05d743d6553939488ca7ce1d Mon Sep 17 00:00:00 2001 From: Lauren Budorick Date: Mon, 9 May 2016 16:20:47 -0700 Subject: [PATCH] Include turn information in debug tiles. --- include/engine/plugins/tile.hpp | 9 +- .../routing_algorithms/routing_base.hpp | 63 ++ .../extractor/compressed_edge_container.hpp | 2 +- include/util/typedefs.hpp | 2 + include/util/vector_tile.hpp | 6 +- src/engine/plugins/tile.cpp | 723 ++++++++++++------ unit_tests/library/tile.cpp | 90 ++- 7 files changed, 664 insertions(+), 231 deletions(-) diff --git a/include/engine/plugins/tile.hpp b/include/engine/plugins/tile.hpp index d154eb511..a8f4cd8c6 100644 --- a/include/engine/plugins/tile.hpp +++ b/include/engine/plugins/tile.hpp @@ -3,6 +3,8 @@ #include "engine/api/tile_parameters.hpp" #include "engine/plugins/plugin_base.hpp" +#include "engine/routing_algorithms/routing_base.hpp" +#include "engine/routing_algorithms/shortest_path.hpp" #include @@ -23,8 +25,13 @@ namespace plugins class TilePlugin final : public BasePlugin { + private: + routing_algorithms::BasicRoutingInterface< + datafacade::BaseDataFacade, + routing_algorithms::ShortestPathRouting> routing_base; + public: - TilePlugin(datafacade::BaseDataFacade &facade) : BasePlugin(facade) {} + TilePlugin(datafacade::BaseDataFacade &facade) : BasePlugin(facade), routing_base(&facade) {} Status HandleRequest(const api::TileParameters ¶meters, std::string &pbf_buffer); }; diff --git a/include/engine/routing_algorithms/routing_base.hpp b/include/engine/routing_algorithms/routing_base.hpp index 6951209d1..8fca648e8 100644 --- a/include/engine/routing_algorithms/routing_base.hpp +++ b/include/engine/routing_algorithms/routing_base.hpp @@ -512,6 +512,69 @@ template class BasicRoutingInterface unpacked_path.emplace_back(t); } + /** + * A duplicate of the above `UnpackEdge` function, but returning full EdgeData + * objects for the unpacked path. Used in the tile plugin to find outgoing + * edges from a given turn. + */ + + void + UnpackEdgeToEdges(const NodeID s, const NodeID t, std::vector &unpacked_path) const + { + std::stack> recursion_stack; + recursion_stack.emplace(s, t); + + std::pair edge; + while (!recursion_stack.empty()) + { + edge = recursion_stack.top(); + recursion_stack.pop(); + + EdgeID smaller_edge_id = SPECIAL_EDGEID; + EdgeWeight edge_weight = std::numeric_limits::max(); + for (const auto edge_id : facade->GetAdjacentEdgeRange(edge.first)) + { + const EdgeWeight weight = facade->GetEdgeData(edge_id).distance; + if ((facade->GetTarget(edge_id) == edge.second) && (weight < edge_weight) && + facade->GetEdgeData(edge_id).forward) + { + smaller_edge_id = edge_id; + edge_weight = weight; + } + } + + if (SPECIAL_EDGEID == smaller_edge_id) + { + for (const auto edge_id : facade->GetAdjacentEdgeRange(edge.second)) + { + const EdgeWeight weight = facade->GetEdgeData(edge_id).distance; + if ((facade->GetTarget(edge_id) == edge.first) && (weight < edge_weight) && + facade->GetEdgeData(edge_id).backward) + { + smaller_edge_id = edge_id; + edge_weight = weight; + } + } + } + BOOST_ASSERT_MSG(edge_weight != std::numeric_limits::max(), + "edge weight invalid"); + + const EdgeData &ed = facade->GetEdgeData(smaller_edge_id); + if (ed.shortcut) + { // unpack + const NodeID middle_node_id = ed.id; + // again, we need to this in reversed order + recursion_stack.emplace(middle_node_id, edge.second); + recursion_stack.emplace(edge.first, middle_node_id); + } + else + { + BOOST_ASSERT_MSG(!ed.shortcut, "edge must be shortcut"); + unpacked_path.emplace_back(ed); + } + } + } + void RetrievePackedPathFromHeap(const SearchEngineData::QueryHeap &forward_heap, const SearchEngineData::QueryHeap &reverse_heap, const NodeID middle_node_id, diff --git a/include/extractor/compressed_edge_container.hpp b/include/extractor/compressed_edge_container.hpp index 9623d539a..a4efc25ba 100644 --- a/include/extractor/compressed_edge_container.hpp +++ b/include/extractor/compressed_edge_container.hpp @@ -33,7 +33,7 @@ class CompressedEdgeContainer const EdgeWeight weight2); void - AddUncompressedEdge(const EdgeID edgei_id, const NodeID target_node, const EdgeWeight weight); + AddUncompressedEdge(const EdgeID edge_id, const NodeID target_node, const EdgeWeight weight); bool HasEntryForID(const EdgeID edge_id) const; void PrintStatistics() const; diff --git a/include/util/typedefs.hpp b/include/util/typedefs.hpp index 36797eea0..50764727d 100644 --- a/include/util/typedefs.hpp +++ b/include/util/typedefs.hpp @@ -85,6 +85,8 @@ static const EdgeWeight INVALID_EDGE_WEIGHT = std::numeric_limits::m using DatasourceID = std::uint8_t; +using DatasourceID = std::uint8_t; + struct SegmentID { SegmentID(const NodeID id_, const bool enabled_) : id{id_}, enabled{enabled_} diff --git a/include/util/vector_tile.hpp b/include/util/vector_tile.hpp index c0a8c1065..c7fa31772 100644 --- a/include/util/vector_tile.hpp +++ b/include/util/vector_tile.hpp @@ -13,16 +13,18 @@ namespace vector_tile const constexpr std::uint32_t LAYER_TAG = 3; const constexpr std::uint32_t NAME_TAG = 1; const constexpr std::uint32_t VERSION_TAG = 15; -const constexpr std::uint32_t EXTEND_TAG = 5; +const constexpr std::uint32_t EXTENT_TAG = 5; const constexpr std::uint32_t FEATURE_TAG = 2; const constexpr std::uint32_t GEOMETRY_TAG = 3; const constexpr std::uint32_t VARIANT_TAG = 4; const constexpr std::uint32_t KEY_TAG = 3; const constexpr std::uint32_t ID_TAG = 1; +const constexpr std::uint32_t GEOMETRY_TYPE_POINT = 1; const constexpr std::uint32_t GEOMETRY_TYPE_LINE = 2; const constexpr std::uint32_t FEATURE_ATTRIBUTES_TAG = 2; const constexpr std::uint32_t FEATURE_GEOMETRIES_TAG = 4; -const constexpr std::uint32_t VARIANT_TYPE_UINT32 = 5; +const constexpr std::uint32_t VARIANT_TYPE_UINT64 = 5; +const constexpr std::uint32_t VARIANT_TYPE_SINT64 = 6; const constexpr std::uint32_t VARIANT_TYPE_BOOL = 7; const constexpr std::uint32_t VARIANT_TYPE_STRING = 1; const constexpr std::uint32_t VARIANT_TYPE_DOUBLE = 3; diff --git a/src/engine/plugins/tile.cpp b/src/engine/plugins/tile.cpp index 67ebc93d5..2595cae2a 100644 --- a/src/engine/plugins/tile.cpp +++ b/src/engine/plugins/tile.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include @@ -68,8 +70,23 @@ struct point_type_i final const std::int64_t y; }; -using FixedLine = std::vector>; -using FloatLine = std::vector>; +struct TurnData final +{ + TurnData(std::size_t _in, std::size_t _out, std::size_t _weight) + : in_angle_offset(_in), turn_angle_offset(_out), weight_offset(_weight) + { + } + + const std::size_t in_angle_offset; + const std::size_t turn_angle_offset; + const std::size_t weight_offset; +}; + +using FixedPoint = detail::Point; +using FloatPoint = detail::Point; + +using FixedLine = std::vector; +using FloatLine = std::vector; typedef boost::geometry::model::point point_t; typedef boost::geometry::model::linestring linestring_t; @@ -113,6 +130,19 @@ inline bool encodeLinestring(const FixedLine &line, return true; } +// from mapnik-vctor-tile +// Encodes a point +inline bool encodePoint(const FixedPoint &pt, protozero::packed_field_uint32 &geometry) +{ + geometry.add_element(9); + const std::int32_t dx = pt.x; + const std::int32_t dy = pt.y; + // Manual zigzag encoding. + geometry.add_element(protozero::encode_zigzag32(dx)); + geometry.add_element(protozero::encode_zigzag32(dy)); + return true; +} + FixedLine coordinatesToTileLine(const util::Coordinate start, const util::Coordinate target, const detail::BBox &tile_bbox) @@ -162,6 +192,25 @@ FixedLine coordinatesToTileLine(const util::Coordinate start, return tile_line; } + +FixedPoint coordinatesToTilePoint(const util::Coordinate point, const detail::BBox &tile_bbox) +{ + const FloatPoint geo_point{static_cast(util::toFloating(point.lon)), + static_cast(util::toFloating(point.lat))}; + + const double px_merc = geo_point.x * util::web_mercator::DEGREE_TO_PX; + const double py_merc = util::web_mercator::latToY(util::FloatLatitude{geo_point.y}) * + util::web_mercator::DEGREE_TO_PX; + + const auto px = static_cast(std::round( + ((px_merc - tile_bbox.minx) * util::web_mercator::TILE_SIZE / tile_bbox.width()) * + util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE)); + const auto py = static_cast(std::round( + ((tile_bbox.maxy - py_merc) * util::web_mercator::TILE_SIZE / tile_bbox.height()) * + util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE)); + + return FixedPoint{px, py}; +} } Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::string &pbf_buffer) @@ -181,12 +230,49 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str // This hits the OSRM StaticRTree const auto edges = facade.GetEdgesInBox(southwest, northeast); - std::vector used_weights; - std::unordered_map weight_offsets; + std::vector used_line_ints; + std::unordered_map line_int_offsets; uint8_t max_datasource_id = 0; std::vector names; std::unordered_map name_offsets; + + std::vector used_point_ints; + std::unordered_map point_int_offsets; + std::vector> all_turn_data; + + const auto use_line_value = [&used_line_ints, &line_int_offsets](const int &value) + { + const auto found = line_int_offsets.find(value); + + if (found == line_int_offsets.end()) + { + used_line_ints.push_back(value); + line_int_offsets[value] = used_line_ints.size() - 1; + } + + return; + }; + + const auto use_point_value = [&used_point_ints, &point_int_offsets](const int &value) + { + const auto found = point_int_offsets.find(value); + std::size_t offset; + + if (found == point_int_offsets.end()) + { + used_point_ints.push_back(value); + offset = used_point_ints.size() - 1; + point_int_offsets[value] = offset; + } + else + { + offset = found->second; + } + + return offset; + }; + // Loop over all edges once to tally up all the attributes we'll need. // We need to do this so that we know the attribute offsets to use // when we encode each feature in the tile. @@ -195,6 +281,9 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str int forward_weight = 0, reverse_weight = 0; uint8_t forward_datasource = 0; uint8_t reverse_datasource = 0; + std::vector edge_turn_data; + // TODO this approach of writing at least an empty vector for any segment is probably stupid + // (inefficient) if (edge.forward_packed_geometry_id != SPECIAL_EDGEID) { @@ -207,10 +296,98 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str forward_datasource_vector); forward_datasource = forward_datasource_vector[edge.fwd_segment_position]; - if (weight_offsets.find(forward_weight) == weight_offsets.end()) + use_line_value(forward_weight); + + std::vector forward_node_vector; + facade.GetUncompressedGeometry(edge.forward_packed_geometry_id, forward_node_vector); + + // If this is the last segment on an edge (i.e. leads to an intersection), find outgoing + // turns to write the turns point layer. + if (edge.fwd_segment_position == forward_node_vector.size() - 1) { - used_weights.push_back(forward_weight); - weight_offsets[forward_weight] = used_weights.size() - 1; + const auto sum_node_weight = + std::accumulate(forward_weight_vector.begin(), forward_weight_vector.end(), 0); + + // coord_a will be the OSM node immediately preceding the intersection, on the + // current edge + const auto coord_a = facade.GetCoordinateOfNode( + forward_node_vector.size() > 1 + ? forward_node_vector[forward_node_vector.size() - 2] + : edge.u); + // coord_b is the OSM intersection node, at the end of the current edge + const auto coord_b = facade.GetCoordinateOfNode(edge.v); + + // There will often be multiple c_nodes. Here, we start by getting all outgoing + // shortcuts, which we can whittle down (and deduplicate) to just the edges + // immediately following intersections. + // NOTE: the approach of only using shortcuts means that we aren't + // getting or writing *every* turn here, but we don't especially care about turns + // that will never be returned in a route anyway. + std::unordered_map c_nodes; + + for (const auto adj_shortcut : + facade.GetAdjacentEdgeRange(edge.forward_segment_id.id)) + { + std::vector unpacked_shortcut; + + // Outgoing shortcuts without `forward` travel enabled: do not want + if (!facade.GetEdgeData(adj_shortcut).forward) + { + continue; + } + + routing_base.UnpackEdgeToEdges(edge.forward_segment_id.id, + facade.GetTarget(adj_shortcut), + unpacked_shortcut); + + // Sometimes a "shortcut" is just an edge itself: this will not return a turn + if (unpacked_shortcut.size() < 2) + { + continue; + } + + // Unpack the data from the second edge (the first edge will be the edge + // we're currently on), to use its geometry in calculating angle + const auto first_geometry_id = + facade.GetGeometryIndexForEdgeID(unpacked_shortcut[1].id); + std::vector first_geometry_vector; + facade.GetUncompressedGeometry(first_geometry_id, first_geometry_vector); + + // EBE weight (the first edge in this shortcut) - EBN weight (calculated + // above by summing the distance of the current node-based edge) = turn weight + const auto sum_edge_weight = unpacked_shortcut[0].distance; + const auto turn_weight = sum_edge_weight - sum_node_weight; + + c_nodes.emplace(first_geometry_vector.front(), turn_weight); + } + + const auto angle_in = + static_cast(util::coordinate_calculation::bearing(coord_a, coord_b)); + + // Only write for those that have angles out + if (c_nodes.size() > 0) + { + const auto angle_in_offset = use_point_value(angle_in); + + for (const auto possible_next_node : c_nodes) + { + const auto coord_c = facade.GetCoordinateOfNode(possible_next_node.first); + const auto c_bearing = static_cast( + util::coordinate_calculation::bearing(coord_b, coord_c)); + + auto turn_angle = c_bearing - angle_in; + while (turn_angle > 180) { turn_angle -= 360; } + while (turn_angle < -180) { turn_angle += 360; } + + const auto turn_angle_offset = use_point_value(turn_angle); + const auto angle_weight_offset = use_point_value(possible_next_node.second); + + // TODO this is not as efficient as it could be because of repeated + // angles_in + edge_turn_data.emplace_back(detail::TurnData{ + angle_in_offset, turn_angle_offset, angle_weight_offset}); + } + } } } @@ -224,11 +401,8 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str reverse_weight = reverse_weight_vector[reverse_weight_vector.size() - edge.fwd_segment_position - 1]; - if (weight_offsets.find(reverse_weight) == weight_offsets.end()) - { - used_weights.push_back(reverse_weight); - weight_offsets[reverse_weight] = used_weights.size() - 1; - } + use_line_value(reverse_weight); + std::vector reverse_datasource_vector; facade.GetUncompressedDatasources(edge.reverse_packed_geometry_id, reverse_datasource_vector); @@ -240,13 +414,15 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str max_datasource_id = std::max(max_datasource_id, forward_datasource); max_datasource_id = std::max(max_datasource_id, reverse_datasource); - std::string name = facade.GetNameForID(edge.name_id); + std::string name = facade.GetNameForID(edge.name_id); if (name_offsets.find(name) == name_offsets.end()) { names.push_back(name); name_offsets[name] = names.size() - 1; } + + all_turn_data.emplace_back(std::move(edge_turn_data)); } // TODO: extract speed values for compressed and uncompressed geometries @@ -256,238 +432,351 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str parameters.x, parameters.y, parameters.z, min_lon, min_lat, max_lon, max_lat); const detail::BBox tile_bbox{min_lon, min_lat, max_lon, max_lat}; - // Protobuf serialized blocks when objects go out of scope, hence + // Protobuf serializes blocks when objects go out of scope, hence // the extra scoping below. protozero::pbf_writer tile_writer{pbf_buffer}; { - // Add a layer object to the PBF stream. 3=='layer' from the vector tile spec (2.1) - protozero::pbf_writer layer_writer(tile_writer, util::vector_tile::LAYER_TAG); - // TODO: don't write a layer if there are no features - - layer_writer.add_uint32(util::vector_tile::VERSION_TAG, 2); // version - // Field 1 is the "layer name" field, it's a string - layer_writer.add_string(util::vector_tile::NAME_TAG, "speeds"); // name - // Field 5 is the tile extent. It's a uint32 and should be set to 4096 - // for normal vector tiles. - layer_writer.add_uint32(util::vector_tile::EXTEND_TAG, util::vector_tile::EXTENT); // extent - - // Begin the layer features block { - // Each feature gets a unique id, starting at 1 - unsigned id = 1; - for (const auto &edge : edges) + // Add a layer object to the PBF stream. 3=='layer' from the vector tile spec (2.1) + protozero::pbf_writer line_layer_writer(tile_writer, util::vector_tile::LAYER_TAG); + // TODO: don't write a layer if there are no features + + line_layer_writer.add_uint32(util::vector_tile::VERSION_TAG, 2); // version + // Field 1 is the "layer name" field, it's a string + line_layer_writer.add_string(util::vector_tile::NAME_TAG, "speeds"); // name + // Field 5 is the tile extent. It's a uint32 and should be set to 4096 + // for normal vector tiles. + line_layer_writer.add_uint32(util::vector_tile::EXTENT_TAG, + util::vector_tile::EXTENT); // extent + + // Begin the layer features block { - // Get coordinates for start/end nodes of segmet (NodeIDs u and v) - const auto a = facade.GetCoordinateOfNode(edge.u); - const auto b = facade.GetCoordinateOfNode(edge.v); - // Calculate the length in meters - const double length = osrm::util::coordinate_calculation::haversineDistance(a, b); - - int forward_weight = 0; - int reverse_weight = 0; - - uint8_t forward_datasource = 0; - uint8_t reverse_datasource = 0; - - std::string name = facade.GetNameForID(edge.name_id); - - if (edge.forward_packed_geometry_id != SPECIAL_EDGEID) + // Each feature gets a unique id, starting at 1 + unsigned id = 1; + for (const auto &edge : edges) { - std::vector forward_weight_vector; - facade.GetUncompressedWeights(edge.forward_packed_geometry_id, - forward_weight_vector); - forward_weight = forward_weight_vector[edge.fwd_segment_position]; + // Get coordinates for start/end nodes of segment (NodeIDs u and v) + const auto a = facade.GetCoordinateOfNode(edge.u); + const auto b = facade.GetCoordinateOfNode(edge.v); + // Calculate the length in meters + const double length = + osrm::util::coordinate_calculation::haversineDistance(a, b); - std::vector forward_datasource_vector; - facade.GetUncompressedDatasources(edge.forward_packed_geometry_id, - forward_datasource_vector); - forward_datasource = forward_datasource_vector[edge.fwd_segment_position]; - } + int forward_weight = 0; + int reverse_weight = 0; - if (edge.reverse_packed_geometry_id != SPECIAL_EDGEID) - { - std::vector reverse_weight_vector; - facade.GetUncompressedWeights(edge.reverse_packed_geometry_id, - reverse_weight_vector); + uint8_t forward_datasource = 0; + uint8_t reverse_datasource = 0; - BOOST_ASSERT(edge.fwd_segment_position < reverse_weight_vector.size()); + std::string name = facade.GetNameForID(edge.name_id); - reverse_weight = reverse_weight_vector[reverse_weight_vector.size() - - edge.fwd_segment_position - 1]; - - std::vector reverse_datasource_vector; - facade.GetUncompressedDatasources(edge.reverse_packed_geometry_id, - reverse_datasource_vector); - reverse_datasource = - reverse_datasource_vector[reverse_datasource_vector.size() - - edge.fwd_segment_position - 1]; - } - - // Keep track of the highest datasource seen so that we don't write unnecessary - // data to the layer attribute values - max_datasource_id = std::max(max_datasource_id, forward_datasource); - max_datasource_id = std::max(max_datasource_id, reverse_datasource); - - const auto encode_tile_line = [&layer_writer, - &edge, - &id, - &max_datasource_id, - &used_weights](const detail::FixedLine &tile_line, - const std::uint32_t speed_kmh, - const std::size_t duration, - const DatasourceID datasource, - const std::size_t name, - std::int32_t &start_x, - std::int32_t &start_y) { - // Here, we save the two attributes for our feature: the speed and the - // is_small - // boolean. We onl serve up speeds from 0-139, so all we do is save the - // first - protozero::pbf_writer feature_writer(layer_writer, - util::vector_tile::FEATURE_TAG); - // Field 3 is the "geometry type" field. Value 2 is "line" - feature_writer.add_enum(util::vector_tile::GEOMETRY_TAG, - util::vector_tile::GEOMETRY_TYPE_LINE); // geometry type - // Field 1 for the feature is the "id" field. - feature_writer.add_uint64(util::vector_tile::ID_TAG, id++); // id + if (edge.forward_packed_geometry_id != SPECIAL_EDGEID) { - // When adding attributes to a feature, we have to write - // pairs of numbers. The first value is the index in the - // keys array (written later), and the second value is the - // index into the "values" array (also written later). We're - // not writing the actual speed or bool value here, we're saving - // an index into the "values" array. This means many features - // can share the same value data, leading to smaller tiles. - protozero::packed_field_uint32 field( - feature_writer, util::vector_tile::FEATURE_ATTRIBUTES_TAG); + std::vector forward_weight_vector; + facade.GetUncompressedWeights(edge.forward_packed_geometry_id, + forward_weight_vector); + forward_weight = forward_weight_vector[edge.fwd_segment_position]; - field.add_element(0); // "speed" tag key offset - field.add_element( - std::min(speed_kmh, 127u)); // save the speed value, capped at 127 - field.add_element(1); // "is_small" tag key offset - field.add_element(128 + - (edge.component.is_tiny ? 0 : 1)); // is_small feature - field.add_element(2); // "datasource" tag key offset - field.add_element(130 + datasource); // datasource value offset - field.add_element(3); // "duration" tag key offset - field.add_element(130 + max_datasource_id + 1 + - duration); // duration value offset - field.add_element(4); // "name" tag key offset - - field.add_element(130 + max_datasource_id + 1 + used_weights.size() + - name); // name value offset + std::vector forward_datasource_vector; + facade.GetUncompressedDatasources(edge.forward_packed_geometry_id, + forward_datasource_vector); + forward_datasource = forward_datasource_vector[edge.fwd_segment_position]; } - { - // Encode the geometry for the feature - protozero::packed_field_uint32 geometry( - feature_writer, util::vector_tile::FEATURE_GEOMETRIES_TAG); - encodeLinestring(tile_line, geometry, start_x, start_y); + if (edge.reverse_packed_geometry_id != SPECIAL_EDGEID) + { + std::vector reverse_weight_vector; + facade.GetUncompressedWeights(edge.reverse_packed_geometry_id, + reverse_weight_vector); + + BOOST_ASSERT(edge.fwd_segment_position < reverse_weight_vector.size()); + + reverse_weight = reverse_weight_vector[reverse_weight_vector.size() - + edge.fwd_segment_position - 1]; + + std::vector reverse_datasource_vector; + facade.GetUncompressedDatasources(edge.reverse_packed_geometry_id, + reverse_datasource_vector); + reverse_datasource = + reverse_datasource_vector[reverse_datasource_vector.size() - + edge.fwd_segment_position - 1]; } - }; - // If this is a valid forward edge, go ahead and add it to the tile - if (forward_weight != 0 && edge.forward_segment_id.enabled) - { - std::int32_t start_x = 0; - std::int32_t start_y = 0; + // Keep track of the highest datasource seen so that we don't write unnecessary + // data to the layer attribute values + max_datasource_id = std::max(max_datasource_id, forward_datasource); + max_datasource_id = std::max(max_datasource_id, reverse_datasource); - // Calculate the speed for this line - std::uint32_t speed_kmh = - static_cast(round(length / forward_weight * 10 * 3.6)); - - auto tile_line = coordinatesToTileLine(a, b, tile_bbox); - if (!tile_line.empty()) + const auto encode_tile_line = [&line_layer_writer, + &edge, + &id, + &max_datasource_id, + &used_line_ints](const detail::FixedLine &tile_line, + const std::uint32_t speed_kmh, + const std::size_t duration, + const DatasourceID datasource, + const std::size_t name, + std::int32_t &start_x, + std::int32_t &start_y) { - encode_tile_line(tile_line, - speed_kmh, - weight_offsets[forward_weight], - forward_datasource, - name_offsets[name], - start_x, - start_y); + // Here, we save the two attributes for our feature: the speed and the + // is_small + // boolean. We only serve up speeds from 0-139, so all we do is save the + // first + protozero::pbf_writer feature_writer(line_layer_writer, + util::vector_tile::FEATURE_TAG); + // Field 3 is the "geometry type" field. Value 2 is "line" + feature_writer.add_enum( + util::vector_tile::GEOMETRY_TAG, + util::vector_tile::GEOMETRY_TYPE_LINE); // geometry type + // Field 1 for the feature is the "id" field. + feature_writer.add_uint64(util::vector_tile::ID_TAG, id++); // id + { + // When adding attributes to a feature, we have to write + // pairs of numbers. The first value is the index in the + // keys array (written later), and the second value is the + // index into the "values" array (also written later). We're + // not writing the actual speed or bool value here, we're saving + // an index into the "values" array. This means many features + // can share the same value data, leading to smaller tiles. + protozero::packed_field_uint32 field( + feature_writer, util::vector_tile::FEATURE_ATTRIBUTES_TAG); + + field.add_element(0); // "speed" tag key offset + field.add_element( + std::min(speed_kmh, 127u)); // save the speed value, capped at 127 + field.add_element(1); // "is_small" tag key offset + field.add_element(128 + + (edge.component.is_tiny ? 0 : 1)); // is_small feature + field.add_element(2); // "datasource" tag key offset + field.add_element(130 + datasource); // datasource value offset + field.add_element(3); // "duration" tag key offset + field.add_element(130 + max_datasource_id + 1 + + duration); // duration value offset + field.add_element(4); // "name" tag key offset + + field.add_element(130 + max_datasource_id + 1 + used_line_ints.size() + + name); // name value offset + } + { + + // Encode the geometry for the feature + protozero::packed_field_uint32 geometry( + feature_writer, util::vector_tile::FEATURE_GEOMETRIES_TAG); + encodeLinestring(tile_line, geometry, start_x, start_y); + } + }; + + // If this is a valid forward edge, go ahead and add it to the tile + if (forward_weight != 0 && edge.forward_segment_id.enabled) + { + std::int32_t start_x = 0; + std::int32_t start_y = 0; + + // Calculate the speed for this line + std::uint32_t speed_kmh = + static_cast(round(length / forward_weight * 10 * 3.6)); + + auto tile_line = coordinatesToTileLine(a, b, tile_bbox); + if (!tile_line.empty()) + { + encode_tile_line(tile_line, + speed_kmh, + line_int_offsets[forward_weight], + forward_datasource, + name_offsets[name], + start_x, + start_y); + } } - } - // Repeat the above for the coordinates reversed and using the `reverse` - // properties - if (reverse_weight != 0 && edge.reverse_segment_id.enabled) - { - std::int32_t start_x = 0; - std::int32_t start_y = 0; - - // Calculate the speed for this line - std::uint32_t speed_kmh = - static_cast(round(length / reverse_weight * 10 * 3.6)); - - auto tile_line = coordinatesToTileLine(b, a, tile_bbox); - if (!tile_line.empty()) + // Repeat the above for the coordinates reversed and using the `reverse` + // properties + if (reverse_weight != 0 && edge.reverse_segment_id.enabled) { - encode_tile_line(tile_line, - speed_kmh, - weight_offsets[reverse_weight], - reverse_datasource, - name_offsets[name], - start_x, - start_y); + std::int32_t start_x = 0; + std::int32_t start_y = 0; + + // Calculate the speed for this line + std::uint32_t speed_kmh = + static_cast(round(length / reverse_weight * 10 * 3.6)); + + auto tile_line = coordinatesToTileLine(b, a, tile_bbox); + if (!tile_line.empty()) + { + encode_tile_line(tile_line, + speed_kmh, + line_int_offsets[reverse_weight], + reverse_datasource, + name_offsets[name], + start_x, + start_y); + } } } } + + // Field id 3 is the "keys" attribute + // We need two "key" fields, these are referred to with 0 and 1 (their array indexes) + // earlier + line_layer_writer.add_string(util::vector_tile::KEY_TAG, "speed"); + line_layer_writer.add_string(util::vector_tile::KEY_TAG, "is_small"); + line_layer_writer.add_string(util::vector_tile::KEY_TAG, "datasource"); + line_layer_writer.add_string(util::vector_tile::KEY_TAG, "duration"); + line_layer_writer.add_string(util::vector_tile::KEY_TAG, "name"); + + // Now, we write out the possible speed value arrays and possible is_tiny + // values. Field type 4 is the "values" field. It's a variable type field, + // so requires a two-step write (create the field, then write its value) + for (std::size_t i = 0; i < 128; i++) + { + // Writing field type 4 == variant type + protozero::pbf_writer values_writer(line_layer_writer, + util::vector_tile::VARIANT_TAG); + // Attribute value 5 == uint64 type + values_writer.add_uint64(util::vector_tile::VARIANT_TYPE_UINT64, i); + } + { + protozero::pbf_writer values_writer(line_layer_writer, + util::vector_tile::VARIANT_TAG); + // Attribute value 7 == bool type + values_writer.add_bool(util::vector_tile::VARIANT_TYPE_BOOL, true); + } + { + protozero::pbf_writer values_writer(line_layer_writer, + util::vector_tile::VARIANT_TAG); + // Attribute value 7 == bool type + values_writer.add_bool(util::vector_tile::VARIANT_TYPE_BOOL, false); + } + for (std::size_t i = 0; i <= max_datasource_id; i++) + { + // Writing field type 4 == variant type + protozero::pbf_writer values_writer(line_layer_writer, + util::vector_tile::VARIANT_TAG); + // Attribute value 1 == string type + values_writer.add_string(util::vector_tile::VARIANT_TYPE_STRING, + facade.GetDatasourceName(i)); + } + for (auto value : used_line_ints) + { + // Writing field type 4 == variant type + protozero::pbf_writer values_writer(line_layer_writer, + util::vector_tile::VARIANT_TAG); + // Attribute value 2 == float type + // Durations come out of OSRM in integer deciseconds, so we convert them + // to seconds with a simple /10 for display + values_writer.add_double(util::vector_tile::VARIANT_TYPE_DOUBLE, value / 10.); + } + + for (const auto &name : names) { + // Writing field type 4 == variant type + protozero::pbf_writer values_writer(line_layer_writer, util::vector_tile::VARIANT_TAG); + // Attribute value 1 == string type + values_writer.add_string(util::vector_tile::VARIANT_TYPE_STRING, name); + } } - // Field id 3 is the "keys" attribute - // We need two "key" fields, these are referred to with 0 and 1 (their array indexes) - // earlier - layer_writer.add_string(util::vector_tile::KEY_TAG, "speed"); - layer_writer.add_string(util::vector_tile::KEY_TAG, "is_small"); - layer_writer.add_string(util::vector_tile::KEY_TAG, "datasource"); - layer_writer.add_string(util::vector_tile::KEY_TAG, "duration"); - layer_writer.add_string(util::vector_tile::KEY_TAG, "name"); + { + // Now write the points layer for turn penalty data: + // Add a layer object to the PBF stream. 3=='layer' from the vector tile spec (2.1) + protozero::pbf_writer point_layer_writer(tile_writer, util::vector_tile::LAYER_TAG); + // TODO: don't write a layer if there are no features + point_layer_writer.add_uint32(util::vector_tile::VERSION_TAG, 2); // version + // Field 1 is the "layer name" field, it's a string + point_layer_writer.add_string(util::vector_tile::NAME_TAG, "turns"); // name + // Field 5 is the tile extent. It's a uint32 and should be set to 4096 + // for normal vector tiles. + point_layer_writer.add_uint32(util::vector_tile::EXTENT_TAG, + util::vector_tile::EXTENT); // extent - // Now, we write out the possible speed value arrays and possible is_tiny - // values. Field type 4 is the "values" field. It's a variable type field, - // so requires a two-step write (create the field, then write its value) - for (std::size_t i = 0; i < 128; i++) - { - // Writing field type 4 == variant type - protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG); - // Attribute value 5 == uin64 type - values_writer.add_uint64(util::vector_tile::VARIANT_TYPE_UINT32, i); - } - { - protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG); - // Attribute value 7 == bool type - values_writer.add_bool(util::vector_tile::VARIANT_TYPE_BOOL, true); - } - { - protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG); - // Attribute value 7 == bool type - values_writer.add_bool(util::vector_tile::VARIANT_TYPE_BOOL, false); - } - for (std::size_t i = 0; i <= max_datasource_id; i++) - { - // Writing field type 4 == variant type - protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG); - // Attribute value 1 == string type - values_writer.add_string(util::vector_tile::VARIANT_TYPE_STRING, - facade.GetDatasourceName(i)); - } - for (auto weight : used_weights) - { - // Writing field type 4 == variant type - protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG); - // Attribute value 2 == float type - // Durations come out of OSRM in integer deciseconds, so we convert them - // to seconds with a simple /10 for display - values_writer.add_double(util::vector_tile::VARIANT_TYPE_DOUBLE, weight / 10.); - } + // Begin the layer features block + { + // Each feature gets a unique id, starting at 1 + unsigned id = 1; + for (uint64_t i = 0; i < edges.size(); i++) + { + const auto &edge = edges[i]; + const auto &edge_turn_data = all_turn_data[i]; - for (const auto &name : names) - { - // Writing field type 4 == variant type - protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG); - // Attribute value 1 == string type - values_writer.add_string(util::vector_tile::VARIANT_TYPE_STRING, name); + // Skip writing for edges with no turn penalty data + if (edge_turn_data.empty()) + { + continue; + } + + std::vector forward_node_vector; + facade.GetUncompressedGeometry(edge.forward_packed_geometry_id, + forward_node_vector); + + // Skip writing for non-intersection segments + if (edge.fwd_segment_position != forward_node_vector.size() - 1) + { + continue; + } + + const auto encode_tile_point = + [&point_layer_writer, &edge, &id](const detail::FixedPoint &tile_point, + const detail::TurnData &point_turn_data) + { + protozero::pbf_writer feature_writer(point_layer_writer, + util::vector_tile::FEATURE_TAG); + // Field 3 is the "geometry type" field. Value 1 is "point" + feature_writer.add_enum( + util::vector_tile::GEOMETRY_TAG, + util::vector_tile::GEOMETRY_TYPE_POINT); // geometry type + // Field 1 for the feature is the "id" field. + feature_writer.add_uint64(util::vector_tile::ID_TAG, id++); // id + { + // See above for explanation + protozero::packed_field_uint32 field( + feature_writer, util::vector_tile::FEATURE_ATTRIBUTES_TAG); + + field.add_element(0); // "bearing_in" tag key offset + field.add_element(point_turn_data.in_angle_offset); + field.add_element(1); // "turn_angle" tag key offset + field.add_element(point_turn_data.turn_angle_offset); + field.add_element(2); // "weight" tag key offset + field.add_element(point_turn_data.weight_offset); + } + { + protozero::packed_field_uint32 geometry( + feature_writer, util::vector_tile::FEATURE_GEOMETRIES_TAG); + encodePoint(tile_point, geometry); + } + }; + + const auto turn_coordinate = facade.GetCoordinateOfNode(edge.v); + const auto tile_point = coordinatesToTilePoint(turn_coordinate, tile_bbox); + + if (!boost::geometry::within(detail::point_t(tile_point.x, tile_point.y), + detail::clip_box)) + { + continue; + } + + for (const auto &individual_turn : edge_turn_data) + { + encode_tile_point(tile_point, individual_turn); + } + } + } + + // Field id 3 is the "keys" attribute + // We need two "key" fields, these are referred to with 0 and 1 (their array indexes) + // earlier + point_layer_writer.add_string(util::vector_tile::KEY_TAG, "bearing_in"); + point_layer_writer.add_string(util::vector_tile::KEY_TAG, "turn_angle"); + point_layer_writer.add_string(util::vector_tile::KEY_TAG, "weight"); + + // Now, we write out the possible integer values. + for (const auto &value : used_point_ints) + { + // Writing field type 4 == variant type + protozero::pbf_writer values_writer(point_layer_writer, + util::vector_tile::VARIANT_TAG); + // Attribute value 6 == sint64 type + values_writer.add_sint64(util::vector_tile::VARIANT_TYPE_SINT64, value); + } } } diff --git a/unit_tests/library/tile.cpp b/unit_tests/library/tile.cpp index 7d10c7e99..7252d1691 100644 --- a/unit_tests/library/tile.cpp +++ b/unit_tests/library/tile.cpp @@ -91,15 +91,19 @@ BOOST_AUTO_TEST_CASE(test_tile) case util::vector_tile::VARIANT_TYPE_STRING: value.get_string(); break; - case util::vector_tile::VARIANT_TYPE_UINT32: - value.get_uint32(); + case util::vector_tile::VARIANT_TYPE_UINT64: + value.get_uint64(); + break; + case util::vector_tile::VARIANT_TYPE_SINT64: + value.get_sint64(); break; } } }; - auto number_of_keys = 0u; - auto number_of_values = 0u; + auto number_of_speed_keys = 0u; + auto number_of_speed_values = 0u; + while (layer_message.next()) { @@ -111,19 +115,19 @@ BOOST_AUTO_TEST_CASE(test_tile) case util::vector_tile::NAME_TAG: BOOST_CHECK_EQUAL(layer_message.get_string(), "speeds"); break; - case util::vector_tile::EXTEND_TAG: + case util::vector_tile::EXTENT_TAG: BOOST_CHECK_EQUAL(layer_message.get_uint32(), util::vector_tile::EXTENT); break; case util::vector_tile::FEATURE_TAG: - check_feature(layer_message.get_message()); + check_speed_feature(layer_message.get_message()); break; case util::vector_tile::KEY_TAG: layer_message.get_string(); - number_of_keys++; + number_of_speed_keys++; break; case util::vector_tile::VARIANT_TAG: check_value(layer_message.get_message()); - number_of_values++; + number_of_speed_values++; break; default: BOOST_CHECK(false); // invalid tag @@ -131,8 +135,74 @@ BOOST_AUTO_TEST_CASE(test_tile) } } - BOOST_CHECK_EQUAL(number_of_keys, 5); - BOOST_CHECK_GT(number_of_values, 128); // speed value resolution + BOOST_CHECK_EQUAL(number_of_speed_keys, 5); + BOOST_CHECK_GT(number_of_speed_values, 128); // speed value resolution + + tile_message.next(); + layer_message = tile_message.get_message(); + + const auto check_turn_feature = [](protozero::pbf_reader feature_message) { + protozero::pbf_reader::const_uint32_iterator value_begin; + protozero::pbf_reader::const_uint32_iterator value_end; + feature_message.next(); // advance parser to first entry + BOOST_CHECK_EQUAL(feature_message.tag(), util::vector_tile::GEOMETRY_TAG); + BOOST_CHECK_EQUAL(feature_message.get_enum(), util::vector_tile::GEOMETRY_TYPE_POINT); + + feature_message.next(); // advance to next entry + BOOST_CHECK_EQUAL(feature_message.tag(), util::vector_tile::ID_TAG); + feature_message.get_uint64(); // id + + feature_message.next(); // advance to next entry + BOOST_CHECK_EQUAL(feature_message.tag(), util::vector_tile::FEATURE_ATTRIBUTES_TAG); + // properties + std::tie(value_begin, value_end) = feature_message.get_packed_uint32(); + BOOST_CHECK_EQUAL(std::distance(value_begin, value_end), 6); + auto iter = value_begin; + BOOST_CHECK_EQUAL(*iter++, 0); // bearing_in key + *iter++; + BOOST_CHECK_EQUAL(*iter++, 1); // bearing_out key + *iter++; + BOOST_CHECK_EQUAL(*iter++, 2); // weight key + *iter++; // skip value check, can be valud uint32 + BOOST_CHECK(iter == value_end); + // geometry + feature_message.next(); + std::tie(value_begin, value_end) = feature_message.get_packed_uint32(); + BOOST_CHECK_GT(std::distance(value_begin, value_end), 1); + }; + + auto number_of_turn_keys = 0u; + + while (layer_message.next()) + { + switch(layer_message.tag()) + { + case util::vector_tile::VERSION_TAG: + BOOST_CHECK_EQUAL(layer_message.get_uint32(), 2); + break; + case util::vector_tile::NAME_TAG: + BOOST_CHECK_EQUAL(layer_message.get_string(), "turns"); + break; + case util::vector_tile::EXTENT_TAG: + BOOST_CHECK_EQUAL(layer_message.get_uint32(), util::vector_tile::EXTENT); + break; + case util::vector_tile::FEATURE_TAG: + check_turn_feature(layer_message.get_message()); + break; + case util::vector_tile::KEY_TAG: + layer_message.get_string(); + number_of_turn_keys++; + break; + case util::vector_tile::VARIANT_TAG: + check_value(layer_message.get_message()); + break; + default: + BOOST_CHECK(false); // invalid tag + break; + } + } + + BOOST_CHECK_EQUAL(number_of_turn_keys, 3); } BOOST_AUTO_TEST_SUITE_END()