Construct an adjacency list in order to discover turns.
This commit is contained in:
parent
0b7b16abc0
commit
805d93912d
@ -28,6 +28,7 @@ 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;
|
||||
const constexpr std::uint32_t VARIANT_TYPE_FLOAT = 2;
|
||||
|
||||
// Vector tiles are 4096 virtual pixels on each side
|
||||
const constexpr double EXTENT = 4096.0;
|
||||
|
@ -17,6 +17,8 @@
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@ -31,8 +33,8 @@ namespace plugins
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
// TODO: Port all this encoding logic to https://github.com/mapbox/vector-tile, which wasn't available
|
||||
// when this code was originally written.
|
||||
// TODO: Port all this encoding logic to https://github.com/mapbox/vector-tile, which wasn't
|
||||
// available when this code was originally written.
|
||||
|
||||
// Simple container class for WGS84 coordinates
|
||||
template <typename T> struct Point final
|
||||
@ -43,6 +45,7 @@ template <typename T> struct Point final
|
||||
const T y;
|
||||
};
|
||||
|
||||
// Simple container to hold a bounding box
|
||||
struct BBox final
|
||||
{
|
||||
BBox(const double _minx, const double _miny, const double _maxx, const double _maxy)
|
||||
@ -68,13 +71,20 @@ struct point_type_i final
|
||||
const std::int64_t y;
|
||||
};
|
||||
|
||||
// Used to accumulate all the information we want in the tile about
|
||||
// a turn.
|
||||
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)
|
||||
TurnData(const util::Coordinate coordinate_,
|
||||
const std::size_t _in,
|
||||
const std::size_t _out,
|
||||
const std::size_t _weight)
|
||||
: coordinate(coordinate_), in_angle_offset(_in), turn_angle_offset(_out),
|
||||
weight_offset(_weight)
|
||||
{
|
||||
}
|
||||
|
||||
const util::Coordinate coordinate;
|
||||
const std::size_t in_angle_offset;
|
||||
const std::size_t turn_angle_offset;
|
||||
const std::size_t weight_offset;
|
||||
@ -86,6 +96,11 @@ using FloatPoint = detail::Point<double>;
|
||||
using FixedLine = std::vector<FixedPoint>;
|
||||
using FloatLine = std::vector<FloatPoint>;
|
||||
|
||||
constexpr const static int MIN_ZOOM_FOR_TURNS = 15;
|
||||
|
||||
// We use boost::geometry to clip lines/points that are outside or cross the boundary
|
||||
// of the tile we're rendering. We need these types defined to use boosts clipping
|
||||
// logic
|
||||
typedef boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian> point_t;
|
||||
typedef boost::geometry::model::linestring<point_t> linestring_t;
|
||||
typedef boost::geometry::model::box<point_t> box_t;
|
||||
@ -147,6 +162,15 @@ inline bool encodePoint(const FixedPoint &pt, protozero::packed_field_uint32 &ge
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returnx the x1,y1,x2,y2 pixel coordinates of a line in a given
|
||||
* tile.
|
||||
*
|
||||
* @param start the first coordinate of the line
|
||||
* @param target the last coordinate of the line
|
||||
* @param tile_bbox the boundaries of the tile, in mercator coordinates
|
||||
* @return a FixedLine with coordinates relative to the tile_bbox.
|
||||
*/
|
||||
FixedLine coordinatesToTileLine(const util::Coordinate start,
|
||||
const util::Coordinate target,
|
||||
const detail::BBox &tile_bbox)
|
||||
@ -197,6 +221,13 @@ FixedLine coordinatesToTileLine(const util::Coordinate start,
|
||||
return tile_line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts lon/lat into coordinates inside a Mercator projection tile (x/y pixel values)
|
||||
*
|
||||
* @param point the lon/lat you want the tile coords for
|
||||
* @param tile_bbox the mercator boundaries of the tile
|
||||
* @return a point (x,y) on the tile defined by tile_bbox
|
||||
*/
|
||||
FixedPoint coordinatesToTilePoint(const util::Coordinate point, const detail::BBox &tile_bbox)
|
||||
{
|
||||
const FloatPoint geo_point{static_cast<double>(util::toFloating(point.lon)),
|
||||
@ -217,7 +248,8 @@ FixedPoint coordinatesToTilePoint(const util::Coordinate point, const detail::BB
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpacks a single CH edge (NodeID->NodeID) down to the original edges, and returns a list of the edge data
|
||||
* Unpacks a single CH edge (NodeID->NodeID) down to the original edges, and returns a list of the
|
||||
* edge data
|
||||
* @param from the node the CH edge starts at
|
||||
* @param to the node the CH edge finishes at
|
||||
* @param unpacked_path the sequence of EdgeData objects along the unpacked path
|
||||
@ -228,15 +260,15 @@ void UnpackEdgeToEdges(const datafacade::BaseDataFacade &facade,
|
||||
std::vector<datafacade::BaseDataFacade::EdgeData> &unpacked_path)
|
||||
{
|
||||
std::array<NodeID, 2> path{{from, to}};
|
||||
UnpackCHEdge(
|
||||
&facade,
|
||||
path.begin(),
|
||||
path.end(),
|
||||
[&unpacked_path](const std::pair<NodeID, NodeID> & /* edge */, const datafacade::BaseDataFacade::EdgeData &data) {
|
||||
unpacked_path.emplace_back(data);
|
||||
});
|
||||
}
|
||||
UnpackCHEdge(&facade,
|
||||
path.begin(),
|
||||
path.end(),
|
||||
[&unpacked_path](const std::pair<NodeID, NodeID> & /* edge */,
|
||||
const datafacade::BaseDataFacade::EdgeData &data) {
|
||||
unpacked_path.emplace_back(data);
|
||||
});
|
||||
}
|
||||
} // ::detail namespace
|
||||
|
||||
Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::string &pbf_buffer)
|
||||
{
|
||||
@ -255,7 +287,12 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
// This hits the OSRM StaticRTree
|
||||
const auto edges = facade.GetEdgesInBox(southwest, northeast);
|
||||
|
||||
// Vector tiles encode data values as lookup tables. This vector is the lookup table
|
||||
// Vector tiles encode properties as references to a common lookup table.
|
||||
// When we add a property to a "feature", we actually attach the index of the value
|
||||
// rather than the value itself. Thus, we need to keep a list of the unique
|
||||
// values we need, and we add this list to the tile as a lookup table. This
|
||||
// vector holds all the actual used values, the feature refernce offsets in
|
||||
// this vector.
|
||||
// for integer values
|
||||
std::vector<int> used_line_ints;
|
||||
// While constructing the tile, we keep track of which integers we have in our table
|
||||
@ -264,16 +301,24 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
|
||||
// Same idea for street names - one lookup table for names for all features
|
||||
std::vector<std::string> names;
|
||||
// And an index of the names and their position in the list
|
||||
std::unordered_map<std::string, std::size_t> name_offsets;
|
||||
|
||||
// And again for integer values used by points.
|
||||
std::vector<int> used_point_ints;
|
||||
std::unordered_map<int, std::size_t> point_int_offsets;
|
||||
|
||||
uint8_t max_datasource_id = 0;
|
||||
std::vector<std::vector<detail::TurnData>> all_turn_data;
|
||||
// And again for float values used by points
|
||||
std::vector<float> used_point_floats;
|
||||
std::unordered_map<float, std::size_t> point_float_offsets;
|
||||
|
||||
uint8_t max_datasource_id = 0;
|
||||
|
||||
// This is where we accumulate information on turns
|
||||
std::vector<detail::TurnData> all_turn_data;
|
||||
|
||||
// Helper function for adding a new value to the line_ints lookup table. Returns
|
||||
// the index of the value in the table, adding the value if it doesn't already
|
||||
// exist
|
||||
const auto use_line_value = [&used_line_ints, &line_int_offsets](const int &value) {
|
||||
const auto found = line_int_offsets.find(value);
|
||||
|
||||
@ -286,7 +331,8 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
return;
|
||||
};
|
||||
|
||||
const auto use_point_value = [&used_point_ints, &point_int_offsets](const int &value) {
|
||||
// Same again
|
||||
const auto use_point_int_value = [&used_point_ints, &point_int_offsets](const int &value) {
|
||||
const auto found = point_int_offsets.find(value);
|
||||
std::size_t offset;
|
||||
|
||||
@ -304,15 +350,253 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
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.
|
||||
// And a third time, should probably template this....
|
||||
const auto use_point_float_value = [&used_point_floats,
|
||||
&point_float_offsets](const float &value) {
|
||||
const auto found = point_float_offsets.find(value);
|
||||
std::size_t offset;
|
||||
|
||||
if (found == point_float_offsets.end())
|
||||
{
|
||||
used_point_floats.push_back(value);
|
||||
offset = used_point_floats.size() - 1;
|
||||
point_float_offsets[value] = offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = found->second;
|
||||
}
|
||||
|
||||
return offset;
|
||||
};
|
||||
|
||||
// If we're zooming into 16 or higher, include turn data. Why? Because turns make the map
|
||||
// really
|
||||
// cramped, so we don't bother including the data for tiles that span a large area.
|
||||
if (parameters.z >= detail::MIN_ZOOM_FOR_TURNS)
|
||||
{
|
||||
// Struct to hold info on all the EdgeBasedNodes that are visible in our tile
|
||||
// When we create these, we insure that (source, target) and packed_geometry_id
|
||||
// are all pointed in the same direction.
|
||||
struct EdgeBasedNodeInfo
|
||||
{
|
||||
unsigned source_intersection; // node-based-node ID
|
||||
unsigned target_intersection; // node-based-node ID
|
||||
unsigned packed_geometry_id;
|
||||
};
|
||||
// Lookup table for edge-based-nodes
|
||||
std::unordered_map<NodeID, EdgeBasedNodeInfo> edge_based_node_info;
|
||||
std::unordered_map<NodeID, std::vector<EdgeID>> outgoing_edges;
|
||||
std::unordered_map<NodeID, std::vector<EdgeID>> incoming_edges;
|
||||
|
||||
// Now, loop over all the road segments we saw, and build up a mini
|
||||
// graph for just the network that's visible.
|
||||
for (const auto &edge : edges)
|
||||
{
|
||||
// Note: edge.u is the node-based node ID of the source intersection
|
||||
// edge.v is the node-based node ID of the target intersection
|
||||
// both these values can be directly looked up for coordinates
|
||||
|
||||
// If forward travel is enabled on this road section, and we haven't seen this
|
||||
// edge-based-node
|
||||
// before
|
||||
if (edge.forward_segment_id.enabled &&
|
||||
edge_based_node_info.count(edge.forward_segment_id.id) == 0)
|
||||
{
|
||||
// Add this edge-based-nodeid as an outgoing from the source intersection
|
||||
auto f = outgoing_edges.find(edge.u);
|
||||
if (f != outgoing_edges.end())
|
||||
{
|
||||
f->second.push_back(edge.forward_segment_id.id);
|
||||
}
|
||||
else
|
||||
{
|
||||
outgoing_edges[edge.u] = {edge.forward_segment_id.id};
|
||||
}
|
||||
|
||||
// Add this edge-based-nodeid as an incoming to the target intersection
|
||||
f = incoming_edges.find(edge.v);
|
||||
if (f != incoming_edges.end())
|
||||
{
|
||||
f->second.push_back(edge.forward_segment_id.id);
|
||||
}
|
||||
else
|
||||
{
|
||||
incoming_edges[edge.v] = {edge.forward_segment_id.id};
|
||||
}
|
||||
|
||||
edge_based_node_info[edge.forward_segment_id.id] = {
|
||||
edge.u, edge.v, edge.forward_packed_geometry_id};
|
||||
}
|
||||
// Same as previous block, but everything flipped
|
||||
if (edge.reverse_segment_id.enabled &&
|
||||
edge_based_node_info.count(edge.reverse_segment_id.id) == 0)
|
||||
{
|
||||
auto f = outgoing_edges.find(edge.v);
|
||||
if (f != outgoing_edges.end())
|
||||
{
|
||||
f->second.push_back(edge.reverse_segment_id.id);
|
||||
}
|
||||
else
|
||||
{
|
||||
outgoing_edges[edge.v] = {edge.reverse_segment_id.id};
|
||||
}
|
||||
|
||||
f = incoming_edges.find(edge.u);
|
||||
if (f != incoming_edges.end())
|
||||
{
|
||||
f->second.push_back(edge.reverse_segment_id.id);
|
||||
}
|
||||
else
|
||||
{
|
||||
incoming_edges[edge.u] = {edge.reverse_segment_id.id};
|
||||
}
|
||||
|
||||
// Save info about this edge-based-node, note reversal from forward
|
||||
// block above.
|
||||
edge_based_node_info[edge.reverse_segment_id.id] = {
|
||||
edge.v, edge.u, edge.reverse_packed_geometry_id};
|
||||
}
|
||||
}
|
||||
|
||||
// Now, for every edge-based-node that we discovered (edge-based-nodes are sources
|
||||
// and targets of turns). EBN is short for edge-based-node
|
||||
for (const auto &source_ebn : edge_based_node_info)
|
||||
{
|
||||
// Grab a copy of the geometry leading up to the intersection.
|
||||
std::vector<NodeID> first_geometry;
|
||||
facade.GetUncompressedGeometry(source_ebn.second.packed_geometry_id, first_geometry);
|
||||
|
||||
// We earlier saved the source and target intersection nodes for every road section.
|
||||
// We can use the target node to find all road sections that lead away from
|
||||
// the intersection, and thus
|
||||
// in the graph after our main
|
||||
for (const auto &target_ebn : outgoing_edges[source_ebn.second.target_intersection])
|
||||
{
|
||||
// Ignore u-turns for now
|
||||
if (edge_based_node_info.at(target_ebn).target_intersection ==
|
||||
source_ebn.second.source_intersection)
|
||||
continue;
|
||||
|
||||
// Find the connection between our source road and the target node
|
||||
EdgeID smaller_edge_id = facade.FindSmallestEdge(
|
||||
source_ebn.first, target_ebn, [](const contractor::QueryEdge::EdgeData &data) {
|
||||
return data.forward;
|
||||
});
|
||||
|
||||
// Depending on how the graph is constructed, we might have to look for
|
||||
// a backwards edge instead. They're equivalent, just one is available for
|
||||
// a forward routing search, and one is used for the backwards dijkstra
|
||||
// steps. Their weight should be the same, we can use either one.
|
||||
// If we didn't find a forward edge, try for a backward one
|
||||
if (SPECIAL_EDGEID == smaller_edge_id)
|
||||
{
|
||||
smaller_edge_id = facade.FindSmallestEdge(
|
||||
target_ebn, source_ebn.first, [](const contractor::QueryEdge::EdgeData &data) {
|
||||
return data.backward;
|
||||
});
|
||||
}
|
||||
|
||||
// If no edge was found, it means that there's no connection between these nodes,
|
||||
// due to oneways or turn restrictions. Given the edge-based-nodes that
|
||||
// we're examining here, we *should* only find directly-connected edges, not
|
||||
// shortcuts
|
||||
if (smaller_edge_id != SPECIAL_EDGEID)
|
||||
{
|
||||
// Check to see if it was a shortcut edge we found. This can happen
|
||||
// when exactly? Anyway, unpack it and get the first "real" edgedata
|
||||
// out of it, which should represent the first hop, which is the one
|
||||
// we want to find the turn.
|
||||
auto data = facade.GetEdgeData(smaller_edge_id);
|
||||
if (data.shortcut)
|
||||
{
|
||||
std::vector<contractor::QueryEdge::EdgeData> unpacked_shortcut;
|
||||
detail::UnpackEdgeToEdges(facade, source_ebn.first, target_ebn, unpacked_shortcut);
|
||||
data = unpacked_shortcut.front();
|
||||
}
|
||||
BOOST_ASSERT_MSG(!data.shortcut, "Connecting edge must not be a shortcut");
|
||||
|
||||
// This is the geometry leading away from the intersection
|
||||
// (i.e. the geometry of the target edge-based-node)
|
||||
std::vector<NodeID> second_geometry;
|
||||
facade.GetUncompressedGeometry(
|
||||
edge_based_node_info.at(target_ebn).packed_geometry_id, second_geometry);
|
||||
|
||||
// Now, calculate the sum of the weight of all the segments.
|
||||
std::vector<EdgeWeight> forward_weight_vector;
|
||||
facade.GetUncompressedWeights(source_ebn.second.packed_geometry_id,
|
||||
forward_weight_vector);
|
||||
const auto sum_node_weight = std::accumulate(
|
||||
forward_weight_vector.begin(), forward_weight_vector.end(), 0);
|
||||
|
||||
// The edge.distance is the whole edge weight, which includes the turn cost.
|
||||
// The turn cost is the edge.distance minus the sum of the individual road
|
||||
// segment weights. This might not be 100% accurate, because some
|
||||
// intersections include stop signs, traffic signals and other penalties,
|
||||
// but at this stage, we can't divide those out, so we just treat the whole
|
||||
// lot as the "turn cost" that we'll stick on the map.
|
||||
const auto turn_cost = data.distance - sum_node_weight;
|
||||
|
||||
// Find the three nodes that make up the turn movement)
|
||||
const auto node_from = first_geometry.size() > 1
|
||||
? *(first_geometry.end() - 2)
|
||||
: source_ebn.second.source_intersection;
|
||||
const auto node_via = source_ebn.second.target_intersection;
|
||||
const auto node_to = second_geometry.front();
|
||||
|
||||
const auto coord_from = facade.GetCoordinateOfNode(node_from);
|
||||
const auto coord_via = facade.GetCoordinateOfNode(node_via);
|
||||
const auto coord_to = facade.GetCoordinateOfNode(node_to);
|
||||
|
||||
// Calculate the bearing that we approach the intersection at
|
||||
const auto angle_in = static_cast<int>(
|
||||
util::coordinate_calculation::bearing(coord_from, coord_via));
|
||||
|
||||
// Add the angle to the values table for the vector tile, and get the index
|
||||
// of that value in the table
|
||||
const auto angle_in_index = use_point_int_value(angle_in);
|
||||
|
||||
// Calculate the bearing leading away from the intersection
|
||||
const auto exit_bearing = static_cast<int>(
|
||||
util::coordinate_calculation::bearing(coord_via, coord_to));
|
||||
|
||||
// Figure out the angle of the turn
|
||||
auto turn_angle = exit_bearing - angle_in;
|
||||
while (turn_angle > 180)
|
||||
{
|
||||
turn_angle -= 360;
|
||||
}
|
||||
while (turn_angle < -180)
|
||||
{
|
||||
turn_angle += 360;
|
||||
}
|
||||
|
||||
// Add the turn angle value to the value lookup table for the vector tile.
|
||||
const auto turn_angle_index = use_point_int_value(turn_angle);
|
||||
// And, same for the actual turn cost value - it goes in the lookup table,
|
||||
// not directly on the feature itself.
|
||||
const auto turn_cost_index =
|
||||
use_point_float_value(turn_cost / 10.0); // Note conversion to float here
|
||||
|
||||
// Save everything we need to later add all the points to the tile.
|
||||
// We need the coordinate of the intersection, the angle in, the turn
|
||||
// angle and the turn cost.
|
||||
all_turn_data.emplace_back(detail::TurnData{
|
||||
coord_via, angle_in_index, turn_angle_index, turn_cost_index});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Vector tiles encode feature properties as indexes into a lookup table. So, we need to
|
||||
// "pre-loop" over all the edges to create the lookup tables. Once we have those, we
|
||||
// can then encode the features, and we'll know the indexes that feature properties
|
||||
// need to refer to.
|
||||
for (const auto &edge : edges)
|
||||
{
|
||||
int forward_weight = 0, reverse_weight = 0;
|
||||
uint8_t forward_datasource = 0;
|
||||
uint8_t reverse_datasource = 0;
|
||||
std::vector<detail::TurnData> edge_turn_data;
|
||||
// TODO this approach of writing at least an empty vector for any segment is probably stupid
|
||||
// (inefficient)
|
||||
|
||||
@ -328,105 +612,6 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
forward_datasource = forward_datasource_vector[edge.fwd_segment_position];
|
||||
|
||||
use_line_value(forward_weight);
|
||||
|
||||
std::vector<NodeID> 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)
|
||||
{
|
||||
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<NodeID, int> c_nodes;
|
||||
|
||||
for (const auto adj_shortcut :
|
||||
facade.GetAdjacentEdgeRange(edge.forward_segment_id.id))
|
||||
{
|
||||
std::vector<contractor::QueryEdge::EdgeData> unpacked_shortcut;
|
||||
|
||||
// Outgoing shortcuts without `forward` travel enabled: do not want
|
||||
if (!facade.GetEdgeData(adj_shortcut).forward)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
detail::UnpackEdgeToEdges(facade,
|
||||
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<NodeID> 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<int>(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<int>(
|
||||
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});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (edge.reverse_packed_geometry_id != SPECIAL_EDGEID)
|
||||
@ -439,13 +624,13 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
reverse_weight =
|
||||
reverse_weight_vector[reverse_weight_vector.size() - edge.fwd_segment_position - 1];
|
||||
|
||||
use_line_value(reverse_weight);
|
||||
|
||||
std::vector<uint8_t> 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];
|
||||
|
||||
use_line_value(reverse_weight);
|
||||
}
|
||||
// Keep track of the highest datasource seen so that we don't write unnecessary
|
||||
// data to the layer attribute values
|
||||
@ -458,12 +643,8 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
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
|
||||
|
||||
// Convert tile coordinates into mercator coordinates
|
||||
util::web_mercator::xyzToMercator(
|
||||
parameters.x, parameters.y, parameters.z, min_lon, min_lat, max_lon, max_lat);
|
||||
@ -714,108 +895,90 @@ Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::str
|
||||
}
|
||||
}
|
||||
|
||||
// Only add the turn layer to the tile if it has some features (we sometimes won't
|
||||
// for tiles of z<16, and tiles that don't show any intersections)
|
||||
if (!all_turn_data.empty())
|
||||
{
|
||||
// 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
|
||||
|
||||
// Begin the layer features block
|
||||
// Begin writing the set of point features
|
||||
{
|
||||
// Each feature gets a unique id, starting at 1
|
||||
unsigned id = 1;
|
||||
for (uint64_t i = 0; i < edges.size(); i++)
|
||||
// Start each features with an ID starting at 1
|
||||
int id = 1;
|
||||
|
||||
// Helper function to encode a new point feature on a vector tile.
|
||||
const auto encode_tile_point = [&point_layer_writer, &used_point_ints, &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
|
||||
feature_writer.add_uint64(util::vector_tile::ID_TAG, id++); // id
|
||||
{
|
||||
// Write out the 3 properties we want on the feature. These
|
||||
// refer to indexes in the properties lookup table, which we
|
||||
// add to the tile after we add all features.
|
||||
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); // "cost" tag key offset
|
||||
field.add_element(used_point_ints.size() + point_turn_data.weight_offset);
|
||||
}
|
||||
{
|
||||
// Add the geometry as the last field in this feature
|
||||
protozero::packed_field_uint32 geometry(
|
||||
feature_writer, util::vector_tile::FEATURE_GEOMETRIES_TAG);
|
||||
encodePoint(tile_point, geometry);
|
||||
}
|
||||
};
|
||||
|
||||
// Loop over all the turns we found and add them as features to the layer
|
||||
for (const auto &turndata : all_turn_data)
|
||||
{
|
||||
const auto &edge = edges[i];
|
||||
const auto &edge_turn_data = all_turn_data[i];
|
||||
|
||||
// Skip writing for edges with no turn penalty data
|
||||
if (edge_turn_data.empty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<NodeID> 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);
|
||||
|
||||
const auto tile_point = coordinatesToTilePoint(turndata.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);
|
||||
}
|
||||
encode_tile_point(tile_point, turndata);
|
||||
}
|
||||
}
|
||||
|
||||
// Field id 3 is the "keys" attribute
|
||||
// We need two "key" fields, these are referred to with 0 and 1 (their array indexes)
|
||||
// earlier
|
||||
// Add the names of the three attributes we added to all the turn penalty
|
||||
// features previously. The indexes used there refer to these keys.
|
||||
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");
|
||||
point_layer_writer.add_string(util::vector_tile::KEY_TAG, "cost");
|
||||
|
||||
// Now, we write out the possible integer values.
|
||||
// Now, save the lists of integers and floats that our features refer to.
|
||||
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);
|
||||
}
|
||||
for (const auto &value : used_point_floats)
|
||||
{
|
||||
protozero::pbf_writer values_writer(point_layer_writer,
|
||||
util::vector_tile::VARIANT_TAG);
|
||||
values_writer.add_float(util::vector_tile::VARIANT_TYPE_FLOAT, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
// protozero serializes data during object destructors, so once the scope closes,
|
||||
// our result buffer will have all the tile data encoded into it.
|
||||
|
||||
return Status::Ok;
|
||||
}
|
||||
|
@ -88,6 +88,9 @@ BOOST_AUTO_TEST_CASE(test_tile)
|
||||
case util::vector_tile::VARIANT_TYPE_DOUBLE:
|
||||
value.get_double();
|
||||
break;
|
||||
case util::vector_tile::VARIANT_TYPE_FLOAT:
|
||||
value.get_float();
|
||||
break;
|
||||
case util::vector_tile::VARIANT_TYPE_STRING:
|
||||
value.get_string();
|
||||
break;
|
||||
@ -160,9 +163,9 @@ BOOST_AUTO_TEST_CASE(test_tile)
|
||||
auto iter = value_begin;
|
||||
BOOST_CHECK_EQUAL(*iter++, 0); // bearing_in key
|
||||
*iter++;
|
||||
BOOST_CHECK_EQUAL(*iter++, 1); // bearing_out key
|
||||
BOOST_CHECK_EQUAL(*iter++, 1); // turn_angle key
|
||||
*iter++;
|
||||
BOOST_CHECK_EQUAL(*iter++, 2); // weight key
|
||||
BOOST_CHECK_EQUAL(*iter++, 2); // cost key
|
||||
*iter++; // skip value check, can be valud uint32
|
||||
BOOST_CHECK(iter == value_end);
|
||||
// geometry
|
||||
|
Loading…
Reference in New Issue
Block a user