From 2490fcb5046ebd5b9e70ed92907bc5a62b8819e1 Mon Sep 17 00:00:00 2001 From: whytro Date: Fri, 30 Jun 2023 22:12:52 +0900 Subject: [PATCH 1/6] Add further engine support --- CMakeLists.txt | 5 + include/parameters/matchparameter_nb.h | 8 ++ include/parameters/nearestparameter_nb.h | 8 ++ include/parameters/tableparameter_nb.h | 8 ++ include/parameters/tileparameter_nb.h | 8 ++ include/parameters/tripparameter_nb.h | 8 ++ src/osrm_nb.cpp | 32 ++++++- src/parameters/matchparameter_nb copy.cpp | 32 +++++++ src/parameters/nearestparameter_nb.cpp | 17 ++++ src/parameters/routeparameter_nb.cpp | 6 +- src/parameters/tableparameter_nb.cpp | 107 ++++++++++++++++++++++ src/parameters/tileparameter_nb.cpp | 28 ++++++ src/parameters/tripparameter_nb.cpp | 72 +++++++++++++++ src/py_osrm/__init__.py | 29 +++++- src/types/approach_nb.cpp | 2 + src/types/bearing_nb.cpp | 2 + src/types/coordinate_nb.cpp | 2 + src/types/optional_nb.cpp | 2 + src/types/status_nb.cpp | 2 + tests/test.py | 24 +---- 20 files changed, 373 insertions(+), 29 deletions(-) create mode 100644 include/parameters/matchparameter_nb.h create mode 100644 include/parameters/nearestparameter_nb.h create mode 100644 include/parameters/tableparameter_nb.h create mode 100644 include/parameters/tileparameter_nb.h create mode 100644 include/parameters/tripparameter_nb.h create mode 100755 src/parameters/matchparameter_nb copy.cpp create mode 100755 src/parameters/nearestparameter_nb.cpp create mode 100755 src/parameters/tableparameter_nb.cpp create mode 100755 src/parameters/tileparameter_nb.cpp create mode 100755 src/parameters/tripparameter_nb.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1511a73..787ebac 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,11 @@ set(SRCS src/parameters/baseparameter_nb.cpp src/parameters/routeparameter_nb.cpp + src/parameters/matchparameter_nb.cpp + src/parameters/nearestparameter_nb.cpp + src/parameters/tableparameter_nb.cpp + src/parameters/tileparameter_nb.cpp + src/parameters/tripparameter_nb.cpp src/types/optional_nb.cpp src/types/status_nb.cpp diff --git a/include/parameters/matchparameter_nb.h b/include/parameters/matchparameter_nb.h new file mode 100644 index 0000000..875a64f --- /dev/null +++ b/include/parameters/matchparameter_nb.h @@ -0,0 +1,8 @@ +#ifndef OSRM_NB_MATCHPARAMETER_H +#define OSRM_NB_MATCHPARAMETER_H + +#include + +void init_MatchParameters(nanobind::module_& m); + +#endif //OSRM_NB_MATCHPARAMETER_H diff --git a/include/parameters/nearestparameter_nb.h b/include/parameters/nearestparameter_nb.h new file mode 100644 index 0000000..ecfe30d --- /dev/null +++ b/include/parameters/nearestparameter_nb.h @@ -0,0 +1,8 @@ +#ifndef OSRM_NB_NEARESTPARAMETER_H +#define OSRM_NB_NEARESTPARAMETER_H + +#include + +void init_NearestParameters(nanobind::module_& m); + +#endif //OSRM_NB_NEARESTPARAMETER_H diff --git a/include/parameters/tableparameter_nb.h b/include/parameters/tableparameter_nb.h new file mode 100644 index 0000000..02ad1ec --- /dev/null +++ b/include/parameters/tableparameter_nb.h @@ -0,0 +1,8 @@ +#ifndef OSRM_NB_TABLEPARAMETER_H +#define OSRM_NB_TABLEPARAMETER_H + +#include + +void init_TableParameters(nanobind::module_& m); + +#endif //OSRM_NB_TABLEPARAMETER_H diff --git a/include/parameters/tileparameter_nb.h b/include/parameters/tileparameter_nb.h new file mode 100644 index 0000000..8fd8ebf --- /dev/null +++ b/include/parameters/tileparameter_nb.h @@ -0,0 +1,8 @@ +#ifndef OSRM_NB_TILEPARAMETER_H +#define OSRM_NB_TILEPARAMETER_H + +#include + +void init_TileParameters(nanobind::module_& m); + +#endif //OSRM_NB_TILEPARAMETER_H diff --git a/include/parameters/tripparameter_nb.h b/include/parameters/tripparameter_nb.h new file mode 100644 index 0000000..8ec920e --- /dev/null +++ b/include/parameters/tripparameter_nb.h @@ -0,0 +1,8 @@ +#ifndef OSRM_NB_TRIPPARAMETER_H +#define OSRM_NB_TRIPPARAMETER_H + +#include + +void init_TripParameters(nanobind::module_& m); + +#endif //OSRM_NB_TRIPPARAMETER_H diff --git a/src/osrm_nb.cpp b/src/osrm_nb.cpp index cd70b8d..de4c634 100755 --- a/src/osrm_nb.cpp +++ b/src/osrm_nb.cpp @@ -1,6 +1,11 @@ #include "osrm/osrm.hpp" #include "osrm/engine_config.hpp" +#include "osrm/match_parameters.hpp" +#include "osrm/nearest_parameters.hpp" #include "osrm/route_parameters.hpp" +#include "osrm/table_parameters.hpp" +#include "osrm/tile_parameters.hpp" +#include "osrm/trip_parameters.hpp" #include #include @@ -14,7 +19,12 @@ #include "types/optional_nb.h" #include "types/status_nb.h" #include "parameters/baseparameter_nb.h" +#include "parameters/matchparameter_nb.h" +#include "parameters/nearestparameter_nb.h" #include "parameters/routeparameter_nb.h" +#include "parameters/tableparameter_nb.h" +#include "parameters/tileparameter_nb.h" +#include "parameters/tripparameter_nb.h" namespace nb = nanobind; @@ -24,7 +34,12 @@ NB_MODULE(osrm_ext, m) { using osrm::OSRM; using osrm::engine::EngineConfig; + using osrm::engine::api::MatchParameters; + using osrm::engine::api::NearestParameters; using osrm::engine::api::RouteParameters; + using osrm::engine::api::TableParameters; + using osrm::engine::api::TileParameters; + using osrm::engine::api::TripParameters; init_EngineConfig(m); @@ -36,7 +51,12 @@ NB_MODULE(osrm_ext, m) { init_Status(m); init_BaseParameters(m); + // init_MatchParameters(m); + init_NearestParameters(m); init_RouteParameters(m); + init_TableParameters(m); + init_TileParameters(m); + init_TripParameters(m); nb::class_(m, "OSRM", nb::is_final()) .def(nb::init()) @@ -52,6 +72,16 @@ NB_MODULE(osrm_ext, m) { new (t) OSRM(config); }) + // .def("Match", nb::overload_cast(&OSRM::Match, nb::const_)) + // .def("Match", nb::overload_cast(&OSRM::Match, nb::const_)) + .def("Nearest", nb::overload_cast(&OSRM::Nearest, nb::const_)) + .def("Nearest", nb::overload_cast(&OSRM::Nearest, nb::const_)) .def("Route", nb::overload_cast(&OSRM::Route, nb::const_)) - .def("Route", nb::overload_cast(&OSRM::Route, nb::const_)); + .def("Route", nb::overload_cast(&OSRM::Route, nb::const_)) + .def("Table", nb::overload_cast(&OSRM::Table, nb::const_)) + .def("Table", nb::overload_cast(&OSRM::Table, nb::const_)) + .def("Tile", nb::overload_cast(&OSRM::Tile, nb::const_)) + .def("Tile", nb::overload_cast(&OSRM::Tile, nb::const_)) + .def("Trip", nb::overload_cast(&OSRM::Trip, nb::const_)) + .def("Trip", nb::overload_cast(&OSRM::Trip, nb::const_)); } diff --git a/src/parameters/matchparameter_nb copy.cpp b/src/parameters/matchparameter_nb copy.cpp new file mode 100755 index 0000000..2566cf2 --- /dev/null +++ b/src/parameters/matchparameter_nb copy.cpp @@ -0,0 +1,32 @@ +#include "parameters/matchparameter_nb.h" + +#include "engine/api/match_parameters.hpp" + +#include +#include +#include + +namespace nb = nanobind; +using namespace nb::literals; + +//=============================== +//this part requires some adjustment to the thing +//==================================== + +void init_MatchParameters(nb::module_& m) { + using osrm::engine::api::BaseParameters; + using osrm::engine::api::RouteParameters; + using osrm::engine::api::MatchParameters; + + nb::enum_(m, "GapsType") + .value("Split", MatchParameters::GapsType::Split) + .value("Ignore", MatchParameters::GapsType::Ignore) + .export_values(); + + nb::class_(m, "MatchParameters") + .def(nb::init<>()) + .def_rw("timestamps", &MatchParameters::timestamps) + .def_rw("gaps", &MatchParameters::gaps) + .def_rw("tidy", &MatchParameters::tidy) + .def("IsValid", &MatchParameters::IsValid); +} diff --git a/src/parameters/nearestparameter_nb.cpp b/src/parameters/nearestparameter_nb.cpp new file mode 100755 index 0000000..d8618f4 --- /dev/null +++ b/src/parameters/nearestparameter_nb.cpp @@ -0,0 +1,17 @@ +#include "parameters/nearestparameter_nb.h" + +#include "engine/api/nearest_parameters.hpp" + +#include + +namespace nb = nanobind; + +void init_NearestParameters(nb::module_& m) { + using osrm::engine::api::BaseParameters; + using osrm::engine::api::NearestParameters; + + nb::class_(m, "NearestParameters") + .def(nb::init<>()) + .def_rw("number_of_results", &NearestParameters::number_of_results) + .def("IsValid", &NearestParameters::IsValid); +} diff --git a/src/parameters/routeparameter_nb.cpp b/src/parameters/routeparameter_nb.cpp index f6862e3..51980b0 100755 --- a/src/parameters/routeparameter_nb.cpp +++ b/src/parameters/routeparameter_nb.cpp @@ -14,19 +14,19 @@ void init_RouteParameters(nb::module_& m) { using osrm::engine::api::BaseParameters; using osrm::engine::api::RouteParameters; - nb::enum_(m, "GeometriesType") + nb::enum_(m, "RouteGeometriesType") .value("Polyline", RouteParameters::GeometriesType::Polyline) .value("Polyline6", RouteParameters::GeometriesType::Polyline6) .value("GeoJSON", RouteParameters::GeometriesType::GeoJSON) .export_values(); - nb::enum_(m, "OverviewType") + nb::enum_(m, "RouteOverviewType") .value("Simplified", RouteParameters::OverviewType::Simplified) .value("Full", RouteParameters::OverviewType::Full) .value("False", RouteParameters::OverviewType::False) .export_values(); - nb::enum_(m, "AnnotationsType", nb::is_arithmetic()) + nb::enum_(m, "RouteAnnotationsType", nb::is_arithmetic()) .value("None_", RouteParameters::AnnotationsType::None) .value("Duration", RouteParameters::AnnotationsType::Duration) .value("Nodes", RouteParameters::AnnotationsType::Nodes) diff --git a/src/parameters/tableparameter_nb.cpp b/src/parameters/tableparameter_nb.cpp new file mode 100755 index 0000000..6d43f36 --- /dev/null +++ b/src/parameters/tableparameter_nb.cpp @@ -0,0 +1,107 @@ +#include "parameters/tableparameter_nb.h" + +#include "engine/api/table_parameters.hpp" + +#include +#include +#include +#include + +namespace nb = nanobind; +using namespace nb::literals; + +void init_TableParameters(nb::module_& m) { + using osrm::engine::api::BaseParameters; + using osrm::engine::api::TableParameters; + + nb::enum_(m, "TableFallbackCoordinateType", nb::is_arithmetic()) + .value("Input", TableParameters::FallbackCoordinateType::Input) + .value("Snapped", TableParameters::FallbackCoordinateType::Snapped) + .export_values(); + + nb::enum_(m, "TableAnnotationsType", nb::is_arithmetic()) + .value("None_", TableParameters::AnnotationsType::None) + .value("Duration", TableParameters::AnnotationsType::Duration) + .value("Distance", TableParameters::AnnotationsType::Distance) + .value("All", TableParameters::AnnotationsType::All) + .export_values(); + + nb::class_(m, "TableParameters") + .def(nb::init<>()) + .def(nb::init< + std::vector, + std::vector, + const TableParameters::AnnotationsType, + std::vector, + std::vector>, + std::vector>, + std::vector>, + std::vector>, + bool, + std::vector, + const BaseParameters::SnappingType + >(), + "sources"_a, + "destinations"_a, + "annotations"_a = TableParameters::AnnotationsType::Duration, + "coordinates"_a = std::vector(), + "hints"_a = std::vector>(), + "radiuses"_a = std::vector>(), + "bearings"_a = std::vector>(), + "approaches"_a = std::vector>(), + "generate_hints"_a = true, + "exclude"_a = std::vector(), + "snapping"_a = BaseParameters::SnappingType::Default + ) + .def(nb::init< + std::vector, + std::vector, + const TableParameters::AnnotationsType, + double, + TableParameters::FallbackCoordinateType, + double, + std::vector, + std::vector>, + std::vector>, + std::vector>, + std::vector>, + bool, + std::vector, + const BaseParameters::SnappingType + >(), + "sources"_a, + "destinations"_a, + "annotations"_a = TableParameters::AnnotationsType::Duration, + "fallback_speed"_a, + "fallback_coordinate_type"_a = TableParameters::FallbackCoordinateType::Input, + "scale_factor"_a = 1.0, + "coordinates"_a = std::vector(), + "hints"_a = std::vector>(), + "radiuses"_a = std::vector>(), + "bearings"_a = std::vector>(), + "approaches"_a = std::vector>(), + "generate_hints"_a = true, + "exclude"_a = std::vector(), + "snapping"_a = BaseParameters::SnappingType::Default + ) + .def_rw("sources", &TableParameters::sources) + .def_rw("destinations", &TableParameters::destinations) + .def_rw("fallback_speed", &TableParameters::fallback_speed) + .def_rw("fallback_coordinate_type", &TableParameters::fallback_coordinate_type) + .def_rw("annotations", &TableParameters::annotations) + .def_rw("scale_factor", &TableParameters::scale_factor) + .def("IsValid", &TableParameters::IsValid) + .def("__and__", [](TableParameters::AnnotationsType lhs, TableParameters::AnnotationsType rhs) { + return static_cast( + static_cast>(lhs) & + static_cast>(rhs)); + }, nb::is_operator()) + .def("__or__", [](TableParameters::AnnotationsType lhs, TableParameters::AnnotationsType rhs) { + return (TableParameters::AnnotationsType)( + static_cast>(lhs) | + static_cast>(rhs)); + }, nb::is_operator()) + .def("__ior__", [](TableParameters::AnnotationsType lhs, TableParameters::AnnotationsType rhs) { + return lhs = lhs | rhs; + }, nb::is_operator()); +} diff --git a/src/parameters/tileparameter_nb.cpp b/src/parameters/tileparameter_nb.cpp new file mode 100755 index 0000000..d4a42b6 --- /dev/null +++ b/src/parameters/tileparameter_nb.cpp @@ -0,0 +1,28 @@ +#include "parameters/tileparameter_nb.h" + +#include "engine/api/tile_parameters.hpp" + +#include +#include +#include + +namespace nb = nanobind; +using namespace nb::literals; + +void init_TileParameters(nb::module_& m) { + using osrm::engine::api::TileParameters; + + nb::class_(m, "TileParameters", nb::is_final()) + .def(nb::init<>()) + .def(nb::init(), + "x"_a, "y"_a, "z"_a + ) + .def("__init__", [](TileParameters* t, const std::vector& coord) { + new (t) TileParameters{coord[0], coord[1], coord[2]}; + }) + .def_rw("x", &TileParameters::x) + .def_rw("y", &TileParameters::y) + .def_rw("z", &TileParameters::z) + .def("IsValid", &TileParameters::IsValid); + nb::implicitly_convertible, TileParameters>(); +} diff --git a/src/parameters/tripparameter_nb.cpp b/src/parameters/tripparameter_nb.cpp new file mode 100755 index 0000000..e29bf2d --- /dev/null +++ b/src/parameters/tripparameter_nb.cpp @@ -0,0 +1,72 @@ +#include "parameters/tripparameter_nb.h" + +#include "engine/api/trip_parameters.hpp" + +#include +#include +#include + +namespace nb = nanobind; +using namespace nb::literals; + +void init_TripParameters(nb::module_& m) { + using osrm::engine::api::BaseParameters; + using osrm::engine::api::RouteParameters; + using osrm::engine::api::TripParameters; + + nb::enum_(m, "TripSourceType") + .value("Any", TripParameters::SourceType::Any) + .value("First", TripParameters::SourceType::First) + .export_values(); + + nb::enum_(m, "TripDestinationType") + .value("Any", TripParameters::DestinationType::Any) + .value("Last", TripParameters::DestinationType::Last) + .export_values(); + + nb::class_(m, "TripParameters") + .def(nb::init<>()) + .def(nb::init< + TripParameters::SourceType, + TripParameters::DestinationType, + bool, + const bool, + const bool, + const RouteParameters::AnnotationsType, + const RouteParameters::GeometriesType, + const RouteParameters::OverviewType, + const boost::optional, + std::vector, + std::vector, + std::vector>, + std::vector>, + std::vector>, + std::vector>, + bool, + std::vector, + const BaseParameters::SnappingType + >(), + "source"_a, + "destination"_a, + "roundtrip"_a, + "steps"_a, + "alternatives"_a, + "annotations"_a = RouteParameters::AnnotationsType::None, + "geometries"_a, + "overview"_a, + "continue_straight"_a, + "waypoints"_a = std::vector(), + "coordinates"_a = std::vector(), + "hints"_a = std::vector>(), + "radiuses"_a = std::vector>(), + "bearings"_a = std::vector>(), + "approaches"_a = std::vector>(), + "generate_hints"_a = true, + "exclude"_a = std::vector(), + "snapping"_a = BaseParameters::SnappingType::Default + ) + .def_rw("source", &TripParameters::source) + .def_rw("destination", &TripParameters::destination) + .def_rw("roundtrip", &TripParameters::roundtrip) + .def("IsValid", &TripParameters::IsValid); +} diff --git a/src/py_osrm/__init__.py b/src/py_osrm/__init__.py index 5ca6dde..8f5334e 100755 --- a/src/py_osrm/__init__.py +++ b/src/py_osrm/__init__.py @@ -3,10 +3,31 @@ EngineConfig, Algorithm, - RouteParameters, - GeometriesType, - AnnotationsType, + Approach, + Bearing, + Coordinate, SnappingType, - OverviewType, + OutputFormatType, + + RouteParameters, + RouteGeometriesType, + RouteOverviewType, + RouteAnnotationsType, + + NearestParameters, + + TableParameters, + TableFallbackCoordinateType, + TableAnnotationsType, + + TileParameters, + + TripParameters, + TripSourceType, + TripDestinationType, + + # MatchParameters, + # MatchGapsType, + Object ) diff --git a/src/types/approach_nb.cpp b/src/types/approach_nb.cpp index 08f9f04..a525e09 100755 --- a/src/types/approach_nb.cpp +++ b/src/types/approach_nb.cpp @@ -1,3 +1,5 @@ +#include "types/approach_nb.h" + #include "engine/approach.hpp" #include diff --git a/src/types/bearing_nb.cpp b/src/types/bearing_nb.cpp index 834cf2a..b0bcdaa 100755 --- a/src/types/bearing_nb.cpp +++ b/src/types/bearing_nb.cpp @@ -1,3 +1,5 @@ +#include "types/bearing_nb.h" + #include "engine/bearing.hpp" #include diff --git a/src/types/coordinate_nb.cpp b/src/types/coordinate_nb.cpp index 7fce38f..15bb768 100755 --- a/src/types/coordinate_nb.cpp +++ b/src/types/coordinate_nb.cpp @@ -1,3 +1,5 @@ +#include "types/coordinate_nb.h" + #include "util/coordinate.hpp" #include diff --git a/src/types/optional_nb.cpp b/src/types/optional_nb.cpp index 81137c6..5cce1b1 100755 --- a/src/types/optional_nb.cpp +++ b/src/types/optional_nb.cpp @@ -1,3 +1,5 @@ +#include "types/optional_nb.h" + #include "engine/api/base_parameters.hpp" #include diff --git a/src/types/status_nb.cpp b/src/types/status_nb.cpp index 9259231..dc12883 100755 --- a/src/types/status_nb.cpp +++ b/src/types/status_nb.cpp @@ -1,3 +1,5 @@ +#include "types/status_nb.h" + #include "osrm/status.hpp" #include diff --git a/tests/test.py b/tests/test.py index 200de5b..166253f 100755 --- a/tests/test.py +++ b/tests/test.py @@ -15,30 +15,12 @@ route_params = py_osrm.RouteParameters( steps = True, alternatives = True, - annotations = py_osrm.AnnotationsType.None_, - geometries = py_osrm.GeometriesType.GeoJSON, - overview = py_osrm.OverviewType.Full, + annotations = py_osrm.RouteAnnotationsType.None_, + geometries = py_osrm.RouteGeometriesType.GeoJSON, + overview = py_osrm.RouteOverviewType.Full, continue_straight = True, #py_osrm.OptionalBool(True), coordinates = [(7.41337, 43.72956), (7.41546, 43.73077)] ) - -# route_params = py_osrm.RouteParameters( -# steps = True, -# alternatives = True, -# annotations = py_osrm.AnnotationsType.None_, -# geometries = py_osrm.GeometriesType.GeoJSON, -# overview = py_osrm.OverviewType.Full, -# continue_straight = True, #py_osrm.OptionalBool(True), -# waypoints = [1], -# coordinates = [(1.0, 2.0)], -# radiuses = [3], -# bearings = [(3,3)], #[py_osrm.Bearing((3,3))], [py_osrm.OptionalBearing((3,3))] -# approaches = [py_osrm.Approach.CURB], -# generate_hints = True, -# exclude = [], -# snapping = py_osrm.SnappingType.Default -# ) - result = py_osrm.Object() status = engine.Route(route_params, result) From 171e825aecdc8fdcedec3875baacc7bd0c74ca9e Mon Sep 17 00:00:00 2001 From: whytro Date: Fri, 30 Jun 2023 22:13:21 +0900 Subject: [PATCH 2/6] Add __repr__ output for custom JSON types --- include/types/jsoncontainer_nb.h | 107 +++++++++++++++++++++++++++++++ src/types/jsoncontainer_nb.cpp | 67 +++++-------------- 2 files changed, 123 insertions(+), 51 deletions(-) diff --git a/include/types/jsoncontainer_nb.h b/include/types/jsoncontainer_nb.h index 8aff2ff..afb61f0 100644 --- a/include/types/jsoncontainer_nb.h +++ b/include/types/jsoncontainer_nb.h @@ -1,8 +1,115 @@ #ifndef OSRM_NB_JSONCONTAINER_H #define OSRM_NB_JSONCONTAINER_H +#include "util/json_container.hpp" + #include +#include + void init_JSONContainer(nanobind::module_& m); +namespace json = osrm::util::json; +using JSONValue = mapbox::util::variant, + mapbox::util::recursive_wrapper, + json::True, + json::False, + json::Null>; +//Custom Type Casters +namespace nanobind::detail { + +template <> struct type_caster : type_caster_base { + template using Caster = make_caster>; + + template + static handle from_cpp(T&& val, rv_policy policy, cleanup_list* cleanup) noexcept { + return mapbox::util::apply_visitor([&](auto &&v) { + return Caster::from_cpp(std::forward(v), policy, cleanup); + }, std::forward(val)); + } +}; + +template <> struct type_caster : type_caster_base { + static handle from_cpp(const json::String& val, rv_policy, cleanup_list*) noexcept { + return PyUnicode_FromStringAndSize(val.value.c_str(), val.value.size()); + } +}; +template <> struct type_caster : type_caster_base { + static handle from_cpp(const json::Number& val, rv_policy, cleanup_list*) noexcept { + return PyFloat_FromDouble((double)val.value); + } +}; + +template <> struct type_caster : type_caster_base { + static handle from_cpp(const json::True&, rv_policy, cleanup_list*) noexcept { + return handle(Py_True).inc_ref(); + } +}; +template <> struct type_caster : type_caster_base { + static handle from_cpp(const json::False&, rv_policy, cleanup_list*) noexcept { + return handle(Py_False).inc_ref(); + } +}; +template <> struct type_caster : type_caster_base { + static handle from_cpp(const json::Null&, rv_policy, cleanup_list*) noexcept { + return none().release(); + } +}; + +} //nanobind::detail + +struct ValueStringifyVisitor { + ValueStringifyVisitor(std::string& output) : _output(output) {}; + std::string& _output; + + void operator()(const json::String& str) { + _output += "'" + str.value + "'"; + } + void operator()(const json::Number& num) { + _output += std::to_string(num.value); + } + void operator()(const json::True& str) { + _output += "True"; + } + void operator()(const json::False&) { + _output += "False"; + } + void operator()(const json::Null&) { + _output += "None"; + } + + void visitarray(const json::Array& arr) { + _output += "["; + for(int i = 0; i < arr.values.size(); ++i) { + if(i != 0) { + _output += ", "; + } + mapbox::util::apply_visitor(*this, arr.values[i]); + } + _output += "]"; + } + void operator()(const mapbox::util::recursive_wrapper& arr) { + visitarray(arr.get()); + } + + void visitobject(const json::Object& obj) { + _output += "{"; + bool first = true; + for(auto itr : obj.values) { + if(!first) { + _output += ", "; + } + _output += "'" + itr.first + "': "; + mapbox::util::apply_visitor(*this, itr.second); + first = false; + } + _output += "}"; + } + void operator()(const mapbox::util::recursive_wrapper& obj) { + visitobject(obj); + } +}; + #endif //OSRM_NB_JSONCONTAINER_H diff --git a/src/types/jsoncontainer_nb.cpp b/src/types/jsoncontainer_nb.cpp index 34b1715..c6d910d 100755 --- a/src/types/jsoncontainer_nb.cpp +++ b/src/types/jsoncontainer_nb.cpp @@ -1,3 +1,5 @@ +#include "types/jsoncontainer_nb.h" + #include "util/json_container.hpp" #include @@ -9,57 +11,6 @@ namespace nb = nanobind; namespace json = osrm::util::json; -using JSONValue = mapbox::util::variant, - mapbox::util::recursive_wrapper, - json::True, - json::False, - json::Null>; - -//Custom Type Casters -namespace nanobind::detail { - -template <> struct type_caster : type_caster_base { - template using Caster = make_caster>; - - template - static handle from_cpp(T&& val, rv_policy policy, cleanup_list* cleanup) noexcept { - return mapbox::util::apply_visitor([&](auto &&v) { - return Caster::from_cpp(std::forward(v), policy, cleanup); - }, std::forward(val)); - } -}; - -template <> struct type_caster : type_caster_base { - static handle from_cpp(const json::String& val, rv_policy, cleanup_list*) noexcept { - return PyUnicode_FromStringAndSize(val.value.c_str(), val.value.size()); - } -}; -template <> struct type_caster : type_caster_base { - static handle from_cpp(const json::Number& val, rv_policy, cleanup_list*) noexcept { - return PyFloat_FromDouble((double)val.value); - } -}; - -template <> struct type_caster : type_caster_base { - static handle from_cpp(const json::True&, rv_policy, cleanup_list*) noexcept { - return handle(Py_True).inc_ref(); - } -}; -template <> struct type_caster : type_caster_base { - static handle from_cpp(const json::False&, rv_policy, cleanup_list*) noexcept { - return handle(Py_False).inc_ref(); - } -}; -template <> struct type_caster : type_caster_base { - static handle from_cpp(const json::Null&, rv_policy, cleanup_list*) noexcept { - return none().release(); - } -}; - -} //nanobind::detail - void init_JSONContainer(nb::module_& m) { nb::class_(m, "Object") .def(nb::init<>()) @@ -69,6 +20,13 @@ void init_JSONContainer(nb::module_& m) { .def("__bool__", [](const json::Object& obj) { return obj.values.empty(); }) + .def("__repr__", [](json::Object& obj) { + std::string output = ""; + ValueStringifyVisitor visitor(output); + visitor.visitobject(obj); + + return output; + }) .def("__getitem__", [](json::Object& obj, const std::string& key) { return obj.values[key]; }) @@ -85,6 +43,13 @@ void init_JSONContainer(nb::module_& m) { .def("__bool__", [](const json::Array& arr) { return arr.values.empty(); }) + .def("__repr__", [](const json::Array& arr) { + std::string output = ""; + ValueStringifyVisitor visitor(output); + visitor.visitarray(arr); + + return output; + }) .def("__iter__", [](const json::Array& arr) { return nb::make_iterator(nb::type(), "iterator", arr.values.begin(), arr.values.end()); From c5b00de35f2c3331e81dc5b08a773571bc50b314 Mon Sep 17 00:00:00 2001 From: whytro Date: Mon, 3 Jul 2023 18:20:27 +0900 Subject: [PATCH 3/6] Add in MatchParameter support --- src/osrm_nb.cpp | 10 +-- src/parameters/matchparameter_nb copy.cpp | 32 --------- src/parameters/matchparameter_nb.cpp | 83 +++++++++++++++++++++++ src/py_osrm/__init__.py | 4 +- 4 files changed, 90 insertions(+), 39 deletions(-) delete mode 100755 src/parameters/matchparameter_nb copy.cpp create mode 100755 src/parameters/matchparameter_nb.cpp diff --git a/src/osrm_nb.cpp b/src/osrm_nb.cpp index de4c634..6b5ebfb 100755 --- a/src/osrm_nb.cpp +++ b/src/osrm_nb.cpp @@ -51,12 +51,12 @@ NB_MODULE(osrm_ext, m) { init_Status(m); init_BaseParameters(m); - // init_MatchParameters(m); init_NearestParameters(m); - init_RouteParameters(m); init_TableParameters(m); - init_TileParameters(m); + init_RouteParameters(m); + init_MatchParameters(m); init_TripParameters(m); + init_TileParameters(m); nb::class_(m, "OSRM", nb::is_final()) .def(nb::init()) @@ -72,8 +72,8 @@ NB_MODULE(osrm_ext, m) { new (t) OSRM(config); }) - // .def("Match", nb::overload_cast(&OSRM::Match, nb::const_)) - // .def("Match", nb::overload_cast(&OSRM::Match, nb::const_)) + .def("Match", nb::overload_cast(&OSRM::Match, nb::const_)) + .def("Match", nb::overload_cast(&OSRM::Match, nb::const_)) .def("Nearest", nb::overload_cast(&OSRM::Nearest, nb::const_)) .def("Nearest", nb::overload_cast(&OSRM::Nearest, nb::const_)) .def("Route", nb::overload_cast(&OSRM::Route, nb::const_)) diff --git a/src/parameters/matchparameter_nb copy.cpp b/src/parameters/matchparameter_nb copy.cpp deleted file mode 100755 index 2566cf2..0000000 --- a/src/parameters/matchparameter_nb copy.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "parameters/matchparameter_nb.h" - -#include "engine/api/match_parameters.hpp" - -#include -#include -#include - -namespace nb = nanobind; -using namespace nb::literals; - -//=============================== -//this part requires some adjustment to the thing -//==================================== - -void init_MatchParameters(nb::module_& m) { - using osrm::engine::api::BaseParameters; - using osrm::engine::api::RouteParameters; - using osrm::engine::api::MatchParameters; - - nb::enum_(m, "GapsType") - .value("Split", MatchParameters::GapsType::Split) - .value("Ignore", MatchParameters::GapsType::Ignore) - .export_values(); - - nb::class_(m, "MatchParameters") - .def(nb::init<>()) - .def_rw("timestamps", &MatchParameters::timestamps) - .def_rw("gaps", &MatchParameters::gaps) - .def_rw("tidy", &MatchParameters::tidy) - .def("IsValid", &MatchParameters::IsValid); -} diff --git a/src/parameters/matchparameter_nb.cpp b/src/parameters/matchparameter_nb.cpp new file mode 100755 index 0000000..144c401 --- /dev/null +++ b/src/parameters/matchparameter_nb.cpp @@ -0,0 +1,83 @@ +#include "parameters/matchparameter_nb.h" + +#include "engine/api/match_parameters.hpp" + +#include +#include +#include + +namespace nb = nanobind; +using namespace nb::literals; + +void init_MatchParameters(nb::module_& m) { + using osrm::engine::api::BaseParameters; + using osrm::engine::api::RouteParameters; + using osrm::engine::api::MatchParameters; + + nb::enum_(m, "MatchGapsType") + .value("Split", MatchParameters::GapsType::Split) + .value("Ignore", MatchParameters::GapsType::Ignore) + .export_values(); + + nb::class_(m, "MatchParameters") + .def(nb::init<>()) + .def("__init__", [](MatchParameters* t, + std::vector timestamps, + MatchParameters::GapsType gaps, + bool tidy, + std::vector waypoints, + const bool steps, + const bool alternatives, + const RouteParameters::AnnotationsType annotations, + const RouteParameters::GeometriesType geometries, + const RouteParameters::OverviewType overview, + const boost::optional continue_straight, + std::vector coordinates, + std::vector> hints, + std::vector> radiuses, + std::vector> bearings, + std::vector> approaches, + bool generate_hints, + std::vector exclude, + const BaseParameters::SnappingType snapping + ) { + new (t) MatchParameters( + timestamps, gaps, tidy, + steps, alternatives, annotations, + geometries, overview, continue_straight + ); + + t->waypoints = std::move(waypoints); + t->coordinates = std::move(coordinates); + t->hints = std::move(hints); + t->radiuses = std::move(radiuses); + t->bearings = std::move(bearings); + t->approaches = std::move(approaches); + t->generate_hints = generate_hints; + t->exclude = std::move(exclude); + t->snapping = snapping; + }, + "timestamps"_a, + "gaps"_a = MatchParameters::GapsType::Split, + "tidy"_a = false, + "waypoints"_a = std::vector(), + "steps"_a, + "alternatives"_a, + "annotations"_a = RouteParameters::AnnotationsType::None, + "geometries"_a, + "overview"_a, + "continue_straight"_a, + "coordinates"_a = std::vector(), + "hints"_a = std::vector>(), + "radiuses"_a = std::vector>(), + "bearings"_a = std::vector>(), + "approaches"_a = std::vector>(), + "generate_hints"_a = true, + "exclude"_a = std::vector(), + "snapping"_a = BaseParameters::SnappingType::Default + ) + .def_rw("timestamps", &MatchParameters::timestamps) + .def_rw("gaps", &MatchParameters::gaps) + .def_rw("tidy", &MatchParameters::tidy) + .def("IsValid", &MatchParameters::IsValid); +} diff --git a/src/py_osrm/__init__.py b/src/py_osrm/__init__.py index 8f5334e..6c6e92b 100755 --- a/src/py_osrm/__init__.py +++ b/src/py_osrm/__init__.py @@ -26,8 +26,8 @@ TripSourceType, TripDestinationType, - # MatchParameters, - # MatchGapsType, + MatchParameters, + MatchGapsType, Object ) From af1adc7a74325475eaf2e68fb7764262ec184fc5 Mon Sep 17 00:00:00 2001 From: whytro Date: Mon, 3 Jul 2023 18:39:48 +0900 Subject: [PATCH 4/6] Add validity check for TileParameter argument --- src/parameters/tileparameter_nb.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/parameters/tileparameter_nb.cpp b/src/parameters/tileparameter_nb.cpp index d4a42b6..bf3c0d1 100755 --- a/src/parameters/tileparameter_nb.cpp +++ b/src/parameters/tileparameter_nb.cpp @@ -6,6 +6,8 @@ #include #include +#include + namespace nb = nanobind; using namespace nb::literals; @@ -14,15 +16,19 @@ void init_TileParameters(nb::module_& m) { nb::class_(m, "TileParameters", nb::is_final()) .def(nb::init<>()) - .def(nb::init(), + .def(nb::init(), "x"_a, "y"_a, "z"_a ) - .def("__init__", [](TileParameters* t, const std::vector& coord) { + .def("__init__", [](TileParameters* t, const std::vector& coord) { + if(coord.size() != 3) { + throw std::runtime_error("Parameter must be an array [x, y, z]"); + } + new (t) TileParameters{coord[0], coord[1], coord[2]}; }) .def_rw("x", &TileParameters::x) .def_rw("y", &TileParameters::y) .def_rw("z", &TileParameters::z) .def("IsValid", &TileParameters::IsValid); - nb::implicitly_convertible, TileParameters>(); + nb::implicitly_convertible, TileParameters>(); } From 0bc51a570fee018a4668715e275a89f8aa7b2edd Mon Sep 17 00:00:00 2001 From: whytro Date: Mon, 3 Jul 2023 18:52:28 +0900 Subject: [PATCH 5/6] Adjust visitor for JSON types to return string --- include/types/jsoncontainer_nb.h | 53 +++++++++++++++----------------- src/types/jsoncontainer_nb.cpp | 16 +++------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/include/types/jsoncontainer_nb.h b/include/types/jsoncontainer_nb.h index afb61f0..9e4fa40 100644 --- a/include/types/jsoncontainer_nb.h +++ b/include/types/jsoncontainer_nb.h @@ -61,54 +61,51 @@ template <> struct type_caster : type_caster_base { } //nanobind::detail struct ValueStringifyVisitor { - ValueStringifyVisitor(std::string& output) : _output(output) {}; - std::string& _output; - - void operator()(const json::String& str) { - _output += "'" + str.value + "'"; + std::string operator()(const json::String& str) { + return "'" + str.value + "'"; } - void operator()(const json::Number& num) { - _output += std::to_string(num.value); + std::string operator()(const json::Number& num) { + return std::to_string(num.value); } - void operator()(const json::True& str) { - _output += "True"; + std::string operator()(const json::True& str) { + return "True"; } - void operator()(const json::False&) { - _output += "False"; + std::string operator()(const json::False&) { + return "False"; } - void operator()(const json::Null&) { - _output += "None"; + std::string operator()(const json::Null&) { + return "None"; } - void visitarray(const json::Array& arr) { - _output += "["; + std::string visitarray(const json::Array& arr) { + std::string output = "["; for(int i = 0; i < arr.values.size(); ++i) { if(i != 0) { - _output += ", "; + output += ", "; } - mapbox::util::apply_visitor(*this, arr.values[i]); + output += mapbox::util::apply_visitor(*this, arr.values[i]); } - _output += "]"; + return output + "]"; } - void operator()(const mapbox::util::recursive_wrapper& arr) { - visitarray(arr.get()); + std::string operator()(const mapbox::util::recursive_wrapper& arr) { + return visitarray(arr.get()); } - void visitobject(const json::Object& obj) { - _output += "{"; + std::string visitobject(const json::Object& obj) { + std::string output = "{"; bool first = true; for(auto itr : obj.values) { if(!first) { - _output += ", "; + output += ", "; } - _output += "'" + itr.first + "': "; - mapbox::util::apply_visitor(*this, itr.second); + output += "'" + itr.first + "': "; + output += mapbox::util::apply_visitor(*this, itr.second); first = false; } - _output += "}"; + return output + "}"; } - void operator()(const mapbox::util::recursive_wrapper& obj) { - visitobject(obj); + std::string operator()(const mapbox::util::recursive_wrapper& obj) { + return visitobject(obj.get()); } }; diff --git a/src/types/jsoncontainer_nb.cpp b/src/types/jsoncontainer_nb.cpp index c6d910d..3930d06 100755 --- a/src/types/jsoncontainer_nb.cpp +++ b/src/types/jsoncontainer_nb.cpp @@ -20,12 +20,9 @@ void init_JSONContainer(nb::module_& m) { .def("__bool__", [](const json::Object& obj) { return obj.values.empty(); }) - .def("__repr__", [](json::Object& obj) { - std::string output = ""; - ValueStringifyVisitor visitor(output); - visitor.visitobject(obj); - - return output; + .def("__repr__", [](const json::Object& obj) { + ValueStringifyVisitor visitor; + return visitor.visitobject(obj); }) .def("__getitem__", [](json::Object& obj, const std::string& key) { return obj.values[key]; @@ -44,11 +41,8 @@ void init_JSONContainer(nb::module_& m) { return arr.values.empty(); }) .def("__repr__", [](const json::Array& arr) { - std::string output = ""; - ValueStringifyVisitor visitor(output); - visitor.visitarray(arr); - - return output; + ValueStringifyVisitor visitor; + return visitor.visitarray(arr); }) .def("__iter__", [](const json::Array& arr) { return nb::make_iterator(nb::type(), "iterator", From cc5ed2b04c90eff078cb59487f2101430fef8299 Mon Sep 17 00:00:00 2001 From: whytro Date: Tue, 4 Jul 2023 19:27:40 +0900 Subject: [PATCH 6/6] Adjust MatchParameter arguments and handling --- src/parameters/matchparameter_nb.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/parameters/matchparameter_nb.cpp b/src/parameters/matchparameter_nb.cpp index 144c401..28ed932 100755 --- a/src/parameters/matchparameter_nb.cpp +++ b/src/parameters/matchparameter_nb.cpp @@ -27,8 +27,8 @@ void init_MatchParameters(nb::module_& m) { bool tidy, std::vector waypoints, const bool steps, - const bool alternatives, - const RouteParameters::AnnotationsType annotations, + int number_of_alternatives, + const std::vector& annotations, const RouteParameters::GeometriesType geometries, const RouteParameters::OverviewType overview, const boost::optional continue_straight, @@ -41,13 +41,24 @@ void init_MatchParameters(nb::module_& m) { std::vector exclude, const BaseParameters::SnappingType snapping ) { - new (t) MatchParameters( - timestamps, gaps, tidy, - steps, alternatives, annotations, - geometries, overview, continue_straight - ); + new (t) MatchParameters(); + t->timestamps = std::move(timestamps); + t->gaps = gaps; + t->tidy = tidy; t->waypoints = std::move(waypoints); + + t->steps = steps; + t->alternatives = (bool)number_of_alternatives; + t->number_of_alternatives = number_of_alternatives; + t->annotations = !annotations.empty(); + for(int i = 0; i < annotations.size(); ++i) { + t->annotations_type = t->annotations_type | annotations[i]; + } + t->geometries = geometries; + t->overview = overview; + t->continue_straight = continue_straight; + t->coordinates = std::move(coordinates); t->hints = std::move(hints); t->radiuses = std::move(radiuses); @@ -63,7 +74,7 @@ void init_MatchParameters(nb::module_& m) { "waypoints"_a = std::vector(), "steps"_a, "alternatives"_a, - "annotations"_a = RouteParameters::AnnotationsType::None, + "annotations"_a = std::vector(), "geometries"_a, "overview"_a, "continue_straight"_a,