libosrm 1.0.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ #ifndef OSRM_MATCH_FUNC
2
+ #define OSRM_MATCH_FUNC
3
+
4
+ #include "globals.hpp"
5
+
6
+ #include <osrm/osrm.hpp>
7
+ #include <osrm/json_container.hpp>
8
+
9
+ using namespace Rice;
10
+
11
+ Rice::Object wrap_match(Rice::Object self, Rice::Array coordinates, Rice::Hash opts);
12
+
13
+ //Array parse_route_legs(osrm::util::json::Value value);
14
+ //Array parse_route_leg_steps(osrm::util::json::Value value);
15
+ //Hash parse_route_leg_annotations(osrm::util::json::Value value);
16
+
17
+ Hash parse_match_result(osrm::json::Object match);
18
+
19
+ #endif
@@ -0,0 +1,80 @@
1
+
2
+ #include "osrm_nearest_func.hpp"
3
+
4
+ #include <osrm/nearest_parameters.hpp>
5
+
6
+ Object wrap_nearest(Object self, double latitude, double longitude, int amount) {
7
+ NearestFunc func;
8
+ return func.wrap_nearest(self, latitude, longitude, amount);
9
+ }
10
+
11
+ Hash NearestFunc::parse_nearest_result(osrm::json::Object nearest) {
12
+ Hash result;
13
+ for(std::pair<std::string, osrm::util::json::Value> e : nearest.values) {
14
+ if(e.first == "code") {
15
+ result[String("code")] = e.second.get<osrm::json::String>().value;
16
+ } else if(e.first == "waypoints") {
17
+ result[String("waypoints")] = parse_waypoints(e.second.get<osrm::json::Array>());
18
+ } else {
19
+ throw Exception(rb_eRuntimeError, "Invalid JSON value when building a match from libosrm.so: %s", e.first.c_str());
20
+ }
21
+ }
22
+
23
+ return result;
24
+ }
25
+
26
+ Object NearestFunc::wrap_nearest(Object self, double latitude, double longitude, int amount) {
27
+ // Convert Ruby object to native type
28
+ osrm::NearestParameters params;
29
+
30
+ params.coordinates.push_back({osrm::util::FloatLongitude{longitude}, osrm::util::FloatLatitude{latitude}});
31
+
32
+ /*
33
+ if(!opts.is_nil()) {
34
+ Object geometry_type = opts[Symbol("geometry_type")];
35
+ if(!geometry_type.is_nil()) {
36
+ Symbol g_type = (Symbol) geometry_type;
37
+ const char *type = g_type.c_str();
38
+ if(strcmp(type, "polyline") == 0) {
39
+ params.geometries = osrm::RouteParameters::GeometriesType::Polyline;
40
+ }
41
+ if(strcmp(type, "polyline6") == 0) {
42
+ params.geometries = osrm::RouteParameters::GeometriesType::Polyline6;
43
+ }
44
+ if(strcmp(type, "geojson") == 0) {
45
+ params.geometries = osrm::RouteParameters::GeometriesType::GeoJSON;
46
+ }
47
+ }
48
+
49
+ Object steps = opts[Symbol("steps")];
50
+ if(steps) {
51
+ params.steps = true;
52
+ }
53
+
54
+ Object annotations = opts[Symbol("annotations")];
55
+ if(annotations) {
56
+ params.annotations = true;
57
+ }
58
+ }
59
+ */
60
+
61
+ params.number_of_results = amount;
62
+
63
+ // Response is in JSON format
64
+ osrm::json::Object result;
65
+
66
+ Data_Object<osrm::OSRM> osrm(self);
67
+
68
+ // Execute routing request, this does the heavy lifting
69
+ const auto status = osrm->Nearest(params, result);
70
+
71
+ if (status != osrm::Status::Ok) {
72
+ const auto code = result.values["code"].get<osrm::json::String>().value;
73
+ const auto message = result.values["message"].get<osrm::json::String>().value;
74
+
75
+ throw Exception(rb_eRuntimeError, "Failed to route with given input. error code: %s, message: %s", code.c_str(), message.c_str());
76
+ }
77
+
78
+ return parse_nearest_result(result);
79
+ }
80
+
@@ -0,0 +1,25 @@
1
+ #ifndef OSRM_NEAREST_FUNC
2
+ #define OSRM_NEAREST_FUNC
3
+
4
+ #include "globals.hpp"
5
+ #include "osrm_action.hpp"
6
+
7
+ #include <osrm/osrm.hpp>
8
+ #include <osrm/json_container.hpp>
9
+
10
+ using namespace Rice;
11
+
12
+ Rice::Object wrap_nearest(Rice::Object self, double latitude, double longitude, int amount);
13
+
14
+ class NearestFunc : private OSRMAction {
15
+
16
+ public:
17
+ // amount defaults to 1
18
+ Rice::Object wrap_nearest(Rice::Object self, double latitude, double longitude, int amount);
19
+
20
+ private:
21
+ Hash parse_nearest_result(osrm::json::Object nearest);
22
+
23
+ };
24
+
25
+ #endif
@@ -0,0 +1,86 @@
1
+
2
+ #include "osrm_route_func.hpp"
3
+
4
+ #include <osrm/route_parameters.hpp>
5
+
6
+ Object wrap_route(Object self, Array coordinates, Hash opts) {
7
+ RouteFunc func;
8
+ return func.wrap_route(self, coordinates, opts);
9
+ }
10
+
11
+ template<>
12
+ Object to_ruby<osrm::json::Value>(osrm::json::Value const & x) {
13
+ if(x.is<osrm::json::String>()) {
14
+ return String(x.get<osrm::json::String>().value);
15
+ } else if(x.is<osrm::json::Number>()) {
16
+ return to_ruby(x.get<osrm::json::Number>().value);
17
+ } else {
18
+ throw Exception(rb_eRuntimeError, "Failed to convert osrm::json::Value, from libosrm.so");
19
+ }
20
+ }
21
+
22
+ Object RouteFunc::wrap_route(Object self, Array coordinates, Hash opts) {
23
+ // Convert Ruby object to native type
24
+ osrm::RouteParameters params;
25
+
26
+ Array::iterator it = coordinates.begin();
27
+ Array::iterator end = coordinates.end();
28
+ for(; it != end; ++it) {
29
+ Hash latlon = (Hash)*it;
30
+ double lat = from_ruby<double>(latlon[Symbol("latitude")]);
31
+ double lon = from_ruby<double>(latlon[Symbol("longitude")]);
32
+ params.coordinates.push_back({osrm::util::FloatLongitude{lon}, osrm::util::FloatLatitude{lat}});
33
+ }
34
+
35
+ Object geometry_type = opts[Symbol("geometry_type")];
36
+ if(!geometry_type.is_nil()) {
37
+ Symbol g_type = (Symbol) geometry_type;
38
+ const char *type = g_type.c_str();
39
+ if(strcmp(type, "polyline") == 0) {
40
+ params.geometries = osrm::RouteParameters::GeometriesType::Polyline;
41
+ }
42
+ if(strcmp(type, "polyline6") == 0) {
43
+ params.geometries = osrm::RouteParameters::GeometriesType::Polyline6;
44
+ }
45
+ if(strcmp(type, "geojson") == 0) {
46
+ params.geometries = osrm::RouteParameters::GeometriesType::GeoJSON;
47
+ }
48
+ }
49
+
50
+ Object steps = opts[Symbol("steps")];
51
+ if(steps) {
52
+ params.steps = true;
53
+ }
54
+
55
+ Object annotations = opts[Symbol("annotations")];
56
+ if(annotations) {
57
+ params.annotations = true;
58
+ }
59
+
60
+ // Response is in JSON format
61
+ osrm::json::Object result;
62
+
63
+ Data_Object<osrm::OSRM> osrm(self);
64
+
65
+ // Execute routing request, this does the heavy lifting
66
+ const auto status = osrm->Route(params, result);
67
+
68
+ if (status != osrm::Status::Ok) {
69
+ const auto code = result.values["code"].get<osrm::json::String>().value;
70
+ const auto message = result.values["message"].get<osrm::json::String>().value;
71
+
72
+ throw Exception(rb_eRuntimeError, "Failed to route with given input. error code: %s, message: %s", code.c_str(), message.c_str());
73
+ }
74
+
75
+ Hash routes_result;
76
+ routes_result[String("code")] = result.values["code"].get<osrm::json::String>().value;
77
+
78
+ Array routes_array;
79
+ auto &routeValues = result.values["routes"].get<osrm::json::Array>();
80
+ for(auto const& routeValue : routeValues.values) {
81
+ routes_array.push(parse_route(routeValue.get<osrm::json::Object>()));
82
+ }
83
+ routes_result[String("routes")] = routes_array;
84
+
85
+ return routes_result;
86
+ }
@@ -0,0 +1,20 @@
1
+ #ifndef OSRM_ROUTE_FUNC
2
+ #define OSRM_ROUTE_FUNC
3
+
4
+ #include "globals.hpp"
5
+ #include "osrm_action.hpp"
6
+
7
+ #include <osrm/osrm.hpp>
8
+ #include <osrm/json_container.hpp>
9
+
10
+ using namespace Rice;
11
+
12
+ Rice::Object wrap_route(Rice::Object self, Rice::Array coordinates, Rice::Hash opts);
13
+
14
+ class RouteFunc : private OSRMAction {
15
+
16
+ public:
17
+ Rice::Object wrap_route(Rice::Object self, Rice::Array coordinates, Rice::Hash opts);
18
+ };
19
+
20
+ #endif
@@ -0,0 +1,101 @@
1
+
2
+ #include "osrm_table_func.hpp"
3
+
4
+ #include <osrm/table_parameters.hpp>
5
+
6
+ #include "osrm_nearest_func.hpp"
7
+
8
+ Object wrap_table(Object self, Array coordinates, Hash opts) {
9
+ TableFunc func;
10
+ return func.wrap_table(self, coordinates, opts);
11
+ }
12
+
13
+ Hash TableFunc::parse_table_result(osrm::json::Object match) {
14
+ Hash result;
15
+ for(std::pair<std::string, osrm::util::json::Value> e : match.values) {
16
+ if(e.first == "code") {
17
+ result[String("code")] = e.second.get<osrm::json::String>().value;
18
+ } else if(e.first == "durations") {
19
+ Array durations_result;
20
+ for(auto const& durationArrayValue : e.second.get<osrm::json::Array>().values) {
21
+ auto durationArray = durationArrayValue.get<osrm::json::Array>();
22
+ Array duration_result;
23
+ for(auto const& durationValue : durationArray.values) {
24
+ duration_result.push(durationValue.get<osrm::json::Number>().value);
25
+ }
26
+ durations_result.push(duration_result);
27
+ }
28
+ result[String("durations")] = durations_result;
29
+ } else if(e.first == "sources") {
30
+ result[String("sources")] = parse_waypoints(e.second.get<osrm::json::Array>());
31
+ } else if(e.first == "destinations") {
32
+ result[String("destinations")] = parse_waypoints(e.second.get<osrm::json::Array>());
33
+ } else {
34
+ throw Exception(rb_eRuntimeError, "Invalid JSON value when building a table from libosrm.so: %s", e.first.c_str());
35
+ }
36
+ }
37
+
38
+ return result;
39
+ }
40
+
41
+ std::vector<std::size_t> TableFunc::table_array_conversion(Object o) {
42
+ std::vector<std::size_t> out;
43
+ if(o.is_a(rb_cArray)) {
44
+ Array a = (Array) o;
45
+ Array::iterator it = a.begin();
46
+ Array::iterator end = a.end();
47
+ for(; it != end; ++it) {
48
+ int index = (int)(Object)*it;
49
+ out.push_back(index);
50
+ }
51
+ } else if(o.is_a(rb_cString)) {
52
+ out.push_back(from_ruby<int>(o));
53
+ } else if(o.is_a(rb_cNumeric)) {
54
+ out.push_back(from_ruby<int>(o));
55
+ }
56
+
57
+ return out;
58
+ }
59
+
60
+ Object TableFunc::wrap_table(Object self, Array coordinates, Hash opts) {
61
+ // Convert Ruby object to native type
62
+ osrm::TableParameters params;
63
+
64
+ Array::iterator it = coordinates.begin();
65
+ Array::iterator end = coordinates.end();
66
+ for(; it != end; ++it) {
67
+ Hash latlon = (Hash)*it;
68
+ double lat = from_ruby<double>(latlon[Symbol("latitude")]);
69
+ double lon = from_ruby<double>(latlon[Symbol("longitude")]);
70
+ params.coordinates.push_back({osrm::util::FloatLongitude{lon}, osrm::util::FloatLatitude{lat}});
71
+ }
72
+
73
+ if(!opts.is_nil()) {
74
+ Object sources = opts[Symbol("sources")];
75
+ if(sources) {
76
+ params.sources = table_array_conversion(sources);
77
+ }
78
+
79
+ Object destinations = opts[Symbol("destinations")];
80
+ if(sources) {
81
+ params.destinations = table_array_conversion(destinations);
82
+ }
83
+ }
84
+
85
+ // Response is in JSON format
86
+ osrm::json::Object result;
87
+
88
+ Data_Object<osrm::OSRM> osrm(self);
89
+
90
+ // Execute routing request, this does the heavy lifting
91
+ const auto status = osrm->Table(params, result);
92
+
93
+ if (status != osrm::Status::Ok) {
94
+ const auto code = result.values["code"].get<osrm::json::String>().value;
95
+ const auto message = result.values["message"].get<osrm::json::String>().value;
96
+
97
+ throw Exception(rb_eRuntimeError, "Failed to route with given input. error code: %s, message: %s", code.c_str(), message.c_str());
98
+ }
99
+
100
+ return parse_table_result(result);
101
+ }
@@ -0,0 +1,25 @@
1
+ #ifndef OSRM_TABLE_FUNC
2
+ #define OSRM_TABLE_FUNC
3
+
4
+ #include "globals.hpp"
5
+ #include "osrm_action.hpp"
6
+
7
+ #include <osrm/osrm.hpp>
8
+ #include <osrm/json_container.hpp>
9
+
10
+ using namespace Rice;
11
+
12
+ Rice::Object wrap_table(Object self, Array coordinates, Hash opts);
13
+
14
+ class TableFunc : private OSRMAction {
15
+
16
+ public:
17
+ Rice::Object wrap_table(Object self, Array coordinates, Hash opts);
18
+
19
+ private:
20
+ std::vector<std::size_t> table_array_conversion(Object o);
21
+ Hash parse_table_result(osrm::json::Object match);
22
+
23
+ };
24
+
25
+ #endif
@@ -0,0 +1,27 @@
1
+
2
+ #include "osrm_tile_func.hpp"
3
+
4
+ #include <osrm/tile_parameters.hpp>
5
+
6
+ Object wrap_tile(Object self, int x, int y, int zoom) {
7
+ // Convert Ruby object to native type
8
+ osrm::TileParameters params;
9
+
10
+ params.x = x;
11
+ params.y = y;
12
+ params.z = zoom;
13
+
14
+ // Response is a std::string, instead of JSON stuff that is elsewhere
15
+ std::string result;
16
+
17
+ Data_Object<osrm::OSRM> osrm(self);
18
+
19
+ // Execute routing request, this does the heavy lifting
20
+ const auto status = osrm->Tile(params, result);
21
+
22
+ if (status != osrm::Status::Ok) {
23
+ throw Exception(rb_eRuntimeError, "Failed to get tile data with given input.");
24
+ }
25
+
26
+ return to_ruby(result);
27
+ }
@@ -0,0 +1,13 @@
1
+ #ifndef OSRM_TILE_FUNC
2
+ #define OSRM_TILE_FUNC
3
+
4
+ #include "globals.hpp"
5
+
6
+ #include <osrm/osrm.hpp>
7
+ #include <osrm/json_container.hpp>
8
+
9
+ using namespace Rice;
10
+
11
+ Rice::Object wrap_tile(Rice::Object self, int x, int y, int zoom);
12
+
13
+ #endif
@@ -0,0 +1,109 @@
1
+
2
+ #include "osrm_trip_func.hpp"
3
+
4
+ #include <osrm/trip_parameters.hpp>
5
+
6
+ Object wrap_trip(Object self, Array coordinates, Hash opts) {
7
+ TripFunc func;
8
+ return func.wrap_trip(self, coordinates, opts);
9
+ }
10
+
11
+ Object TripFunc::wrap_trip(Object self, Array coordinates, Hash opts) {
12
+ // Convert Ruby object to native type
13
+ osrm::TripParameters params;
14
+
15
+ Array::iterator it = coordinates.begin();
16
+ Array::iterator end = coordinates.end();
17
+ for(; it != end; ++it) {
18
+ Hash latlon = (Hash)*it;
19
+ double lat = from_ruby<double>(latlon[Symbol("latitude")]);
20
+ double lon = from_ruby<double>(latlon[Symbol("longitude")]);
21
+ params.coordinates.push_back({osrm::util::FloatLongitude{lon}, osrm::util::FloatLatitude{lat}});
22
+ }
23
+
24
+ Object roundtrip = opts[Symbol("roundtrip")];
25
+ if(roundtrip) {
26
+ params.roundtrip = true;
27
+ }
28
+
29
+ Object source = opts[Symbol("source")];
30
+ if(!source.is_nil()) {
31
+ Symbol source_symbol = (Symbol) source;
32
+ const char *source_string = source_symbol.c_str();
33
+ if(strcmp(source_string, "any") == 0) {
34
+ params.source = osrm::TripParameters::SourceType::Any;
35
+ } else if(strcmp(source_string, "first") == 0) {
36
+ params.source = osrm::TripParameters::SourceType::First;
37
+ } else {
38
+ throw Exception(rb_eRuntimeError, "libosrm.so#wrap_trip(): failed to recognize given source symbol: %s", source_string);
39
+ }
40
+ }
41
+
42
+ Object destination = opts[Symbol("destination")];
43
+ if(!destination.is_nil()) {
44
+ Symbol destination_symbol = (Symbol) destination;
45
+ const char *destination_string = destination_symbol.c_str();
46
+ if(strcmp(destination_string, "any") == 0) {
47
+ params.destination = osrm::TripParameters::DestinationType::Any;
48
+ } else if(strcmp(destination_string, "last") == 0) {
49
+ params.destination = osrm::TripParameters::DestinationType::Last;
50
+ } else {
51
+ throw Exception(rb_eRuntimeError, "libosrm.so#wrap_trip(): failed to recognize given destination symbol: %s", destination_string);
52
+ }
53
+ }
54
+
55
+ // TODO: since this option is same as in route thing, maybe we could have some kind of abstraction?
56
+ Object geometry_type = opts[Symbol("geometry_type")];
57
+ if(!geometry_type.is_nil()) {
58
+ Symbol g_type = (Symbol) geometry_type;
59
+ const char *type = g_type.c_str();
60
+ if(strcmp(type, "polyline") == 0) {
61
+ params.geometries = osrm::RouteParameters::GeometriesType::Polyline;
62
+ }
63
+ if(strcmp(type, "polyline6") == 0) {
64
+ params.geometries = osrm::RouteParameters::GeometriesType::Polyline6;
65
+ }
66
+ if(strcmp(type, "geojson") == 0) {
67
+ params.geometries = osrm::RouteParameters::GeometriesType::GeoJSON;
68
+ }
69
+ }
70
+
71
+ Object steps = opts[Symbol("steps")];
72
+ if(steps) {
73
+ params.steps = true;
74
+ }
75
+
76
+ Object annotations = opts[Symbol("annotations")];
77
+ if(annotations) {
78
+ params.annotations = true;
79
+ }
80
+
81
+ // Response is in JSON format
82
+ osrm::json::Object result;
83
+
84
+ Data_Object<osrm::OSRM> osrm(self);
85
+
86
+ // Execute routing request, this does the heavy lifting
87
+ const auto status = osrm->Trip(params, result);
88
+
89
+ if (status != osrm::Status::Ok) {
90
+ const auto code = result.values["code"].get<osrm::json::String>().value;
91
+ const auto message = result.values["message"].get<osrm::json::String>().value;
92
+
93
+ throw Exception(rb_eRuntimeError, "Failed to calculate a trip with given input. error code: %s, message: %s", code.c_str(), message.c_str());
94
+ }
95
+
96
+ Hash trip_result;
97
+ trip_result[String("code")] = result.values["code"].get<osrm::json::String>().value;
98
+
99
+ trip_result[String("waypoints")] = parse_waypoints(result.values["waypoints"].get<osrm::json::Array>());
100
+
101
+ Array trips_array;
102
+ auto &tripValues = result.values["trips"].get<osrm::json::Array>();
103
+ for(auto const& tripValue : tripValues.values) {
104
+ trips_array.push(parse_route(tripValue.get<osrm::json::Object>()));
105
+ }
106
+ trip_result[String("trips")] = trips_array;
107
+
108
+ return trip_result;
109
+ }