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,20 @@
1
+ #ifndef OSRM_TRIP_FUNC
2
+ #define OSRM_TRIP_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_trip(Rice::Object self, Rice::Array coordinates, Rice::Hash opts);
13
+
14
+ class TripFunc : private OSRMAction {
15
+
16
+ public:
17
+ Rice::Object wrap_trip(Rice::Object self, Rice::Array coordinates, Rice::Hash opts);
18
+ };
19
+
20
+ #endif
@@ -0,0 +1,89 @@
1
+
2
+ #include "ruby_engine_config.hpp"
3
+
4
+ Data_Type<osrm::EngineConfig> rb_cEngineConfig;
5
+ Enum<osrm::EngineConfig::Algorithm> rb_eEngineConfigAlgorithm;
6
+
7
+ template<>
8
+ osrm::EngineConfig::Algorithm from_ruby<osrm::EngineConfig::Algorithm>(Object x) {
9
+ Data_Object<osrm::EngineConfig::Algorithm> d(x, rb_eEngineConfigAlgorithm);
10
+ return *d;
11
+ }
12
+
13
+ #define EC_ACC_INT(variable) \
14
+ Object EngineConfig_##variable##_get(Object self) { \
15
+ Data_Object<osrm::EngineConfig> c(self, rb_cEngineConfig); \
16
+ return c->variable; \
17
+ }\
18
+ \
19
+ Object EngineConfig_##variable##_set(Object self, Object value) { \
20
+ Data_Object<osrm::EngineConfig> c(self, rb_cEngineConfig); \
21
+ c->variable = value.value(); \
22
+ return value; \
23
+ }
24
+
25
+ Object EngineConfig_storage_config_get(Object self) {
26
+ Data_Object<osrm::EngineConfig> c(self, rb_cEngineConfig);
27
+ //return c->storage_config;
28
+ return self;
29
+ }
30
+
31
+ Object EngineConfig_storage_config_set(Object self, Object value) {
32
+ Data_Object<osrm::EngineConfig> c(self, rb_cEngineConfig);
33
+ c->storage_config = from_ruby<osrm::storage::StorageConfig>(value.value());
34
+ return value;
35
+ }
36
+
37
+ Object EngineConfig_use_shared_memory_get(Object self) {
38
+ Data_Object<osrm::EngineConfig> c(self, rb_cEngineConfig);
39
+ return c->use_shared_memory;
40
+ }
41
+
42
+ Object EngineConfig_use_shared_memory_set(Object self, Object value) {
43
+ Data_Object<osrm::EngineConfig> c(self, rb_cEngineConfig);
44
+ c->use_shared_memory = value.value();
45
+ return value;
46
+ }
47
+
48
+ EC_ACC_INT(max_locations_trip)
49
+ EC_ACC_INT(max_locations_viaroute)
50
+ EC_ACC_INT(max_locations_distance_table)
51
+ EC_ACC_INT(max_locations_map_matching)
52
+ EC_ACC_INT(max_results_nearest)
53
+
54
+ char const * description(osrm::EngineConfig::Algorithm e) {
55
+ switch(e) {
56
+ case osrm::EngineConfig::Algorithm::CH:
57
+ return "Contraction hierarchies";
58
+ case osrm::EngineConfig::Algorithm::CoreCH:
59
+ return "Partial contraction hierarchies";
60
+ case osrm::EngineConfig::Algorithm::MLD:
61
+ return "Multi level Dijkstra";
62
+ }
63
+ return "[invalid]";
64
+ }
65
+
66
+ void init_engine_config() {
67
+ rb_cEngineConfig =
68
+ define_class_under<osrm::EngineConfig>(rb_mLibOSRM, "EngineConfig")
69
+ .define_constructor(Constructor<osrm::EngineConfig>())
70
+ .define_method("valid?", &osrm::EngineConfig::IsValid)
71
+ ATTR_ACCESSOR_DECL(EngineConfig, storage_config)
72
+ ATTR_ACCESSOR_DECL(EngineConfig, max_locations_trip)
73
+ ATTR_ACCESSOR_DECL(EngineConfig, max_locations_viaroute)
74
+ ATTR_ACCESSOR_DECL(EngineConfig, max_locations_distance_table)
75
+ ATTR_ACCESSOR_DECL(EngineConfig, max_locations_map_matching)
76
+ ATTR_ACCESSOR_DECL(EngineConfig, max_results_nearest)
77
+ ATTR_ACCESSOR_DECL(EngineConfig, use_shared_memory)
78
+ ;
79
+
80
+ rb_eEngineConfigAlgorithm =
81
+ define_enum<osrm::EngineConfig::Algorithm>("Algorithm", rb_cEngineConfig)
82
+ .define_value("CH", osrm::EngineConfig::Algorithm::CH)
83
+ .define_value("CoreCH", osrm::EngineConfig::Algorithm::CoreCH)
84
+ .define_value("MLD", osrm::EngineConfig::Algorithm::MLD);
85
+
86
+ rb_eEngineConfigAlgorithm.define_method("description", description);
87
+
88
+ init_storage_config();
89
+ }
@@ -0,0 +1,26 @@
1
+ #ifndef RUBY_ENGINE_CONFIG_HPP_
2
+ #define RUBY_ENGINE_CONFIG_HPP_
3
+
4
+ #include "globals.hpp"
5
+
6
+ #include "ruby_storage_config.hpp"
7
+
8
+ using namespace Rice;
9
+
10
+ #include "osrm/engine_config.hpp"
11
+
12
+ // EngineConfig.storage_config
13
+ Object EngineConfig_storage_config_get(Object self);
14
+
15
+ // EngineConfig.storage_config
16
+ Object EngineConfig_storage_config_set(Object self, Object value);
17
+
18
+ Object EngineConfig_use_shared_memory_get(Object self);
19
+
20
+ Object EngineConfig_use_shared_memory_set(Object self, Object value);
21
+
22
+ char const * description(osrm::EngineConfig::Algorithm e);
23
+
24
+ void init_engine_config();
25
+
26
+ #endif
@@ -0,0 +1,78 @@
1
+
2
+ #include "ruby_osrm_object.hpp"
3
+ #include "osrm_match_func.hpp"
4
+ #include "osrm_nearest_func.hpp"
5
+ #include "osrm_route_func.hpp"
6
+ #include "osrm_tile_func.hpp"
7
+ #include "osrm_table_func.hpp"
8
+ #include "osrm_trip_func.hpp"
9
+
10
+ using namespace Rice;
11
+
12
+ Data_Type<osrm::OSRM> rb_cOsrm;
13
+
14
+ template<>
15
+ osrm::engine::EngineConfig from_ruby<osrm::engine::EngineConfig>(Object x) {
16
+ if(x.is_nil()) {
17
+ // TODO: raise error
18
+ }
19
+
20
+ osrm::engine::EngineConfig config;
21
+ config.storage_config = { x.to_s().c_str() };
22
+ config.use_shared_memory = false;
23
+ return config;
24
+ }
25
+
26
+ Object wrap_distance_by_roads(Object self, Object o) {
27
+
28
+ // Convert Ruby object to native type
29
+ osrm::RouteParameters params;
30
+
31
+ Array coordinates = o;
32
+ Array::iterator it = coordinates.begin();
33
+ Array::iterator end = coordinates.end();
34
+ for(; it != end; ++it) {
35
+ Hash latlon = (Hash)*it;
36
+ double lat = from_ruby<double>(latlon[Symbol("latitude")]);
37
+ double lon = from_ruby<double>(latlon[Symbol("longitude")]);
38
+ params.coordinates.push_back({osrm::util::FloatLongitude{lon}, osrm::util::FloatLatitude{lat}});
39
+ }
40
+
41
+ params.overview = osrm::RouteParameters::OverviewType::False;
42
+
43
+ // Response is in JSON format
44
+ osrm::json::Object result;
45
+
46
+ Data_Object<osrm::OSRM> osrm(self);
47
+
48
+ // Execute routing request, this does the heavy lifting
49
+ const auto status = osrm->Route(params, result);
50
+
51
+ if (status != osrm::Status::Ok) {
52
+ const auto code = result.values["code"].get<osrm::json::String>().value;
53
+ const auto message = result.values["message"].get<osrm::json::String>().value;
54
+
55
+ throw Exception(rb_eRuntimeError, "Failed to route with given input. error code: %s, message: %s", code.c_str(), message.c_str());
56
+ }
57
+
58
+ // We can take first route since we only want the distance.
59
+ auto &routes = result.values["routes"].get<osrm::json::Array>();
60
+ auto &route = routes.values.at(0).get<osrm::json::Object>();
61
+
62
+ const auto distance = route.values["distance"].get<osrm::json::Number>().value;
63
+
64
+ return to_ruby(distance);
65
+ }
66
+
67
+ void init_osrm_object() {
68
+ rb_cOsrm = define_class_under<osrm::OSRM>(rb_mLibOSRM, "OSRM")
69
+ .define_constructor(Constructor<osrm::OSRM, osrm::EngineConfig>(), Arg("config"))
70
+ .define_method("distance_by_roads", &wrap_distance_by_roads)
71
+ .define_method("route", &wrap_route, (Arg("coordinates"), Arg("opts") = Nil))
72
+ .define_method("table", &wrap_table, (Arg("coordinates"), Arg("opts") = Nil))
73
+ .define_method("nearest", &wrap_nearest, (Arg("latitude"), Arg("longitude"), Arg("amount") = 1))
74
+ .define_method("trip", &wrap_trip, (Arg("coordinates"), Arg("opts") = Nil))
75
+ .define_method("match", &wrap_match, (Arg("coordinates"), Arg("opts") = Nil))
76
+ .define_method("tile", &wrap_tile, (Arg("x"), Arg("y"), Arg("zoom")))
77
+ ;
78
+ }
@@ -0,0 +1,20 @@
1
+ #ifndef RUBY_OSRM_OBJECT_H_
2
+ #define RUBY_OSRM_OBJECT_H_
3
+
4
+ #include "globals.hpp"
5
+
6
+ using namespace Rice;
7
+
8
+ #include <osrm/osrm.hpp>
9
+
10
+ #include <osrm/engine_config.hpp>
11
+
12
+ #include <osrm/table_parameters.hpp>
13
+ #include <osrm/trip_parameters.hpp>
14
+ #include <osrm/json_container.hpp>
15
+
16
+ Object wrap_distance_by_roads(Object self, Object o);
17
+
18
+ void init_osrm_object();
19
+
20
+ #endif
@@ -0,0 +1,86 @@
1
+
2
+ #include "ruby_storage_config.hpp"
3
+
4
+
5
+ Data_Type<osrm::storage::StorageConfig> rb_cStorageConfig;
6
+
7
+ template<>
8
+ Object to_ruby<boost::filesystem::path>(boost::filesystem::path const & x) {
9
+ String s(x.c_str());
10
+ return s;
11
+ }
12
+
13
+ template<>
14
+ boost::filesystem::path from_ruby<boost::filesystem::path>(Object x) {
15
+ return x.to_s().c_str();
16
+ }
17
+
18
+ #define SC_ACC(variable) \
19
+ Object StorageConfig_##variable##_get(Object self) { \
20
+ Data_Object<osrm::storage::StorageConfig> c(self, rb_cStorageConfig); \
21
+ Object o = to_ruby(c->variable); \
22
+ return o; \
23
+ }\
24
+ \
25
+ Object StorageConfig_##variable##_set(Object self, Object value) { \
26
+ Data_Object<osrm::storage::StorageConfig> c(self, rb_cStorageConfig); \
27
+ c->variable = value.to_s().c_str(); \
28
+ return value; \
29
+ }
30
+
31
+ /*
32
+ SC_ACC(ram_index_path)
33
+ SC_ACC(file_index_path)
34
+ SC_ACC(hsgr_data_path)
35
+ SC_ACC(node_based_nodes_data_path)
36
+ SC_ACC(edge_based_nodes_data_path)
37
+ SC_ACC(edges_data_path)
38
+ SC_ACC(core_data_path)
39
+ SC_ACC(geometries_path)
40
+ SC_ACC(timestamp_path)
41
+ SC_ACC(turn_weight_penalties_path)
42
+ SC_ACC(turn_duration_penalties_path)
43
+ SC_ACC(datasource_names_path)
44
+ SC_ACC(datasource_indexes_path)
45
+ SC_ACC(names_data_path)
46
+ SC_ACC(properties_path)
47
+ SC_ACC(intersection_class_path)
48
+ SC_ACC(turn_lane_data_path)
49
+ SC_ACC(turn_lane_description_path)
50
+ SC_ACC(mld_partition_path)
51
+ SC_ACC(mld_storage_path)
52
+ SC_ACC(mld_graph_path)
53
+ */
54
+
55
+ void init_storage_config() {
56
+ rb_cStorageConfig =
57
+ define_class_under<osrm::storage::StorageConfig>(rb_mLibOSRM, "StorageConfig")
58
+ .define_constructor(Constructor<osrm::storage::StorageConfig>())
59
+ .define_constructor(Constructor<osrm::storage::StorageConfig, const boost::filesystem::path>(), Arg("base"))
60
+ .define_method("valid?", &osrm::storage::StorageConfig::IsValid)
61
+ /*ATTR_ACCESSOR_DECL(StorageConfig, ram_index_path)
62
+ ATTR_ACCESSOR_DECL(StorageConfig, file_index_path)
63
+ ATTR_ACCESSOR_DECL(StorageConfig, hsgr_data_path)
64
+ ATTR_ACCESSOR_DECL(StorageConfig, node_based_nodes_data_path)
65
+ ATTR_ACCESSOR_DECL(StorageConfig, edge_based_nodes_data_path)
66
+ ATTR_ACCESSOR_DECL(StorageConfig, edges_data_path)
67
+ ATTR_ACCESSOR_DECL(StorageConfig, core_data_path)
68
+ ATTR_ACCESSOR_DECL(StorageConfig, geometries_path)
69
+ ATTR_ACCESSOR_DECL(StorageConfig, timestamp_path)
70
+ ATTR_ACCESSOR_DECL(StorageConfig, turn_weight_penalties_path)
71
+ ATTR_ACCESSOR_DECL(StorageConfig, turn_duration_penalties_path)
72
+ ATTR_ACCESSOR_DECL(StorageConfig, datasource_names_path)
73
+ ATTR_ACCESSOR_DECL(StorageConfig, datasource_indexes_path)
74
+ ATTR_ACCESSOR_DECL(StorageConfig, names_data_path)
75
+ ATTR_ACCESSOR_DECL(StorageConfig, properties_path)
76
+ ATTR_ACCESSOR_DECL(StorageConfig, intersection_class_path)
77
+ ATTR_ACCESSOR_DECL(StorageConfig, turn_lane_data_path)
78
+ ATTR_ACCESSOR_DECL(StorageConfig, turn_lane_description_path)
79
+ ATTR_ACCESSOR_DECL(StorageConfig, mld_partition_path)
80
+ ATTR_ACCESSOR_DECL(StorageConfig, mld_storage_path)
81
+ ATTR_ACCESSOR_DECL(StorageConfig, mld_graph_path)
82
+ */
83
+ ;
84
+
85
+
86
+ }
@@ -0,0 +1,25 @@
1
+ #ifndef RUBY_STORAGE_CONFIG_H_
2
+ #define RUBY_STORAGE_CONFIG_H_
3
+
4
+ #include "globals.hpp"
5
+ #include "rice/String.hpp"
6
+
7
+ #include "osrm/storage_config.hpp"
8
+
9
+ using namespace Rice;
10
+
11
+ /*
12
+ template<>
13
+ osrm::storage::StorageConfig from_ruby<osrm::storage::StorageConfig>(Object x) {
14
+ // ...
15
+ }
16
+
17
+ template<>
18
+ Object to_ruby<osrm::storage::StorageConfig>(osrm::storage::StorageConfig const & x) {
19
+
20
+ }
21
+ */
22
+
23
+ void init_storage_config();
24
+
25
+ #endif
data/lib/libosrm.rb ADDED
@@ -0,0 +1,12 @@
1
+
2
+ require_relative "libosrm/version"
3
+
4
+ #require "libosrm/ruby_libosrm"
5
+ require_relative "libosrm/ruby_libosrm"
6
+ #require "ruby-libosrm"
7
+
8
+ # LibOSRM module contains {OSRM} class, which contains public methods for using OSRM.
9
+ #
10
+ # For general instructions, please see {file:README.md}. For API documentation, please see {OSRM}.
11
+ module LibOSRM
12
+ end
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../libosrm"
4
+
5
+ # OSRM routing interface.
6
+ #
7
+ # Provides methods to calculate various data using Openstreetmap data with OSRM.
8
+ #
9
+ # For example, to get distance by roads, you could use
10
+ #
11
+ # osrm = OSRM.new "map.osrm"
12
+ # distance = osrm.distance_by_roads { latitude: 60.1681473, longitude: 24.9417190 }, { latitude: 60.1694561, longitude: 24.9385663 }
13
+ #
14
+ # Please see {file:README.md} for general instructions of how to use this gem.
15
+ class LibOSRM::OSRM
16
+
17
+ # @!macro [new] native
18
+ # @note This method is implemented in native code.
19
+
20
+ # @!macro [new] api_method
21
+ # @note This method is a wrapper for OSRM’s API.
22
+ # Please see the {http://project-osrm.org/docs/v5.10.0/api HTTP API} for documentation of response values.
23
+ #
24
+ # Any differences should be documented in corresponding methods;
25
+ # please report should you find any.
26
+
27
+ # @!macro [new] api_method_return
28
+ # @return [Hash] Hash of response data as documented at {http://project-osrm.org/docs/v5.10.0/api HTTP API}
29
+
30
+ # @!macro [new] coordinates_param
31
+ # @param coordinates [Array] Array of coordinates. See {#distance_by_roads} for an example.
32
+
33
+ # @!method initialize(osrm_file)
34
+ # @!macro native
35
+ # Initializes OSRM object for routing actions.
36
+ #
37
+ # @param osrm_file [String] Path to the OSRM file.
38
+
39
+ # @!method distance_by_roads(coordinates)
40
+ # @!macro native
41
+ #
42
+ # Calculates distance by roads from given coordinates.
43
+ #
44
+ # Internally operates by invoking {#route}, but
45
+ # this method is implemented in native code so this works faster
46
+ # than {#route} if you only need distance and no other data.
47
+ #
48
+ # @example Input coordinates array
49
+ # [
50
+ # { latitude: from_lat, longitude: from_lon },
51
+ # { latitude: to_lat, longitude: to_lon }
52
+ # ]
53
+ #
54
+ # @param coordinates [Array] Array of coordinate hashes
55
+ # @return Float distance by roads in meters
56
+
57
+ # @!method route(coordinates, opts = nil)
58
+ # @!macro native
59
+ # @!macro api_method
60
+ #
61
+ # OSRM routing action. Takes two or more coordinate pairs and calculates distance
62
+ # by roads between them. A successful response contains an array of resulting routing data, between each
63
+ # point in the request.
64
+ #
65
+ # @note If you only want to know distance by roads for the trip, use {#distance_by_roads} instead.
66
+ #
67
+ # @!macro coordinates_param
68
+ # @param opts [Hash] Options to customize the request.
69
+ # @option opts [Symbol] :geometry_type Format in which the geometries will be returned at. Possible values: :polyline, :polyline6, :geojson. Defaults to :polyline.
70
+ # @option opts [Boolean] :steps Whether steps should be included in response
71
+ # @option opts [Boolean] :annotations Whether annotations should be included in response
72
+ #
73
+ # @!macro api_method_return
74
+
75
+ # @!method table(coordinates, opts = nil)
76
+ # @!macro native
77
+ # @!macro api_method
78
+ #
79
+ # OSRM table action. Takes a list of coordinate pairs and calculates distances between each pair to all other pairs.
80
+ # The calculations can be customized by sources and destinations parameters. By default full table is calculated.
81
+ #
82
+ # @!macro coordinates_param
83
+ # @param opts [Hash] Options to customize the request.
84
+ # @option opts [Array] :sources List of indices of coordinates array that should be used as a source location
85
+ # @option opts [Array] :destinations List of indices of coordinates array that should be used as a destination location
86
+ #
87
+ # @!macro api_method_return
88
+
89
+ # @!method nearest(latitude, longitude, amount = 1)
90
+ # @!macro native
91
+ # @!macro api_method
92
+ #
93
+ # OSRM nearest action. Returns nearest street network points.
94
+ #
95
+ # @param latitude [Float] Source position’s latitude
96
+ # @param longitude [Float] Source position’s longitude
97
+ # @param amount [Integer] Amount of nearest entries to be returned
98
+ #
99
+ # @!macro api_method_return
100
+
101
+ # @!method trip(coordinates)
102
+ # @!macro native
103
+ # @!macro api_method
104
+ #
105
+ # Tries to calculate shortest possible route between given coordinates (and then back to original point;
106
+ # i.e. the first coordinate pair in the input), in order to solve the travelling salesman problem.
107
+ #
108
+ # @!macro coordinates_param
109
+ #
110
+ # @!macro api_method_return
111
+
112
+ # @!method match(coordinates, opts = nil)
113
+ # @!macro native
114
+ # @!macro api_method
115
+ #
116
+ # Tries to match given coordinates to road network in most plausible way.
117
+ #
118
+ # @todo This action most likely does not work as expected.
119
+ #
120
+ # @!macro coordinates_param
121
+ #
122
+ # @!macro api_method_return
123
+
124
+ # @!method tile(x, y, zoom)
125
+ # @!macro native
126
+ # @!macro api_method
127
+ #
128
+ # Calculates a vector tile image that can be used to examine the routing graph. See the official documentation
129
+ # for more comprehensive documentation about this feature.
130
+ #
131
+ # @note You should most likely use {#tile_with_coordinates} instead.
132
+ #
133
+ # @param x [Integer] Tile index as documented at https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
134
+ # @param y [Integer] Tile index as documented at https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
135
+ # @param zoom [Integer] Map zoom level to generate the tile data
136
+ #
137
+ # @!macro api_method_return
138
+
139
+ # Invokes {#tile}, but first converts given coordinates to tile numbers.
140
+ #
141
+ # @param latitude [Float] Latitude of the location to get the tile data for
142
+ # @param longitude [Float] Longitude of the location to get the tile data for
143
+ # @param zoom [Integer] Map zoom level for the tile image. Controls size of the image.
144
+ def tile_with_coordinates latitude, longitude, zoom
145
+ xy = coordinates_to_tile_numbers latitude, longitude, zoom
146
+ tile xy[:x], xy[:y], zoom
147
+ end
148
+
149
+ # Converts coordinates to tile numbers as expected by {#tile}.
150
+ #
151
+ # @param latitude [Float] Latitude of the location to get the tile data for
152
+ # @param longitude [Float] Longitude of the location to get the tile data for
153
+ # @param zoom [Integer] Map zoom level for the tile image. Controls size of the image.
154
+ def coordinates_to_tile_numbers latitude, longitude, zoom
155
+ lat_rad = latitude / 180 * Math::PI
156
+ n = 2.0 ** zoom
157
+ x = ((longitude + 180.0) / 360.0 * n).to_i
158
+ y = ((1.0 - Math::log(Math::tan(lat_rad) + (1 / Math::cos(lat_rad))) / Math::PI) / 2.0 * n).to_i
159
+
160
+ {:x => x, :y =>y}
161
+ end
162
+ end