routeplanner

package module
v0.0.0-...-7f59a9f Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 9 Imported by: 0

README

RoutePlanner Backend

This repository contains code that powers the routeplanner app. The app suggests circular walks or runs based on start location and desired distance.

Motivation

I have been interested in making an app like that for a while since I started running more frequently. I normally use google maps and add multiple destinations to work out a distance of a potential run, but that is rather cumbersome way of doing it. Secondly I thought it would be a good challenge since I would have to figure out how to get suitable map data and then design an algorithm that would produce decent quality circular paths.

Implementation

This service is designed to be used as serverless function hosted on google cloud platform with an API gateway to it.

Map data is retrieved from a public OSM server and this is also the slowest step in the process as it can take a few seconds to download the data. The data is then parsed and routes are calculated. Best 25 results are returned encoded as JSON.

Running locally

Requires Go 1.22 or later.

go test ./...
go run ./cmd/server   # listens on $PORT, default 8080
curl 'localhost:8080/?lat=51.5313&lon=-0.1570&distance=10'

distance is in km and must be between 1 and 35.

Map data comes from tiles generated ahead of time from an OSM extract, read from tiledata/ or the directory in $TILES_DIR. Where there are no tiles the API responds with 422.

To generate tiles, download an extract from https://download.geofabrik.de and run the generator from its own directory:

cd cmd/tilegen
CGO_ENABLED=0 go run . -in path/to/greater-london-latest.osm.pbf -out ../../tiledata

CGO_ENABLED=0 lets the OSM reading library build without pkg-config. Greater London takes a few seconds and makes about 24MB of tiles.

Result

The quality of routes does vary a lot. Some areas especially urban areas with a lot of suitable roads and paths can produce great results. Other times the routes might be hard to navigate due to large number of turns or the fact that they keep going back on themselves. This variability was the main reason to return best 25 as it makes it more likely that some of the suggestions are suitable.

Example of good suggestions:

good routes in knole park

Back in high school I took part in the Knole run which is a 10km cross country run made up of two loops.

Last thing to note is while I tried to eliminate the possibility of roads and paths that are not suitable for pedestrians being used, they still will likely appear sometimes as OSM data is not always complete or up to date in different parts of the world.

Documentation

Index

Constants

View Source
const (
	MinDistance = 1
	MaxDistance = 35
)

Route distances in km.

Variables

This section is empty.

Functions

func RoutePlannerAPI

func RoutePlannerAPI(w http.ResponseWriter, r *http.Request)

Handler function that is invoked by GCP.

func TilesToGraph

func TilesToGraph(ts []*tiles.Tile, south, west, north, east float64) (graph routing.Graph)

TilesToGraph takes tiles and returns a graph of the edges with at least one node inside the bounding box, and without dead ends.

Types

type CoordPair

type CoordPair [2]float64

type Request

type Request struct {
	Lat      float64 `schema:"lat,required"`
	Lon      float64 `schema:"lon,required"`
	Distance float64 `schema:"distance,required"`
}

type Responce

type Responce []Route

type Route

type Route struct {
	Path     []CoordPair `json:"path"`
	Distance float64     `json:"distance"`
}

Directories

Path Synopsis
cmd
server command
Command server runs the route planner API locally, outside Google Cloud Functions.
Command server runs the route planner API locally, outside Google Cloud Functions.
Package routing provides functions to navigate on and process graphs in the context of routeplanner project.
Package routing provides functions to navigate on and process graphs in the context of routeplanner project.
Package tiles stores the walkable graph of a region as small files, one per web map tile, so that a request only has to load the area it needs.
Package tiles stores the walkable graph of a region as small files, one per web map tile, so that a request only has to load the area it needs.
Package walkfilter decides which OSM ways are suitable for running and how suitable they are, based on their tags.
Package walkfilter decides which OSM ways are suitable for running and how suitable they are, based on their tags.

Jump to

Keyboard shortcuts

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