Class: LibOSRM::OSRM

Inherits:
Object
  • Object
show all
Defined in:
lib/libosrm/osrm.rb

Overview

OSRM routing interface.

Provides methods to calculate various data using Openstreetmap data with OSRM.

For example, to get distance by roads, you could use

osrm = OSRM.new "map.osrm"
distance = osrm.distance_by_roads { latitude: 60.1681473, longitude: 24.9417190 }, { latitude: 60.1694561, longitude: 24.9385663 }

Please see README for general instructions of how to use this gem.

Instance Method Summary collapse

Constructor Details

#initialize(osrm_file) ⇒ Object

Note:

This method is implemented in native code.

Initializes OSRM object for routing actions.


# File 'lib/libosrm/osrm.rb', line 33

Instance Method Details

#coordinates_to_tile_numbers(latitude, longitude, zoom) ⇒ Object

Converts coordinates to tile numbers as expected by #tile.


154
155
156
157
158
159
160
161
# File 'lib/libosrm/osrm.rb', line 154

def coordinates_to_tile_numbers latitude, longitude, zoom
  lat_rad = latitude / 180 * Math::PI
  n = 2.0 ** zoom
  x = ((longitude + 180.0) / 360.0 * n).to_i
  y = ((1.0 - Math::log(Math::tan(lat_rad) + (1 / Math::cos(lat_rad))) / Math::PI) / 2.0 * n).to_i

  {:x => x, :y =>y}
end

#distance_by_roads(coordinates) ⇒ Object

Note:

This method is implemented in native code.

Calculates distance by roads from given coordinates.

Internally operates by invoking #route, but this method is implemented in native code so this works faster than #route if you only need distance and no other data.

Examples:

Input coordinates array

[
  { latitude: from_lat, longitude: from_lon },
  { latitude: to_lat, longitude: to_lon }
]

# File 'lib/libosrm/osrm.rb', line 39

#match(coordinates, opts = nil) ⇒ Hash

TODO:

This action most likely does not work as expected.

Note:

This method is implemented in native code.

Note:

This method is a wrapper for OSRM’s API. Please see the HTTP API for documentation of response values.

Any differences should be documented in corresponding methods; please report should you find any.

Tries to match given coordinates to road network in most plausible way.


# File 'lib/libosrm/osrm.rb', line 112

#nearest(latitude, longitude, amount = 1) ⇒ Hash

Note:

This method is implemented in native code.

Note:

This method is a wrapper for OSRM’s API. Please see the HTTP API for documentation of response values.

Any differences should be documented in corresponding methods; please report should you find any.

OSRM nearest action. Returns nearest street network points.


# File 'lib/libosrm/osrm.rb', line 89

#route(coordinates, opts = nil) ⇒ Hash

Note:

This method is implemented in native code.

Note:

This method is a wrapper for OSRM’s API. Please see the HTTP API for documentation of response values.

Any differences should be documented in corresponding methods; please report should you find any.

Note:

If you only want to know distance by roads for the trip, use #distance_by_roads instead.

OSRM routing action. Takes two or more coordinate pairs and calculates distance by roads between them. A successful response contains an array of resulting routing data, between each point in the request.

Options Hash (opts):

  • :geometry_type (Symbol)

    Format in which the geometries will be returned at. Possible values: :polyline, :polyline6, :geojson. Defaults to :polyline.

  • :steps (Boolean)

    Whether steps should be included in response

  • :annotations (Boolean)

    Whether annotations should be included in response


# File 'lib/libosrm/osrm.rb', line 57

#table(coordinates, opts = nil) ⇒ Hash

Note:

This method is implemented in native code.

Note:

This method is a wrapper for OSRM’s API. Please see the HTTP API for documentation of response values.

Any differences should be documented in corresponding methods; please report should you find any.

OSRM table action. Takes a list of coordinate pairs and calculates distances between each pair to all other pairs. The calculations can be customized by sources and destinations parameters. By default full table is calculated.

Options Hash (opts):

  • :sources (Array)

    List of indices of coordinates array that should be used as a source location

  • :destinations (Array)

    List of indices of coordinates array that should be used as a destination location


# File 'lib/libosrm/osrm.rb', line 75

#tile(x, y, zoom) ⇒ Hash

Note:

This method is implemented in native code.

Note:

This method is a wrapper for OSRM’s API. Please see the HTTP API for documentation of response values.

Any differences should be documented in corresponding methods; please report should you find any.

Note:

You should most likely use #tile_with_coordinates instead.

Calculates a vector tile image that can be used to examine the routing graph. See the official documentation for more comprehensive documentation about this feature.


# File 'lib/libosrm/osrm.rb', line 124

#tile_with_coordinates(latitude, longitude, zoom) ⇒ Object

Invokes #tile, but first converts given coordinates to tile numbers.


144
145
146
147
# File 'lib/libosrm/osrm.rb', line 144

def tile_with_coordinates latitude, longitude, zoom
  xy = coordinates_to_tile_numbers latitude, longitude, zoom
  tile xy[:x], xy[:y], zoom
end

#trip(coordinates) ⇒ Hash

Note:

This method is implemented in native code.

Note:

This method is a wrapper for OSRM’s API. Please see the HTTP API for documentation of response values.

Any differences should be documented in corresponding methods; please report should you find any.

Tries to calculate shortest possible route between given coordinates (and then back to original point; i.e. the first coordinate pair in the input), in order to solve the travelling salesman problem.


# File 'lib/libosrm/osrm.rb', line 101