2012-08-29 12:33:18 -04:00
|
|
|
/*
|
|
|
|
|
2015-02-19 03:19:51 -05:00
|
|
|
Copyright (c) 2015, Project OSRM contributors
|
2013-10-14 07:42:28 -04:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2012-08-29 12:33:18 -04:00
|
|
|
|
2014-11-28 09:00:48 -05:00
|
|
|
#include "extractor_callbacks.hpp"
|
|
|
|
#include "extraction_containers.hpp"
|
|
|
|
#include "extraction_node.hpp"
|
|
|
|
#include "extraction_way.hpp"
|
2012-08-29 12:33:18 -04:00
|
|
|
|
2014-11-28 06:13:18 -05:00
|
|
|
#include "../data_structures/external_memory_node.hpp"
|
|
|
|
#include "../data_structures/restriction.hpp"
|
2015-01-27 11:44:46 -05:00
|
|
|
#include "../util/container.hpp"
|
|
|
|
#include "../util/simple_logger.hpp"
|
2014-01-09 10:13:35 -05:00
|
|
|
|
2015-09-18 08:26:32 -04:00
|
|
|
#include <boost/optional/optional.hpp>
|
|
|
|
|
|
|
|
#include <osmium/osm.hpp>
|
|
|
|
|
2014-11-24 11:57:01 -05:00
|
|
|
#include <osrm/coordinate.hpp>
|
2013-12-16 05:29:38 -05:00
|
|
|
|
2014-05-09 13:35:09 -04:00
|
|
|
#include <limits>
|
2013-12-16 05:29:38 -05:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-04-10 06:40:30 -04:00
|
|
|
ExtractorCallbacks::ExtractorCallbacks(ExtractionContainers &extraction_containers)
|
|
|
|
: external_memory(extraction_containers)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2015-04-10 06:40:30 -04:00
|
|
|
string_map[""] = 0;
|
2014-05-09 10:17:31 -04:00
|
|
|
}
|
2012-08-29 12:33:18 -04:00
|
|
|
|
2015-04-10 06:35:24 -04:00
|
|
|
/**
|
|
|
|
* Takes the node position from osmium and the filtered properties from the lua
|
|
|
|
* profile and saves them to external memory.
|
|
|
|
*
|
|
|
|
* warning: caller needs to take care of synchronization!
|
|
|
|
*/
|
2014-10-13 07:53:06 -04:00
|
|
|
void ExtractorCallbacks::ProcessNode(const osmium::Node &input_node,
|
2014-08-27 10:24:40 -04:00
|
|
|
const ExtractionNode &result_node)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2015-01-22 06:19:11 -05:00
|
|
|
external_memory.all_nodes_list.push_back(
|
|
|
|
{static_cast<int>(input_node.location().lat() * COORDINATE_PRECISION),
|
|
|
|
static_cast<int>(input_node.location().lon() * COORDINATE_PRECISION),
|
2015-11-24 19:33:19 -05:00
|
|
|
OSMNodeID(input_node.id()),
|
2015-01-22 06:19:11 -05:00
|
|
|
result_node.barrier,
|
|
|
|
result_node.traffic_lights});
|
2012-08-29 12:33:18 -04:00
|
|
|
}
|
|
|
|
|
2014-08-27 10:24:40 -04:00
|
|
|
void ExtractorCallbacks::ProcessRestriction(
|
2015-09-18 08:26:32 -04:00
|
|
|
const boost::optional<InputRestrictionContainer> &restriction)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2014-08-29 05:27:51 -04:00
|
|
|
if (restriction)
|
2014-08-26 11:50:34 -04:00
|
|
|
{
|
2014-08-29 05:27:51 -04:00
|
|
|
external_memory.restrictions_list.push_back(restriction.get());
|
2014-11-18 10:54:48 -05:00
|
|
|
// SimpleLogger().Write() << "from: " << restriction.get().restriction.from.node <<
|
|
|
|
// ",via: " << restriction.get().restriction.via.node <<
|
|
|
|
// ", to: " << restriction.get().restriction.to.node <<
|
2015-01-22 06:19:11 -05:00
|
|
|
// ", only: " << (restriction.get().restriction.flags.is_only ?
|
|
|
|
// "y" : "n");
|
2014-08-26 11:50:34 -04:00
|
|
|
}
|
2012-08-29 12:33:18 -04:00
|
|
|
}
|
2015-04-08 12:53:10 -04:00
|
|
|
/**
|
2015-04-10 06:35:24 -04:00
|
|
|
* Takes the geometry contained in the ```input_way``` and the tags computed
|
|
|
|
* by the lua profile inside ```parsed_way``` and computes all edge segments.
|
2015-04-08 12:53:10 -04:00
|
|
|
*
|
2015-04-10 06:35:24 -04:00
|
|
|
* Depending on the forward/backwards weights the edges are split into forward
|
|
|
|
* and backward edges.
|
2015-04-08 12:53:10 -04:00
|
|
|
*
|
|
|
|
* warning: caller needs to take care of synchronization!
|
|
|
|
*/
|
2014-10-13 07:53:06 -04:00
|
|
|
void ExtractorCallbacks::ProcessWay(const osmium::Way &input_way, const ExtractionWay &parsed_way)
|
2014-05-09 10:17:31 -04:00
|
|
|
{
|
2014-09-29 05:37:36 -04:00
|
|
|
if (((0 >= parsed_way.forward_speed) ||
|
2014-08-27 10:24:40 -04:00
|
|
|
(TRAVEL_MODE_INACCESSIBLE == parsed_way.forward_travel_mode)) &&
|
2014-08-20 11:05:39 -04:00
|
|
|
((0 >= parsed_way.backward_speed) ||
|
2014-08-27 10:24:40 -04:00
|
|
|
(TRAVEL_MODE_INACCESSIBLE == parsed_way.backward_travel_mode)) &&
|
2014-08-20 11:05:39 -04:00
|
|
|
(0 >= parsed_way.duration))
|
2014-05-09 10:17:31 -04:00
|
|
|
{ // Only true if the way is specified by the speed profile
|
2014-05-18 16:44:19 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-08-29 12:33:18 -04:00
|
|
|
|
2014-10-13 07:53:06 -04:00
|
|
|
if (input_way.nodes().size() <= 1)
|
2014-06-02 13:23:50 -04:00
|
|
|
{ // safe-guard against broken data
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-13 09:51:40 -04:00
|
|
|
if (std::numeric_limits<decltype(input_way.id())>::max() == input_way.id())
|
2014-05-18 16:44:19 -04:00
|
|
|
{
|
2014-10-13 09:51:40 -04:00
|
|
|
SimpleLogger().Write(logDEBUG) << "found bogus way with id: " << input_way.id()
|
2014-10-13 07:53:06 -04:00
|
|
|
<< " of size " << input_way.nodes().size();
|
2014-05-18 16:44:19 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-05-09 11:21:36 -04:00
|
|
|
|
|
|
|
InternalExtractorEdge::WeightData forward_weight_data;
|
|
|
|
InternalExtractorEdge::WeightData backward_weight_data;
|
|
|
|
|
2014-05-18 16:44:19 -04:00
|
|
|
if (0 < parsed_way.duration)
|
|
|
|
{
|
2015-05-09 11:21:36 -04:00
|
|
|
const unsigned num_edges = (input_way.nodes().size() - 1);
|
|
|
|
// FIXME We devide by the numer of nodes here, but should rather consider
|
|
|
|
// the length of each segment. We would eigther have to compute the length
|
|
|
|
// of the whole way here (we can't: no node coordinates) or push that back
|
|
|
|
// to the container and keep a reference to the way.
|
|
|
|
forward_weight_data.duration = parsed_way.duration / num_edges;
|
|
|
|
forward_weight_data.type = InternalExtractorEdge::WeightType::WAY_DURATION;
|
|
|
|
backward_weight_data.duration = parsed_way.duration / num_edges;
|
|
|
|
backward_weight_data.type = InternalExtractorEdge::WeightType::WAY_DURATION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (parsed_way.forward_speed > 0 &&
|
|
|
|
parsed_way.forward_travel_mode != TRAVEL_MODE_INACCESSIBLE)
|
|
|
|
{
|
|
|
|
forward_weight_data.speed = parsed_way.forward_speed;
|
|
|
|
forward_weight_data.type = InternalExtractorEdge::WeightType::SPEED;
|
|
|
|
}
|
|
|
|
if (parsed_way.backward_speed > 0 &&
|
|
|
|
parsed_way.backward_travel_mode != TRAVEL_MODE_INACCESSIBLE)
|
|
|
|
{
|
|
|
|
backward_weight_data.speed = parsed_way.backward_speed;
|
|
|
|
backward_weight_data.type = InternalExtractorEdge::WeightType::SPEED;
|
|
|
|
}
|
2014-05-18 16:44:19 -04:00
|
|
|
}
|
2013-02-27 11:36:44 -05:00
|
|
|
|
2015-09-18 08:26:32 -04:00
|
|
|
if (forward_weight_data.type == InternalExtractorEdge::WeightType::INVALID &&
|
|
|
|
backward_weight_data.type == InternalExtractorEdge::WeightType::INVALID)
|
2014-05-18 16:44:19 -04:00
|
|
|
{
|
2014-10-13 09:51:40 -04:00
|
|
|
SimpleLogger().Write(logDEBUG) << "found way with bogus speed, id: " << input_way.id();
|
2014-05-18 16:44:19 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-08-29 12:33:18 -04:00
|
|
|
|
2014-05-18 16:44:19 -04:00
|
|
|
// Get the unique identifier for the street name
|
|
|
|
const auto &string_map_iterator = string_map.find(parsed_way.name);
|
2015-12-13 14:02:55 -05:00
|
|
|
unsigned name_id = external_memory.name_lengths.size();
|
2014-05-18 16:44:19 -04:00
|
|
|
if (string_map.end() == string_map_iterator)
|
|
|
|
{
|
2015-12-09 13:01:45 -05:00
|
|
|
unsigned name_length = 0;
|
|
|
|
for (const char &c : parsed_way.name)
|
|
|
|
{
|
|
|
|
// Cap name length at 255 characters
|
|
|
|
if (name_length == 255u)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
external_memory.name_char_data.push_back(c);
|
|
|
|
name_length++;
|
|
|
|
}
|
2015-12-13 14:02:55 -05:00
|
|
|
external_memory.name_lengths.push_back(name_length);
|
2014-08-26 11:50:34 -04:00
|
|
|
string_map.insert(std::make_pair(parsed_way.name, name_id));
|
2014-05-18 16:44:19 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-26 11:50:34 -04:00
|
|
|
name_id = string_map_iterator->second;
|
2014-05-18 16:44:19 -04:00
|
|
|
}
|
|
|
|
|
2014-08-27 10:24:40 -04:00
|
|
|
const bool split_edge = (parsed_way.forward_speed > 0) &&
|
|
|
|
(TRAVEL_MODE_INACCESSIBLE != parsed_way.forward_travel_mode) &&
|
|
|
|
(parsed_way.backward_speed > 0) &&
|
|
|
|
(TRAVEL_MODE_INACCESSIBLE != parsed_way.backward_travel_mode) &&
|
|
|
|
((parsed_way.forward_speed != parsed_way.backward_speed) ||
|
|
|
|
(parsed_way.forward_travel_mode != parsed_way.backward_travel_mode));
|
2014-08-12 08:18:02 -04:00
|
|
|
|
2015-06-27 09:27:08 -04:00
|
|
|
std::transform(input_way.nodes().begin(), input_way.nodes().end(),
|
|
|
|
std::back_inserter(external_memory.used_node_id_list),
|
2015-09-18 08:26:32 -04:00
|
|
|
[](const osmium::NodeRef &ref)
|
|
|
|
{
|
2015-11-24 19:33:19 -05:00
|
|
|
return OSMNodeID(ref.ref());
|
2015-09-18 08:26:32 -04:00
|
|
|
});
|
2014-08-26 11:50:34 -04:00
|
|
|
|
|
|
|
const bool is_opposite_way = TRAVEL_MODE_INACCESSIBLE == parsed_way.forward_travel_mode;
|
2015-06-27 09:27:08 -04:00
|
|
|
|
2015-05-09 11:21:36 -04:00
|
|
|
// traverse way in reverse in this case
|
2014-08-26 11:50:34 -04:00
|
|
|
if (is_opposite_way)
|
|
|
|
{
|
2015-06-27 09:27:08 -04:00
|
|
|
BOOST_ASSERT(split_edge == false);
|
|
|
|
BOOST_ASSERT(parsed_way.backward_travel_mode != TRAVEL_MODE_INACCESSIBLE);
|
2015-01-22 06:19:11 -05:00
|
|
|
osrm::for_each_pair(input_way.nodes().crbegin(), input_way.nodes().crend(),
|
2015-06-27 09:27:08 -04:00
|
|
|
[&](const osmium::NodeRef &first_node, const osmium::NodeRef &last_node)
|
|
|
|
{
|
|
|
|
external_memory.all_edges_list.push_back(InternalExtractorEdge(
|
2015-11-24 19:33:19 -05:00
|
|
|
OSMNodeID(first_node.ref()), OSMNodeID(last_node.ref()), name_id,
|
2015-09-18 08:26:32 -04:00
|
|
|
backward_weight_data, true, false, parsed_way.roundabout,
|
|
|
|
parsed_way.is_access_restricted,
|
2015-06-28 16:13:54 -04:00
|
|
|
parsed_way.backward_travel_mode, false));
|
2015-06-27 09:27:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
external_memory.way_start_end_id_list.push_back(
|
2015-11-24 19:33:19 -05:00
|
|
|
{OSMWayID(input_way.id()),
|
|
|
|
OSMNodeID(input_way.nodes().back().ref()),
|
|
|
|
OSMNodeID(input_way.nodes()[input_way.nodes().size() - 2].ref()),
|
|
|
|
OSMNodeID(input_way.nodes()[1].ref()),
|
|
|
|
OSMNodeID(input_way.nodes()[0].ref())});
|
2014-08-26 11:50:34 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-18 08:26:32 -04:00
|
|
|
const bool forward_only =
|
|
|
|
split_edge || TRAVEL_MODE_INACCESSIBLE == parsed_way.backward_travel_mode;
|
2015-01-22 06:19:11 -05:00
|
|
|
osrm::for_each_pair(input_way.nodes().cbegin(), input_way.nodes().cend(),
|
2015-06-27 09:27:08 -04:00
|
|
|
[&](const osmium::NodeRef &first_node, const osmium::NodeRef &last_node)
|
|
|
|
{
|
|
|
|
external_memory.all_edges_list.push_back(InternalExtractorEdge(
|
2015-11-24 19:33:19 -05:00
|
|
|
OSMNodeID(first_node.ref()), OSMNodeID(last_node.ref()), name_id, forward_weight_data,
|
2015-09-18 08:26:32 -04:00
|
|
|
true, !forward_only, parsed_way.roundabout,
|
|
|
|
parsed_way.is_access_restricted, parsed_way.forward_travel_mode,
|
|
|
|
split_edge));
|
2015-06-27 09:27:08 -04:00
|
|
|
});
|
|
|
|
if (split_edge)
|
2014-08-26 11:50:34 -04:00
|
|
|
{
|
2015-06-27 09:27:08 -04:00
|
|
|
BOOST_ASSERT(parsed_way.backward_travel_mode != TRAVEL_MODE_INACCESSIBLE);
|
2015-09-18 08:26:32 -04:00
|
|
|
osrm::for_each_pair(
|
|
|
|
input_way.nodes().cbegin(), input_way.nodes().cend(),
|
|
|
|
[&](const osmium::NodeRef &first_node, const osmium::NodeRef &last_node)
|
|
|
|
{
|
|
|
|
external_memory.all_edges_list.push_back(InternalExtractorEdge(
|
2015-11-24 19:33:19 -05:00
|
|
|
OSMNodeID(first_node.ref()), OSMNodeID(last_node.ref()), name_id, backward_weight_data, false,
|
2015-09-18 08:26:32 -04:00
|
|
|
true, parsed_way.roundabout, parsed_way.is_access_restricted,
|
|
|
|
parsed_way.backward_travel_mode, true));
|
|
|
|
});
|
2012-08-29 12:33:18 -04:00
|
|
|
}
|
2014-08-26 11:50:34 -04:00
|
|
|
|
2014-05-09 10:17:31 -04:00
|
|
|
external_memory.way_start_end_id_list.push_back(
|
2015-11-24 19:33:19 -05:00
|
|
|
{OSMWayID(input_way.id()),
|
|
|
|
OSMNodeID(input_way.nodes().back().ref()),
|
|
|
|
OSMNodeID(input_way.nodes()[input_way.nodes().size() - 2].ref()),
|
|
|
|
OSMNodeID(input_way.nodes()[1].ref()),
|
|
|
|
OSMNodeID(input_way.nodes()[0].ref())});
|
2012-08-29 12:33:18 -04:00
|
|
|
}
|
|
|
|
}
|