package main
import (
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/planar"
)
// Point はデータ番号と緯度、経度を保持する構造体です
type Point struct {
Number int
Lat, Lon float64
}
// トラッキングデータとバス路線データの変換
func convertToOrbPoint(p Point) orb.Point {
return orb.Point{p.Lon, p.Lat}
}
func main() {
// トラッキングデータ
trackingData := []Point{
{1, 139.6268272, 35.3715607},
{2, 139.627135, 35.3719136},
{3, 139.6274291, 35.3721591},
{11, 139.6276967, 35.3724112},
{101, 139.6276253, 35.372575},
{59, 139.6274377, 35.3727348},
{7, 139.6272077, 35.3728485},
{99, 139.6263279, 35.3728985},
{111, 139.6262229, 35.3728182},
{131, 139.6260982, 35.3726421},
{200, 139.6260349, 35.3724965},
{32, 139.6257477, 35.3720127},
{42, 139.6254685, 35.3705626},
{44, 139.6254273, 35.3703156},
{46, 139.62361383758, 35.36644117332},
}
// バス路線データ
busRoute := []Point{
{0, 139.6272077, 35.3728485},
{0, 139.6263279, 35.3728985},
{0, 139.6262229, 35.3728182},
{0, 139.6260982, 35.3726421},
{0, 139.6260349, 35.3724965},
{0, 139.6257477, 35.3720127},
{0, 139.6254685, 35.3705626},
{0, 139.6254273, 35.3703156},
{0, 139.62361383758, 35.36644117332},
{0, 139.62386146588, 35.3665604971},
{0, 139.62412597981, 35.36668746811},
{0, 139.6245189, 35.3668106},
{0, 139.6250347, 35.367043},
{0, 139.62548981146, 35.36726113676},
}
// orbライブラリを使用して、トラッキングデータとバス路線データをorb.Pointのスライスに変換
var trackingPoints []orb.Point
var busRoutePoints []orb.Point
for _, p := range trackingData {
trackingPoints = append(trackingPoints, convertToOrbPoint(p))
}
for _, p := range busRoute {
busRoutePoints = append(busRoutePoints, convertToOrbPoint(p))
}
// トラッキングポイントからバス路線上の最も近い点までの距離を計算し、データ番号とともに出力
for _, trackPoint := range trackingData {
distance := planar.DistanceFrom(orb.LineString(busRoutePoints), convertToOrbPoint(trackPoint))
fmt.Printf("Tracking Point %d: Lat: %v, Lng: %v, Distance: %v meters\n", trackPoint.Number, trackPoint.Lat, trackPoint.Lon, distance)
}
}
出力結果
Tracking Point 1: Lat: 139.6268272, Lng: 35.3715607, Distance: 0.001145488357906135 meters
Tracking Point 2: Lat: 139.627135, Lng: 35.3719136, Distance: 0.0009375188544507943 meters
Tracking Point 3: Lat: 139.6274291, Lng: 35.3721591, Distance: 0.0007240789459736531 meters
Tracking Point 11: Lat: 139.6276967, Lng: 35.3724112, Distance: 0.0006560124160327593 meters
Tracking Point 101: Lat: 139.6276253, Lng: 35.372575, Distance: 0.0004991913560911751 meters
Tracking Point 59: Lat: 139.6274377, Lng: 35.3727348, Distance: 0.000256569074508911 meters
Tracking Point 7: Lat: 139.6272077, Lng: 35.3728485, Distance: 0 meters
Tracking Point 99: Lat: 139.6263279, Lng: 35.3728985, Distance: 0 meters
Tracking Point 111: Lat: 139.6262229, Lng: 35.3728182, Distance: 0 meters
Tracking Point 131: Lat: 139.6260982, Lng: 35.3726421, Distance: 0 meters
Tracking Point 200: Lat: 139.6260349, Lng: 35.3724965, Distance: 0 meters
Tracking Point 32: Lat: 139.6257477, Lng: 35.3720127, Distance: 0 meters
Tracking Point 42: Lat: 139.6254685, Lng: 35.3705626, Distance: 0 meters
Tracking Point 44: Lat: 139.6254273, Lng: 35.3703156, Distance: 0 meters
Tracking Point 46: Lat: 139.62361383758, Lng: 35.36644117332, Distance: 0 meters