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/include/types/jsoncontainer_nb.h b/include/types/jsoncontainer_nb.h index 8aff2ff..9e4fa40 100644 --- a/include/types/jsoncontainer_nb.h +++ b/include/types/jsoncontainer_nb.h @@ -1,8 +1,112 @@ #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 { + std::string operator()(const json::String& str) { + return "'" + str.value + "'"; + } + std::string operator()(const json::Number& num) { + return std::to_string(num.value); + } + std::string operator()(const json::True& str) { + return "True"; + } + std::string operator()(const json::False&) { + return "False"; + } + std::string operator()(const json::Null&) { + return "None"; + } + + 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]); + } + return output + "]"; + } + std::string operator()(const mapbox::util::recursive_wrapper& arr) { + return visitarray(arr.get()); + } + + std::string visitobject(const json::Object& obj) { + std::string output = "{"; + bool first = true; + for(auto itr : obj.values) { + if(!first) { + output += ", "; + } + output += "'" + itr.first + "': "; + output += mapbox::util::apply_visitor(*this, itr.second); + first = false; + } + return output + "}"; + } + std::string operator()(const mapbox::util::recursive_wrapper& obj) { + return visitobject(obj.get()); + } +}; + #endif //OSRM_NB_JSONCONTAINER_H diff --git a/src/osrm_nb.cpp b/src/osrm_nb.cpp index cd70b8d..6b5ebfb 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_NearestParameters(m); + init_TableParameters(m); init_RouteParameters(m); + init_MatchParameters(m); + init_TripParameters(m); + init_TileParameters(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.cpp b/src/parameters/matchparameter_nb.cpp new file mode 100755 index 0000000..28ed932 --- /dev/null +++ b/src/parameters/matchparameter_nb.cpp @@ -0,0 +1,94 @@ +#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, + int number_of_alternatives, + const std::vector& 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(); + + 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); + 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 = std::vector(), + "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/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..bf3c0d1 --- /dev/null +++ b/src/parameters/tileparameter_nb.cpp @@ -0,0 +1,34 @@ +#include "parameters/tileparameter_nb.h" + +#include "engine/api/tile_parameters.hpp" + +#include +#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) { + 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>(); +} 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..6c6e92b 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/jsoncontainer_nb.cpp b/src/types/jsoncontainer_nb.cpp index 34b1715..3930d06 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,10 @@ void init_JSONContainer(nb::module_& m) { .def("__bool__", [](const json::Object& obj) { return obj.values.empty(); }) + .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]; }) @@ -85,6 +40,10 @@ void init_JSONContainer(nb::module_& m) { .def("__bool__", [](const json::Array& arr) { return arr.values.empty(); }) + .def("__repr__", [](const json::Array& arr) { + ValueStringifyVisitor visitor; + return visitor.visitarray(arr); + }) .def("__iter__", [](const json::Array& arr) { return nb::make_iterator(nb::type(), "iterator", arr.values.begin(), arr.values.end()); 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)