Implements a vector tileserver so you can see what's going on inside

OSRM.
This commit is contained in:
Daniel Patterson
2016-02-16 10:51:04 -08:00
committed by Patrick Niklaus
parent 33403efc8e
commit 5dc7b79bb6
18 changed files with 709 additions and 14 deletions
@@ -76,6 +76,9 @@ template <class EdgeDataT> class BaseDataFacade
virtual extractor::TravelMode GetTravelModeForEdgeID(const unsigned id) const = 0;
virtual std::vector<RTreeLeaf> GetEdgesInBox(const util::FixedPointCoordinate & south_west,
const util::FixedPointCoordinate & north_east) = 0;
virtual std::vector<PhantomNodeWithDistance>
NearestPhantomNodesInRange(const util::FixedPointCoordinate input_coordinate,
const float max_distance,
@@ -15,6 +15,7 @@
#include "util/range_table.hpp"
#include "util/graph_loader.hpp"
#include "util/simple_logger.hpp"
#include "util/rectangle.hpp"
#include "osrm/coordinate.hpp"
@@ -357,6 +358,20 @@ template <class EdgeDataT> class InternalDataFacade final : public BaseDataFacad
return m_travel_mode_list.at(id);
}
std::vector<RTreeLeaf> GetEdgesInBox(const util::FixedPointCoordinate & south_west,
const util::FixedPointCoordinate & north_east)
override final
{
if (!m_static_rtree.get())
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
util::RectangleInt2D bbox = {south_west.lon, north_east.lon,
south_west.lat, north_east.lat};
return m_geospatial_query->Search(bbox);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodesInRange(const util::FixedPointCoordinate input_coordinate,
const float max_distance,
@@ -13,6 +13,7 @@
#include "util/static_rtree.hpp"
#include "util/make_unique.hpp"
#include "util/simple_logger.hpp"
#include "util/rectangle.hpp"
#include <cstddef>
@@ -407,6 +408,20 @@ template <class EdgeDataT> class SharedDataFacade final : public BaseDataFacade<
return m_travel_mode_list.at(id);
}
std::vector<RTreeLeaf> GetEdgesInBox(const util::FixedPointCoordinate & south_west,
const util::FixedPointCoordinate & north_east)
override final
{
if (!m_static_rtree.get() || CURRENT_TIMESTAMP != m_static_rtree->first)
{
LoadRTree();
BOOST_ASSERT(m_geospatial_query.get());
}
util::RectangleInt2D bbox = {south_west.lon, north_east.lon,
south_west.lat, north_east.lat};
return m_geospatial_query->Search(bbox);
}
std::vector<PhantomNodeWithDistance>
NearestPhantomNodesInRange(const util::FixedPointCoordinate input_coordinate,
const float max_distance,
+9
View File
@@ -5,6 +5,7 @@
#include "util/typedefs.hpp"
#include "engine/phantom_node.hpp"
#include "util/bearing.hpp"
#include "util/rectangle.hpp"
#include "osrm/coordinate.hpp"
@@ -32,6 +33,14 @@ template <typename RTreeT> class GeospatialQuery
{
}
std::vector<EdgeData>
Search(const util::RectangleInt2D & bbox)
{
return rtree.SearchInBox(bbox);
}
// Returns nearest PhantomNodes in the given bearing range within max_distance.
// Does not filter by small/big component!
std::vector<PhantomNodeWithDistance>
+349
View File
@@ -0,0 +1,349 @@
#ifndef TILEPLUGIN_HPP
#define TILEPLUGIN_HPP
#include "engine/plugins/plugin_base.hpp"
#include "osrm/json_container.hpp"
#include "util/tile_bbox.hpp"
#include <protozero/varint.hpp>
#include <protozero/pbf_writer.hpp>
#include <string>
#include <cmath>
/*
* This plugin generates Mapbox Vector tiles that show the internal
* routing geometry and speed values on all road segments.
* You can use this along with a vector-tile viewer, like Mapbox GL,
* to display maps that show the exact road network that
* OSRM is routing. This is very useful for debugging routing
* errors
*/
namespace osrm
{
namespace engine
{
namespace plugins
{
// from mapnik/well_known_srs.hpp
static const double EARTH_RADIUS = 6378137.0;
static const double EARTH_DIAMETER = EARTH_RADIUS * 2.0;
static const double EARTH_CIRCUMFERENCE = EARTH_DIAMETER * M_PI;
static const double MAXEXTENT = EARTH_CIRCUMFERENCE / 2.0;
static const double M_PI_by2 = M_PI / 2;
static const double D2R = M_PI / 180;
static const double R2D = 180 / M_PI;
static const double M_PIby360 = M_PI / 360;
static const double MAXEXTENTby180 = MAXEXTENT / 180;
static const double MAX_LATITUDE = R2D * (2 * std::atan(std::exp(180 * D2R)) - M_PI_by2);
// from mapnik-vector-tile
namespace detail_pbf {
inline unsigned encode_length(unsigned len)
{
return (len << 3u) | 2u;
}
}
inline void lonlat2merc(double & x, double & y)
{
if (x > 180) x = 180;
else if (x < -180) x = -180;
if (y > MAX_LATITUDE) y = MAX_LATITUDE;
else if (y < -MAX_LATITUDE) y = -MAX_LATITUDE;
x = x * MAXEXTENTby180;
y = std::log(std::tan((90 + y) * M_PIby360)) * R2D;
y = y * MAXEXTENTby180;
}
const static int tile_size_ = 256;
void from_pixels(double shift, double & x, double & y)
{
double b = shift/2.0;
x = (x - b)/(shift/360.0);
double g = (y - b)/-(shift/(2 * M_PI));
y = R2D * (2.0 * std::atan(std::exp(g)) - M_PI_by2);
}
void xyz(int x,
int y,
int z,
double & minx,
double & miny,
double & maxx,
double & maxy)
{
minx = x * tile_size_;
miny = (y + 1.0) * tile_size_;
maxx = (x + 1.0) * tile_size_;
maxy = y * tile_size_;
double shift = std::pow(2.0,z) * tile_size_;
from_pixels(shift,minx,miny);
from_pixels(shift,maxx,maxy);
lonlat2merc(minx,miny);
lonlat2merc(maxx,maxy);
}
void xyz2wsg84(int x,
int y,
int z,
double & minx,
double & miny,
double & maxx,
double & maxy)
{
minx = x * tile_size_;
miny = (y + 1.0) * tile_size_;
maxx = (x + 1.0) * tile_size_;
maxy = y * tile_size_;
double shift = std::pow(2.0,z) * tile_size_;
from_pixels(shift,minx,miny);
from_pixels(shift,maxx,maxy);
}
// emulates mapbox::box2d
class bbox {
public:
double minx;
double miny;
double maxx;
double maxy;
bbox(double _minx,double _miny,double _maxx,double _maxy) :
minx(_minx),
miny(_miny),
maxx(_maxx),
maxy(_maxy) { }
double width() const {
return maxx - minx;
}
double height() const {
return maxy - miny;
}
};
// should start using core geometry class across mapnik, osrm, mapbox-gl-native
class point_type_d {
public:
double x;
double y;
point_type_d(double _x, double _y) :
x(_x),
y(_y) {
}
};
class point_type_i {
public:
std::int64_t x;
std::int64_t y;
point_type_i(std::int64_t _x, std::int64_t _y) :
x(_x),
y(_y) {
}
};
using line_type = std::vector<point_type_i>;
using line_typed = std::vector<point_type_d>;
// from mapnik-vector-tile
inline bool encode_linestring(line_type line,
protozero::packed_field_uint32 & geometry,
int32_t & start_x,
int32_t & start_y) {
std::size_t line_size = line.size();
//line_size -= detail_pbf::repeated_point_count(line);
if (line_size < 2)
{
return false;
}
unsigned line_to_length = static_cast<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;
geometry.add_element(detail_pbf::encode_length(line_to_length));
for (++pt; pt != line.end(); ++pt)
{
int32_t dx = pt->x - start_x;
int32_t dy = pt->y - start_y;
/*if (dx == 0 && dy == 0)
{
continue;
}*/
geometry.add_element(protozero::encode_zigzag32(dx));
geometry.add_element(protozero::encode_zigzag32(dy));
start_x = pt->x;
start_y = pt->y;
}
return true;
}
template <class DataFacadeT> class TilePlugin final : public BasePlugin
{
public:
explicit TilePlugin(DataFacadeT *facade) : facade(facade), descriptor_string("tile") {}
const std::string GetDescriptor() const override final { return descriptor_string; }
Status HandleRequest(const RouteParameters &route_parameters,
util::json::Object &json_result) override final
{
const unsigned tile_extent = 4096;
double min_lon, min_lat, max_lon, max_lat;
xyz2wsg84(route_parameters.x, route_parameters.y, route_parameters.z, min_lon, min_lat, max_lon, max_lat);
FixedPointCoordinate southwest = { static_cast<int32_t>(min_lat * COORDINATE_PRECISION), static_cast<int32_t>(min_lon * COORDINATE_PRECISION) };
FixedPointCoordinate northeast = { static_cast<int32_t>(max_lat * COORDINATE_PRECISION), static_cast<int32_t>(max_lon * COORDINATE_PRECISION) };
auto edges = facade->GetEdgesInBox(southwest, northeast);
xyz(route_parameters.x, route_parameters.y, route_parameters.z, min_lon, min_lat, max_lon, max_lat);
bbox tile_bbox(min_lon, min_lat, max_lon, max_lat);
std::string buffer;
protozero::pbf_writer tile_writer(buffer);
{
protozero::pbf_writer layer_writer(tile_writer,3);
// TODO: don't write a layer if there are no features
layer_writer.add_uint32(15,2); // version
layer_writer.add_string(1,"speeds"); // name
layer_writer.add_uint32(5,4096); // extent
std::vector<double> speeds;
std::vector<bool> is_smalls;
{
unsigned id = 1;
for (const auto & edge : edges)
{
const auto a = facade->GetCoordinateOfNode(edge.u);
const auto b = facade->GetCoordinateOfNode(edge.v);
double length = osrm::util::coordinate_calculation::haversineDistance( a.lon, a.lat, b.lon, b.lat );
if (edge.forward_weight != 0 && edge.forward_edge_based_node_id != SPECIAL_NODEID) {
std::int32_t start_x = 0;
std::int32_t start_y = 0;
line_typed geo_line;
geo_line.emplace_back(a.lon / COORDINATE_PRECISION, a.lat / COORDINATE_PRECISION);
geo_line.emplace_back(b.lon / COORDINATE_PRECISION, b.lat / COORDINATE_PRECISION);
double speed = round(length / edge.forward_weight * 10 ) * 3.6;
speeds.push_back(speed);
is_smalls.push_back(edge.component.is_tiny);
line_type tile_line;
for (auto const & pt : geo_line) {
double px_merc = pt.x;
double py_merc = pt.y;
lonlat2merc(px_merc,py_merc);
// convert to integer tile coordinat
std::int64_t px = std::round((px_merc - tile_bbox.minx) * tile_extent/16 / tile_bbox.width());
std::int64_t py = std::round((tile_bbox.maxy - py_merc) * tile_extent/16 / tile_bbox.height());
tile_line.emplace_back(px*tile_extent/256,py*tile_extent/256);
}
protozero::pbf_writer feature_writer(layer_writer,2);
feature_writer.add_enum(3,2); // geometry type
feature_writer.add_uint64(1,id++); // id
{
protozero::packed_field_uint32 field(feature_writer, 2);
field.add_element(0); // "speed" tag key offset
field.add_element((speeds.size()-1)*2); // "speed" tag value offset
field.add_element(1); // "is_small" tag key offset
field.add_element((is_smalls.size()-1)*2+1); // "is_small" tag value offset
}
{
protozero::packed_field_uint32 geometry(feature_writer,4);
encode_linestring(tile_line,geometry,start_x,start_y);
}
}
if (edge.reverse_weight != 0 && edge.reverse_edge_based_node_id != SPECIAL_NODEID) {
std::int32_t start_x = 0;
std::int32_t start_y = 0;
line_typed geo_line;
geo_line.emplace_back(b.lon / COORDINATE_PRECISION, b.lat / COORDINATE_PRECISION);
geo_line.emplace_back(a.lon / COORDINATE_PRECISION, a.lat / COORDINATE_PRECISION);
double speed = round(length / edge.reverse_weight * 10 ) * 3.6;
speeds.push_back(speed);
is_smalls.push_back(edge.component.is_tiny);
line_type tile_line;
for (auto const & pt : geo_line) {
double px_merc = pt.x;
double py_merc = pt.y;
lonlat2merc(px_merc,py_merc);
// convert to integer tile coordinat
std::int64_t px = std::round((px_merc - tile_bbox.minx) * tile_extent/16 / tile_bbox.width());
std::int64_t py = std::round((tile_bbox.maxy - py_merc) * tile_extent/16 / tile_bbox.height());
tile_line.emplace_back(px*tile_extent/256,py*tile_extent/256);
}
protozero::pbf_writer feature_writer(layer_writer,2);
feature_writer.add_enum(3,2); // geometry type
feature_writer.add_uint64(1,id++); // id
{
protozero::packed_field_uint32 field(feature_writer, 2);
field.add_element(0); // "speed" tag key offset
field.add_element((speeds.size()-1)*2); // "speed" tag value offset
field.add_element(1); // "is_small" tag key offset
field.add_element((is_smalls.size()-1)*2+1); // "is_small" tag value offset
}
{
protozero::packed_field_uint32 geometry(feature_writer,4);
encode_linestring(tile_line,geometry,start_x,start_y);
}
}
}
}
layer_writer.add_string(3,"speed");
layer_writer.add_string(3,"is_small");
for (size_t i=0; i<speeds.size(); i++) {
{
protozero::pbf_writer values_writer(layer_writer,4);
values_writer.add_double(3, speeds[i]);
}
{
protozero::pbf_writer values_writer(layer_writer,4);
values_writer.add_bool(7, is_smalls[i]);
}
}
}
json_result.values["pbf"] = buffer;
return Status::Ok;
}
private:
DataFacadeT *facade;
std::string descriptor_string;
};
}
}
}
#endif /* TILEPLUGIN_HPP */
+7
View File
@@ -92,6 +92,10 @@ struct RouteParameters
void SetCoordinatesFromGeometry(const std::string &geometry_string);
void SetX(const int &x);
void SetZ(const int &z);
void SetY(const int &y);
short zoom_level;
bool print_instructions;
bool alternate_route;
@@ -115,6 +119,9 @@ struct RouteParameters
std::vector<FixedPointCoordinate> coordinates;
std::vector<bool> is_destination;
std::vector<bool> is_source;
int z;
int x;
int y;
};
}
}
+7 -2
View File
@@ -47,7 +47,7 @@ template <typename Iterator, class HandlerT> struct APIGrammar : qi::grammar<Ite
query = ('?') >> +(zoom | output | jsonp | checksum | uturns | location_with_options |
destination_with_options | source_with_options | cmp | language |
instruction | geometry | alt_route | old_API | num_results |
matching_beta | gps_precision | classify | locs);
matching_beta | gps_precision | classify | locs | x | y | z);
// all combinations of timestamp, uturn, hint and bearing without duplicates
t_u = (u >> -timestamp) | (timestamp >> -u);
t_h = (hint >> -timestamp) | (timestamp >> -hint);
@@ -110,6 +110,11 @@ template <typename Iterator, class HandlerT> struct APIGrammar : qi::grammar<Ite
locs = (-qi::lit('&')) >> qi::lit("locs") >> '=' >>
stringforPolyline[boost::bind(&HandlerT::SetCoordinatesFromGeometry, handler, ::_1)];
z = (-qi::lit('&')) >> qi::lit("tz") >> '=' >> qi::int_[boost::bind<void>(&HandlerT::SetZ, handler, ::_1)];
x = (-qi::lit('&')) >> qi::lit("tx") >> '=' >> qi::int_[boost::bind<void>(&HandlerT::SetX, handler, ::_1)];
y = (-qi::lit('&')) >> qi::lit("ty") >> '=' >> qi::int_[boost::bind<void>(&HandlerT::SetY, handler, ::_1)];
string = +(qi::char_("a-zA-Z"));
stringwithDot = +(qi::char_("a-zA-Z0-9_.-"));
stringwithPercent = +(qi::char_("a-zA-Z0-9_.-") | qi::char_('[') | qi::char_(']') |
@@ -122,7 +127,7 @@ template <typename Iterator, class HandlerT> struct APIGrammar : qi::grammar<Ite
qi::rule<Iterator, std::string()> service, zoom, output, string, jsonp, checksum, location,
destination, source, hint, timestamp, bearing, stringwithDot, stringwithPercent, language,
geometry, cmp, alt_route, u, uturns, old_API, num_results, matching_beta, gps_precision,
classify, locs, instruction, stringforPolyline;
classify, locs, instruction, stringforPolyline, x, y, z;
HandlerT *handler;
};
+1 -1
View File
@@ -15,13 +15,13 @@ class exception final : public std::exception
public:
explicit exception(const char *message) : message(message) {}
explicit exception(std::string message) : message(std::move(message)) {}
const char *what() const noexcept override { return message.c_str(); }
private:
// This function exists to 'anchor' the class, and stop the compiler from
// copying vtable and RTTI info into every object file that includes
// this header. (Caught by -Wweak-vtables under Clang.)
virtual void anchor() const;
const char *what() const noexcept override { return message.c_str(); }
const std::string message;
};
}
+8 -7
View File
@@ -26,6 +26,10 @@ struct RectangleInt2D
{
}
RectangleInt2D(int32_t min_lon_, int32_t max_lon_, int32_t min_lat_, int32_t max_lat_) :
min_lon(min_lon_), max_lon(max_lon_),
min_lat(min_lat_), max_lat(max_lat_) {}
int32_t min_lon, max_lon;
int32_t min_lat, max_lat;
@@ -53,13 +57,10 @@ struct RectangleInt2D
bool Intersects(const RectangleInt2D &other) const
{
FixedPointCoordinate upper_left(other.max_lat, other.min_lon);
FixedPointCoordinate upper_right(other.max_lat, other.max_lon);
FixedPointCoordinate lower_right(other.min_lat, other.max_lon);
FixedPointCoordinate lower_left(other.min_lat, other.min_lon);
return (Contains(upper_left) || Contains(upper_right) || Contains(lower_right) ||
Contains(lower_left));
// Standard box intersection test - check if boxes *don't* overlap,
// and return the negative of that
return ! (max_lon < other.min_lon || min_lon > other.max_lon
|| max_lat < other.min_lat || min_lat > other.max_lat);
}
double GetMinDist(const FixedPointCoordinate location) const
+56
View File
@@ -321,6 +321,62 @@ class StaticRTree
leaves_stream.read((char *)&m_element_count, sizeof(uint64_t));
}
/* Returns all features inside the bounding box */
std::vector<EdgeDataT> SearchInBox(const Rectangle & search_rectangle)
{
std::vector<EdgeDataT> results;
std::queue<TreeNode> traversal_queue;
traversal_queue.push(m_search_tree[0]);
while (!traversal_queue.empty())
{
auto const current_tree_node = traversal_queue.front();
traversal_queue.pop();
if (current_tree_node.child_is_on_disk)
{
LeafNode current_leaf_node;
LoadLeafFromDisk(current_tree_node.children[0], current_leaf_node);
for (const auto i : irange(0u, current_leaf_node.object_count))
{
const auto &current_edge = current_leaf_node.objects[i];
Rectangle bbox =
{std::min((*m_coordinate_list)[current_edge.u].lon, (*m_coordinate_list)[current_edge.v].lon),
std::max((*m_coordinate_list)[current_edge.u].lon, (*m_coordinate_list)[current_edge.v].lon),
std::min((*m_coordinate_list)[current_edge.u].lat, (*m_coordinate_list)[current_edge.v].lat),
std::max((*m_coordinate_list)[current_edge.u].lat, (*m_coordinate_list)[current_edge.v].lat)};
if (bbox.Intersects(search_rectangle))
{
results.push_back(current_edge);
}
}
}
else
{
// If it's a tree node, look at all children and add them
// to the search queue if their bounding boxes intersect
for (uint32_t i = 0; i < current_tree_node.child_count; ++i)
{
const int32_t child_id = current_tree_node.children[i];
const auto &child_tree_node = m_search_tree[child_id];
const auto &child_rectangle = child_tree_node.minimum_bounding_rectangle;
if (child_rectangle.Intersects(search_rectangle))
{
traversal_queue.push(m_search_tree[child_id]);
}
}
}
}
return results;
}
// Override filter and terminator for the desired behaviour.
std::vector<EdgeDataT> Nearest(const FixedPointCoordinate input_coordinate,
const std::size_t max_results)
+32
View File
@@ -0,0 +1,32 @@
#ifndef TILE_BBOX
#define TILE_BBOX
#include "util/rectangle.hpp"
#include <cmath>
namespace osrm
{
namespace util
{
inline RectangleInt2D TileToBBOX(int z, int x, int y)
{
double minx = x / pow(2.0, z) * 360 - 180;
double n = M_PI - 2.0 * M_PI * y / pow(2.0, z);
double miny = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
double maxx = (x + 1) / pow(2.0, z) * 360 - 180;
double mn = M_PI - 2.0 * M_PI * (y + 1) / pow(2.0, z);
double maxy = 180.0 / M_PI * atan(0.5 * (exp(mn) - exp(-mn)));
return {
static_cast<int32_t>(std::min(minx,maxx) * COORDINATE_PRECISION),
static_cast<int32_t>(std::max(minx,maxx) * COORDINATE_PRECISION),
static_cast<int32_t>(std::min(miny,maxy) * COORDINATE_PRECISION),
static_cast<int32_t>(std::min(miny,maxy) * COORDINATE_PRECISION)
};
}
}
}
#endif