Adds .properties file to osrm-extract ouput
This file contains global properties set by the lua profile, such as enabling uturns at vias and penalties. This file will be consumed by the server.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "extractor/internal_extractor_edge.hpp"
|
||||
#include "extractor/external_memory_node.hpp"
|
||||
#include "extractor/raster_source.hpp"
|
||||
#include "extractor/profile_properties.hpp"
|
||||
#include "util/lua_util.hpp"
|
||||
#include "util/exception.hpp"
|
||||
#include "util/simple_logger.hpp"
|
||||
@@ -51,122 +52,137 @@ int luaErrorCallback(lua_State *state)
|
||||
}
|
||||
}
|
||||
|
||||
ScriptingEnvironment::Context::Context() : state(luaL_newstate()) {}
|
||||
|
||||
ScriptingEnvironment::Context::~Context() { lua_close(state); }
|
||||
|
||||
ScriptingEnvironment::ScriptingEnvironment(const std::string &file_name) : file_name(file_name)
|
||||
{
|
||||
util::SimpleLogger().Write() << "Using script " << file_name;
|
||||
}
|
||||
|
||||
void ScriptingEnvironment::InitLuaState(lua_State *lua_state)
|
||||
void ScriptingEnvironment::InitContext(ScriptingEnvironment::Context &context)
|
||||
{
|
||||
typedef double (osmium::Location::*location_member_ptr_type)() const;
|
||||
|
||||
luabind::open(lua_state);
|
||||
luabind::open(context.state);
|
||||
// open utility libraries string library;
|
||||
luaL_openlibs(lua_state);
|
||||
luaL_openlibs(context.state);
|
||||
|
||||
util::luaAddScriptFolderToLoadPath(lua_state, file_name.c_str());
|
||||
util::luaAddScriptFolderToLoadPath(context.state, file_name.c_str());
|
||||
|
||||
// Add our function to the state's global scope
|
||||
luabind::module(
|
||||
lua_state)[luabind::def("print", util::LUA_print<std::string>),
|
||||
luabind::def("durationIsValid", durationIsValid),
|
||||
luabind::def("parseDuration", parseDuration),
|
||||
luabind::class_<TravelMode>("mode")
|
||||
.enum_("enums")[luabind::value("inaccessible", TRAVEL_MODE_INACCESSIBLE),
|
||||
luabind::value("driving", TRAVEL_MODE_DRIVING),
|
||||
luabind::value("cycling", TRAVEL_MODE_CYCLING),
|
||||
luabind::value("walking", TRAVEL_MODE_WALKING),
|
||||
luabind::value("ferry", TRAVEL_MODE_FERRY),
|
||||
luabind::value("train", TRAVEL_MODE_TRAIN),
|
||||
luabind::value("pushing_bike", TRAVEL_MODE_PUSHING_BIKE),
|
||||
luabind::value("movable_bridge", TRAVEL_MODE_MOVABLE_BRIDGE),
|
||||
luabind::value("steps_up", TRAVEL_MODE_STEPS_UP),
|
||||
luabind::value("steps_down", TRAVEL_MODE_STEPS_DOWN),
|
||||
luabind::value("river_up", TRAVEL_MODE_RIVER_UP),
|
||||
luabind::value("river_down", TRAVEL_MODE_RIVER_DOWN),
|
||||
luabind::value("route", TRAVEL_MODE_ROUTE)],
|
||||
luabind::class_<SourceContainer>("sources")
|
||||
.def(luabind::constructor<>())
|
||||
.def("load", &SourceContainer::loadRasterSource)
|
||||
.def("query", &SourceContainer::getRasterDataFromSource)
|
||||
.def("interpolate", &SourceContainer::getRasterInterpolateFromSource),
|
||||
luabind::class_<const float>("constants")
|
||||
.enum_("enums")[luabind::value("precision", COORDINATE_PRECISION)],
|
||||
luabind::module(context.state)
|
||||
[luabind::def("print", util::LUA_print<std::string>),
|
||||
luabind::def("durationIsValid", durationIsValid),
|
||||
luabind::def("parseDuration", parseDuration),
|
||||
luabind::class_<TravelMode>("mode")
|
||||
.enum_("enums")[luabind::value("inaccessible", TRAVEL_MODE_INACCESSIBLE),
|
||||
luabind::value("driving", TRAVEL_MODE_DRIVING),
|
||||
luabind::value("cycling", TRAVEL_MODE_CYCLING),
|
||||
luabind::value("walking", TRAVEL_MODE_WALKING),
|
||||
luabind::value("ferry", TRAVEL_MODE_FERRY),
|
||||
luabind::value("train", TRAVEL_MODE_TRAIN),
|
||||
luabind::value("pushing_bike", TRAVEL_MODE_PUSHING_BIKE),
|
||||
luabind::value("movable_bridge", TRAVEL_MODE_MOVABLE_BRIDGE),
|
||||
luabind::value("steps_up", TRAVEL_MODE_STEPS_UP),
|
||||
luabind::value("steps_down", TRAVEL_MODE_STEPS_DOWN),
|
||||
luabind::value("river_up", TRAVEL_MODE_RIVER_UP),
|
||||
luabind::value("river_down", TRAVEL_MODE_RIVER_DOWN),
|
||||
luabind::value("route", TRAVEL_MODE_ROUTE)],
|
||||
luabind::class_<SourceContainer>("sources")
|
||||
.def(luabind::constructor<>())
|
||||
.def("load", &SourceContainer::loadRasterSource)
|
||||
.def("query", &SourceContainer::getRasterDataFromSource)
|
||||
.def("interpolate", &SourceContainer::getRasterInterpolateFromSource),
|
||||
luabind::class_<const float>("constants")
|
||||
.enum_("enums")[luabind::value("precision", COORDINATE_PRECISION)],
|
||||
|
||||
luabind::class_<std::vector<std::string>>("vector").def(
|
||||
"Add", static_cast<void (std::vector<std::string>::*)(const std::string &)>(
|
||||
&std::vector<std::string>::push_back)),
|
||||
luabind::class_<ProfileProperties>("ProfileProperties")
|
||||
.def(luabind::constructor<>())
|
||||
.property("traffic_signal_penalty", &ProfileProperties::GetTrafficSignalPenalty,
|
||||
&ProfileProperties::SetTrafficSignalPenalty)
|
||||
.property("u_turn_penalty", &ProfileProperties::GetUturnPenalty,
|
||||
&ProfileProperties::SetUturnPenalty)
|
||||
.def_readwrite("allow_u_turn_at_via", &ProfileProperties::allow_u_turn_at_via),
|
||||
|
||||
luabind::class_<osmium::Location>("Location")
|
||||
.def<location_member_ptr_type>("lat", &osmium::Location::lat)
|
||||
.def<location_member_ptr_type>("lon", &osmium::Location::lon),
|
||||
luabind::class_<std::vector<std::string>>("vector")
|
||||
.def("Add", static_cast<void (std::vector<std::string>::*)(const std::string &)>(
|
||||
&std::vector<std::string>::push_back)),
|
||||
|
||||
luabind::class_<osmium::Node>("Node")
|
||||
// .def<node_member_ptr_type>("tags", &osmium::Node::tags)
|
||||
.def("location", &osmium::Node::location)
|
||||
.def("get_value_by_key", &osmium::Node::get_value_by_key)
|
||||
.def("get_value_by_key", &get_value_by_key<osmium::Node>)
|
||||
.def("id", &osmium::Node::id),
|
||||
luabind::class_<osmium::Location>("Location")
|
||||
.def<location_member_ptr_type>("lat", &osmium::Location::lat)
|
||||
.def<location_member_ptr_type>("lon", &osmium::Location::lon),
|
||||
|
||||
luabind::class_<ExtractionNode>("ResultNode")
|
||||
.def_readwrite("traffic_lights", &ExtractionNode::traffic_lights)
|
||||
.def_readwrite("barrier", &ExtractionNode::barrier),
|
||||
luabind::class_<osmium::Node>("Node")
|
||||
// .def<node_member_ptr_type>("tags", &osmium::Node::tags)
|
||||
.def("location", &osmium::Node::location)
|
||||
.def("get_value_by_key", &osmium::Node::get_value_by_key)
|
||||
.def("get_value_by_key", &get_value_by_key<osmium::Node>)
|
||||
.def("id", &osmium::Node::id),
|
||||
|
||||
luabind::class_<ExtractionWay>("ResultWay")
|
||||
// .def(luabind::constructor<>())
|
||||
.def_readwrite("forward_speed", &ExtractionWay::forward_speed)
|
||||
.def_readwrite("backward_speed", &ExtractionWay::backward_speed)
|
||||
.def_readwrite("name", &ExtractionWay::name)
|
||||
.def_readwrite("roundabout", &ExtractionWay::roundabout)
|
||||
.def_readwrite("is_access_restricted", &ExtractionWay::is_access_restricted)
|
||||
.def_readwrite("is_startpoint", &ExtractionWay::is_startpoint)
|
||||
.def_readwrite("duration", &ExtractionWay::duration)
|
||||
.property("forward_mode", &ExtractionWay::get_forward_mode,
|
||||
&ExtractionWay::set_forward_mode)
|
||||
.property("backward_mode", &ExtractionWay::get_backward_mode,
|
||||
&ExtractionWay::set_backward_mode),
|
||||
luabind::class_<osmium::Way>("Way")
|
||||
.def("get_value_by_key", &osmium::Way::get_value_by_key)
|
||||
.def("get_value_by_key", &get_value_by_key<osmium::Way>)
|
||||
.def("id", &osmium::Way::id),
|
||||
luabind::class_<InternalExtractorEdge>("EdgeSource")
|
||||
.def_readonly("source_coordinate", &InternalExtractorEdge::source_coordinate)
|
||||
.def_readwrite("weight_data", &InternalExtractorEdge::weight_data),
|
||||
luabind::class_<InternalExtractorEdge::WeightData>("WeightData")
|
||||
.def_readwrite("speed", &InternalExtractorEdge::WeightData::speed),
|
||||
luabind::class_<ExternalMemoryNode>("EdgeTarget")
|
||||
.property("lon", &lonToDouble<ExternalMemoryNode>)
|
||||
.property("lat", &latToDouble<ExternalMemoryNode>),
|
||||
luabind::class_<util::Coordinate>("Coordinate")
|
||||
.property("lon", &lonToDouble<util::Coordinate>)
|
||||
.property("lat", &latToDouble<util::Coordinate>),
|
||||
luabind::class_<RasterDatum>("RasterDatum")
|
||||
.def_readonly("datum", &RasterDatum::datum)
|
||||
.def("invalid_data", &RasterDatum::get_invalid)];
|
||||
luabind::class_<ExtractionNode>("ResultNode")
|
||||
.def_readwrite("traffic_lights", &ExtractionNode::traffic_lights)
|
||||
.def_readwrite("barrier", &ExtractionNode::barrier),
|
||||
|
||||
if (0 != luaL_dofile(lua_state, file_name.c_str()))
|
||||
luabind::class_<ExtractionWay>("ResultWay")
|
||||
// .def(luabind::constructor<>())
|
||||
.def_readwrite("forward_speed", &ExtractionWay::forward_speed)
|
||||
.def_readwrite("backward_speed", &ExtractionWay::backward_speed)
|
||||
.def_readwrite("name", &ExtractionWay::name)
|
||||
.def_readwrite("roundabout", &ExtractionWay::roundabout)
|
||||
.def_readwrite("is_access_restricted", &ExtractionWay::is_access_restricted)
|
||||
.def_readwrite("is_startpoint", &ExtractionWay::is_startpoint)
|
||||
.def_readwrite("duration", &ExtractionWay::duration)
|
||||
.property("forward_mode", &ExtractionWay::get_forward_mode,
|
||||
&ExtractionWay::set_forward_mode)
|
||||
.property("backward_mode", &ExtractionWay::get_backward_mode,
|
||||
&ExtractionWay::set_backward_mode),
|
||||
luabind::class_<osmium::Way>("Way")
|
||||
.def("get_value_by_key", &osmium::Way::get_value_by_key)
|
||||
.def("get_value_by_key", &get_value_by_key<osmium::Way>)
|
||||
.def("id", &osmium::Way::id),
|
||||
luabind::class_<InternalExtractorEdge>("EdgeSource")
|
||||
.def_readonly("source_coordinate", &InternalExtractorEdge::source_coordinate)
|
||||
.def_readwrite("weight_data", &InternalExtractorEdge::weight_data),
|
||||
luabind::class_<InternalExtractorEdge::WeightData>("WeightData")
|
||||
.def_readwrite("speed", &InternalExtractorEdge::WeightData::speed),
|
||||
luabind::class_<ExternalMemoryNode>("EdgeTarget")
|
||||
.property("lon", &lonToDouble<ExternalMemoryNode>)
|
||||
.property("lat", &latToDouble<ExternalMemoryNode>),
|
||||
luabind::class_<util::Coordinate>("Coordinate")
|
||||
.property("lon", &lonToDouble<util::Coordinate>)
|
||||
.property("lat", &latToDouble<util::Coordinate>),
|
||||
luabind::class_<RasterDatum>("RasterDatum")
|
||||
.def_readonly("datum", &RasterDatum::datum)
|
||||
.def("invalid_data", &RasterDatum::get_invalid)];
|
||||
|
||||
luabind::globals(context.state)["properties"] = context.properties;
|
||||
luabind::globals(context.state)["sources"] = context.sources;
|
||||
|
||||
if (0 != luaL_dofile(context.state, file_name.c_str()))
|
||||
{
|
||||
luabind::object error_msg(luabind::from_stack(lua_state, -1));
|
||||
luabind::object error_msg(luabind::from_stack(context.state, -1));
|
||||
std::ostringstream error_stream;
|
||||
error_stream << error_msg;
|
||||
throw util::exception("ERROR occurred in profile script:\n" + error_stream.str());
|
||||
}
|
||||
}
|
||||
|
||||
lua_State *ScriptingEnvironment::GetLuaState()
|
||||
ScriptingEnvironment::Context &ScriptingEnvironment::GetContex()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(init_mutex);
|
||||
bool initialized = false;
|
||||
auto &ref = script_contexts.local(initialized);
|
||||
if (!initialized)
|
||||
{
|
||||
std::shared_ptr<lua_State> state(luaL_newstate(), lua_close);
|
||||
std::shared_ptr<Context> state = std::make_shared<Context>();
|
||||
ref = state;
|
||||
InitLuaState(ref.get());
|
||||
InitContext(*ref);
|
||||
}
|
||||
luabind::set_pcall_callback(&luaErrorCallback);
|
||||
|
||||
return ref.get();
|
||||
return *ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user