2016-03-02 12:26:44 -05:00
|
|
|
#include "engine/plugins/tile.hpp"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "engine/plugins/plugin_base.hpp"
|
2016-03-02 12:26:44 -05:00
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
#include "util/coordinate_calculation.hpp"
|
2016-04-14 10:33:23 -04:00
|
|
|
#include "util/vector_tile.hpp"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "util/web_mercator.hpp"
|
2016-03-16 16:54:29 -04:00
|
|
|
|
2016-03-21 21:13:31 -04:00
|
|
|
#include <boost/geometry.hpp>
|
|
|
|
#include <boost/geometry/geometries/geometries.hpp>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <boost/geometry/geometries/point_xy.hpp>
|
2016-03-23 13:55:56 -04:00
|
|
|
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
|
2016-03-21 21:13:31 -04:00
|
|
|
|
2016-03-02 12:26:44 -05:00
|
|
|
#include <protozero/pbf_writer.hpp>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <protozero/varint.hpp>
|
2016-03-02 12:26:44 -05:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <vector>
|
2016-03-02 12:26:44 -05:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace engine
|
|
|
|
{
|
|
|
|
namespace plugins
|
|
|
|
{
|
2016-03-16 16:54:29 -04:00
|
|
|
namespace detail
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
2016-03-28 14:38:19 -04:00
|
|
|
// Simple container class for WGS84 coordinates
|
2016-03-16 16:54:29 -04:00
|
|
|
template <typename T> struct Point final
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
2016-03-16 16:54:29 -04:00
|
|
|
Point(T _x, T _y) : x(_x), y(_y) {}
|
2016-03-02 12:26:44 -05:00
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
const T x;
|
|
|
|
const T y;
|
|
|
|
};
|
2016-03-02 12:26:44 -05:00
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
// from mapnik-vector-tile
|
|
|
|
namespace pbf
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
2016-03-16 16:54:29 -04:00
|
|
|
inline unsigned encode_length(const unsigned len) { return (len << 3u) | 2u; }
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
struct BBox final
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
2016-03-16 16:54:29 -04:00
|
|
|
BBox(const double _minx, const double _miny, const double _maxx, const double _maxy)
|
2016-03-02 12:26:44 -05:00
|
|
|
: minx(_minx), miny(_miny), maxx(_maxx), maxy(_maxy)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
double width() const { return maxx - minx; }
|
|
|
|
double height() const { return maxy - miny; }
|
|
|
|
|
|
|
|
const double minx;
|
|
|
|
const double miny;
|
|
|
|
const double maxx;
|
|
|
|
const double maxy;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Simple container for integer coordinates (i.e. pixel coords)
|
|
|
|
struct point_type_i final
|
|
|
|
{
|
|
|
|
point_type_i(std::int64_t _x, std::int64_t _y) : x(_x), y(_y) {}
|
|
|
|
|
|
|
|
const std::int64_t x;
|
|
|
|
const std::int64_t y;
|
|
|
|
};
|
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
using FixedLine = std::vector<detail::Point<std::int32_t>>;
|
|
|
|
using FloatLine = std::vector<detail::Point<double>>;
|
2016-03-02 12:26:44 -05:00
|
|
|
|
2016-03-21 21:13:31 -04:00
|
|
|
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;
|
|
|
|
typedef boost::geometry::model::multi_linestring<linestring_t> multi_linestring_t;
|
2016-04-14 10:33:23 -04:00
|
|
|
const static box_t clip_box(point_t(-util::vector_tile::BUFFER, -util::vector_tile::BUFFER),
|
|
|
|
point_t(util::vector_tile::EXTENT + util::vector_tile::BUFFER,
|
|
|
|
util::vector_tile::EXTENT + util::vector_tile::BUFFER));
|
2016-03-21 21:13:31 -04:00
|
|
|
|
2016-03-02 12:26:44 -05:00
|
|
|
// from mapnik-vector-tile
|
|
|
|
// Encodes a linestring using protobuf zigzag encoding
|
2016-03-18 16:57:36 -04:00
|
|
|
inline bool encodeLinestring(const FixedLine &line,
|
2016-03-16 16:54:29 -04:00
|
|
|
protozero::packed_field_uint32 &geometry,
|
|
|
|
std::int32_t &start_x,
|
|
|
|
std::int32_t &start_y)
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
|
|
|
const std::size_t line_size = line.size();
|
|
|
|
if (line_size < 2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned line_to_length = static_cast<const unsigned>(line_size) - 1;
|
|
|
|
|
|
|
|
auto pt = line.begin();
|
|
|
|
geometry.add_element(9); // move_to | (1 << 3)
|
|
|
|
geometry.add_element(protozero::encode_zigzag32(pt->x - start_x));
|
|
|
|
geometry.add_element(protozero::encode_zigzag32(pt->y - start_y));
|
|
|
|
start_x = pt->x;
|
|
|
|
start_y = pt->y;
|
2016-03-16 16:54:29 -04:00
|
|
|
geometry.add_element(detail::pbf::encode_length(line_to_length));
|
2016-03-02 12:26:44 -05:00
|
|
|
for (++pt; pt != line.end(); ++pt)
|
|
|
|
{
|
|
|
|
const std::int32_t dx = pt->x - start_x;
|
|
|
|
const std::int32_t dy = pt->y - start_y;
|
|
|
|
geometry.add_element(protozero::encode_zigzag32(dx));
|
|
|
|
geometry.add_element(protozero::encode_zigzag32(dy));
|
|
|
|
start_x = pt->x;
|
|
|
|
start_y = pt->y;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
FixedLine coordinatesToTileLine(const util::Coordinate start,
|
|
|
|
const util::Coordinate target,
|
|
|
|
const detail::BBox &tile_bbox)
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
2016-03-16 16:54:29 -04:00
|
|
|
FloatLine geo_line;
|
|
|
|
geo_line.emplace_back(static_cast<double>(util::toFloating(start.lon)),
|
|
|
|
static_cast<double>(util::toFloating(start.lat)));
|
|
|
|
geo_line.emplace_back(static_cast<double>(util::toFloating(target.lon)),
|
|
|
|
static_cast<double>(util::toFloating(target.lat)));
|
2016-03-21 21:13:31 -04:00
|
|
|
|
|
|
|
linestring_t unclipped_line;
|
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
for (auto const &pt : geo_line)
|
|
|
|
{
|
2016-04-08 20:18:47 -04:00
|
|
|
double px_merc = pt.x * util::web_mercator::DEGREE_TO_PX;
|
|
|
|
double py_merc = util::web_mercator::latToY(util::FloatLatitude(pt.y)) *
|
|
|
|
util::web_mercator::DEGREE_TO_PX;
|
2016-03-16 16:54:29 -04:00
|
|
|
// convert lon/lat to tile coordinates
|
|
|
|
const auto px = std::round(
|
2016-04-08 20:18:47 -04:00
|
|
|
((px_merc - tile_bbox.minx) * util::web_mercator::TILE_SIZE / tile_bbox.width()) *
|
2016-04-14 10:33:23 -04:00
|
|
|
util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE);
|
2016-03-16 16:54:29 -04:00
|
|
|
const auto py = std::round(
|
2016-04-08 20:18:47 -04:00
|
|
|
((tile_bbox.maxy - py_merc) * util::web_mercator::TILE_SIZE / tile_bbox.height()) *
|
2016-04-14 10:33:23 -04:00
|
|
|
util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE);
|
2016-03-21 21:13:31 -04:00
|
|
|
|
|
|
|
boost::geometry::append(unclipped_line, point_t(px, py));
|
2016-03-16 16:54:29 -04:00
|
|
|
}
|
2016-03-21 21:13:31 -04:00
|
|
|
|
|
|
|
multi_linestring_t clipped_line;
|
|
|
|
|
|
|
|
boost::geometry::intersection(clip_box, unclipped_line, clipped_line);
|
|
|
|
|
|
|
|
FixedLine tile_line;
|
|
|
|
|
|
|
|
// b::g::intersection might return a line with one point if the
|
|
|
|
// original line was very short and coords were dupes
|
|
|
|
if (!clipped_line.empty() && clipped_line[0].size() == 2)
|
|
|
|
{
|
|
|
|
if (clipped_line[0].size() == 2)
|
|
|
|
{
|
|
|
|
for (const auto &p : clipped_line[0])
|
|
|
|
{
|
|
|
|
tile_line.emplace_back(p.get<0>(), p.get<1>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
return tile_line;
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 12:26:44 -05:00
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
Status TilePlugin::HandleRequest(const api::TileParameters ¶meters, std::string &pbf_buffer)
|
|
|
|
{
|
2016-03-18 10:30:28 -04:00
|
|
|
BOOST_ASSERT(parameters.IsValid());
|
|
|
|
|
2016-03-02 12:26:44 -05:00
|
|
|
double min_lon, min_lat, max_lon, max_lat;
|
|
|
|
|
2016-03-28 14:38:19 -04:00
|
|
|
// Convert the z,x,y mercator tile coordinates into WGS84 lon/lat values
|
2016-05-27 15:05:04 -04:00
|
|
|
util::web_mercator::xyzToWGS84(
|
|
|
|
parameters.x, parameters.y, parameters.z, min_lon, min_lat, max_lon, max_lat);
|
2016-03-02 12:26:44 -05:00
|
|
|
|
|
|
|
util::Coordinate southwest{util::FloatLongitude(min_lon), util::FloatLatitude(min_lat)};
|
|
|
|
util::Coordinate northeast{util::FloatLongitude(max_lon), util::FloatLatitude(max_lat)};
|
|
|
|
|
|
|
|
// Fetch all the segments that are in our bounding box.
|
|
|
|
// This hits the OSRM StaticRTree
|
|
|
|
const auto edges = facade.GetEdgesInBox(southwest, northeast);
|
|
|
|
|
2016-03-18 16:57:36 -04:00
|
|
|
std::vector<int> used_weights;
|
|
|
|
std::unordered_map<int, std::size_t> weight_offsets;
|
|
|
|
uint8_t max_datasource_id = 0;
|
2016-06-10 14:15:14 -04:00
|
|
|
std::vector<std::string> names;
|
|
|
|
std::unordered_map<std::string, std::size_t> name_offsets;
|
2016-03-18 16:57:36 -04:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
for (const auto &edge : edges)
|
|
|
|
{
|
|
|
|
int forward_weight = 0, reverse_weight = 0;
|
|
|
|
uint8_t forward_datasource = 0;
|
|
|
|
uint8_t reverse_datasource = 0;
|
|
|
|
|
|
|
|
if (edge.forward_packed_geometry_id != SPECIAL_EDGEID)
|
|
|
|
{
|
|
|
|
std::vector<EdgeWeight> forward_weight_vector;
|
|
|
|
facade.GetUncompressedWeights(edge.forward_packed_geometry_id, forward_weight_vector);
|
|
|
|
forward_weight = forward_weight_vector[edge.fwd_segment_position];
|
|
|
|
|
|
|
|
std::vector<uint8_t> forward_datasource_vector;
|
|
|
|
facade.GetUncompressedDatasources(edge.forward_packed_geometry_id,
|
|
|
|
forward_datasource_vector);
|
|
|
|
forward_datasource = forward_datasource_vector[edge.fwd_segment_position];
|
|
|
|
|
|
|
|
if (weight_offsets.find(forward_weight) == weight_offsets.end())
|
|
|
|
{
|
|
|
|
used_weights.push_back(forward_weight);
|
|
|
|
weight_offsets[forward_weight] = used_weights.size() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edge.reverse_packed_geometry_id != SPECIAL_EDGEID)
|
|
|
|
{
|
|
|
|
std::vector<EdgeWeight> 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];
|
|
|
|
|
|
|
|
if (weight_offsets.find(reverse_weight) == weight_offsets.end())
|
|
|
|
{
|
|
|
|
used_weights.push_back(reverse_weight);
|
|
|
|
weight_offsets[reverse_weight] = used_weights.size() - 1;
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
// 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);
|
2016-06-10 14:15:14 -04:00
|
|
|
|
|
|
|
std::string name = facade.GetNameForID(edge.name_id);
|
|
|
|
|
|
|
|
if (name_offsets.find(name) == name_offsets.end())
|
|
|
|
{
|
2016-06-21 10:54:16 -04:00
|
|
|
names.push_back(name);
|
|
|
|
name_offsets[name] = names.size() - 1;
|
2016-06-10 14:15:14 -04:00
|
|
|
}
|
2016-03-18 16:57:36 -04:00
|
|
|
}
|
|
|
|
|
2016-03-02 12:26:44 -05:00
|
|
|
// TODO: extract speed values for compressed and uncompressed geometries
|
|
|
|
|
|
|
|
// Convert tile coordinates into mercator coordinates
|
2016-05-27 15:05:04 -04:00
|
|
|
util::web_mercator::xyzToMercator(
|
|
|
|
parameters.x, parameters.y, parameters.z, min_lon, min_lat, max_lon, max_lat);
|
2016-03-16 16:54:29 -04:00
|
|
|
const detail::BBox tile_bbox{min_lon, min_lat, max_lon, max_lat};
|
2016-03-02 12:26:44 -05:00
|
|
|
|
|
|
|
// Protobuf serialized 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)
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::pbf_writer layer_writer(tile_writer, util::vector_tile::LAYER_TAG);
|
2016-03-02 12:26:44 -05:00
|
|
|
// TODO: don't write a layer if there are no features
|
|
|
|
|
2016-04-14 10:33:23 -04:00
|
|
|
layer_writer.add_uint32(util::vector_tile::VERSION_TAG, 2); // version
|
2016-03-02 12:26:44 -05:00
|
|
|
// Field 1 is the "layer name" field, it's a string
|
2016-04-14 10:33:23 -04:00
|
|
|
layer_writer.add_string(util::vector_tile::NAME_TAG, "speeds"); // name
|
2016-03-02 12:26:44 -05:00
|
|
|
// Field 5 is the tile extent. It's a uint32 and should be set to 4096
|
|
|
|
// for normal vector tiles.
|
2016-04-14 10:33:23 -04:00
|
|
|
layer_writer.add_uint32(util::vector_tile::EXTEND_TAG, util::vector_tile::EXTENT); // extent
|
2016-03-02 12:26:44 -05:00
|
|
|
|
|
|
|
// Begin the layer features block
|
|
|
|
{
|
|
|
|
// Each feature gets a unique id, starting at 1
|
|
|
|
unsigned id = 1;
|
|
|
|
for (const auto &edge : edges)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2016-03-15 02:03:19 -04:00
|
|
|
uint8_t forward_datasource = 0;
|
|
|
|
uint8_t reverse_datasource = 0;
|
|
|
|
|
2016-06-10 14:15:14 -04:00
|
|
|
std::string name = facade.GetNameForID(edge.name_id);
|
|
|
|
|
2016-03-02 12:26:44 -05:00
|
|
|
if (edge.forward_packed_geometry_id != SPECIAL_EDGEID)
|
|
|
|
{
|
|
|
|
std::vector<EdgeWeight> forward_weight_vector;
|
|
|
|
facade.GetUncompressedWeights(edge.forward_packed_geometry_id,
|
|
|
|
forward_weight_vector);
|
|
|
|
forward_weight = forward_weight_vector[edge.fwd_segment_position];
|
2016-03-15 02:03:19 -04:00
|
|
|
|
|
|
|
std::vector<uint8_t> forward_datasource_vector;
|
|
|
|
facade.GetUncompressedDatasources(edge.forward_packed_geometry_id,
|
|
|
|
forward_datasource_vector);
|
|
|
|
forward_datasource = forward_datasource_vector[edge.fwd_segment_position];
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (edge.reverse_packed_geometry_id != SPECIAL_EDGEID)
|
|
|
|
{
|
|
|
|
std::vector<EdgeWeight> 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];
|
2016-03-15 02:03:19 -04:00
|
|
|
|
|
|
|
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];
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
|
2016-03-15 02:03:19 -04:00
|
|
|
// 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);
|
|
|
|
|
2016-06-21 10:54:16 -04:00
|
|
|
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 std::uint8_t datasource,
|
|
|
|
const std::size_t name,
|
|
|
|
std::int32_t &start_x,
|
|
|
|
std::int32_t &start_y) {
|
2016-03-02 12:26:44 -05:00
|
|
|
// 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
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::pbf_writer feature_writer(layer_writer,
|
|
|
|
util::vector_tile::FEATURE_TAG);
|
2016-03-02 12:26:44 -05:00
|
|
|
// Field 3 is the "geometry type" field. Value 2 is "line"
|
2016-04-14 10:33:23 -04:00
|
|
|
feature_writer.add_enum(util::vector_tile::GEOMETRY_TAG,
|
|
|
|
util::vector_tile::GEOMETRY_TYPE_LINE); // geometry type
|
2016-03-02 12:26:44 -05:00
|
|
|
// Field 1 for the feature is the "id" field.
|
2016-04-14 10:33:23 -04:00
|
|
|
feature_writer.add_uint64(util::vector_tile::ID_TAG, id++); // id
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
|
|
|
// 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.
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::packed_field_uint32 field(
|
|
|
|
feature_writer, util::vector_tile::FEATURE_ATTRIBUTES_TAG);
|
2016-03-02 12:26:44 -05:00
|
|
|
|
|
|
|
field.add_element(0); // "speed" tag key offset
|
|
|
|
field.add_element(
|
2016-03-16 16:54:29 -04:00
|
|
|
std::min(speed_kmh, 127u)); // save the speed value, capped at 127
|
2016-03-18 16:57:36 -04:00
|
|
|
field.add_element(1); // "is_small" tag key offset
|
2016-03-15 02:03:19 -04:00
|
|
|
field.add_element(128 +
|
|
|
|
(edge.component.is_tiny ? 0 : 1)); // is_small feature
|
2016-03-18 16:57:36 -04:00
|
|
|
field.add_element(2); // "datasource" tag key offset
|
2016-03-16 16:54:29 -04:00
|
|
|
field.add_element(130 + datasource); // datasource value offset
|
2016-03-18 16:57:36 -04:00
|
|
|
field.add_element(3); // "duration" tag key offset
|
|
|
|
field.add_element(130 + max_datasource_id + 1 +
|
|
|
|
duration); // duration value offset
|
2016-06-21 10:54:16 -04:00
|
|
|
field.add_element(4); // "name" tag key offset
|
2016-06-10 14:15:14 -04:00
|
|
|
|
2016-06-21 10:54:16 -04:00
|
|
|
field.add_element(130 + max_datasource_id + 1 + used_weights.size() +
|
|
|
|
name); // name value offset
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
{
|
2016-03-21 21:13:31 -04:00
|
|
|
|
2016-03-02 12:26:44 -05:00
|
|
|
// Encode the geometry for the feature
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::packed_field_uint32 geometry(
|
|
|
|
feature_writer, util::vector_tile::FEATURE_GEOMETRIES_TAG);
|
2016-03-16 16:54:29 -04:00
|
|
|
encodeLinestring(tile_line, geometry, start_x, start_y);
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
2016-03-16 16:54:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// If this is a valid forward edge, go ahead and add it to the tile
|
2016-03-28 11:06:51 -04:00
|
|
|
if (forward_weight != 0 && edge.forward_segment_id.enabled)
|
2016-03-16 16:54:29 -04:00
|
|
|
{
|
|
|
|
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<std::uint32_t>(round(length / forward_weight * 10 * 3.6));
|
|
|
|
|
|
|
|
auto tile_line = coordinatesToTileLine(a, b, tile_bbox);
|
2016-03-21 21:13:31 -04:00
|
|
|
if (!tile_line.empty())
|
|
|
|
{
|
2016-05-27 15:05:04 -04:00
|
|
|
encode_tile_line(tile_line,
|
|
|
|
speed_kmh,
|
|
|
|
weight_offsets[forward_weight],
|
|
|
|
forward_datasource,
|
2016-06-10 14:15:14 -04:00
|
|
|
name_offsets[name],
|
2016-05-27 15:05:04 -04:00
|
|
|
start_x,
|
|
|
|
start_y);
|
2016-03-21 21:13:31 -04:00
|
|
|
}
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat the above for the coordinates reversed and using the `reverse`
|
|
|
|
// properties
|
2016-03-28 11:06:51 -04:00
|
|
|
if (reverse_weight != 0 && edge.reverse_segment_id.enabled)
|
2016-03-02 12:26:44 -05:00
|
|
|
{
|
|
|
|
std::int32_t start_x = 0;
|
|
|
|
std::int32_t start_y = 0;
|
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
// Calculate the speed for this line
|
|
|
|
std::uint32_t speed_kmh =
|
|
|
|
static_cast<std::uint32_t>(round(length / reverse_weight * 10 * 3.6));
|
2016-03-02 12:26:44 -05:00
|
|
|
|
2016-03-16 16:54:29 -04:00
|
|
|
auto tile_line = coordinatesToTileLine(b, a, tile_bbox);
|
2016-03-21 21:13:31 -04:00
|
|
|
if (!tile_line.empty())
|
|
|
|
{
|
2016-05-27 15:05:04 -04:00
|
|
|
encode_tile_line(tile_line,
|
|
|
|
speed_kmh,
|
|
|
|
weight_offsets[reverse_weight],
|
|
|
|
reverse_datasource,
|
2016-06-10 14:15:14 -04:00
|
|
|
name_offsets[name],
|
2016-05-27 15:05:04 -04:00
|
|
|
start_x,
|
|
|
|
start_y);
|
2016-03-21 21:13:31 -04:00
|
|
|
}
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field id 3 is the "keys" attribute
|
|
|
|
// We need two "key" fields, these are referred to with 0 and 1 (their array indexes)
|
|
|
|
// earlier
|
2016-04-14 10:33:23 -04:00
|
|
|
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");
|
2016-06-10 14:15:14 -04:00
|
|
|
layer_writer.add_string(util::vector_tile::KEY_TAG, "name");
|
2016-03-02 12:26:44 -05:00
|
|
|
|
|
|
|
// 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++)
|
|
|
|
{
|
2016-03-18 16:57:36 -04:00
|
|
|
// Writing field type 4 == variant type
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG);
|
2016-03-18 16:57:36 -04:00
|
|
|
// Attribute value 5 == uin64 type
|
2016-04-14 10:33:23 -04:00
|
|
|
values_writer.add_uint64(util::vector_tile::VARIANT_TYPE_UINT32, i);
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
{
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG);
|
2016-03-02 12:26:44 -05:00
|
|
|
// Attribute value 7 == bool type
|
2016-04-14 10:33:23 -04:00
|
|
|
values_writer.add_bool(util::vector_tile::VARIANT_TYPE_BOOL, true);
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
{
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG);
|
2016-03-02 12:26:44 -05:00
|
|
|
// Attribute value 7 == bool type
|
2016-04-14 10:33:23 -04:00
|
|
|
values_writer.add_bool(util::vector_tile::VARIANT_TYPE_BOOL, false);
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
2016-03-15 02:03:19 -04:00
|
|
|
for (std::size_t i = 0; i <= max_datasource_id; i++)
|
|
|
|
{
|
2016-03-18 16:57:36 -04:00
|
|
|
// Writing field type 4 == variant type
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG);
|
2016-03-18 16:57:36 -04:00
|
|
|
// Attribute value 1 == string type
|
2016-04-14 10:33:23 -04:00
|
|
|
values_writer.add_string(util::vector_tile::VARIANT_TYPE_STRING,
|
|
|
|
facade.GetDatasourceName(i));
|
2016-03-18 16:57:36 -04:00
|
|
|
}
|
|
|
|
for (auto weight : used_weights)
|
|
|
|
{
|
|
|
|
// Writing field type 4 == variant type
|
2016-04-14 10:33:23 -04:00
|
|
|
protozero::pbf_writer values_writer(layer_writer, util::vector_tile::VARIANT_TAG);
|
2016-03-18 16:57:36 -04:00
|
|
|
// 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
|
2016-04-14 10:33:23 -04:00
|
|
|
values_writer.add_double(util::vector_tile::VARIANT_TYPE_DOUBLE, weight / 10.);
|
2016-03-15 02:03:19 -04:00
|
|
|
}
|
2016-06-10 14:15:14 -04:00
|
|
|
|
2016-06-21 10:54:16 -04:00
|
|
|
for (const auto &name : names)
|
2016-06-10 14:15:14 -04:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
2016-03-02 12:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::Ok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|