diff --git a/include/extractor/scripting_environment.hpp b/include/extractor/scripting_environment.hpp index a450b3154f1..e04d1b0768a 100644 --- a/include/extractor/scripting_environment.hpp +++ b/include/extractor/scripting_environment.hpp @@ -15,6 +15,11 @@ #include #include +#include +#include +#include +#include + namespace osmium { class Node; @@ -36,6 +41,12 @@ class RestrictionParser; struct ExtractionNode; struct ExtractionWay; +using index_type = osmium::index::map::SparseFileArray; +// using index_type = osmium::index::map::DenseFileArray; + +// The location handler always depends on the index type +using location_handler_type = osmium::handler::NodeLocationsForWays; + /** * Abstract class that handles processing osmium ways, nodes and relation objects by applying * user supplied profiles. @@ -65,6 +76,11 @@ class ScriptingEnvironment tbb::concurrent_vector> &resulting_ways, tbb::concurrent_vector> &resulting_restrictions) = 0; + + static location_handler_type *location_handler; + static void setCache(location_handler_type &cache) { + location_handler = &cache; + }; }; } } diff --git a/profiles/car.lua b/profiles/car.lua index 3fd87208300..58db2e43eed 100644 --- a/profiles/car.lua +++ b/profiles/car.lua @@ -230,6 +230,14 @@ function node_function (node, result) end function way_function (way, result) + + print("way id : ", way:id()); + for node in way:get_nodes() do + node:location(); + print(" node id : ", node:id()) + print(" node lat :", node:location():lat()) + end + local highway = way:get_value_by_key("highway") local route = way:get_value_by_key("route") local bridge = way:get_value_by_key("bridge") diff --git a/src/extractor/extractor.cpp b/src/extractor/extractor.cpp index 39af53615b3..9e4474e3da5 100644 --- a/src/extractor/extractor.cpp +++ b/src/extractor/extractor.cpp @@ -117,6 +117,21 @@ int Extractor::run(ScriptingEnvironment &scripting_environment) std::min(recommended_num_threads, config.requested_num_threads); tbb::task_scheduler_init init(number_of_threads); + // Node Location Cache + const osmium::io::File input_file_cache(config.input_path.string()); + int fd = open("nodes.cache", O_RDWR | O_CREAT, 0666); + if (fd == -1) + { + throw std::runtime_error(strerror(errno)); + } + + index_type index{fd}; + + location_handler_type location_handler(index); + location_handler.ignore_errors(); + + scripting_environment.setCache(location_handler); + util::SimpleLogger().Write() << "Input file: " << config.input_path.filename().string(); if (!config.profile_path.empty()) { @@ -174,6 +189,10 @@ int Extractor::run(ScriptingEnvironment &scripting_environment) for (auto iter = std::begin(buffer), end = std::end(buffer); iter != end; ++iter) { osm_elements.push_back(iter); + + // Feed the cache, we assume the nodes are stored first, into data source. + if(iter->type() == osmium::item_type::node) + location_handler.node(static_cast(*iter)); } // clear resulting vectors diff --git a/src/extractor/scripting_environment_lua.cpp b/src/extractor/scripting_environment_lua.cpp index b0aadf97e46..4b0167db48c 100644 --- a/src/extractor/scripting_environment_lua.cpp +++ b/src/extractor/scripting_environment_lua.cpp @@ -67,6 +67,8 @@ LuaScriptingEnvironment::LuaScriptingEnvironment(const std::string &file_name) util::SimpleLogger().Write() << "Using script " << file_name; } +location_handler_type *ScriptingEnvironment::location_handler = nullptr; + void LuaScriptingEnvironment::InitContext(LuaScriptingContext &context) { typedef double (osmium::Location::*location_member_ptr_type)() const; @@ -195,6 +197,10 @@ void LuaScriptingEnvironment::InitContext(LuaScriptingContext &context) .def(luabind::constructor<>()) // Dear ambitious reader: registering .location() as in: // .def("location", +[](const osmium::NodeRef& nref){ return nref.location(); }) + .def("location", +[](const osmium::NodeRef& nref){ + osmium::Location location = LuaScriptingEnvironment::location_handler->get_node_location(nref.ref()); + return location; + }) // will crash at runtime, since we're not (yet?) using libosnmium's // NodeLocationsForWays cache .def("id", &osmium::NodeRef::ref),