Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--pyhelpers/pair.hpp29
-rw-r--r--pyhelpers/vector_uint8.hpp47
-rw-r--r--tracking/pytracking/bindings.cpp61
-rw-r--r--traffic/CMakeLists.txt3
-rw-r--r--traffic/pytraffic/CMakeLists.txt48
-rw-r--r--traffic/pytraffic/bindings.cpp64
7 files changed, 194 insertions, 59 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 20ad050b20..a48e2db26f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -159,4 +159,5 @@ add_subdirectory(editor)
add_subdirectory(indexer)
add_subdirectory(routing)
add_subdirectory(search)
+add_subdirectory(traffic)
add_subdirectory(tracking)
diff --git a/pyhelpers/pair.hpp b/pyhelpers/pair.hpp
new file mode 100644
index 0000000000..54155aed4c
--- /dev/null
+++ b/pyhelpers/pair.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include "std/utility.hpp"
+
+#include <boost/python.hpp>
+#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
+
+namespace
+{
+using namespace boost::python;
+
+// Converts a std::pair instance to a Python tuple.
+template <typename T1, typename T2>
+struct pair_to_tuple
+{
+ static PyObject * convert(pair<T1, T2> const & p)
+ {
+ return incref(make_tuple(p.first, p.second).ptr());
+ }
+
+ static PyTypeObject const * get_pytype() { return &PyTuple_Type; }
+};
+
+template <typename T1, typename T2>
+struct pair_to_python_converter
+{
+ pair_to_python_converter() { to_python_converter<pair<T1, T2>, pair_to_tuple<T1, T2>, true>(); }
+};
+} // namespace
diff --git a/pyhelpers/vector_uint8.hpp b/pyhelpers/vector_uint8.hpp
new file mode 100644
index 0000000000..f1bebc5ff3
--- /dev/null
+++ b/pyhelpers/vector_uint8.hpp
@@ -0,0 +1,47 @@
+#pragma once
+
+#include "std/vector.hpp"
+
+#include <boost/python.hpp>
+#include <boost/python/suite/indexing/map_indexing_suite.hpp>
+
+namespace
+{
+using namespace boost::python;
+
+// Converts a vector<uint8_t> to/from Python str.
+struct vector_uint8t_to_str
+{
+ static PyObject * convert(vector<uint8_t> const & v)
+ {
+ str s(reinterpret_cast<char const *>(v.data()), v.size());
+ return incref(s.ptr());
+ }
+};
+
+struct vector_uint8t_from_python_str
+{
+ vector_uint8t_from_python_str()
+ {
+ converter::registry::push_back(&convertible, &construct, type_id<vector<uint8_t>>());
+ }
+
+ static void * convertible(PyObject * obj_ptr)
+ {
+ if (!PyString_Check(obj_ptr))
+ return nullptr;
+ return obj_ptr;
+ }
+
+ static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data)
+ {
+ const char * value = PyString_AsString(obj_ptr);
+ if (value == nullptr)
+ throw_error_already_set();
+ void * storage =
+ ((converter::rvalue_from_python_storage<vector<uint8_t>> *)data)->storage.bytes;
+ new (storage) vector<uint8_t>(value, value + PyString_Size(obj_ptr));
+ data->convertible = storage;
+ }
+};
+} // namespace
diff --git a/tracking/pytracking/bindings.cpp b/tracking/pytracking/bindings.cpp
index 699f0a06b3..3bd9bdd70b 100644
--- a/tracking/pytracking/bindings.cpp
+++ b/tracking/pytracking/bindings.cpp
@@ -2,67 +2,12 @@
#include "coding/traffic.hpp"
+#include "pyhelpers/pair.hpp"
+#include "pyhelpers/vector_uint8.hpp"
+
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
-namespace
-{
-using namespace boost::python;
-
-// Converts a std::pair instance to a Python tuple.
-template <typename T1, typename T2>
-struct pair_to_tuple
-{
- static PyObject * convert(pair<T1, T2> const & p)
- {
- return incref(make_tuple(p.first, p.second).ptr());
- }
-
- static PyTypeObject const * get_pytype() { return &PyTuple_Type; }
-};
-
-template <typename T1, typename T2>
-struct pair_to_python_converter
-{
- pair_to_python_converter() { to_python_converter<pair<T1, T2>, pair_to_tuple<T1, T2>, true>(); }
-};
-
-// Converts a vector<uint8_t> to/from Python str.
-struct vector_uint8t_to_str
-{
- static PyObject * convert(vector<uint8_t> const & v)
- {
- str s(reinterpret_cast<char const *>(v.data()), v.size());
- return incref(s.ptr());
- }
-};
-
-struct vector_uint8t_from_python_str
-{
- vector_uint8t_from_python_str()
- {
- converter::registry::push_back(&convertible, &construct, type_id<vector<uint8_t>>());
- }
-
- static void * convertible(PyObject * obj_ptr)
- {
- if (!PyString_Check(obj_ptr))
- return nullptr;
- return obj_ptr;
- }
-
- static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data)
- {
- const char * value = PyString_AsString(obj_ptr);
- if (value == nullptr)
- throw_error_already_set();
- void * storage =
- ((converter::rvalue_from_python_storage<vector<uint8_t>> *)data)->storage.bytes;
- new (storage) vector<uint8_t>(value, value + PyString_Size(obj_ptr));
- data->convertible = storage;
- }
-};
-} // namespace
BOOST_PYTHON_MODULE(pytracking)
{
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);
+}