geopath

package module
v0.0.0-...-cc73e3d Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 7 Imported by: 0

README

geopath

Dumb shortest-path implementation using GeoJSON linestrings to navigate.

Just plain ol Dijkstra's.

Usage:

package main

import (
	"fmt"
	"log"
	"os"

	geopath "github.com/sebnyberg/geopath"
)

func main() {
	f, err := os.OpenFile("path/to/geo.json", os.O_RDONLY, 0444)
	if err != nil {
		log.Fatalln(err)
	}
	paths, err := geopath.ParsePaths(f)
	if err != nil {
		log.Fatalln(err)
	}
	start := [2]float64{-84.397252, 33.792997}
	end := [2]float64{-84.395111, 33.791666}
	precision := 0.00001
	path, distance, err := geopath.FindShortestPath(paths, start, end, precision)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("Shortest distance: %.05f\n", distance)
	fmt.Printf("Path:\n")
	for _, p := range path {
		fmt.Printf("(%.06f, %.06f)\n", p[0], p[1])
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGeoJSONParse = errors.New("geojson parse")
View Source
var ErrNoPath = errors.New("no path")

Functions

func FindShortestPath

func FindShortestPath(
	paths []path,
	startLongLat [2]float64,
	endLongLat [2]float64,
	precision float64,
) (path [][2]float64, dist float64, err error)

FindShortestPath finds the shortest path between fromLongLat and toLongLat using LineString paths found in the provided io.Reader's GeoJSON contents.

Input coordinates are expected to be in ESPG:4326 format.

Precision determines the fuzziness of linking together the start and end points, and the start and ends of the line segments. If precision is unset no rounding will occur, and start/end points are required to perfectly match across the line segments and the start and end points.

If a shortest path is found, the return path will include a set of points, starting with startLongLat and ending in endLongLat along with the total path distance.

If no path is found, an error of type ErrNoPath is returned.

func ParsePaths

func ParsePaths(file io.Reader) ([]path, error)

ParsePaths parses lines found within the provided GeoJSON. Any non line-strings are ignored. An error is returned if the file does not contain valid GeoJSON.

Types

This section is empty.

Jump to

Keyboard shortcuts

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