Skip to content

Commit

Permalink
Add cache into scripting_environment.
Browse files Browse the repository at this point in the history
Signed-off-by: FILLAU Jean-Maxime <[email protected]>
  • Loading branch information
FILLAU Jean-Maxime committed Nov 7, 2016
1 parent 513a799 commit 57ccb8b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/extractor/scripting_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#include <string>
#include <vector>

#include <osmium/index/map/sparse_file_array.hpp>
#include <osmium/index/map/dense_file_array.hpp>
#include <osmium/handler/node_locations_for_ways.hpp>
#include <osmium/dynamic_handler.hpp>

namespace osmium
{
class Node;
Expand All @@ -36,6 +41,12 @@ class RestrictionParser;
struct ExtractionNode;
struct ExtractionWay;

using index_type = osmium::index::map::SparseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
// using index_type = osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type, osmium::Location>;

// The location handler always depends on the index type
using location_handler_type = osmium::handler::NodeLocationsForWays<index_type>;

/**
* Abstract class that handles processing osmium ways, nodes and relation objects by applying
* user supplied profiles.
Expand Down Expand Up @@ -65,6 +76,11 @@ class ScriptingEnvironment
tbb::concurrent_vector<std::pair<std::size_t, ExtractionWay>> &resulting_ways,
tbb::concurrent_vector<boost::optional<InputRestrictionContainer>>
&resulting_restrictions) = 0;

static location_handler_type *location_handler;
static void setCache(location_handler_type &cache) {
location_handler = &cache;
};
};
}
}
Expand Down
8 changes: 8 additions & 0 deletions profiles/car.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions src/extractor/extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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<const osmium::Node &>(*iter));
}

// clear resulting vectors
Expand Down
6 changes: 6 additions & 0 deletions src/extractor/scripting_environment_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 57ccb8b

Please sign in to comment.