Documentation
¶
Overview ¶
Package osrm provides a client library for OSRM API. Please see https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md for API overview.
Index ¶
- Constants
- Variables
- type Annotation
- type Annotations
- type Bearing
- type Config
- type ContinueStraight
- type Destination
- type FallbackCoordinate
- type Gaps
- type Geometries
- type Geometry
- type HTTPClient
- type Intersection
- type Lane
- type MatchRequest
- type MatchResponse
- type Matching
- type NearestRequest
- type NearestResponse
- type NearestWaypoint
- type OSRM
- func (o OSRM) Match(ctx context.Context, r MatchRequest) (*MatchResponse, error)
- func (o OSRM) Nearest(ctx context.Context, r NearestRequest) (*NearestResponse, error)
- func (o OSRM) Route(ctx context.Context, r RouteRequest) (*RouteResponse, error)
- func (o OSRM) Table(ctx context.Context, r TableRequest) (*TableResponse, error)
- func (o OSRM) Trip(ctx context.Context, r TripRequest) (*TripResponse, error)
- type Overview
- type ResponseStatus
- type Roundtrip
- type Route
- type RouteLeg
- type RouteRequest
- type RouteResponse
- type RouteStep
- type Source
- type StepManeuver
- type Steps
- type TableRequest
- type TableResponse
- type Tidy
- type Tracepoint
- type TripRequest
- type TripResponse
- type TripWaypoint
- type Waypoint
Examples ¶
Constants ¶
const ( ErrorCodeInvalidURL = "InvalidUrl" ErrorCodeInvalidService = "InvalidService" ErrorCodeInvalidVersion = "InvalidVersion" ErrorCodeInvalidOptions = "InvalidOptions" ErrorCodeInvalidQuery = "InvalidQuery" ErrorCodeInvalidValue = "InvalidValue" ErrorCodeNoSegment = "NoSegment" ErrorCodeTooBig = "TooBig" ErrorCodeNoRoute = "NoRoute" ErrorCodeNoTable = "NoTable" ErrorCodeNoMatch = "NoMatch" ErrorCodeNoTrips = "NoTrips" )
Error codes that could be returned from OSRM
Variables ¶
var ( ErrorNotImplemented = errors.New("osrm5: the request is not implemented") ErrEmptyProfileName = errors.New("osrm5: the request should contain a profile name") ErrNoCoordinates = errors.New("osrm5: the request should contain coordinates") ErrEmptyServiceName = errors.New("osrm5: the request should contain a service name") )
Invalid request errors
Functions ¶
This section is empty.
Types ¶
type Annotation ¶
type Annotation struct {
Duration []float32 `json:"duration,omitempty"`
Distance []float32 `json:"distance,omitempty"`
Nodes []uint64 `json:"nodes,omitempty"`
}
Annotation contains additional metadata for each coordinate along the route geometry
type Annotations ¶
type Annotations string
Annotations represents a annotations param for osrm5 request
const ( AnnotationsTrue Annotations = "true" AnnotationsFalse Annotations = "false" AnnotationsNodes Annotations = "nodes" AnnotationsDistance Annotations = "distance" AnnotationsDuration Annotations = "duration" AnnotationsDatasources Annotations = "datasources" AnnotationsWeight Annotations = "weight" AnnotationsSpeed Annotations = "speed" )
Supported annotations param values
func (Annotations) String ¶
func (a Annotations) String() string
String returns Annotations as a string
type Bearing ¶
type Bearing struct {
Value, Range uint16
}
Bearing limits the search to segments with given bearing in degrees towards true north in clockwise direction.
type Config ¶
type Config struct {
// ServerURL is OSRM server URL to be used for queries.
// Local http://127.0.0.1:5000 URL will be used as default if not set.
ServerURL string
// Client is custom pre-configured http client to be used for queries.
// New http.Client instance with default settings and one second timeout will be used if not set.
Client HTTPClient
}
Config represents OSRM client configuration options
type ContinueStraight ¶
type ContinueStraight string
ContinueStraight represents continue_straight OSRM routing parameter
const ( ContinueStraightDefault ContinueStraight = "default" ContinueStraightTrue ContinueStraight = "true" ContinueStraightFalse ContinueStraight = "false" )
ContinueStraight values
func (ContinueStraight) String ¶
func (c ContinueStraight) String() string
String returns ContinueStraight as string
type Destination ¶
type Destination string
const ( DestinationDefault Destination = "any" DestinationAny Destination = "any" DestinationLast Destination = "last" )
func (Destination) String ¶
func (d Destination) String() string
type FallbackCoordinate ¶
type FallbackCoordinate string
const ( FallbackCoordinateDefault FallbackCoordinate = "input" FallbackCoordinateInput FallbackCoordinate = "input" FallbackCoordinateSnapped FallbackCoordinate = "snapped" )
func (FallbackCoordinate) String ¶
func (f FallbackCoordinate) String() string
type Geometries ¶
type Geometries string
Geometries represents a geometries param for osrm5
const ( GeometriesPolyline6 Geometries = "polyline6" GeometriesGeojson Geometries = "geojson" )
Supported geometries param values
func (Geometries) String ¶
func (g Geometries) String() string
String returns Geometries as a string
type Geometry ¶
Geometry represents a points set
func NewGeometryFromPath ¶
NewGeometryFromPath creates a geometry from a path.
func NewGeometryFromPointSet ¶
NewGeometryFromPointSet creates a geometry from points set.
func (Geometry) MarshalJSON ¶
MarshalJSON generates a polyline in Google polyline6 format
func (*Geometry) UnmarshalJSON ¶
UnmarshalJSON parses a geo path from points set or a polyline
type HTTPClient ¶
HTTPClient defines minimal interface necessary for making HTTP requests. Standard library http.Client{} implements this interface. A non-2xx status code doesn't cause an error.
type Intersection ¶
type MatchRequest ¶
type MatchRequest struct {
Profile string
Coordinates Geometry
Bearings []Bearing
Steps Steps
Annotations Annotations
Tidy Tidy
Timestamps []int64
Radiuses []float64
Hints []string
Overview Overview
Gaps Gaps
Geometries Geometries
}
MatchRequest represents a request to the match method
type MatchResponse ¶
type MatchResponse struct {
ResponseStatus
Matchings []Matching `json:"matchings"`
Tracepoints []*Tracepoint `json:"tracepoints"`
}
MatchResponse represents a response from the match method
type Matching ¶
type Matching struct {
Route
Confidence float64 `json:"confidence"`
Geometry Geometry `json:"geometry"`
}
Matching represents an array of Route objects that assemble the trace
type NearestRequest ¶
NearestRequest represents a request to the nearest method
type NearestResponse ¶
type NearestResponse struct {
ResponseStatus
Waypoints []NearestWaypoint `json:"waypoints"`
}
NearestResponse represents a response from the nearest method
type NearestWaypoint ¶
type NearestWaypoint struct {
Location geo.Point `json:"location"`
Distance float64 `json:"distance"`
Name string `json:"name"`
Hint string `json:"hint"`
Nodes []uint64 `json:"nodes"`
}
NearestWaypoint represents a nearest point on a nearest query
type OSRM ¶
type OSRM struct {
// contains filtered or unexported fields
}
OSRM implements the common OSRM API v5. See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md for details. TODO: implement (trip, tile) methods
func NewFromURL ¶
NewFromURL creates a client with custom server url and default timeout
func NewFromURLWithTimeout ¶
NewFromURLWithTimeout creates a client with custom timeout connection
func NewWithConfig ¶
NewWithConfig creates a client with given config
func (OSRM) Match ¶
func (o OSRM) Match(ctx context.Context, r MatchRequest) (*MatchResponse, error)
Match matches given GPS points to the road network in the most plausible way. See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#match-service for details.
func (OSRM) Nearest ¶
func (o OSRM) Nearest(ctx context.Context, r NearestRequest) (*NearestResponse, error)
Nearest matches given GPS point to the nearest road network. See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#nearest-service for details.
func (OSRM) Route ¶
func (o OSRM) Route(ctx context.Context, r RouteRequest) (*RouteResponse, error)
Route searches the shortest path between given coordinates. See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#route-service for details.
Example ¶
package main
import (
"context"
"fmt"
"log"
osrm "github.com/gojuno/go.osrm"
geo "github.com/paulmach/go.geo"
)
func main() {
client := osrm.NewFromURL("https://router.project-osrm.org")
resp, err := client.Route(context.Background(), osrm.RouteRequest{
Profile: "car",
Coordinates: osrm.NewGeometryFromPointSet(geo.PointSet{
{-73.87946, 40.75833},
{-73.87925, 40.75837},
{-73.87918, 40.75837},
{-73.87911, 40.75838},
}),
Steps: osrm.StepsTrue,
Annotations: osrm.AnnotationsTrue,
Overview: osrm.OverviewFalse,
Geometries: osrm.GeometriesPolyline6,
})
if err != nil {
log.Fatalf("route failed: %v", err)
}
fmt.Println(len(resp.Routes))
}
Output: 1
func (OSRM) Table ¶
func (o OSRM) Table(ctx context.Context, r TableRequest) (*TableResponse, error)
Table computes duration tables for the given locations. See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#table-service for details.
func (OSRM) Trip ¶
func (o OSRM) Trip(ctx context.Context, r TripRequest) (*TripResponse, error)
The trip plugin solves the Traveling Salesman Problem using a greedy heuristic (farthest-insertion algorithm). See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#trip-service for details
type Overview ¶
type Overview string
Overview represents level of overview of geometry in a response
type ResponseStatus ¶
type ResponseStatus struct {
Code string `json:"code"`
Message string `json:"message"`
DataVersion string `json:"data_version"`
}
ResponseStatus represent OSRM API response
func (ResponseStatus) ErrCode ¶
func (r ResponseStatus) ErrCode() string
ErrCode returns error code from OSRM response
func (ResponseStatus) Error ¶
func (r ResponseStatus) Error() string
type Route ¶
type Route struct {
Distance float32 `json:"distance"`
Duration float32 `json:"duration"`
WeightName string `json:"weight_name"`
Wieght float32 `json:"weight"`
Geometry Geometry `json:"geometry"`
Legs []RouteLeg `json:"legs"`
}
Route represents a route through (potentially multiple) points.
type RouteLeg ¶
type RouteLeg struct {
Annotation Annotation `json:"annotation"`
Distance float32 `json:"distance"`
Duration float32 `json:"duration"`
Summary string `json:"summary"`
Weight float32 `json:"weight"`
Steps []RouteStep `json:"steps"`
}
RouteLeg represents a route between two waypoints.
type RouteRequest ¶
type RouteRequest struct {
Profile string
Coordinates Geometry
Bearings []Bearing
Steps Steps
Annotations Annotations
Overview Overview
Geometries Geometries
ContinueStraight ContinueStraight
Waypoints []int
}
RouteRequest represents a request to the route method
type RouteResponse ¶
type RouteResponse struct {
ResponseStatus
Routes []Route `json:"routes"`
Waypoints []Waypoint `json:"waypoints"`
}
RouteResponse represents a response from the route method
type RouteStep ¶
type RouteStep struct {
Distance float32 `json:"distance"`
Duration float32 `json:"duration"`
Geometry Geometry `json:"geometry"`
Name string `json:"name"`
Mode string `json:"mode"`
DrivingSide string `json:"driving_side"`
Weight float32 `json:"weight"`
Maneuver StepManeuver `json:"maneuver"`
Intersections []Intersection `json:"intersections,omitempty"`
}
RouteStep represents a route geometry
type StepManeuver ¶
type StepManeuver struct {
Location geo.Point `json:"location"`
BearingBefore float32 `json:"bearing_before"`
BearingAfter float32 `json:"bearing_after"`
Type string `json:"type"`
Modifier string `json:"modifier,omitempty"`
Exit *uint32 `json:"exit,omitempty"`
}
StepManeuver contains information about maneuver in step
type TableRequest ¶
type TableRequest struct {
Profile string
Coordinates Geometry
Sources, Destinations []int
Annotations Annotations
FallbackSpeed float64
FallbackCoordinate FallbackCoordinate
ScaleFactor float64
}
TableRequest represents a request to the table method
type TableResponse ¶
type TableResponse struct {
ResponseStatus
Durations [][]float32 `json:"durations"`
Distances [][]float32 `json:"distances"`
Sources []Waypoint `json:"sources"`
Destinations []Waypoint `json:"destinations"`
FallbackSpeedCells [][]bool `json:"fallback_speed_cells"`
}
TableResponse resresents a response from the table method
type Tracepoint ¶
type Tracepoint struct {
Index int `json:"waypoint_index"`
Location geo.Point `json:"location"`
MatchingIndex int `json:"matchings_index"`
AlternativesCount int `json:"alternatives_count"`
Hint string `json:"hint"`
}
Tracepoint represents a matched point on a route
type TripRequest ¶
type TripRequest struct {
Profile string
Coordinates Geometry
Roundtrip Roundtrip
Source Source
Destination Destination
Steps Steps
Annotations Annotations
Geometries Geometries
Overview Overview
}
func (TripRequest) IsSupported ¶
func (r TripRequest) IsSupported() bool
type TripResponse ¶
type TripResponse struct {
ResponseStatus
Waypoints []TripWaypoint `json:"waypoints"`
Trips []Route `json:"trips"`
}