routingkit

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package routingkit provides measures that calculate the cost of travelling between points on road networks. It relies on Open Street Map (OSM) files without the need for a server.

Example (CustomProfile)

The following code example shows how to create your own vehicle profile and use it with routingkit. It customizes the vehicle speed and makes it depend on the tags present. In this example the speed is fixed to a single value (defined in `customVehicleSpeedMapper`). Furthermore, only ways are allowed to be used by the `customVehicle` which have the highway tag and its value is not motorway (defined in `customVehicleTagMapFilter`). Please refer to the OpenStreetMaps [documentation on ways][osm-docs] to learn more about [tags and their values][osm-ways]. [osm-docs]: https://wiki.openstreetmap.org/wiki/Way [osm-ways]: https://wiki.openstreetmap.org/wiki/Key:highway

package main

import (
	"fmt"

	goroutingkit "github.com/nextmv-io/go-routingkit/routingkit"
	"github.com/nextmv-io/sdk/measure"
	"github.com/nextmv-io/sdk/measure/routingkit"
)

func main() {
	fallbackMeasure := measure.ScaleByPoint(measure.HaversineByPoint(), 1.3)

	// Restricts ways to defined OSM way tags.
	filter := func(id int, tags map[string]string) bool {
		// Uses the default filter to filter out ways which are not routable by
		// a car.
		if !routingkit.Car().Filter(id, tags) {
			return false
		}
		// Additionally filter out motorway and trunk (only use small
		// streets/roads).
		// Returning true here, should give different costs for the points used
		// below. The shortest path uses a 'trunk' way.
		return tags["highway"] != "motorway" && tags["highway"] != "trunk"
	}

	// Defines a speed per OSM way tag.
	speedMapper := func(_ int, _ map[string]string) int {
		return 10
	}

	// Defines the custom profile.
	p := goroutingkit.NewProfile(
		"customVehicle", // Profile name
		// TransportationMode, other values: BikeMode, PedestrianMode.
		goroutingkit.VehicleMode,
		// Prevent left turns, only for TransportationMode "VehicleMode".
		false,
		// Prevent U-turns, only for TransportationMode "VehicleMode".
		false,
		filter,
		speedMapper,
	)

	// Defines a routingkit measure using the custom profile.
	m, err := routingkit.DurationByPoint(
		"testdata/rk_test.osm.pbf",
		1000,
		1<<30,
		p,
		fallbackMeasure,
	)
	if err != nil {
		panic(err)
	}
	cost := m.Cost(

		measure.Point{7.34375, 52.16391},
		measure.Point{7.32165, 52.15834},
	)
	fmt.Println(int(cost))
}
Output:

1194

Index

Examples

Constants

This section is empty.

Variables

View Source
var Bike = rk.Bike

Bike constructs a bike routingkit profile.

View Source
var Car = rk.Car

Car constructs a car routingkit profile.

View Source
var Pedestrian = rk.Pedestrian

Pedestrian constructs a pedestrian routingkit profile.

View Source
var Truck = rk.Truck

Truck constructs a truck routingkit profile.

Functions

func ByPoint

func ByPoint(
	mapFile string,
	radius float64,
	cacheSize int64,
	profile rk.Profile,
	m measure.ByPoint,
) (measure.ByPoint, error)

ByPoint constructs a measure.ByPoint that computes the road network distance connecting any two points found within the provided mapFile. It needs a radius in which points can be snapped to the road network, a cache size in bytes (1 << 30 = 1 GB), a profile and a measure that is used in case no travel time can be computed.

Example
package main

import (
	"fmt"

	"github.com/nextmv-io/sdk/measure"
	"github.com/nextmv-io/sdk/measure/routingkit"
)

func main() {
	carProfile := routingkit.Car()
	fallbackMeasure := measure.ScaleByPoint(measure.HaversineByPoint(), 1.3)
	byPointDistance, err := routingkit.ByPoint(
		"testdata/rk_test.osm.pbf",
		1000,
		1<<30,
		carProfile,
		fallbackMeasure,
	)
	if err != nil {
		panic(err)
	}
	cost := byPointDistance.Cost(
		measure.Point{7.33745, 52.14758},
		measure.Point{7.34979, 52.15149},
	)
	fmt.Println(int(cost))
}
Output:

