diff options
author | Sergey Yershov <syershov@maps.me> | 2016-11-15 17:14:43 +0300 |
---|---|---|
committer | Sergey Yershov <syershov@maps.me> | 2016-11-16 15:04:22 +0300 |
commit | 715c6cac0c6297f350c83c1126954f8d5b188bf4 (patch) | |
tree | dc449a5e7789632a2a638d64e46b167cc6825184 /traffic | |
parent | cf1f3d1fccbb11c6a1bf14450f43ffd3e4d1852f (diff) |
Add python bindings for traffic
Diffstat (limited to 'traffic')
-rw-r--r-- | traffic/CMakeLists.txt | 3 | ||||
-rw-r--r-- | traffic/pytraffic/CMakeLists.txt | 48 | ||||
-rw-r--r-- | traffic/pytraffic/bindings.cpp | 64 |
3 files changed, 114 insertions, 1 deletions
diff --git a/traffic/CMakeLists.txt b/traffic/CMakeLists.txt index 40ac62dd71..c77f3f67ee 100644 --- a/traffic/CMakeLists.txt +++ b/traffic/CMakeLists.txt @@ -10,4 +10,5 @@ set( add_library(${PROJECT_NAME} ${SRC}) -add_subdirectory(traffic_tests) +add_subdirectory(pytraffic) +#add_subdirectory(traffic_tests) diff --git a/traffic/pytraffic/CMakeLists.txt b/traffic/pytraffic/CMakeLists.txt new file mode 100644 index 0000000000..95aa28894d --- /dev/null +++ b/traffic/pytraffic/CMakeLists.txt @@ -0,0 +1,48 @@ +project(pytraffic) + +check_pybindings() + +set( + SRC + bindings.cpp +) + +# Suppress boost-python warnings +add_compile_options( + "-Wno-unused-local-typedef" +) + +set(Boost_USE_MULTITHREADED ON) + +# For macOS we can use static linking, on Linux we can't. +if (PLATFORM_MAC) + set(Boost_USE_STATIC_LIBS ON) + set(Boost_USE_STATIC_RUNTIME ON) +endif() + +find_package(PythonLibs 2.7 REQUIRED) +find_package(Boost 1.54 REQUIRED COMPONENTS python) +include_directories(${PYTHON_INCLUDE_DIRS}) + +add_library(${PROJECT_NAME} MODULE ${SRC}) + +if (PLATFORM_MAC) + omim_link_libraries( + ${PROJECT_NAME} + ${Qt5Widgets_LIBRARIES} + "-framework Cocoa" + "-framework IOKit" + "-framework QuartzCore" + "-framework SystemConfiguration" + ) +endif() + +if (PLATFORM_WIN OR PLATFORM_LINUX) + omim_link_libraries( + ${PROJECT_NAME} + ${Qt5Widgets_LIBRARIES} + ) +endif() + +omim_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES} traffic platform geometry base) +set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") diff --git a/traffic/pytraffic/bindings.cpp b/traffic/pytraffic/bindings.cpp new file mode 100644 index 0000000000..7d31ee2e27 --- /dev/null +++ b/traffic/pytraffic/bindings.cpp @@ -0,0 +1,64 @@ +#include "traffic/traffic_info.hpp" +#include "traffic/speed_groups.hpp" + +#include "pyhelpers/vector_uint8.hpp" + +#include <boost/python.hpp> +#include <boost/python/suite/indexing/map_indexing_suite.hpp> + +namespace +{ +using namespace boost::python; + +vector<uint8_t> Serialize(traffic::TrafficInfo::Coloring const & coloring) +{ + vector<uint8_t> data; + traffic::TrafficInfo::SerializeTrafficData(coloring, data); + return data; +} + +traffic::TrafficInfo::Coloring Deserialize(vector<uint8_t> const & data) +{ + traffic::TrafficInfo::Coloring coloring; + traffic::TrafficInfo::DeserializeTrafficData(data, coloring); + return coloring; +} + +string RoadSegmentIdRepr(traffic::TrafficInfo::RoadSegmentId const & v) +{ + stringstream ss; + ss << "RoadSegmentId(" << v.m_fid << ", " << v.m_idx << ", " << int(v.m_dir) << ")"; + return ss.str(); +} +} // namespace + +BOOST_PYTHON_MODULE(pytraffic) +{ + using namespace boost::python; + + // Register the to-python converters. + to_python_converter<vector<uint8_t>, vector_uint8t_to_str>(); + vector_uint8t_from_python_str(); + + class_<traffic::TrafficInfo::RoadSegmentId>("RoadSegmentId", init<uint32_t, uint16_t, uint8_t>()) + .def("__repr__", &RoadSegmentIdRepr) + ; + + enum_<traffic::SpeedGroup>("SpeedGroup") + .value("G0", traffic::SpeedGroup::G0) + .value("G1", traffic::SpeedGroup::G1) + .value("G2", traffic::SpeedGroup::G2) + .value("G3", traffic::SpeedGroup::G3) + .value("G4", traffic::SpeedGroup::G4) + .value("G5", traffic::SpeedGroup::G5) + .value("TempBlock", traffic::SpeedGroup::TempBlock) + .value("Unknown", traffic::SpeedGroup::Unknown) + ; + + class_<traffic::TrafficInfo::Coloring>("Coloring") + .def(map_indexing_suite<traffic::TrafficInfo::Coloring>()) + ; + + def("dumps", Serialize); + def("loads", Deserialize); +} |