Skip to content

Commit 2acbdde

Browse files
committed
Merge branch 'master' of https://github.com/mapzen/valhalla into gdg
2 parents 9a97cb0 + 669342c commit 2acbdde

File tree

7 files changed

+219
-31
lines changed

7 files changed

+219
-31
lines changed

Makefile.am

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
ACLOCAL_AMFLAGS = -I m4
22
AM_LDFLAGS = @BOOST_LDFLAGS@ @LUA_LIB@ @COVERAGE_LDFLAGS@
3-
AM_CPPFLAGS = \
4-
-Iinclude \
5-
@BOOST_CPPFLAGS@
6-
@LUA_INCLUDE@
7-
AM_CXXFLAGS = @COVERAGE_CXXFLAGS@
3+
AM_CPPFLAGS = -Iinclude @BOOST_CPPFLAGS@
4+
AM_CXXFLAGS = @COVERAGE_CXXFLAGS@ @LUA_INCLUDE@
85

96
if HAVE_SQLITE3
107
AM_CPPFLAGS += @SQLITE3_CFLAGS@
@@ -90,7 +87,7 @@ include_HEADERS = \
9087
include/mjolnir/graphtilebuilder.h \
9188
include/mjolnir/graphtileheaderbuilder.h \
9289
include/mjolnir/nodeinfobuilder.h \
93-
include/mjolnir/edgeinfobuilder.h
90+
include/mjolnir/edgeinfobuilder.h
9491
libvalhalla_la_SOURCES = \
9592
src/valhalla.cc \
9693
src/baldr/directededge.cc \
@@ -124,7 +121,7 @@ libvalhalla_la_SOURCES = \
124121
src/mjolnir/graphtilebuilder.cc \
125122
src/mjolnir/graphtileheaderbuilder.cc \
126123
src/mjolnir/nodeinfobuilder.cc \
127-
src/mjolnir/edgeinfobuilder.cc
124+
src/mjolnir/edgeinfobuilder.cc
128125
libvalhalla_la_CPPFLAGS = $(DEPS_CFLAGS) $(BOOST_CPPFLAGS)
129126
libvalhalla_la_LIBADD = $(DEPS_LIBS) @PROTOC_LIBS@ @BOOST_PROGRAM_OPTIONS_LIB@
130127
if HAVE_SQLITE3
@@ -140,12 +137,14 @@ valhalla_SOURCES = \
140137
valhalla_CPPFLAGS = $(DEPS_CFLAGS) $(BOOST_CPPFLAGS)
141138
valhalla_LDADD = $(DEPS_LIBS) @BOOST_LDFLAGS@ @BOOST_PROGRAM_OPTIONS_LIB@
142139

140+
143141
#pbfgraphbuilder_SOURCES = \
144142
# src/mjolnir/pbfgraphbuilder/pbfgraphbuilder.cc \
145-
# src/mjolnir/pbfgraphbuilder/graphbuilder.cc
143+
# src/mjolnir/pbfgraphbuilder/graphbuilder.cc \
144+
# src/mjolnir/pbfgraphbuilder/luatagtransform.cc
146145
#pbfgraphbuilder_CPPFLAGS = $(DEPS_CFLAGS) $(BOOST_CPPFLAGS)
147146
#pbfgraphbuilder_LDADD = $(DEPS_LIBS) @BOOST_LDFLAGS@ @BOOST_PROGRAM_OPTIONS_LIB@ \
148-
# -losmpbf -lprotobuf -lz -lpthread libvalhalla.la
147+
# -losmpbf -lprotobuf -lz -lpthread -llua5.2 libvalhalla.la
149148

150149
# tests
151150
check_PROGRAMS = \

configure.ac

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ AX_BOOST_PROGRAM_OPTIONS
3636
AX_PROG_LUA([5.2],[],[
3737
AX_LUA_HEADERS([
3838
AX_LUA_LIBS([
39-
AC_DEFINE([HAVE_LUA], [1], [Requirements for lua are met])
40-
HAVE_LUA=yes
4139
],[AC_MSG_ERROR([Cannot find Lua libs. Please install lua5.2 liblua5.2-dev])])
4240
],[AC_MSG_ERROR([Cannot find Lua includes. Please install lua5.2 liblua5.2-dev])])
4341
],[AC_MSG_ERROR([Cannot find Lua interpreter. Please install lua5.2 liblua5.2-dev])])

src/mjolnir/pbfgraphbuilder/graphbuilder.cc