1225

func DurationByPoint

func DurationByPoint(
	mapFile string,
	radius float64,
	cacheSize int64,
	profile rk.Profile,
	m measure.ByPoint,
) (measure.ByPoint, error)

DurationByPoint is a measure that uses routingkit to calculate car travel times between given points. It needs a .osm.pbf map file, a radius in which points can be snapped to the road network, a cache size in bytes (1 << 30 = 1 GB), a profile and a measure that is used in case no travel time can be computed.

Example
package main

import (
	"fmt"

	"github.com/nextmv-io/sdk/measure"
	"github.com/nextmv-io/sdk/measure/routingkit"
)

func main() {
	carProfile := routingkit.Car()
	fallbackMeasure := measure.ScaleByPoint(measure.HaversineByPoint(), 1.2)
	byPointDuration, err := routingkit.DurationByPoint(
		"testdata/rk_test.osm.pbf",
		1000,
		1<<30,
		carProfile,
		fallbackMeasure,
	)
	if err != nil {
		panic(err)
	}
	cost := byPointDuration.Cost(
		measure.Point{7.33745, 52.14758},
		measure.Point{7.34979, 52.15149},
	)
	fmt.Println(int(cost))
}
Output:

187

func DurationMatrix

func DurationMatrix(
	mapFile string,
	radius float64,
	srcs []measure.Point,
	dests []measure.Point,
	profile rk.Profile,
	m measure.ByPoint,
) (measure.ByIndex, error)

DurationMatrix uses the provided mapFile to construct a measure.ByIndex that can find the road network durations between any point in srcs and any point in dests. In addition to the mapFile, srcs and dests it needs a radius in which points can be snapped to the road network, a profile and a measure that is used in case no travel durations can be computed.

Example
package main

import (
	"fmt"

	"github.com/nextmv-io/sdk/measure"
	"github.com/nextmv-io/sdk/measure/routingkit"
)

func main() {
	srcs := []measure.Point{
		{7.33745, 52.14758},
		{7.32486, 52.14280},
	}
	dests := []measure.Point{
		{7.34979, 52.15149},
		{7.33293, 52.13893},
	}
	fallbackMeasure := measure.ScaleByPoint(measure.HaversineByPoint(), 1.2)
	byIndexDistance, err := routingkit.DurationMatrix(
		"testdata/rk_test.osm.pbf",
		1000,
		srcs,
		dests,
		routingkit.Car(),
		fallbackMeasure,
	)
	if err != nil {
		panic(err)
	}
	cost := byIndexDistance.Cost(0, 1)
	fmt.Println(int(cost))
}
Output:

215

func Matrix

func Matrix(
	mapFile string,
	radius float64,
	srcs []measure.Point,
	dests []measure.Point,
	profile rk.Profile,
	m measure.ByPoint,
) (measure.ByIndex, error)

Matrix uses the provided mapFile to construct a measure.ByIndex that can find the road network distance between any point in srcs and any point in dests. In addition to the mapFile, srcs and dests it needs a radius in which points can be snapped to the road network, a profile and a measure that is used in case no distances can be computed.

Example
package main

import (
	"fmt"

	"github.com/nextmv-io/sdk/measure"
	"github.com/nextmv-io/sdk/measure/routingkit"
)

func main() {
	srcs := []measure.Point{
		{7.33745, 52.14758},
		{7.32486, 52.14280},
	}
	dests := []measure.Point{
		{7.34979, 52.15149},
		{7.33293, 52.13893},
	}
	fallbackMeasure := measure.ScaleByPoint(measure.HaversineByPoint(), 1.3)
	byIndexDistance, err := routingkit.Matrix(
		"testdata/rk_test.osm.pbf",
		1000,
		srcs,
		dests,
		routingkit.Car(),
		fallbackMeasure,
	)
	if err != nil {
		panic(err)
	}
	cost := byIndexDistance.Cost(0, 1)
	fmt.Println(int(cost))
}
Output:

