Skip to content

Nearest

Find nearest road segment to coordinates.

Parameters:

Name Type Description Default
coordinates

List of (lon, lat) tuples or NearestParameters object

None
number

Number of nearest segments to return (default 1)

required
radiuses

Search radius in meters for each coordinate

required
bearings

Bearing constraints

required
hints

Hints from previous request

required
approaches

Approach constraints

required
exclude

Road classes to avoid (e.g., ['motorway'])

required
generate_hints

Generate hints for response

required
snapping

Snapping mode: 'default' or 'any'

required

Returns:

Type Description

Nearest result as osrm.Object

Source code in osrm/__init__.py
def Nearest(self, coordinates=None, **kwargs):
    """
    Find nearest road segment to coordinates.

    Args:
        coordinates: List of (lon, lat) tuples or NearestParameters object
        number: Number of nearest segments to return (default 1)
        radiuses: Search radius in meters for each coordinate
        bearings: Bearing constraints
        hints: Hints from previous request
        approaches: Approach constraints
        exclude: Road classes to avoid (e.g., ['motorway'])
        generate_hints: Generate hints for response
        snapping: Snapping mode: 'default' or 'any'

    Returns:
        Nearest result as osrm.Object
    """        
    # Support traditional usage with NearestParameters object
    if isinstance(coordinates, NearestParameters):
        return self._engine.Nearest(coordinates).to_dict()

    # Convenience method: build parameters from kwargs
    params = NearestParameters()

    # Set coordinates
    if coordinates is not None:
        params.coordinates = coordinates

    # Set other parameters with automatic enum conversion
    for key, value in kwargs.items():
        _set_param(params, key, value)

    return self._engine.Nearest(params).to_dict()

Nearest Parameters

Bases: osrm.osrm_ext.BaseParameters

Overloaded function.

  1. __init__(self) -> None

Instantiates an instance of NearestParameters.

Examples:

>>> nearest_params = osrm.NearestParameters(
        coordinates = [(7.41337, 43.72956)],
        exclude = ['motorway']
    )
>>> nearest_params.IsValid()
True

Parameters:

Name Type Description Default
BaseParameters BaseParameters

Keyword arguments from parent class.

required

Returns:

Name Type Description
__init__ NearestParameters

A NearestParameters object, for usage in osrm.OSRM.Nearest.

IsValid bool

A bool value denoting validity of parameter values.

Attributes:

Name Type Description
number_of_results unsigned int

Number of nearest segments that should be returned.

BaseParameters BaseParameters

Attributes from parent class.

  1. __init__(self, coordinates: collections.abc.Sequence[osrm.osrm_ext.Coordinate] = [], hints: collections.abc.Sequence[std::optional<osrm::engine::Hint>] = [], radiuses: collections.abc.Sequence[osrm.osrm_ext.OptionalDouble] = [], bearings: collections.abc.Sequence[osrm.osrm_ext.OptionalBearing] = [], approaches: collections.abc.Sequence[osrm.osrm_ext.OptionalApproach] = [], generate_hints: bool = True, exclude: collections.abc.Sequence[str] = [], snapping: osrm.osrm_ext.SnappingType = '') -> None