+25-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ namespace valhalla {
1313
namespace mjolnir {
1414

1515
void GraphBuilder::node_callback(uint64_t osmid, double lng, double lat,
16-
const Tags &/*tags*/) {
16+
const Tags &tags) {
17+
18+
Tags results = lua_.TransformInLua(false,tags);
19+
20+
if (results.size() == 0)
21+
return;
22+
23+
//TODO:: Save the tag results to disk.
24+
1725
nodes_.insert(std::make_pair(osmid, OSMNode((float)lat, (float)lng)));
1826
nodecount++;
1927

@@ -24,24 +32,23 @@ void GraphBuilder::way_callback(uint64_t osmid, const Tags &tags,
2432
const std::vector<uint64_t> &refs) {
2533
// Do not add ways with < 2 nodes. Log error or add to a problem list
2634
// TODO - find out if we do need these, why they exist...
27-
// if (refs.size() < 2) {
28-
// std::cout << "ERROR - way " << osmid << " with < 2 nodes" << std::endl;
29-
// return;
30-
// }
31-
32-
// Add the way if it has a highway tag.
33-
// There are other tags that correspond to the street network,
34-
// however for simplicity, we don't manage them
35-
if (tags.find("highway") != tags.end()) {
36-
OSMWay w(osmid);
37-
w.nodelist_ = refs;
38-
39-
// TODO: Read more properties!!!
40-
41-
ways_.push_back(w);
42-
// if (ways.size() % 100000 == 0)
43-
// std::cout << ways.size() << " ways" << std::endl;
35+
if (refs.size() < 2) {
36+
std::cout << "ERROR - way " << osmid << " with < 2 nodes" << std::endl;
37+
return;
4438
}
39+
40+
Tags results = lua_.TransformInLua(true,tags);
41+
42+
if (results.size() == 0)
43+
return;
44+
45+
OSMWay w(osmid);
46+
w.nodelist_ = refs;
47+
48+
//TODO:: Save the tag results to disk.
49+
50+
ways_.push_back(w);
51+
4552
}
4653

4754
void GraphBuilder::relation_callback(uint64_t /*osmid*/, const Tags &/*tags*/,

src/mjolnir/pbfgraphbuilder/graphbuilder.h

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pbfgraphbuilder.h"
1010
#include "osmpbfreader.h"
1111
#include "geo/pointll.h"
12+
#include "luatagtransform.h"
1213

1314
using namespace CanalTP; // For OSM pbf reader
1415
//using namespace std;
@@ -34,6 +35,7 @@ class GraphBuilder {
3435
// Initialize counts
3536
relationcount = 0;
3637
nodecount = 0;
38+
lua_.Init();
3739
}
3840

3941
/**
@@ -105,6 +107,9 @@ class GraphBuilder {
105107
// Map of OSM node Ids to GraphIds
106108
node_graphid_map_type node_graphids_;
107109

110+
// Lua Tag Transformation class
111+
LuaTagTransform lua_;
112+
108113
// Tiled nodes
109114
std::vector<std::vector<uint64_t>> tilednodes_;
110115
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "luatagtransform.h"
2+
3+
#include <stdexcept>
4+
#include <boost/format.hpp>
5+
6+
using namespace valhalla::mjolnir;
7+
8+
LuaTagTransform::LuaTagTransform()
9+
{
10+
// Create a new lua state
11+
luastate_ = luaL_newstate();
12+
13+
}
14+
15+
LuaTagTransform::~LuaTagTransform(){
16+
if (luastate_ != NULL) {
17+
18+
lua_close(luastate_);
19+
20+
}
21+
}
22+
23+
void LuaTagTransform::Init() const {
24+
25+
luaL_openlibs(luastate_);
26+
27+
//TODO:add lua script to options.
28+
std::string tag_transform_script = "/data/valhalla/import/osm2pgsql/edges.lua";
29+
luaL_dofile(luastate_, tag_transform_script.c_str());
30+
CheckLuaFuncExists("ways_proc");
31+
32+
tag_transform_script = "/data/valhalla/import/osm2pgsql/vertices.lua";
33+
luaL_dofile(luastate_, tag_transform_script.c_str());
34+
CheckLuaFuncExists("nodes_proc");
35+
}
36+
37+
void LuaTagTransform::SetLuaWayFunc(std::string luawayfunc) {
38+
luawayfunc_ = luawayfunc;
39+
}
40+
41+
void LuaTagTransform::SetLuaNodeFunc(std::string luanodefunc) {
42+
luanodefunc_ = luanodefunc;
43+
}
44+
45+
std::string LuaTagTransform::GetLuaWayFunc() const {
46+
return luawayfunc_;
47+
}
48+
49+
std::string LuaTagTransform::GetLuaNodeFunc() const {
50+
return luanodefunc_;
51+
}
52+
53+
void LuaTagTransform::CheckLuaFuncExists(const std::string &func_name) const {
54+
55+
lua_getglobal(luastate_, func_name.c_str());
56+
57+
if (!lua_isfunction (luastate_, -1)) {
58+
throw std::runtime_error((boost::format("Lua script does not contain a function %1%")
59+
% func_name).str());
60+
}
61+
lua_pop(luastate_,1);
62+
}
63+
64+
Tags LuaTagTransform::TransformInLua(bool isWay, const Tags &maptags) {
65+
66+
Tags result;
67+
68+
int filter;
69+
int count = 0;
70+
struct keyval *item;
71+
const char * key, * value;
72+
73+
int polygon = 0;
74+
int roads = 0;
75+
76+
//TODO::add boost options.
77+
if (isWay)
78+
lua_getglobal(luastate_,"ways_proc");
79+
else
80+
lua_getglobal(luastate_,"nodes_proc");
81+
82+
lua_newtable(luastate_); /* key value table */
83+
84+
for (auto tag : maptags) {
85+
lua_pushstring(luastate_, tag.first.c_str());
86+
lua_pushstring(luastate_, tag.second.c_str());
87+
lua_rawset(luastate_, -3);
88+
count++;
89+
}
90+
91+
lua_pushinteger(luastate_, count);
92+
93+
if (lua_pcall(luastate_,2,4,0)) {
94+
fprintf(stderr, "Failed to execute lua function for basic tag processing: %s\n", lua_tostring(luastate_, -1));
95+
/* lua function failed */
96+
}
97+
98+
roads = lua_tointeger(luastate_, -1);
99+
lua_pop(luastate_,1);
100+
polygon = lua_tointeger(luastate_, -1);
101+
lua_pop(luastate_,1);
102+
103+
lua_pushnil(luastate_);
104+
while (lua_next(luastate_,-2) != 0) {
105+
key = lua_tostring(luastate_,-2);
106+
value = lua_tostring(luastate_,-1);
107+
108+
result[key] = value;
109+
110+
lua_pop(luastate_,1);
111+
}
112+
113+
filter = lua_tointeger(luastate_, -2);
114+
115+
lua_pop(luastate_,2);
116+
117+
return result;
118+
119+
}
120+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef VALHALLA_MJOLNIR_LUA_H
2+
#define VALHALLA_MJOLNIR_LUA_H
3+
4+
extern "C" {
5+
#include "lua.h"
6+
#include <lualib.h>
7+
#include <lauxlib.h>
8+
}
9+
10+
#include <string>
11+
#include <map>
12+
13+
// Represents the key/values of an object
14+
typedef std::map<std::string, std::string> Tags;
15+
16+
namespace valhalla {
17+
namespace mjolnir {
18+
19+
/**
20+
*/
21+
class LuaTagTransform {
22+
public:
23+
/**
24+
* Constructor
25+
*/
26+
LuaTagTransform();
27+
28+
/**
29+
* Destructor
30+
*/
31+
~LuaTagTransform();
32+
33+
void Init() const;
34+
35+
void SetLuaWayFunc(std::string luawayfunc);
36+
37+
void SetLuaNodeFunc(std::string luanodefunc);
38+
39+
std::string GetLuaWayFunc() const;
40+
41+
std::string GetLuaNodeFunc() const;
42+
43+
void CheckLuaFuncExists(const std::string &func_name) const;
44+
45+
Tags TransformInLua(bool isWay, const Tags &tags);
46+
47+
protected:
48+
49+
lua_State *luastate_;
50+
51+
std::string luanodefunc_;
52+
std::string luawayfunc_;
53+
54+
};
55+
56+
}
57+
}
58+
59+
#endif // VALHALLA_MJOLNIR_LUA_H

src/mjolnir/pbfgraphbuilder/pbfgraphbuilder.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ int main(int argc, char** argv) {
4444

4545
// TODO - make bounding box and tilesize configurable (via properties file?)
4646
// Iterate through edges - tile the end nodes to create connected graph
47-
std::string outputdir = "/home/dave/data/tiles/";
48-
graphbuilder.BuildLocalTiles(outputdir, level);
47+
std::string outputdir = "/data/valhalla/data";
48+
graphbuilder.BuildLocalTiles(outputdir, tilesize);
4949

5050
return 0;
5151
}

0 commit comments

Comments
 (0)