1219

Types

type ByIndexLoader

type ByIndexLoader struct {
	// contains filtered or unexported fields
}

ByIndexLoader can be embedded in schema structs and unmarshals a ByIndex JSON object into the appropriate implementation, including a routingkit.ByIndex.

func (ByIndexLoader) MarshalJSON

func (l ByIndexLoader) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON representation for the underlying ByIndex.

func (*ByIndexLoader) To

func (l *ByIndexLoader) To() measure.ByIndex

To returns the underlying ByIndex.

func (*ByIndexLoader) UnmarshalJSON

func (l *ByIndexLoader) UnmarshalJSON(b []byte) error

UnmarshalJSON converts the bytes into the appropriate implementation of ByIndex.

type ByPointLoader

type ByPointLoader struct {
	// contains filtered or unexported fields
}

ByPointLoader can be embedded in schema structs and unmarshals a ByPoint JSON object into the appropriate implementation, including a routingkit.ByPoint.

func (ByPointLoader) MarshalJSON

func (l ByPointLoader) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON representation for the underlying ByPoint.

func (*ByPointLoader) To

func (l *ByPointLoader) To() measure.ByPoint

To returns the underlying ByPoint.

func (*ByPointLoader) UnmarshalJSON

func (l *ByPointLoader) UnmarshalJSON(b []byte) error

UnmarshalJSON converts the bytes into the appropriate implementation of ByPoint.

type DistanceClient

type DistanceClient interface {
	// Measure returns a measure.ByPoint that can calculate the road network
	// distance between any two points found within the provided mapFile.
	Measure(radius float64, cacheSize int64, fallback measure.ByPoint) (measure.ByPoint, error)
	// Matrix returns a measure.ByIndex that represents the road network distance
	// matrix as a measure.
	Matrix(srcs []measure.Point, dests []measure.Point) (measure.ByIndex, error)
	// Polyline requests polylines for the given points. The first parameter
	// returns a polyline from start to end and the second parameter returns a
	// list of polylines, one per leg.
	Polyline(points []measure.Point) (string, []string, error)
}

DistanceClient represents a RoutingKit distance client.

func NewDistanceClient

func NewDistanceClient(
	mapFile string,
	profile rk.Profile,
) (DistanceClient, error)

NewDistanceClient returns a new RoutingKit client.

type DurationClient

type DurationClient interface {
	// Measure returns a measure.ByPoint that can calculate the road network
	// travel time between any two points found within the provided mapFile.
	Measure(radius float64, cacheSize int64, fallback measure.ByPoint) (measure.ByPoint, error)
	// Matrix returns a measure.ByIndex that represents the road network travel
	// time matrix as a measure.
	Matrix(srcs []measure.Point, dests []measure.Point) (measure.ByIndex, error)
	// Polyline requests polylines for the given points. The first parameter
	// returns a polyline from start to end and the second parameter returns a
	// list of polylines, one per leg.
	Polyline(points []measure.Point) (string, []string, error)
}

DurationClient represents a RoutingKit duration client.

func NewDurationClient

func NewDurationClient(
	mapFile string,
	profile rk.Profile,
) (DurationClient, error)

NewDurationClient returns a new RoutingKit client.

type ProfileLoader

type ProfileLoader struct {
	// contains filtered or unexported fields
}

ProfileLoader can be embedded in schema structs and unmarshals a routingkit.Profile JSON object into the appropriate implementation.

func (ProfileLoader) MarshalJSON

func (l ProfileLoader) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON representation for the underlying Profile.

func (*ProfileLoader) To

To returns the underlying Profile.

func (*ProfileLoader) UnmarshalJSON

func (l *ProfileLoader) UnmarshalJSON(b []byte) error

UnmarshalJSON converts the bytes into the appropriate implementation of Profile.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL